[GIS] the Python expression for calculating Lat/Long in Decimal Degrees in ArcGIS

arcgis-10.3arcgis-desktopcalculate-valuesfield-calculatorpython-parser

I'm calculating 4 double fields for a polygon feature class (exploded, single-part features): SquareFeet, Acreage, Lat, and Long

I found the correct python expressions to calculate SquareFeet & Acreage using the Calculate Field (Data Management) tool.

For the Lat and Long fields, I've tried using !SHAPE.CENTROID.Y! which calculates in either feet or meters depending on which unit is set on the data, but since I need the calculation in decimal degrees this isn't acceptable. And I've tried !SHAPE.CENTROID.Y@DECIMALDEGREES! to get the calculation in decimal degrees, but this expression just flat out doesn't return anything. I've tried setting the parser to PYTHON and PYTHON_9.3.

Does anyone know if there's a python expression to calculate latitude and longitude in decimal degrees?

(!SHAPE.CENTROID.X@DECIMALDEGREES! of course was used to try and calculate Long)

Calculate Field tool

Best Answer

Found out that for some reason, the data has to be in a geographic coordinate system without a projection, and you can on-the-fly project into WGS84 (in the case in the code below) or NAD83 when calculating.

The code below ended up being a solution to calculating Lat/Long for polygons in decimal degrees:

arcpy.CalculateField_management(inputData, "LAT", "arcpy.PointGeometry(!Shape.centroid!,!Shape.spatialReference!).projectAs(arcpy.SpatialReference(4326)).firstPoint.Y","PYTHON_9.3","#")

arcpy.CalculateField_management(inputData, "LONG", "arcpy.PointGeometry(!Shape.centroid!,!Shape.spatialReference!).projectAs(arcpy.SpatialReference(4326)).firstPoint.X","PYTHON_9.3","#")
Related Question