-
Notifications
You must be signed in to change notification settings - Fork 371
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
TypeError: 'GeometryCollection' object is not subscriptable (cartopy calling shapely) #2176
Comments
This is likely some bad projection going on when transforming the geometries near a boundary. As a workaround, you can use the |
Thanks @greglucas! This tweak gets me back up and running again, great. Should I close this issue, or leave this open for a more permanent diagnosis and fix? Apologies, I became confused as to what was going on, so without guidance you will not be getting a PR from me. |
I think we should leave it open and hope for a better solution rather than a workaround. If you're interested in tackling this that would be great! It may not be too difficult, but it is sometimes hard to tell how deep into the code you'll have to get. If you want some guidance, I'd suggest isolating the failure geometry from the contour call. Adding some print or
Then find out why that input geometry gets transformed into an output |
I encountered the same problem. Trace to cartopy.crs.Projection_ Rings_ To_ Multi_ Polygon method. When boundary_poly. intersection (polygon), a GeoCollection object is obtained instead of a Polygon object, resulting in Exception thrown when sgeom.MultiPolygon(polygon_bits).
|
I'm also seeing this error with cartopy (version 0.22.0) and shapely version 2 and above (dropping down to version 1.8.x allows things to work properly). In this particular case it's coming from Xarray DataArray.plot which uses pcolormesh and pcolor I can try to dig into this a bit more and/or put together a MRE, but wanted to at least log this here in case it helps other users or devs.
```
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[7], line 13
10 tas_present = test.sel(time="2014-07").squeeze()
12 # plot the model data
---> 13 (tas_present - 273.15).plot(
14 ax=ax_present,
15 x="lon",
16 y="lat",
17 transform=ccrs.PlateCarree(),
18 vmin=-10,
19 vmax=30,
20 cmap="magma"
21 #robust=True
22 )
23 ax_present.coastlines()
24 ax_present.set_title("July 2014")
File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/xarray/plot/accessor.py:48, in DataArrayPlotAccessor.call(self, **kwargs) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/xarray/plot/dataarray_plot.py:309, in plot(darray, row, col, col_wrap, ax, hue, subplot_kws, **kwargs) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/xarray/plot/dataarray_plot.py:1601, in _plot2d..newplotfunc(failed resolving arguments) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/xarray/plot/dataarray_plot.py:2309, in pcolormesh(x, y, z, ax, xscale, yscale, infer_intervals, **kwargs) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/cartopy/mpl/geoaxes.py:315, in _add_transform..wrapper(self, *args, **kwargs) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/cartopy/mpl/geoaxes.py:1783, in GeoAxes.pcolormesh(self, *args, **kwargs) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/cartopy/mpl/geoaxes.py:1965, in GeoAxes._wrap_quadmesh(self, collection, **kwargs) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/cartopy/mpl/geoaxes.py:315, in _add_transform..wrapper(self, *args, **kwargs) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/cartopy/mpl/geoaxes.py:2012, in GeoAxes.pcolor(self, *args, **kwargs) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/matplotlib/collections.py:266, in Collection.get_datalim(self, transData) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/matplotlib/collections.py:266, in (.0) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/matplotlib/transforms.py:2439, in CompositeGenericTransform.transform_path_non_affine(self, path) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/cartopy/mpl/geoaxes.py:186, in InterProjectionTransform.transform_path_non_affine(self, src_path) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/cartopy/crs.py:817, in Projection.project_geometry(self, geometry, src_crs) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/cartopy/crs.py:972, in Projection._project_polygon(self, polygon, src_crs) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/cartopy/crs.py:1233, in Projection._rings_to_multi_polygon(self, rings, is_ccw) File ~/miniconda3/envs/geocat-examples/lib/python3.11/site-packages/shapely/geometry/multipolygon.py:79, in MultiPolygon.new(self, polygons) TypeError: 'GeometryCollection' object is not subscriptable
|
We are getting the same issue. Somehow a GeometryCollection appears. Modifying rings_to_multipolygon to skip any GeometryCollections fixes the issue. It's probably the result of a weird geometry in the underlying contours, but impossible to tell exactly what when working with very big datasets. I would suggest adding a mechanism to skip GeometrieCollection if present within polygon_bits and throw a warning, or recursively call rings_to_multipolygon with the GeometryCollection parts ? |
Description
I have hit a repeatable, but confusing error with ~20 timesteps in a long ~1800 timestep global array. The error appears to be sensitive to the levels specified in the optional
levels
argument, and is reproduced below. It triggers aGeometryCollection
error in the shapely library call (see traceback below).I have included a working example below, which loops through two timesteps, the first works great plotting cleanly, and the second bombs, triggering the
GeometryCollection
bug. The error continues on subsequent timesteps, after ~20 or so, it starts to work again.I would appreciate any pointers to resolving this issue, or tips at identifying a workaround to get me plotting through the problem ~20 timesteps.
Code to reproduce
Data to reproduce
cartopyLevelBugTwoTimesteps.nc.gz
Traceback
Full environment definition
Operating system
Cartopy version
conda list
pip list
The text was updated successfully, but these errors were encountered: