I'm trying to automate a very tedious process in our company that involves switching the data frame's coordinate system and performing calculate geometry on a couple of fields, these are the two situations I'm having trouble with :

First situation would be !SHAPE.CENTROID.X.! using data source's coordinate system with the unit conversion specified in the picture

enter image description here

Second is switching the data frame coordinate system, choosing to use it and perform the following calculation :

enter image description here

So, two main questions :

1) How to switch the data frame's coordinate system and calculate based on it

2) Get !SHAPE.CENTROID.X.! with the specified unit on picture 1

I'm using arcpy.CalculateField_management for this since there already is a selection

I really searched for this one but couldn't find what I'm looking for

  • 1
    It's not quite clear what your broader context is. Do you have a list of feature classes that you need to calculate centroids for using two separate coordinate systems? Do you have a map within which you need to iterate over layers and calculate centroids using two separate coordinate systems? Do you have a series of maps within which you need to grab a single layer and calculate centroids...? The iteration is the challenge, but we don't know what you're iterating over. – Tom Jul 20 '16 at 19:09
  • @Tom feature class containing points – Mojimi Jul 20 '16 at 19:13
  • Since you have Points, I don't think you need Shape.Centroid.X just Shape.X. They might produce identical results. – klewis Jul 20 '16 at 20:14
  • @Mojimi, so does it need to be repeated on the same feature class? Where is the "tedious" part coming in? – Tom Jul 20 '16 at 20:25

You can set the spatial reference of the data frame object in Python.

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
df.spatialReference = 31982 # SIRGAS_2000_UTM_Zone_22S

However, I think it will be much more efficient to leave the data frame as is and just project the point geometry on the fly.

sr = arcpy.SpatialReference(31982) # SIRGAS_2000_UTM_Zone_22S
with arcpy.da.UpdateCursor(<layer_name>, ["SHAPE@", "X", "Y", "newX", "newY"]) as cursor:
    for row in cursor:
        newGeom = row[0].projectAs(sr)
        row[1] = row[0].centroid.X # current X
        row[2] = row[0].centroid.Y # current Y
        row[3] = newGeom.centroid.X # projected X
        row[4] = newGeom.centroid.Y # projected Y
| improve this answer | |

The Add Geometry Attributes tool will allow you to perform many of the functions found in the Calculate Geometry tool from within a model or Python script.

| improve this answer | |
  • Doesn't that add fields? They already exist, just need to update them – Mojimi Jul 20 '16 at 19:12
  • Excellent, you can also define the Coordinate System inside the Add Geometry Attributes tool. – klewis Jul 20 '16 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.