I am trying to calculate the centroids of a polygon and add the x- and y-coordinate to their respective fields:
arcpy.env.workspace = r"C:/..."
input_fc = "shapefiles/precincts_v.shp"
arcpy.AddField_management(input_fc, "X", "DOUBLE")
arcpy.AddField_management(input_fc, "Y", "DOUBLE")
arcpy.CalculateField_management(input_fc, "X", "!SHAPE.CENTROID@DECIMALDEGREES!.split()[0]", "PYTHON")
arcpy.CalculateField_management(input_fc, "Y", "!SHAPE.CENTROID@DECIMALDEGREES!.split()[1]", "PYTHON")
but I an invalid syntax error. Where is the mistake?
Edit: exact error message
Traceback (most recent call last):
File "C:\Users\mquentel\Dropbox\PLSS\CA voting data\Python\CONSTRUCT_merge_voting_and_census.py", line 17, in <module>
arcpy.CalculateField_management(input_fc, "X", "!SHAPE.CENTROID@DECIMALDEGREES!.split()[0]", "PYTHON")
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\management.py", line 3360, in CalculateField
raise e
ExecuteError: ERROR 000539: SyntaxError: invalid syntax (<expression>, line 1)
Failed to execute (CalculateField).
asked Jan 11, 2017 at 14:01
1 Answer 1
Replace your Field Calculator lines with
arcpy.CalculateField_management(input_fc, "X", "!SHAPE.CENTROID.X!", "PYTHON_9.3")
arcpy.CalculateField_management(input_fc, "Y", "!SHAPE.CENTROID.Y!", "PYTHON_9.3")
Works for me with Decimal Degrees.
answered Jan 11, 2017 at 16:02
-
Thank you for the suggestion. This seems to give me the following error: ExecuteError: ERROR 000539: Error running expression: GPVARIANTOBJECT0.CENTROID.X Traceback (most recent call last): File "<expression>", line 1, in <module> SystemError: error return without exception set Failed to execute (CalculateField). Any idea why?eigenvector– eigenvector2017年01月11日 16:27:23 +00:00Commented Jan 11, 2017 at 16:27
-
Does your file have a Shape field? Try using RepairGeometry_management first as per gis.stackexchange.com/questions/127232/… and then try again to get the centroidsClubdebambos– Clubdebambos2017年01月12日 07:44:04 +00:00Commented Jan 12, 2017 at 7:44
-
Thanks for the suggestions and the linked question. My shapefile has a shape field. And using RepairGeometry_management before did not change anything. Is there anything else I can do to get a clue what is going wrong?eigenvector– eigenvector2017年01月12日 12:33:31 +00:00Commented Jan 12, 2017 at 12:33
-
Is it possible to provide a zipped file of the shapefile or a sample of the data?Clubdebambos– Clubdebambos2017年01月12日 14:16:18 +00:00Commented Jan 12, 2017 at 14:16
-
I found a workaround with a similar (but different) shapefile. Your code works perfectly fine now. My problem was ultimately rooted in the broken shapefile which even could not be repaired with the RepairGeomety_management (good suggestion, though!). In any case, it is fixed now. Thank you.eigenvector– eigenvector2017年02月06日 00:39:21 +00:00Commented Feb 6, 2017 at 0:39
Explore related questions
See similar questions with these tags.
lang-py
input_fc
. How do we know that is not what you have actually run? If it is it is invalid, you need the full path or have set the geoprocessing workspace environment setting which we see neither.