pangeo-data / xESMF

Universal Regridder for Geospatial Data
http://xesmf.readthedocs.io/
MIT License
189 stars 34 forks source link

Warn of SpatialAverager error over large region and densify polygons #293

Closed charlesgauthier-udm closed 1 year ago

charlesgauthier-udm commented 1 year ago

This adresses #221. I added a check that verifies if sides of polygons passed to the SpatialAverager are longer than 1° in lat/lon space. This threshold was determined from a few tests indicating that past that length, errors on the weights computed by the SpatialAverager would be significant. If the threshold is met, a warning is given with a suggestion to "densify" the polygons using a function. The function is accessible through the SpatialAverager and takes in any polygon or multipolygon weither it has holes or not and adds points to sides that are larger than a specified max_length which is set to the same 1° threshold by default.

Example For a large polygon: p1 = Polygon([(-80, -40), (80, -40), (80, 40), (-80, 40)])

Using the densify_polys function results in: p2 = xe.SpatialAverager.densify_polys([p1]) p2: [<POLYGON ((-80 -40, -79.006 -40, -78.012 -40, -77.019 -40, -76.025 -40, -75....>]

With no side being larger than the default max_length of 1°

charlesgauthier-udm commented 1 year ago

Wow! Yes we can definitively use shapely.segmentize I kept thinking "why is this not already implemented somewhere?" Guess I have my answer. I'll also make the other requested changes.

charlesgauthier-udm commented 1 year ago

Implemented suggested changes. The densify_polys function is now very simple with the use of shapely.segmentize to the point where I'm wondering if its even needed. All the densify_polys function adds is the for loop on the polys passed to the function which I feel could be done by the user. Let me know what you think. If we decide to remove it, I can modify the warning in the SpatialRegridder to point to the shapely.segmentize function instead.

review-notebook-app[bot] commented 1 year ago

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB