Thursday 16 November 2017

coordinate system - Calculating centroids in decimal degrees using ArcPy without reprojecting vector data?


I have vector data not in geographical coordinate system (i.e. not in WGS 1984).


How to calculate object's centroids using arcpy without reprojecting the data itself?



I have tried to do this but it doesn't work (centroids are in meters):


arcpy.CalculateField_management(feature, fieldName, "!SHAPE.CENTROID.X!","PYTHON_9.3")
arcpy.CalculateField_management(feature, fieldName, "!SHAPE.CENTROID.Y!","PYTHON_9.3")

arcpy.CalculateField_management(feature, fieldName, "!SHAPE.CENTROID@DECIMALDEGREES!.split()[0]", "PYTHON")
arcpy.CalculateField_management(feature, fieldName, "!SHAPE.CENTROID@DECIMALDEGREES!.split()[1]", "PYTHON")

Answer



Use arcpy.CalculateField_managementwith following expressions:


For x coordinates:


expression = 'arcpy.PointGeometry(!Shape!.centroid,!Shape!.spatialReference).projectAs(arcpy.SpatialReference(4326)).centroid.X'

arcpy.CalculateField_management(feature, fieldName, expression,"PYTHON_9.3")

For y coordinates:


expression = 'arcpy.PointGeometry(!Shape!.centroid,!Shape!.spatialReference).projectAs(arcpy.SpatialReference(4326)).centroid.Y'
arcpy.CalculateField_management(feature, fieldName, expression, "PYTHON_9.3")

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