2

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).
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jan 11, 2017 at 14:01
7
  • 2
    I don't immediately see any "syntax" errors. Can you post the exact error that you're getting? Commented Jan 11, 2017 at 14:36
  • Also it is unhelpful when you replace the true code with some generic message as you have for the variable 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. Commented Jan 11, 2017 at 14:57
  • I have updated the exact error message. Thank you for the hint, Kristen G. Unfortunately, there is no new information in there, is it? What else information could I provide? What else could I test? Commented Jan 11, 2017 at 15:48
  • 1
    Thank you for your comment, Hornbydd. I have added the "true" code. However, I find the downvote (if its related) rather unfair. With any problem and asking for advice it is always a balance between providing too few or providing too much code. If it helps you detect the error, though, fair enough. Commented Jan 11, 2017 at 15:51
  • 1
    Have you tried using PYTHON_9.3 instead of PYTHON (last parameter)? Commented Jan 11, 2017 at 15:57

1 Answer 1

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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Jan 11, 2017 at 16:02
5
  • 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? Commented 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 centroids Commented 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? Commented Jan 12, 2017 at 12:33
  • Is it possible to provide a zipped file of the shapefile or a sample of the data? Commented 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. Commented Feb 6, 2017 at 0:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.