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

Shapefile shape has invalid polygon: no exterior rings found (must have clockwise orientation #219

Closed
coneheadusa opened this issue Aug 24, 2021 · 2 comments

Comments

@coneheadusa
Copy link

The shape file issue I'm having was closed on the cartopy site by upgrading to the latest gshhg-gmt. (SciTools/cartopy#1659). I have the latest version of the gshhg-gmt files. I'm using using pyshp==2.1.3 and the cartopy reference is using pyshp==2.1.2. I have not tried to use pyshp==2.1.2.
Please advise.

Using this OS and listed python modules below.

OS sles:15:sp2
Python 3.8.6
pyshp==2.1.3
Shape files : gshhg-gmt-2.3.7

import matplotlib.pyplot as plt;
import cartopy.crs as ccrs;
import cartopy;
fig = plt.figure();
proj = ccrs.Mercator(central_longitude = 0);
ax = plt.axes(projection = proj);
ax.add_feature(cartopy.feature.GSHHSFeature(levels = (1, 2), linewidth = 2.0));
print("before");
fig.savefig("test.png");
print("after");

/apps/prod/python-modules/3.8.6/intel/19.1.3.304/lib/python3.8/site-packages/shapefile.py:391: UserWarning: Shapefile shape has invalid polygon: no exterior rings found (must have clockwise orientation); interpreting holes as exteriors.
warnings.warn('Shapefile shape has invalid polygon: no exterior rings found (must have clockwise orientation); interpreting holes as exteriors.')

@karimbahgat
Copy link
Collaborator

Thanks for raising this issue. In short, this is nothing to worry about, it's just a warning. The warning indicates that the GSHHS shapefile (GSHHS_c_L1.shp in the cartopoy repo) contains some incorrectly written shapes which may have resulted in incorrect GeoJSON representation of the shapefile. After some digging, the culprits are the shapes at index 95 and 471, which probably are just buggy artifacts. For instance, shape 95 only has a single polygon ring and yet that ring has a counter-clockwise orientation (positive area) which according to the shapefile spec is reserved for polygon holes:

>>> import shapefile
>>> r = shapefile.Reader('path/to/GSHHS_c_L1.shp')
>>> shape = r.shape(95)
>>> shape.parts
[0]
>>> shapefile.signed_area(shape.points)
40.67041399237628

Because a hole without a polygon exterior doesn't make much sense, PyShp displays a warning. It means that either the shape in the original shapefile was supposed to be a polygon exterior but was written using the wrong orientation, or that it was truly a hole and was accidentally written to the file. To preserve as much of the data as possible, PyShp makes the assumption that it was intended to be an polygon exterior and includes them in the GeoJSON as such, and notifies the user about the issue.

The warning messages were not necessarily the most informative, so I just added improvements that makes this more clear and also reports the shapefile index of the affected shapes. Should be available in the next version.

@karimbahgat
Copy link
Collaborator

Included in the latest v2.2.0. Now prints logging.warning() with info for each shape where this happens so that the user can actually do something about it. To suppress the warnings, the user can set shapefile.VERBOSE = False.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants