“左撇子”Spatia几何l Dataframe (with shapely)-- Any way to fix?

871
4
06-22-2021 07:17 PM
jcarlson
MVP Esteemed Contributor

I'm trying to do some simple overlays between a couple of spatially enabled dataframes. The layers are coming from feature services, usingquery(... as_df=True).

When I attempt an overlay, I get the following message, or something like it:

TopologyException: Input geom 1 is invalid: Hole lies outside shell at or near point

Which, in many cases, is absolute nonsense, because the geometry in question has no holes. It's not even multipart!

A bit of digging reveals that this is an issue with the right-hand-rule, that the order of vertices in my polygons is causing this behavior. The thing is, I can't find any way to "reverse direction" on polygons, except maybe breaking apart the SHAPE column and reversing the arrays of vertices.

Before I go to all that trouble, I wanted to see if anyone else has encountered this. Any pointers or ideas?

EDIT: I'm in a custom python env, so my SDFs are coming in as shapely geometries. Arcpy is able to handle this, but if possible, I would like to keep this process workable with the ArcGIS Python API only. The resulting script is intended to be run in various settings and by different users, not all of whom are able to utilize arcpy.

- Josh Carlson
Kendall County GIS
0Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

I would be surprised if the python api doesn't have a builtin method for doing this automatically, since having to use "orient" to change the polygon vertices order seems to be the way to go.

perhaps one of the other spatial representations does the reorientation behind the scenes

The Shapely User Manual — Shapely 1.7.1 documentation

pyqgis - Convert QGIS geometry into Shapely geometry to use orient method defined in Shapely - Geogr...


... sort of retired...
jcarlson
MVP Esteemed Contributor

I was surprised myself, but the only reference to polygon orientation in the API is:

Exterior rings are oriented clockwise, while holes are oriented counter-clockwise.

Simply a statement that theyareoriented a certain way, but no mention of a way to re-orient a polygon, or what else to do when the orientation does not follow the proper order.

Orientis the thing I want, but annoyingly, shapely.polygon != shapely.Polygon. Also my features are being returned as MultiPolygon types, so lots of nested iteration to break those out and orient them correctly, then reassemble into a geometry series of multipolygons. Sheesh.

- Josh Carlson
Kendall County GIS
0Kudos
DanPatterson
MVP Esteemed Contributor

this won't workshapely.polygon != shape.Polygon (I assume you are wanting to compare to arcpy

because shapely doesn't have M and Z defined by default.

I would just skip shapely and just indicate that geometry operations are open to arcpy people.


... sort of retired...
0Kudos
jcarlson
MVP Esteemed Contributor

Ha, no, I just meant that the polygon submodule and the Polygon geometry object were literally not equal to one another, i.e., different things. (I should probably leave code conventions out of my narrative text).

Theorientmethod takes a Polygon as an argument, so it would be

shapely.geometry.polygon.orient(Polygon(some WKT or something))

I'd hoped I could simply call orient on a Polygon object,Polygon.orient(), but no such luck. Definitely not comparing the actual shapes with anything else.

- Josh Carlson
Kendall County GIS
0Kudos