1

I am using Python and shapely to do certain spatial calculations (difference, intersections etc).

Bur before I can do the calculations I first need to get the object into a shapely object. so I iterate through the shapes in a feature class using arcpy.da.searchcursor:

with arcpy.da.SearchCursor(OurCADLAYER, ['SHAPE@'], whereclause) as onerows:
        for onerow in onerows:

and then assign the shapely object here:

            Shape2 = shape(onerow[0])

and this is where I get the error message: TypeError: 'NoneType' object has no attribute getitem

PS. THIS ONLY HAPPENS when the shape is a Polygon that has holes (interior rings). no issues with normal polygons..

  • 1
    Passing an Esri Shape object into another software implementation is a huge leap of faith. I'd recommend using Well -Known Text or Binary. What calculations are you doing that ArcPy doesn't support? – Vince Sep 2 '16 at 11:02
  • ArcPy has all the necessary calculations, but I am getting errors, for example: I am trying to use the difference method: ShapeDiff = Shape1.difference(Shape2) but I get a ValueError: <geoprocessing describe geometry object object at 0x000000000D05A5A8 – P Holtzhausen Sep 2 '16 at 12:39
  • This is a different question, which probably relates back to not using the correct format. – Vince Sep 3 '16 at 1:04
1

I don't know the result of arcpy.da.SearchCursor(OurCADLAYER, ['SHAPE@'], whereclause) (I don't use ArGIS) but the function shape of Shapely is for the the GeoJSON-like Python geo_interface protocol (conversion of a GeoJSON feature to a shapely geometry)

1) GeoJSON

from shapely.geometry import shape
poly_hole={'type': 'Polygon', 'coordinates': (((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0), (0.0, 0.0)), ((1.0, 1.0), (1.0, 1.5), (1.5, 1.5), (1.5, 1.0), (1.0, 1.0)))}
poly = shape(poly_hole)
print(poly)
POLYGON ((0 0, 0 2, 2 2, 2 0, 0 0), (1 1, 1 1.5, 1.5 1.5, 1.5 1, 1 1))
mapping(poly)
{'type': 'Polygon', 'coordinates': (((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0), (0.0, 0.0)), ((1.0, 1.0), (1.0, 1.5), (1.5, 1.5), (1.5, 1.0), (1.0, 1.0)))}
# or
poly.__geo_interface__
{'type': 'Polygon', 'coordinates': (((0.0, 0.0), (0.0, 2.0), (2.0, 2.0), (2.0, 0.0), (0.0, 0.0)), ((1.0, 1.0), (1.0, 1.5), (1.5, 1.5), (1.5, 1.0), (1.0, 1.0)))}

Look at:

ArcGIS 10 has the AsShape function for converting GeoJSON to arcpy geometries, which mimics Shapely's (As)Shape, and arcpy geometry objects implement the geo_interface standard (also from Shapely) for getting arcpy geometries back as GeoJSON.

2) as Vince says you can use the WKT format (same for WKB)

wkt = poly.wkt
from shapely.wkt import loads
poly_wkt = loads(wkt)
print poly
POLYGON ((0 0, 0 2, 2 2, 2 0, 0 0), (1 1, 1 1.5, 1.5 1.5, 1.5 1, 1 1))
0

Thank you gents for your assistance, I revisited this one after Vince's comment, And I managed to resolved it by using only arcpy (no shapely).

 Shape2 = shape(onerow[0])

now replaced with:

 Shape2 = onerow[0]

and then on with the calculations. The "Polygon with hole" does not seem to be an issue anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.