Thursday, 25 August 2016

coordinate system - Changing Dataframe Projection of .MXD to UTM Zone matching each polygon in ArcPy cursor?



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?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...