Error on Centroid from Polygon script

1882
11
Jump to solution
05-04-2017 01:09 PM
MosquitoGIS
Occasional Contributor

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?

0 Kudos
1 Solution

Accepted Solutions
IanMurray
Frequent Contributor

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

View solution in original post

11 Replies
IanMurray
Frequent Contributor

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

MosquitoGIS
Occasional Contributor

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?

0 Kudos
MicahBabinski
Occasional Contributor III

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

MosquitoGIS
Occasional Contributor

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.

MicahBabinski
Occasional Contributor III

Cool, man! Good for you for customizing your way out of that license level conundrum.

0 Kudos
MosquitoGIS
Occasional Contributor

That is the idea!  So far, so good!  

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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.

NeilAyres
MVP Alum

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.

MosquitoGIS
Occasional Contributor

That is exactly what I did to look for them!  Great advice!

0 Kudos