Sunday, 16 October 2016

arcpy - Calculating XY coordinates in Decimal Degrees from projected dataset in model or script?



Is it possible to calculate points XY coordinates in Decimal Degrees in model or script when a feature class is in projected coordinate system?


It's easy when a FC is in geographic coordinate system:



  • using Add XY Coordinates tool or

  • using Python expression eg. !shape.extent.XMax!


I've found that area and length properties of geometry field can be modified with geometry unit conversion keyword. For linear units of measue one can use @DECIMALDEGREES.
Unfortunatelly, !shape.extent.XMax@decimaldegrees! doesn't work as XMax is not a length.


In Calculate Geometry function (accessed from right-click) there's a possibility to choose Decimal Degrees output type even for projected feature class.
Can I do this using ArcPy?





Here's a code snippet based on iRfAn's solution:


import arcpy, os
projectedFC = r"C:\tmp\test.gdb\points01_Projected"
prjFile = os.path.join(arcpy.GetInstallInfo()["InstallDir"],
r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj")
spatialRef = arcpy.SpatialReference(prjFile)

updCursor = arcpy.UpdateCursor(projectedFC,"", spatialRef)
for row in updCursor:

pnt = row.Shape.getPart(0)
row.X = pnt.X
row.Y = pnt.Y
updCursor.updateRow(row)

del updCursor, row

Answer



I think you can.


Just define the Spatial Reference in WGS-84 and use cursor using this Spatial Reference.


Coordinates are specified in the spatial_reference provided, and converted on the fly to the coordinate system of the dataset.



Fore more detail see this.


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...