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).
  • 2
    I don't immediately see any "syntax" errors. Can you post the exact error that you're getting? – Kristen G. Jan 11 '17 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. – Hornbydd Jan 11 '17 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? – eigenvector Jan 11 '17 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. – eigenvector Jan 11 '17 at 15:51
  • 1
    Have you tried using PYTHON_9.3 instead of PYTHON (last parameter)? – BritishSteel Jan 11 '17 at 15:57
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.

  • 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 Jan 11 '17 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 – Clubdebambos Jan 12 '17 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 Jan 12 '17 at 12:33
  • Is it possible to provide a zipped file of the shapefile or a sample of the data? – Clubdebambos Jan 12 '17 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 Feb 6 '17 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.