diff --git a/setup.cfg b/setup.cfg index 06de1dd..e713a80 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,21 +22,17 @@ url = https://github.com/metno/pyaro-readers python_version = >=3.9 install_requires = pyaro >= 0.0.6 + geocoder_reverse_natural_earth >= 0.0.1 netCDF4 requests - fiona - shapely - rtree tqdm package_dir = =src -packages = pyaro_readers.aeronetsunreader, pyaro_readers.aeronetsdareader, geocoder_reverse_natural_earth, pyaro_readers.ascii2netcdf +packages = pyaro_readers.aeronetsunreader, pyaro_readers.aeronetsdareader, pyaro_readers.ascii2netcdf test_require = tox:tox [options.package_data] -geocoder_reverse_natural_earth = - *.zip [options.entry_points] pyaro.timeseries = diff --git a/src/geocoder_reverse_natural_earth/__init__.py b/src/geocoder_reverse_natural_earth/__init__.py deleted file mode 100644 index ead40f3..0000000 --- a/src/geocoder_reverse_natural_earth/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .geocoder_reverse_ne import Geocoder_Reverse_NE, Geocoder_Reverse_Exception diff --git a/src/geocoder_reverse_natural_earth/geocoder_reverse_ne.py b/src/geocoder_reverse_natural_earth/geocoder_reverse_ne.py deleted file mode 100644 index ef4a748..0000000 --- a/src/geocoder_reverse_natural_earth/geocoder_reverse_ne.py +++ /dev/null @@ -1,86 +0,0 @@ -import os -import fiona -import rtree -import shapely.geometry - - -class Geocoder_Reverse_Exception(Exception): - pass - - -class Geocoder_Reverse_NE: - _index = None - - @staticmethod - def __generator_function(): - ne = os.path.join(os.path.dirname(__file__), "ne_10m_admin_0_countries_deu.zip") - with fiona.open(f"zip://{ne}", "r") as fi: - for fid, feat in enumerate(fi): - geom = shapely.geometry.shape(feat["geometry"]) - yield (fid, geom.bounds, (feat["properties"], geom)) - - """Get geocode from the Natural Earth Admin country shape-files""" - - def __init__(self) -> None: - if Geocoder_Reverse_NE._index is None: - Geocoder_Reverse_NE._index = rtree.index.Index( - Geocoder_Reverse_NE.__generator_function() - ) - - def intersect(self, geometry) -> [dict()]: - """Find intersecting area with geometry - - :param geometry: a shapely geometry - :return: a list of dicts of natural-earth properties - """ - ret_list = [] - for obj in self._index.intersection(geometry.bounds, objects=True): - props, geom = obj.object - if geometry.intersects(geom): - ret_list.append(props) - return ret_list - - def lookup(self, latitude: float, longitude: float) -> dict(): - """Lookup coordinates for a country - - :param self: _description_ - :raises Geocoder_Reverse_Exception: _description_ - :return: _description_ - """ - point = shapely.geometry.Point((longitude, latitude)) - objs = self.intersect(point) - if len(objs) > 0: - return objs[0] - else: - raise Geocoder_Reverse_Exception( - f"coordinates (latitude, longitude) not found in NE-admin" - ) - - def lookup_nearest(self, latitude: float, longitude: float) -> dict(): - point = shapely.geometry.Point((longitude, latitude)) - objs = self.intersect(point) - if len(objs) == 0: - nearest = list(self._index.nearest(point.bounds, 1, objects=True)) - props, geom = nearest[0].object - return props - return objs[0] - - -if __name__ == "__main__": - from tqdm import tqdm - - geo = Geocoder_Reverse_NE() - print(geo.lookup(60, 10)["ISO_A2_EH"]) - print(geo.lookup(78, 15)["ISO_A2_EH"]) - print(geo.lookup_nearest(78.2361926, 15.3692614)["ISO_A2_EH"]) - count = 0 - for j in tqdm(range(-60, 85)): - for i in range(-180, 180): - count += 1 - try: - geo.lookup(i, j) - except Geocoder_Reverse_Exception as grex: - pass - if False: - if geo.lookup_nearest(i, j) is None: - print(f"error: {i},{j}") diff --git a/src/geocoder_reverse_natural_earth/ne_10m_admin_0_countries_deu.zip b/src/geocoder_reverse_natural_earth/ne_10m_admin_0_countries_deu.zip deleted file mode 100644 index 70ca04e..0000000 Binary files a/src/geocoder_reverse_natural_earth/ne_10m_admin_0_countries_deu.zip and /dev/null differ diff --git a/tests/test_geocoder_reverse_natural_earth.py b/tests/test_geocoder_reverse_natural_earth.py deleted file mode 100644 index 5e6ade0..0000000 --- a/tests/test_geocoder_reverse_natural_earth.py +++ /dev/null @@ -1,18 +0,0 @@ -import unittest -from geocoder_reverse_natural_earth import ( - Geocoder_Reverse_NE, - Geocoder_Reverse_Exception, -) - - -class TestGeocoderRevereseNE(unittest.TestCase): - def test_lookup(self): - geo = Geocoder_Reverse_NE() - self.assertEqual(geo.lookup(60, 10)["ISO_A2_EH"], "NO") - self.assertEqual(geo.lookup(78, 15)["ISO_A2_EH"], "NO") - with self.assertRaises(Geocoder_Reverse_Exception): - geo.lookup(78.2361926, 15.3692614) - - def test_lookup(self): - geo = Geocoder_Reverse_NE() - self.assertEqual(geo.lookup_nearest(78.2361926, 15.3692614)["ISO_A2_EH"], "NO")