Open guziy opened 8 months ago
Hello @guziy, thanks for the report and the reproducing code.
Unfortunately pickle files are not portable across python environments, so it is unlikely that we will be able to run the reproducing example as-is. Could you save in some other format? NetCDF should be fine if you are used to using that.
It would also be useful to know what versions of other packages you are using, particularly shapely and matplotlib. I note that the error is being raised in shapely.
Here are the versions of matplotlib and shapely:
Python 3.10.12 | packaged by conda-forge | (main, Jun 23 2023, 22:40:32) [GCC 12.3.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.14.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import matplotlib, shapely
In [2]: matplotlib.__version__
Out[2]: '3.6.3'
In [3]: shapely.__version__
Out[3]: '2.0.1'
I've updated the code and uploaded the input netcdf file.
Thanks @guziy I confirm I have reproduced the error with our main
development branch.
The specific error here is caused by this line
https://github.com/SciTools/cartopy/blob/97c6a6488d2edfd4780ac2d8df7b0014560e4917/lib/cartopy/crs.py#L1210
It seems that adding a zero buffer to a Polygon
can create a MultiPolygon
.
I tried this
diff --git a/lib/cartopy/crs.py b/lib/cartopy/crs.py
index ba505dab..1c6c7f2d 100644
--- a/lib/cartopy/crs.py
+++ b/lib/cartopy/crs.py
@@ -1226,7 +1226,11 @@ class Projection(CRS, metaclass=ABCMeta):
polygon = boundary_poly.intersection(polygon)
if not polygon.is_empty:
- polygon_bits.append(polygon)
+ if isinstance(polygon, sgeom.MultiPolygon):
+ polygon_bits.extend(polygon.geoms)
+ print('multipoly')
+ else:
+ polygon_bits.append(polygon)
if polygon_bits:
multi_poly = sgeom.MultiPolygon(polygon_bits)
which does get us past the error. However the plot that comes out looks like
when it should look more like
So my patch appears to treat one symptom rather than the cause of the problem.
Description
I am trying to plot a field on a stereographic projection with contourf. The contourf fails if lat/lon are provided along with the transformation. It seems to be working if I project the coordinates first and pass the projected coordinates to contourf. If I convert the data array into a plain numpy array contourf does not fail but the plot is wrong.
Below I provide the input file along with the code to reproduce the problem.
Code to reproduce
input file 2024010400-dump.nc.zip
Traceback
Full environment definition
### Operating system Linux ### Cartopy version 0.21.1 ### conda list ``` ``` ### pip list ``` ```