[GIS] ArcPy script error – NoneType object has no attribute X

arcgis-10.2arcpyattributeerror

I'm familiarising myself with using scripting in ArcMap 10.2 and as part of this I'm using the following to extract vertices from some polygons.

... 
... import arcpy
... from arcpy import env
... env.workspace = "C:/WORKING"
... fc = "Dist_Footprint.shp"
... cursor = arcpy.da.SearchCursor(fc, ["OID@", "SHAPE@"])
... for row in cursor:
...     print("Feature {0}: ".format(row[0]))
...     for point in row[1].getPart(0):
...         print ("{0}, {1}".format(point.X,point.Y))

It chugs through pulling out co-ordinates ok until I get the following error message.

Runtime error 
Traceback (most recent call last):
  File "<string>", line 11, in <module>
AttributeError: 'NoneType' object has no attribute 'X'

I've had a look at a few previous questions to try to get some answers eg. https://stackoverflow.com/questions/19946947/tkinter-nonetype-object-has-no-attribute-pack-still-works. These flag that there is a variable whose value is None, and the script is trying to do None.X(), or NoneType means that the data source could not be opened.


1) The script does not finish the first polygon. I have displayed the Python-generated co-ordinates in ArcMap and know which polygon it is. Also have run the geometry check on the polygon by itself. No apparent issues. ArcGIS still says no issues with main file. I have checked with QGIS which says there are some issues but NOT related to the polygon flagged by Python. In QGIS I get 4 invalid geometry errors along the lines of "segment 0 of ring 0 of polygon 0 intersect 0 of ring 0 of polygon 2 at X, Y location" related to other polygons.

2) I have run the script on a previous version of the problem file and it gets to Feature 16 before falling over. Running the original script on some simple polygons works fine so looks like the script is ok. Back to the drawing board with the original file (not sure where to go as the check geometry tool says it's ok).

Anybody suggest what other tools I can use to work out where the problem is?

Best Answer

This look like there is a null geometry somewhere. To solve this, you should first run the "repair" tool. To avoid you code to crash (as you said that it works for the first polygons), you can add some testing

if point:
    print ("{0}, {1}".format(point.X,point.Y))
else:
    print "No point"
Related Question