1

Using ArcGIS 10.1 and Python 2.7- I am trying to write an expression that calculates the mid point of a line. I've tried many different expressions, but I continue to get syntax errors.

#An example of one of my many failed attempts
arcpy.CalculateField_management(StreetCenterlines, "Latitude", "!SHAPE@X!")
arcpy.CalculateField_management(StreetCenterlines, "Longitude", "!SHAPE@Y!")

Can anyone lend a hand at how my expression should read in order to accomplish this?

The error I receive is: ERROR 999999: Error executing function. Syntax error Failed to execute (CalculateField).

  • 1
    What is the error you get from a full code snippet that you present? – PolyGeo Jun 23 '16 at 20:50
  • The error I receive is: ERROR 999999: Error executing function. Syntax error Failed to execute (CalculateField). – Salty Delgado Jun 23 '16 at 20:56
  • !Shape!.positionAlongLine( !Shape!.length/2).firstPoint.X Don't forget Python – FelixIP Jun 23 '16 at 22:06
  • Thanks for the help! Adding "Python" seems to have fixed this method (I thought that argument was optional, guess not). If I wanted to use this method and convert the Lat/Long coords to decimal degrees, do you know how I would do that? When I add the @DECIMALDEGREES conversion, I get a syntax error. arcpy.CalculateField_management(workingBrazosCenterlines, "Lat_MidPt", "!SHAPE.CENTROID.X@DECIMALDEGREES!", "PYTHON_9.3") ERROR 000539: SyntaxError: invalid syntax (<expression>, line 1) – Salty Delgado Jun 24 '16 at 13:02
3

It looks like you're mixing up data access update cursors and calculate field.

Use the data access update cursor (it's faster):

with arcpy.da.UpdateCursor (StreetCenterlines, ["Latitude", "Longitude", "SHAPE@X", "SHAPE@Y"]) as cursor:
    for lat, long, x, y in cursor:
        row = (x, y, x, y)
        cursor.updateRow (row)
  • Thank you for the help! This method worked as well. Looks like I need to learn the new data access methods. If I wanted to convert these X, Y points to decimal degrees, is there a way with this method? – Salty Delgado Jun 24 '16 at 13:07

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.