Tuesday 29 August 2017

arcpy - Calculating geometry for feature class with x/y and Lat/Long values in different projections?


I have a feature class with x/y values populated on the native projection of the feature class (state plane).


In the same feature class I also have Lat/Long fields that I need to populate (WGS 84 in decimal degree).


The problem I have is the calculate geometry function doesn't allow me to choose a different projection other than State Plane when I try to auto populate the Lat/Long values.



Does anyone have any python code or ideas that might help with this request?



Answer



you just need to run a cursor on it and use the projectAs() geometry method.


import arcpy

fc = r'C:\path_to\your_data\points.shp'

wgs = arcpy.SpatialReference(4326)
with arcpy.da.UpdateCursor(fc, ['SHAPE@', 'lat_field', 'long_field']) as rows:
for row in rows:

pnt_wgs = row[0].projectAs(wgs)
row[1:] = [pnt_wgs.centroid.Y, pnt_wgs.centroid.X] #will be in decimal degrees
rows.updateRow(row)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...