Change a data frame's coordinate system based on a shapefile attribute

284
2
Jump to solution
02-12-2018 02:53 PM
NickDierks
New Contributor III

I'm trying to write a script tool that, among other things, sets the coordinate system of the map to a given UTM zone based on the attribute data of a shapefile (generated by a spatial join earlier in the script). The relevant bit is as follows:

NZone = arcpy.da.SearchCursor(ShpShp, ["UTM_Zone"])
#UTMRef = r"Coordinate Systems\Projected Coordinate Systems\UTM\NAD 1983\NAD 1983 UTM Zone 12N.prj"
UTMRef = r"Coordinate Systems\Projected Coordinate Systems\UTM\NAD 1983\NAD 1983 UTM Zone " + str(NZone) + "N.prj"
MXD = arcpy.mapping.MapDocument("CURRENT")
DF = arcpy.mapping.ListDataFrames(MXD)[0]
sr = arcpy.SpatialReference(UTMRef)
DF.spatialReference = sr

(ShpShp is a variable for the shapefile in question, including ".shp")

Note that the commented-out line works if I use that instead. This also works if I change the first line to "NZone = 12". Since I want this to work for any potential UTM zone 1 through 20 (all North), I wanted to use the UTM_Zone field that was the result of the spatial join.


The field is type string, and the values are written exactly as the projection files display (ie no leading zeroes or anything).

Am I using SearchCursor wrong? Everything else seems to work fine; I adapted the rest of the above code from this earlier discussion.

Any tips?

0 Kudos
1 Solution

Accepted Solutions
JamesMacKay3
Occasional Contributor

You need to fetch the first row from the cursor, then get the first value from the row.

NZone = "12"  # Default value in case no rows found
with arcpy.da.SearchCursor(ShpShp, ["UTM_Zone"]) as searchCursor:
    for row in searchCursor:
        NZone = row[0]
        break‍‍‍‍‍

# Remainder of code down here...
‍‍‍‍‍‍‍UTMRef = r"Coordinate Systems\Projected Coordinate Systems\UTM\NAD 1983\NAD 1983 UTM Zone " + str(NZone) + "N.prj"‍‍‍‍‍‍‍‍

There are more concise ways to create a cursor and fetch only its first row and first value, but I generally stick to this pattern of "with" on the first line and iteration on the second because it suits the majority of my use cases (may differ for others).

View solution in original post

2 Replies
JamesMacKay3
Occasional Contributor

You need to fetch the first row from the cursor, then get the first value from the row.

NZone = "12"  # Default value in case no rows found
with arcpy.da.SearchCursor(ShpShp, ["UTM_Zone"]) as searchCursor:
    for row in searchCursor:
        NZone = row[0]
        break‍‍‍‍‍

# Remainder of code down here...
‍‍‍‍‍‍‍UTMRef = r"Coordinate Systems\Projected Coordinate Systems\UTM\NAD 1983\NAD 1983 UTM Zone " + str(NZone) + "N.prj"‍‍‍‍‍‍‍‍

There are more concise ways to create a cursor and fetch only its first row and first value, but I generally stick to this pattern of "with" on the first line and iteration on the second because it suits the majority of my use cases (may differ for others).

NickDierks
New Contributor III

It works! Thank you so much!

And there are probably more concise ways to do a lot of things in my script, but I've only just started learning Python, so anything that works is a big help!

0 Kudos