Calc the lat/long of a point in python using new ArcPy tools

Calculating the latitude and longitude of a point that is not in a geographic coordinate system was a tricky thing for me to figure out one day.

My data was not in the “World Projection” (as ArcMap calls it) so when I finally figured out how to access the x,y coordinates of the point, they were not in Lat/Long.  So basically I reproject the file, extract the x,y coordinates, then reproject back to State Plane which is what I need.  Not too elegant, but it gets the job done.

Here’s how I accomplished it:

import arcpy
from arcpy import env
myFC = "C:\\Temp\\temp.gdb\\mydata"
myDataWGS= "c:\\temp\\temp.gdb\\myDataWGS"

SR = 'c:\\temp\\decdeg.prj' #spatial reference set to geographic
arcpy.env.outputCoordinateSystem = SR
xExpression = 'float(!shape.firstpoint!.split() [0])'
yExpression = 'float(!shape.firstpoint!.split() [1])'
arcpy.MakeFeatureLayer_management(myFC, "myFC_lyr")  #export and reproject at the same time,

arcpy.AddField_management(myFC, "GIS_LAT", "FLOAT")
arcpy.AddField_management(myFC, "GIS_LONG", "FLOAT")
arcpy.CopyFeatures_management(myFC, myDataWGS)
arcpy.MakeFeatureLayer_management(myDataWGS, "myDataWGS_lyr")
#since the new FC has a geographic coordinate systems, the shape.firstpoint command will return a latitude and longitude
try:
 arcpy.CalculateField_management("myDataWGS_lyr", "GIS_LONG", xExpression, "PYTHON")
 arcpy.CalculateField_management("myDataWGS_lyr", "GIS_LAT", yExpression, "PYTHON")
except:
 print arcpy.GetMessages(2)
sr = myFC #spatial reference set back to State Plane
env.outputCoordinateSystem = sr
arcpy.CopyFeatures_management("myDataWGS_lyr", "c:\\temp\\temp.gdb\\myData_final") 
# the final output carries the GIS_LAT and GIS_LONG fields along with it so they persist into the newly projected feature class
print 'done'
Advertisements

2 thoughts on “Calc the lat/long of a point in python using new ArcPy tools

  1. import arcpy
    dsc = arcpy.Describe(FC)
    cursor4 = arcpy.UpdateCursor(FC, “”, “Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj”)
    for row in cursor4:
    shape=row.getValue(dsc.shapeFieldName)
    geom = shape.getPart(0)
    x = geom.X
    y = geom.Y
    row.setValue(‘LONG_DD’, x)
    row.setValue(‘LAT_DD’, y)
    cursor.updateRow(row)

    del cursor4, row

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s