get cursor spatial reference from current map data frame?

1909
4
12-02-2016 12:35 PM
JustinWolff
Occasional Contributor

Is it possible to use the coordinate system from a data frame in the current map document within an update cursor?

I have data in GCS WGS84 and I want to update area/perimeter/length fields across a geodatabase with UTM WGS84 units.  I want to acquire a coordinate system for the cursor from the current map data frame coordinate system, not a reference feature class.  A piece of my current script is below, which would return useless area/length values from GCS coordinates:

for fc in fcList:
        fc = fc +"\\"
        fieldList = arcpy.ListFields(fc, 'lengthSize')#lengthSize field
        fieldCount = len(fieldList)
        if (fieldCount > 0):
            print "perimeterSize field present in: " + fc
            arcpy.AddMessage ("perimeterSize field present in: " + fc)
            with arcpy.da.UpdateCursor (fc, ['SHAPE@LENGTH', 'lengthSize']) as cursor:
                for row in cursor:
                    row[1] = row[0]
                    cursor.updateRow(row)
                    print "Updating 'lengthSize' field in: " + fc
                    arcpy.AddMessage ("Updating 'lengthSize' field in: " + fc)
        else:
            print "No lengthSize field in: ", fc
            arcpy.AddMessage ("No lengthSize field in: " + fc)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Is this possible, or does a "describe" (like that found here: http://pro.arcgis.com/en/pro-app/arcpy/get-started/setting-a-cursor-s-spatial-reference.htm) have to be used?

Thanks

Justin

0 Kudos
4 Replies
DarrenWiens2
MVP Honored Contributor

You need to get to the DataFrame object, which has a spatialReference property (the same type of object that would be returned by arcpy.Describe().spatialReference).

0 Kudos
JustinWolff
Occasional Contributor

Thanks Darren, I needed to fix a syntax and I have that working.  Now...is it possible for me to assign variables based on the data frame coordinate system?  Specifically, if the data frame coordinate system is UTM WGS84, the units are meters.  I'd like to establish something like, "if data frame coordinate system units are meters, then variable1 = 'metre', variable2 = 'squareMetre', etc." 

I don't know how to 'query' the df.

Thanks again.

Justin

0 Kudos
DarrenWiens2
MVP Honored Contributor

You get to the df(s) by listing the dfs in a MapDocument:

>>> mxd = arcpy.mapping.MapDocument('CURRENT')
... for df in arcpy.mapping.ListDataFrames(mxd):
...   print df.spatialReference.name
...   print df.spatialReference.linearUnitName
... 
NAD_1983_UTM_Zone_11N
Meter
NAD_1983_UTM_Zone_11N
Meter‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
JustinWolff
Occasional Contributor

Thanks again Darren.  That nudged me in the correct direction and I have things running the way I need them to now.

Greatly appreciated.

Sincerely,

Justin

0 Kudos