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:

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

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

- 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