So, for the record, I don't know jack about Python! And I am pretty sure this is a pretty basic question.
I want to create Centroids from polygon features. I found some code that does this:
import arcpy
arcpy.env.overwriteOutput = True
input_fc = "N:\\GeoDatabase.gdb\\SpotHistory"
output_fc = "\\\\SERVER\\Send\\CentroidHist"
cursor = arcpy.da.SearchCursor(input_fc, "SHAPE@XY")
centroid_coords = []
for feature in cursor:
centroid_coords.append(feature[0])
point = arcpy.Point()
pointGeometryList = []
for pt in centroid_coords:
point.X = pt[0]
point.Y = pt[1]
pointGeometry = arcpy.PointGeometry(point)
pointGeometryList.append(pointGeometry)
arcpy.CopyFeatures_management(pointGeometryList, output_fc)
The issue is it works on most of my polygon features no problem. But on another, I get the following error:
Traceback (most recent call last):
File "N:/Script.py", line 16 in <module>
point.X = pt[0]
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\arcobjets\_base.py", line 89, in _set
return setattr(self._arc_object, attr_name, cval(val))
RuntimeError: Point: Input value is not numeric
What do I need to do to fix this?
Solved! Go to Solution.
I'd recommend adding a print statement to see what value pt[0] and pt[1]is pulling for each point. The error is clearly receiving a non-numeric value which is causing it to crash(maybe a NaN, have you tried to check the geometry for errors or repair it?).
Instead of:
for pt in centroid_coords:
point.X = pt[0]
point.Y = pt[1]
just do
for pt in centroid_coords:
print str(pt[0])
print str(pt[1])
That should print to your screen each pair of values, and you should be able to determine which is a non-numeric value. I hope your polygon dataset isn't to big
I'd recommend adding a print statement to see what value pt[0] and pt[1]is pulling for each point. The error is clearly receiving a non-numeric value which is causing it to crash(maybe a NaN, have you tried to check the geometry for errors or repair it?).
Instead of:
for pt in centroid_coords:
point.X = pt[0]
point.Y = pt[1]
just do
for pt in centroid_coords:
print str(pt[0])
print str(pt[1])
That should print to your screen each pair of values, and you should be able to determine which is a non-numeric value. I hope your polygon dataset isn't to big
So the print out gives me all coordinates except for one set of "None." I am going to look through the features. Any tips on what would cause a none?
Hi Devin,
That's probably a feature with "Null Geometry". It's technically a feature in your feature class (and a row in the attribute table) but an empty value for the SHAPE field, which in Python-speak is represented by the None keyword. So, you could delete that feature or create a shape for it. Use the Check Geometry or Repair Geometry tools to find and correct that feature and any others like it.
On a side note, are you familiar with the Feature to Point tool? It'll get you polygon centroids nice and easy.
Micah
Makes sense, found the culprit. I am familiar with that tool, however when I go to use it, it says I am not authorized to use that tool. We only use ArcGIS Basic with no extensions, so we are a limited.
Cool, man! Good for you for customizing your way out of that license level conundrum.
That is the idea! So far, so good!
Since the OP is using the ArcPy Data Access cursors, it could either be an empty geometry or NULL. Unlike the original ArcPy cursors, the Data Access cursors return all empty geometries as None, which is beyond lame in my opinion. I discuss the topic in more detail in /blogs/tilting/2015/05/29/whats-in-your-feature-class-nothing-null-or-empty. Fortunately for the OP, Check Geometry catches empty geometries and NULL.
Or, if these are polygon geometries...
Open the attribute table and sort by shape_area. Those ones with area = 0, are likely to be null geometries.
That is exactly what I did to look for them! Great advice!