Select to view content in your preferred language

Python Field Calculator - State Plane to Lat/Long project on the fly?

1231
4
06-13-2019 11:54 AM
RichardCitkowicz1
New Contributor

Without creating a new feature class to define the projection, I am looking for a way to Field Calculate values to WGS84 Lat/Long from State Plane ft.

The field calculator expression works great but uses the projection of the feature class using this expression !shape.extent.XMax! and !shape.extent.YMax! and adds the state plane feet values into the field specified but I want it to be Lat/Long not feet nor do I not want to create new data nor new fields.

How can I add the cursor? expression to project on the fly and calculate Lat and Long Values instead of State Plane feet which is what the projection is of the feature class.

0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor

You can do this by specifying a Geographic Coordinate system.  For example WGS 84 has a WKID of 4326.  Here is an example of using an update cursor with this projection and updating the lat, long fields:

import arcpy
from arcpy import env
env.workspace = r"C:\temp\python\test.gdb"

fc = "vehicles"

sr = arcpy.SpatialReference(4326)

with arcpy.da.UpdateCursor(fc, ["LONG", "LAT", "SHAPE@X", "SHAPE@Y"], "", sr) as cursor:
    for row in cursor:
        row[0] = row[2]
        row[1] = row[3]
        cursor.updateRow(row)

del cursor

Note, to find the WKID you can right-click on the Data Frame > Properties > select the coordinate system > Details:

RichardCitkowicz1
New Contributor

Still not getting the results.

import arcpy
from arcpy import env
env.workspace = "C:\Users\rcitkowicz\OneDrive - SJ Industries\SJI Files\MaximoSample_WorkOrders.gdb"

fc = "WO_POINT"

sr = arcpy.SpatialReference(4326)

with arcpy.da.UpdateCursor(fc, ["LONG", "LAT", "SHAPE@X", "SHAPE@Y"], "", sr) as cursor:
for row in cursor:
row[0] = row[2]
row[1] = row[3]
cursor.updateRow(row)

del cursor

My LAT/LONG Fields are !POINT_X!  !POINT_Y!

Where do I specify this? tried a few spots by no avail.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Emulate...

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Replace LONG and LAT in the following line with POINT_X and POINT_Y:

with arcpy.da.UpdateCursor(fc, ["LONG", "LAT", "SHAPE@X", "SHAPE@Y"], "", sr) as cursor:

Should be:

with arcpy.da.UpdateCursor(fc, ["POINT_X", "POINT_Y", "SHAPE@X", "SHAPE@Y"], "", sr) as cursor: