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

upgrade_shapely_2.0_fix #731

Merged
merged 5 commits into from
Jun 5, 2023
Merged

Conversation

emanuel-schmid
Copy link
Collaborator

@emanuel-schmid emanuel-schmid commented May 31, 2023

Changes proposed in this PR:

  • use the .geoms attribute as mask argument in case of MultiPolygon

This PR fixes CLIMADA-project/climada_petals#79

PR Author Checklist

PR Reviewer Checklist

with shapely 2.0 MultiPolygon is not iterable anymore
@emanuel-schmid emanuel-schmid changed the base branch from main to develop May 31, 2023 14:15
@emanuel-schmid emanuel-schmid changed the title Feature/fix multipolygon mask upgrade_shapely_2.0_fix May 31, 2023
@tovogt
Copy link
Collaborator

tovogt commented May 31, 2023

I think that there is a more fundamental issue here.

The read_raster function takes an argument geometry that is passed on to rasterio.mask.mask as the shapes argument. The docstring of read_raster specifies the type of the geometry to be a shapely.geometry, but the docstring of rasterio.mask.mask specifies the type of shapes to be an iterable of geometries.

This was never correct, even before the 2.0 upgrade for shapely.

The tests for read_raster are currently passing a list of geometries so that the tests don't fail even though the tests aren't according to the docstring:

meta, inten_ras = u_coord.read_raster(HAZ_DEMO_FL, geometry=[poly])

I suggest to rename the kwarg to geometries and correctly specify its type as list of shapely.geometries in the docstring. Then go through the core and petals code base and replace the calls to read_raster accordingly. There are only a few places that are affected by this. Furthermore, I don't think that we need to have backwards compatibility here, because my impression is that the read_raster function is so overwhelming that almost nobody uses it outside of the CLIMADA code.

Note that this issue also affects the Exposures.from_raster functionality since the arguments are basically passed on to the read_raster function:

def from_raster(cls, file_name, band=1, src_crs=None, window=False,
geometry=False, dst_crs=False, transform=None,
width=None, height=None, resampling=Resampling.nearest):
"""Read raster data and set latitude, longitude, value and meta
Parameters
----------
file_name : str
file name containing values
band : int, optional
bands to read (starting at 1)
src_crs : crs, optional
source CRS. Provide it if error without it.
window : rasterio.windows.Windows, optional
window where data is
extracted
geometry : shapely.geometry, optional
consider pixels only in shape
I would still claim that we don't need backwards compatibility for the geometry argument in the Exposures.from_raster function, and propose to change this argument to geometries, as well.

@emanuel-schmid
Copy link
Collaborator Author

@tovogt Excellent! 🥇 Many thanks for the profound analysis. Your suggestions make a lot of sense. I think I'm just going to implement them.

@emanuel-schmid emanuel-schmid marked this pull request as draft June 1, 2023 08:23
@emanuel-schmid
Copy link
Collaborator Author

I've reversed the check for MultiPolygon and made it officially the responsibility of the caller to make sure the geometry argument is a list of shapely.geometry. That is, I updated the doc strings, but not the methods.
This is now the case for 5 related methods.

While at it, I changed the default values for many arguments in these methods to None where False seemed less appropriate.

I didn't change the argument names, reluctant to alter the signature without being forced to. 🤷

Bottom line: nothing has changed, the PR is reduced to cosmetics.

@emanuel-schmid emanuel-schmid marked this pull request as ready for review June 2, 2023 16:05
Copy link
Collaborator

@tovogt tovogt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have a tiny suggestion how to clean up the code. But since it only affects the tests, it's not really important. The rest is fine with me.

climada/entity/exposures/test/test_nightlight.py Outdated Show resolved Hide resolved
climada/entity/exposures/test/test_nightlight.py Outdated Show resolved Hide resolved
@tovogt
Copy link
Collaborator

tovogt commented Jun 5, 2023

Thanks, looks good!

@emanuel-schmid emanuel-schmid merged commit 5c5f87d into develop Jun 5, 2023
@emanuel-schmid emanuel-schmid deleted the feature/fix_multipolygon_mask branch June 5, 2023 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

shapely 2.0 breaks tests in TestRiverFlood
2 participants