2

I have a polygon shapefile.

I used ArcPy scripts to:

  • add a new field to the shapefile
  • use Calculate UTM Zones on each polygon.

It spits out UTM projection information into the field I've added.

I would like to use a cursor to go through each row of the polygon shapefile's attribute table, using this added field.

For each row in the field, the cursor would:

  • Change the projection of the entire data frame in the .MXD to the UTM projection in the calculated UTM field for that polygon <-- The part I need help with.
  • Zoom to the polygon (with a 1400% margin)
  • Export a PDF
  • (continue)
  • (at the end) - Combine all PDFs into one book, and delete the extras.

All I've been able to find so far are scripts that change to things in projection files, or to one given projection. Are the UTM zone calculations from this command usable in this way, to alter the projection? I'm not sure how to do it if so.

-

I tried doing something similar with data driven pages, using the calculated UTM zone as the spatial reference in the data driven page setting, but it is not changing the projection for some reason - which is the entire point of this code - so this is the alternative that I've come up with.

=================

OP here again.

I should probably point out that I'm a serious novice in Python.

I have looked at the .PRJ file example but honestly don't understand it.

I tried using WKID's but they didn't do what I wanted, either - I just want to use the information I already have if I can, of the calculated UTM's. Their data looks like this for every row of the table:

PROJCS["GCS WGS 1984 UTM Zone 57H (Calculated)",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",

I have this code that I'm trying to modify to do what I want and it's just not working.

(unique_name is defined as the UTM shapefile elsewhere in the code)

fc = unique_name
cursor = arcpy.da.UpdateCursor(fc, ["UTM_Zone"])

with arcpy.da.UpdateCursor(fc, ["UTM_Zone"]) as cursor:
    for row in cursor:
        inData = fc
        coordinateSystem = row[0]
        arcpy.DefineProjection_management(inData, coordinateSystem)

My thinking was, if inData is the shapefile, UTM_Zone is the field I want to use, and the projections are in each row of the UTM_Zone field, this would work.

I searched elsewhere for how to clear the projection so that I can define it with this code, and got this, which isn't failing, but I don't know if it's the proper code to do this either.

try:
    newSpatialReference = arcpy.SpatialReference()  
    newSpatialReference.loadFromString('{B286C06B-0879-11D2-AACA-00C04FA33C20}')
    print ""
    print "* SUCCESS: Map's Spatial Reference cleared."

except Exception as ex:
    print ex.args[0]

Help?

4
  • This might help
    – Adam
    Dec 8, 2015 at 6:32
  • 1
    You can also create a spatial reference with the well-known ID. If you're using WGS84 UTM zones, the WKIDs are 326 + zone in northern hemisphere, 327 + zone in southern. Thus, zone 10 North is 32610. Zone 1 is 32701. NAD83 works the same way, but the "prefix" is 269. NAD27 is 267. Not all UTM zones work like this.
    – mkennedy
    Dec 8, 2015 at 18:54
  • What's the ".PRJ file example" that you have looked at? For example, my answer does not reference any *.prj files. It also does not use the Calculate UTM Zones tool because that seemed to only work in one quadrant of the world last time I tested it - see gis.stackexchange.com/questions/172410/….
    – PolyGeo
    Dec 9, 2015 at 11:52
  • You seem to have made this site much harder for you to use by having created two accounts gis.stackexchange.com/users/63323/zee and gis.stackexchange.com/users/63567/zee - please follow the instructions here to merge them ASAP.
    – PolyGeo
    Dec 9, 2015 at 23:29

1 Answer 1

2

The code that I think you need for where you are stuck is some that I just grabbed from one of my training courses. It uses MGA Zones in Australia but you can think of these as being the same as the UTM Zones that you are using.

The trick is to zoom to the extent of each polygon using the cursor and while at that extent determine its midpoint in the X direction and then use a formula to convert that into the relevant zone number to change the spatial reference of the data frame to.

extStr = str(df.extent)
xMin = extStr.split()[0]
xMax = extStr.split()[2]
xCen = (float(xMax)+float(xMin))/2

mgaZone = str(1 + int((xCen + 180)/6))

sr = arcpy.SpatialReference("GDA 1994 MGA Zone {0}".format(mgaZone))
df.spatialReference = sr

I also recommend that you change:

  • Change the projection of the entire data frame in the .MXD to the UTM projection in the calculated UTM field for that polygon <-- The part I need help with.
  • Zoom to the polygon (with a 1400% margin)

to:

  • Zoom to the polygon (with a 1400% margin)
  • Change the projection of the entire data frame in the .MXD to a calculated UTM Zone for that polygon using code like that in this answer <-- The part I need help with.

because determining the appropriate UTM Zone for a polygon is, I think, easier to do after your extent has changed to one centred on that polygon.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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