Select to view content in your preferred language

Calculating coordinates in different coordinate system programmatically

10242
11
11-19-2010 08:33 AM
TomBuckler
Deactivated User
I have a Python script that adds two fields and populates them with the X coordinate and Y coordinate of the centroid of the features. The feature class is in one projection, but I would like the centroid coordinates to be displayed in Lat/Lon. Is there a way to specify the coordinate system when you write something like !Shape.centroid!

I know when you right click on a field in the attribute table and select "Calculate Geometry" that you have the option to use the Data Frame's coordinate system. Is there a way to do this programmatically?

I've tried this function call and definition:

Lat(!Shape!)

def Lat(shape):
  arcpy.env.cartographicCoordinateSystem = "Coordinate Systems\Geographic Coordinate\Systems\World\WGS 1984.prj"
  lat = shape.centroid.x
  return lat


but it returns the x coordinate in the the same projection of the feature class rather than longitude.
0 Kudos
11 Replies
danbecker
Frequent Contributor
not the most elegant solution, but to calc. geometries of an unprojected feat. class, I ended up:

projecting feat. class - output to temp.gdb
calculated geometry of temp feat. class
make feat. layer of temp feat. class
join temp feat. layer "OBJECTID" ----- with ----- original feat. class "OBJECTID"
calc field of orig. feat. class with the calculated geometries
0 Kudos
MichaelWalden
Emerging Contributor
I am very new to Python and have a similar challenge.

I have a point feature class with about 8,500 records that is in WGS84.  The feature class already has 6 fields (data type = double, format = Decimal Degrees) created to calculate the longitude and latitude in 3 different coordinate systems (WGS84_Longitude, WGS84_Latitude, NAD83_Longitude, NAD83_Latitude, NAD27_Longitude, and NAD27_Latitude).  I am currently manually doing this in an ArcMap edit session, calculating the geometry using the dataframe's coordinate system, however I would like to automate the process and schedule it to run at a predetermined frequency.

I need to write a python script that will calculate the 6 fields using the correct coordinate system and a predetermined datum transformation.  Can this be done?
0 Kudos