Python and calculating the X and Y coordinates of line start and line end.

3444
3
Jump to solution
10-12-2012 05:53 AM
GregParent
New Contributor III
Hello ArcGIS Forum Users,
I have been able to use Python to copy a polyline dataset and add four fields (FROM_X, TO_X, FROM_Y, TO_Y) but am having trouble with populating the fields with the appropriate information. I would like to use Python to populate them in the following manner:

FROM_X = X Coordinate of Line Start
TO_X = X Coordinate of Line End
FROM_Y = Y Coordinate of Line Start
TO_Y = Y Coordinate of Line End

I know it can be done manually by opening the datasets attribute table, right clicking on a field, selecting Calculate Geometry and then the appropriate Property but I would like to have a Python script be able to do this so it can be embedded in a SAS script. The COPY and ADD FIELD Python script works in SAS so I am hoping the CALCULATE FIELD will as well.

I tried using the Data Management - Fields - Calculate Field and its Python option for FROM_X using:

arcpy.CalculateField_management(dataset, fieldname, !FROM_X!.firstpoint.x,"PYTHON_9.3","") 


The script runs without errors but does nothing to my dataset. I suspect I am missing a step but do not know what to do next.

Any input you may provide is greatly appreciated.

Thank you.

Greg Parent
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
T__WayneWhitley
Frequent Contributor
Try this:
- put the '!' around the whole expression and fetch the value from the shape field, i.e., "!SHAPE.FIRSTPOINT.X!"
- your field name goes where "fieldname" is, i.e., "FROM_X"
- no code block, so you don't really need to include the empty string for the 5th param, but this may not matter.
- the 'dataset' param is your fc
- remember, all parameters are strings, so you need the quotes (unless you use variables)

See:
http://resources.arcgis.com/en/help/main/10.1/index.html#//005s00000029000000

View solution in original post

0 Kudos
3 Replies
T__WayneWhitley
Frequent Contributor
Try this:
- put the '!' around the whole expression and fetch the value from the shape field, i.e., "!SHAPE.FIRSTPOINT.X!"
- your field name goes where "fieldname" is, i.e., "FROM_X"
- no code block, so you don't really need to include the empty string for the 5th param, but this may not matter.
- the 'dataset' param is your fc
- remember, all parameters are strings, so you need the quotes (unless you use variables)

See:
http://resources.arcgis.com/en/help/main/10.1/index.html#//005s00000029000000
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Greg,

Try the following:

arcpy.CalculateField_management(dataset, "FROM_X", !Shape!.firstPoint.X,"PYTHON_9.3","")


Since you are calculating the value from the geometry, you will need to use the Shape field.

Note:  Python is case sensitive.
0 Kudos
GregParent
New Contributor III
Thank you Wayne and Jake.
You both deserve the green check mark for providing the answer.
Thanks again.
Greg Parent
0 Kudos