Want Python code for what Calculate Geonetry tool does for xy to meters

446
5
01-06-2014 08:13 PM
AOrlich
New Contributor
Hello

I have points with XY coordinates in GCS84 and need to convert them to meters before extracting raster cell values from the points. I know I can do this with the Calculate Geometry window from the drop-down menu off the field name in the attribute table, but I'd like to add this step into a longer python code after adding the new field.

Should be easy, right? The Calculate Geometry window asks for the following input (I've entered mine):
Property: Y Coordinate of Point
Coordinate System: Use coordinate system of the data frame (PCS:Polar Stereographic)
Units: Meters

Thanks in advance for any ideas or help!
~Alice
Tags (2)
0 Kudos
5 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Alice,

You can do this using the UpdateCursor.  You can specify a coordinate system that's in meters.  Ex:

fc = "Points"

sr = arcpy.SpatialReference(3032)

with arcpy.da.UpdateCursor(fc, ["SHAPE@XY", "XCOORD", "YCOORD"], "", sr) as cursor:
    for row in cursor:
        row[1] = row[0][0]
        row[2] = row[0][1]
        cursor.updateRow(row)

del row, cursor


You can find the code of your coordinate system within the data frame properties > Coordinate System tab:

[ATTACH=CONFIG]30312[/ATTACH]
0 Kudos
RichardFairhurst
MVP Honored Contributor
Hi Alice,

You can do this using the UpdateCursor.  You can specify a coordinate system that's in meters.  Ex:

fc = "Points"

sr = arcpy.SpatialReference(3032)

with arcpy.da.UpdateCursor(fc, ["SHAPE@XY", "XCOORD", "YCOORD"], "", sr) as cursor:
    for row in cursor:
        row[1] = row[0][0]
        row[2] = row[0][1]
        cursor.updateRow(row)

del row, cursor


You can find the code of your coordinate system within the data frame properties > Coordinate System tab:

[ATTACH=CONFIG]30312[/ATTACH]


In addition to the above, you can also set the Transformation environment setting prior to opening your cursor and the transformation will be applied (you don't have to reference the transformation in the cursor set up).  For example:

arcpy.env.geographicTransformations = "NAD_1983_To_WGS_1984_5"

I also wanted to mention that if you use a cursor with an alternative spatial reference on an XY Event Layer, the original layer coordinates in the table will automatically be converted just by opening the cursor even if you don't loop through any records.  That behavior is potentially useful if you wanted the coordinates overwritten in their original fields anyway, but dangerous if you meant to keep the original spatial reference coordinate values and assign the transformed coordinates to another set of fields.  To only transform coordinates in a new set of fields in an XY Event layer you should create the new coordinate fields in the table, duplicate the original spatial reference coordinates from the original fields into the new fields, create the XY Event layer using the new fields for the coordinates and the original spatial reference of those coordinates, and then apply the cursor with the alternative spatial reference.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Also, if you upgrade to 10.2.1, there is a new tool called Add Geometry Attributes that will do this.
0 Kudos
AOrlich
New Contributor
Hello again~

Thanks for your quick replies. I did try to apply the script you provided after combining both of your suggestions and got some runtime errors, a few that were typos, but the final claimed the field is not nullable (my Lon_M field). I have a IT order in and hope someone will update my license to 10.2 so I can work directly with the Add Geometry Attributes tool!

If I don't get the update in the next few days, I might have to ask for more specific direction.

Cheers,
Alice
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Alice,

Be sure you are requesting to upgrade to 10.2.1 and not just 10.2.  This tool just became available at 10.2.1.
0 Kudos