Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add user warning when data crs conflicts with provided crs #490

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions hydromt/data_adapter/geodataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
19 changes: 13 additions & 6 deletions hydromt/data_adapter/rasterdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions hydromt/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/test_data_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
Loading