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

Subscript-crop Raster or Vector by bracket call [], consistency and bug fixes #335

Merged
merged 23 commits into from
Jan 20, 2023

Conversation

rhugonnet
Copy link
Member

@rhugonnet rhugonnet commented Jan 4, 2023

Summary

This PR updates the get_geoprojected_bounds function, moving its core to projtools to make it consistent and accessible for both Raster and Vector classes.
Then, it adds the same options in Vector.crop() than for Raster.crop() (= passing another Raster, Vector or a list of bounds).
Finally, it implements the __getitem__ (= []) method for Raster and Vector, which calls their underlying crop functionality (= subsetting), as suggested previously by @erikmannerfelt.

rst = Raster(fn_rst)
vec = Vector(fn_vec)

# Get only Raster on extent of Vector
rst = rst[vec]

Other than that, several bug fixes and test additions.

Discussion on the __getitem__ implementation

Several things:

  • We could add the option of provides slice objects, which would then be used on self.data for Raster (subsetting np.MaskedArray in 2D) and self.ds on Vector (subsetting gpd.GeoDataFrame in 1D).
  • We could, by default, reproject instead of crop when it is Raster[Raster] (only one where it makes sense)? The resampling algorithm could be passed as second argument Raster[Raster, "bilinear"].

Related issues and more details

This PR:

Also it:

  • Removes the dependency of projtools.py on raster.py and geovector.py,
  • Moves the core of the get_geoprojected_bounds function in Raster to projtools.py,
  • Adds get_geoprojected_bounds as a method for the Vector class,
  • Mirrors the crop functionality of the Raster class for Vector objects (can provide any type of geometry: bounds, Raster, Vector),
  • Adds more test for Raster.polygonize(),
  • Solves a new bug with checking void in intersection triggered by package updates.

@rhugonnet rhugonnet changed the title Small fixes and consistency update Subsetting by bracket call [], consistency and bug fixes Jan 4, 2023
@rhugonnet rhugonnet changed the title Subsetting by bracket call [], consistency and bug fixes Subscript-crop by bracket call [], consistency and bug fixes Jan 4, 2023
@rhugonnet rhugonnet changed the title Subscript-crop by bracket call [], consistency and bug fixes Subscript-crop Raster or Vector by bracket call [], consistency and bug fixes Jan 4, 2023
Copy link
Contributor

@erikmannerfelt erikmannerfelt left a comment

Choose a reason for hiding this comment

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

Hey Romain!
These additions are awesome. You're really contributing to this being maintainable in the long term! On the subject of maintainability, I have some comments on details that I struggle to understand, but generally, it's great!

Regarding your two suggestions about the subscripting:

  • On the first, do you mean to slice by indices like arr.isel() or df.iloc[] in xarray and pandas, respectively? If so, it would actually be pretty cool, but I struggle to see a use case. For iterating along blocks perhaps? Or subsetting for debug purposes? Those two are good arguments in my head, but for the amount of complexity they introduce, it might not be a great idea unless there are more uses.
  • I personally think the subscripting syntax should only use crop. Reproject can be very cpu-intensive and should in my opinion always be explicit. Cropping, however, is really fast, so there the subscripting syntax makes a ton of sense to me.

tests/test_georaster.py Show resolved Hide resolved
tests/test_georaster.py Show resolved Hide resolved
geoutils/spatial_tools.py Outdated Show resolved Hide resolved
tests/test_georaster.py Outdated Show resolved Hide resolved
tests/test_georaster.py Show resolved Hide resolved
Copy link
Member

@adehecq adehecq left a comment

Choose a reason for hiding this comment

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

Great idea to make crop more consistent and thanks for fixing all the small issues.
The brackets are a nice feature, but they would need to be documented at some point, otherwise, we'll just forget that it's possible !

@rhugonnet
Copy link
Member Author

Thanks a lot! All updated, merging 😄

@rhugonnet rhugonnet merged commit 7e7ad86 into GlacioHack:main Jan 20, 2023
@rhugonnet rhugonnet deleted the small_fixes branch January 20, 2023 05:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants