diff --git a/grompy/__init__.py b/grompy/__init__.py index 9c91518dff37ef553d00152717868e202e7a723b..2203242eca95d40791824b7fb96bfd22bb98cc74 100644 --- a/grompy/__init__.py +++ b/grompy/__init__.py @@ -3,7 +3,7 @@ # Allard de Wit (allard.dewit@wur.nl), October 2021 """Grompy is a tool to process and access parcel-based satellite observations from GroenMonitor.nl. """ -__version__ = "1.6.1" +__version__ = "1.6.2" from .dap import DataAccessProvider from .cmd import cli diff --git a/grompy/dap.py b/grompy/dap.py index 660f30d32be53b7704b630c34412ea0666d7f265..75883b1c07359e0a451265f4a3ef5ad42551cdcf 100644 --- a/grompy/dap.py +++ b/grompy/dap.py @@ -233,7 +233,11 @@ class DataAccessProvider: d[dataset_name] = df c = SimpleNamespace(**d) + assert (50 < parcel_info.latitude < 54), f"Latitude out of range {parcel_info.latitude}, did you invert x and y?" + assert (3.0 < parcel_info.longitude < 7.5), f"Longitude out of range {parcel_info.longitude}, did you invert x and y?" + yield parcel_info, c + rows = r.fetchmany(100) def __len__(self): diff --git a/grompy/load_data.py b/grompy/load_data.py index a27d2f53a9a17d5fd8e1879b20313cc431685c23..7c833959bdd7227ec6f8a2d9b8522fc935c57e10 100644 --- a/grompy/load_data.py +++ b/grompy/load_data.py @@ -13,7 +13,7 @@ import numpy as np import yaml import duckdb -from .util import prepare_db, make_path_absolute, get_latlon_from_RD +from .util import prepare_db, make_path_absolute, get_latlon_from_RD, check_naam, check_PC4 class CSVLoadingError(Exception): @@ -53,7 +53,9 @@ def load_parcel_info(grompy_yaml, gpkg_fname, counts_file_10m, counts_file_20m, r = [] for row in df.itertuples(): x, y = float(row.geometry.centroid.x), float(row.geometry.centroid.y) - lon, lat = get_latlon_from_RD(x, y) + lat, lon = get_latlon_from_RD(x, y) + assert (50 < lat < 54), f"Latitude out of range {lat}, did you invert x and y?" + assert (3.0 < lon < 7.5), f"Longitude out of range {lon}, did you invert x and y?" r.append({"fieldid": row.Index, "latitude": lat, "longitude":lon, "rdx": x, "rdy": y}) df_latlon = pd.DataFrame(r).set_index("fieldid") df = df.join(df_latlon) @@ -82,15 +84,15 @@ def load_parcel_info(grompy_yaml, gpkg_fname, counts_file_10m, counts_file_20m, "rdy": df.rdy, "longitude": df.longitude, "latitude": df.latitude, - "cat_gewasc": df.cat_gewasc.apply(str), + "cat_gewasc": df.cat_gewasc.apply(check_naam), "gws_gewasc": df.gws_gewasc.astype(np.int32), - "provincie": df.provincie.apply(str), - "gemeente": df.gemeente.apply(str), - "regio": df.regio.apply(str), - "pc4": df.PC4.apply(lambda pc4: str(int(pc4))), + "provincie": df.provincie.apply(check_naam), + "gemeente": df.gemeente.apply(check_naam), + "regio": df.regio.apply(check_naam), + "pc4": df.PC4.apply(check_PC4), "AHN2": df.AHN2, - "woonplaats": df.woonplaats.apply(str), - "waterschap": df.waterschap.apply(str), + "woonplaats": df.woonplaats.apply(check_naam), + "waterschap": df.waterschap.apply(check_naam), "S2_Tiles": df.S2_Tiles.apply(str), "geometry": df.geometry }, crs=df.crs) diff --git a/grompy/util.py b/grompy/util.py index 6110e0d10fbad8b8e344e1da320484c1c268b6be..c227ac7cfc855a57843becf29de4dc086e3b7ae4 100644 --- a/grompy/util.py +++ b/grompy/util.py @@ -105,6 +105,8 @@ def open_DB_connection(grompy_yaml, dsn): def get_latlon_from_RD(x, y, _cache={}): """Converts given X,Y in the Dutch RD coordinate system towards latitude longitude in WGS84 + returns: (latitude, longitude) + For checking: in RD coordinate system X, Y: 197903, 309096 should be in WGS84 latitude, longitude: 50.770195900951045, 5.995343676754713 @@ -128,4 +130,20 @@ def check_grompy_version(grompy_yaml): msg = f"grompy.yaml ({v1}) does not match grompy version ({v2})! " \ "Generate a new grompy.yaml file with `grompy init` " \ "and adapt it." - raise RuntimeError(msg) \ No newline at end of file + raise RuntimeError(msg) + +def check_PC4(postal_code): + try: + pc4 = str(int(postal_code)) + except ValueError as e: + pc4 = "0000" + return pc4 + + +def check_naam(name): + try: + n = str(name) + except ValueError as e: + n = "Unknown" + return n + diff --git a/pyproject.toml b/pyproject.toml index c75eb3de7d11542eaa4f4074e901ef5adc6ab310..4f8509f1c453b4e00a42003022dad1679428c6c4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "flit_core.buildapi" [project] name = "grompy" -version = "1.6.1" +version = "1.6.2" description = "GROMPY is a tool for storing and accessing Groenmonitor satellite time-series for agricultural parcels." authors = [ {name = "Allard de Wit", email = "allard.dewit@wur.nl"},