From 0f0068b643d3cab7c126e73f41bfe0b53e7c102a Mon Sep 17 00:00:00 2001 From: Sam Vente Date: Mon, 28 Aug 2023 15:19:00 +0200 Subject: [PATCH] add user warning when data crs conflicts with provided crs --- hydromt/data_adapter/geodataset.py | 16 ++++++++++++++-- hydromt/data_adapter/rasterdataset.py | 19 +++++++++++++------ hydromt/io.py | 20 ++++++++++++++++---- tests/test_data_adapter.py | 4 ++++ 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/hydromt/data_adapter/geodataset.py b/hydromt/data_adapter/geodataset.py index 56554e909..ab4e82885 100644 --- a/hydromt/data_adapter/geodataset.py +++ b/hydromt/data_adapter/geodataset.py @@ -323,8 +323,20 @@ def get_data( ds_out = ds_out[variables] # set crs - if ds_out.vector.crs is None and self.crs is not None: - ds_out.vector.set_crs(self.crs) + if self.crs is not None: + if ds_out.vector.crs is None: + ds_out.vector.set_crs(self.crs) + elif ds_out.vector.crs != self.crs: + raise UserWarning( + f"DataCatalog entry and data attributes specify" + f"inconsistent CRS. Data CRS: {ds_out.vector.crs}," + f" user specified CRS: {self.crs}. The data specified CRS will " + "be used." + ) + else: + # specified and data CRS are equal, we don't have to do anything + pass + if ds_out.vector.crs is None: raise ValueError( "GeoDataset: The data has no CRS, set in GeoDatasetAdapter." diff --git a/hydromt/data_adapter/rasterdataset.py b/hydromt/data_adapter/rasterdataset.py index 9834d5ef0..22d16f575 100644 --- a/hydromt/data_adapter/rasterdataset.py +++ b/hydromt/data_adapter/rasterdataset.py @@ -379,12 +379,19 @@ def get_data( raise IndexError("RasterDataset: Time slice out of range.") # set crs - if ds_out.raster.crs is None and self.crs is not None: - ds_out.raster.set_crs(self.crs) - elif ds_out.raster.crs is None: - raise ValueError( - "RasterDataset: The data has no CRS, set in RasterDatasetAdapter." - ) + if self.crs is not None: + if ds_out.raster.crs is None: + ds_out.raster.set_crs(self.crs) + elif ds_out.raster.crs != self.crs: + raise UserWarning( + f"DataCatalog entry and data attributes specify" + f"inconsistent CRS. Data CRS: {ds_out.raster.crs}," + f" user specified CRS: {self.crs}. The data specified CRS will " + "be used." + ) + else: + # specified and data CRS are equal, we don't have to do anything + pass # clip # make sure bbox is in data crs diff --git a/hydromt/io.py b/hydromt/io.py index 9ffac2754..3699ac1e5 100644 --- a/hydromt/io.py +++ b/hydromt/io.py @@ -586,10 +586,22 @@ def open_vector( raise ValueError(f"{fn} contains other geometries than {assert_gtype}") # check if crs and filter - if gdf.crs is None and crs is not None: - gdf = gdf.set_crs(pyproj.CRS.from_user_input(crs)) - elif gdf.crs is None: - raise ValueError("The GeoDataFrame has no CRS. Set one using the crs option.") + if gdf.crs is None: + if crs is not None: + gdf = gdf.set_crs(pyproj.CRS.from_user_input(crs)) + else: + raise ValueError( + "The GeoDataFrame has no CRS. Set one using the crs option." + ) + else: + if gdf.crs != dst_crs: + logger.warning( + f"DataCatalog entry and data attributes specify" + f"inconsistent CRS. Data CRS: {gdf.crs}," + f" user specified CRS: {dst_crs}. The user specified CRS will " + "be used." + ) + if dst_crs is not None: gdf = gdf.to_crs(dst_crs) # filter points diff --git a/tests/test_data_adapter.py b/tests/test_data_adapter.py index 68d841d47..79e09dfbc 100644 --- a/tests/test_data_adapter.py +++ b/tests/test_data_adapter.py @@ -215,6 +215,10 @@ def test_geodataset(geoda, geodf, ts, tmpdir): da3 = data_catalog.get_geodataset( fn_csv_locs, driver_kwargs=dict(fn_data=fn_csv), crs=geodf.crs ).sortby("index") + with pytest.warns(UserWarning): + _ = data_catalog.get_geodataset( + fn_csv_locs, driver_kwargs=dict(fn_data=fn_csv), crs=2380 + ) assert np.allclose(da3, geoda) assert da3.vector.crs.to_epsg() == 4326 with pytest.raises(FileNotFoundError, match="No such file or catalog source"):