I can create a centroid latitude field in ArcMap10.1 using "Calculate Geometry..." in the attribute table, but when I try the same thing using: arcpy.CalculateField_management(bins_WGS84, "cent_lat", "!SHAPE.CENTROID.Y!", "PYTHON_9.3"),
I get the following error: arcgisscripting.ExecuteError: ERROR 000539: Error running expression: GPVARIANTOBJECT0.CENTROID.Y. It only happens occasionally, but kills my script and costs me lots of time/data. On the particular dataset I'm testing, it happens about 10 rows in, although the geometry looks fine and Arcmap doesn't complain when finding the centroid.
I know it is possible, because it's working for the same dataset in ArcMap, however, this action does not show up in the Results window.
Answer
I would guess there's something wrong with your geometry.
Try using RepairGeometry_management
prior to your field calculation.
If this doesn't do the trick, a workaround is probably needed.
I would use FeatureToPoint_management
, followed by AddXY_management
, followed by SpatialJoin_analysis
.
Edit: The best method might be via an UpdateCursor. Then you can use a try/except to find out just which features are causing the issue, and investigate further.
cursor = arcpy.da.UpdateCursor (bins_WGS84, ["cent_lat", "OID@", "SHAPE@TRUECENTROID"])
for row in cursor:
try:
row[0] = row[2][1]
cursor.updateRow(row)
except:
print "Could not add Y value to", row[1]
del row
del cursor
No comments:
Post a Comment