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

Aggregation to a polygon #36

Open
BSchilperoort opened this issue Apr 3, 2024 · 1 comment
Open

Aggregation to a polygon #36

BSchilperoort opened this issue Apr 3, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@BSchilperoort
Copy link
Contributor

While it is not really regridding, aggregation to a shapefile (polygons) should also be possible as an extension to the current capabilities. This can aid, e.g., hydrologists in aggregation of precipitation to a basin.

PyShp can read shapefiles and is implemented in pure python.
Shapely has binary wheels on PyPI so it does not require compilation or conda.

In a quick test I have managed to get a 2D array with overlap fractions between a polygon from a shapefile and a grid.

  1. Load in shapefile, grab first polygon
  2. Turn grid coordinates into polygons using shapely.box
  3. Compute overlap between every grid box and the shapefile polygon
  4. Massage back into 2D array

This 2D array can be converted into weights to transform the gridded data into mean values. The 2D array can be (very) sparse.

Additional optimization can occur by making off part of the grid that is out of bounds of the shapefile polygon. This will speed up computation significantly.

Quick demo here: https://gist.github.com/BSchilperoort/db955c4183b3721493875688d7417026

@BSchilperoort BSchilperoort added the enhancement New feature or request label Apr 3, 2024
@BSchilperoort
Copy link
Contributor Author

BSchilperoort commented Apr 3, 2024

This post by @rabernat actually goes through the entire workflow, in an efficient manner: https://discourse.pangeo.io/t/conservative-region-aggregation-with-xarray-geopandas-and-sparse/2715

Instead of geopandas, we can make use of shapely and pyshp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant