-
Notifications
You must be signed in to change notification settings - Fork 215
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
BUG: Fix handling of polygon holes when calculating area in Geod #686
Conversation
That is the intended behavior of
In shapely.geometry.polygon.orient() the interiors and exteriors are handled such that he signs change as expected. This method is used in shapely.ops.orient(). So, if the polygon's sign is changed using |
The sign may or may not be useful to some people. So, I think adding a |
Looking at this closer, it seems like I missed the switch between using the Your suggested solution in this PR might be the way to go. I will need to look into this and think on it 🤔. |
Related: pygeos/pygeos#71 |
@jacob-indigo I think your fix in this PR is the way to go as it is consistent with PostGIS and shapely's behavior. |
@snowman2 Thanks. Are you interested in having an unsigned variant? If so, I'm happy to add it. If not, 👍 |
I think that would be useful. Go for it 🚀 |
Having recently tripped over the pitfalls of winding order and signed areas when implementing this against |
@jacob-indigo this looks good. Do you want to sqash your commits or do you mind if I do |
@snowman2 Thanks. Feel free to squash and merge |
Because
geod.geometry_area_perimeter
is signed, the handedness of the geometries coming in matters. There is an existing comment that says that counter-clockwise windings result in positive areas. The docs also point to theshapely.geometry.polygon.orient
function which winds a polygon according to either the right-hand or left-hand rule. From the Shapely docs:That means that if you are finding the area of a polygon wound using
orient
, the exterior would generate a positive area and the interiors (holes) would generate negative areas. As is,Geod
subtracts the holes' areas which results inarea_exterior + area_interiors
instead ofarea_exterior - area_interiors
. Simply changing to an addition resolves the issue.For reference,
geos
computes the area of a polygon using unsigned areas of the rings, i.e.abs(area_exterior) - sum(abs(area_interior_x))
. See: https://github.com/libgeos/geos/blob/master/src/geom/Polygon.cpp#L374. It may be worthwhile considering either moving to an unsigned area inGeod
or offering an unsigned function as an option.