1

I'm looking to automate a process in ArcGIS that I believe needs Python. However, I have zero experience with Python only with ModelBuilder.

Here is the process:

I have 400 individual layers. I have a standard page setup in the layout view. I need to zoom to each layer in the layout data frame and then export each one as its own PDF. How would I automate this?

2

Its hard to give a good answer without some sort of code but generally the process would be loop through your table of contents (assuming you only have one data frame, you could have multiple though) create a feature layer, turn on the layer in the for loop, zoom to that layer,export and then turn off that layer.

import arcpy
def zoom_export(zlayer,pdfname,ratio):
    zoom = arcpy.mapping.Layer(zlayer)
    arcpy.SelectLayerByAttribute_management(zoom)
    df.extent = zoom.getSelectedExtent()   #zooming to selected feature layer
    df.scale = df.scale * ratio
    arcpy.RefreshActiveView()
    zoomin = arcpy.mapping.ExportToPDF(mxd, pdfname)
    arcpy.SelectLayerByAttribute_management(zoom, "CLEAR_SELECTION")

def turn_on(layer,tf,mxd):
    mxd = mxd
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    layers = arcpy.mapping.ListLayers(mxd, "*", df)
    for x in layers:
        if x.name == layer:
            print x.name
            x.visible = tf
            arcpy.RefreshTOC()
            arcpy.RefreshActiveView()

mxd = arcpy.mapping.MapDocument(r"C:/mxdpath/abid.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layers = arcpy.mapping.ListLayers(mxd, "*", df)
for layer in layers:
    arcpy.MakeFeatureLayer_management(layer,"blah")
    turn_on(layer.name,True,mxd)
    pdfname = layer.name+"PDF"
    zoom_export("blah",pdfname,1.1)
    turn_on(layer.name,False,mxd)

For map creation you should always run a standalone script, not a script that runs in the arcmap python window

also note, you need to create a feature layer out of the layers for them to be correctly called in two separate functions. I have zero idea why that is but if you try and call

for layer in layers:
    turn_on(layer.name,True,mxd)
    pdfname = layer.name+"PDF"
    zoom_export(layer.name,pdfname,1.1)

it will not work. you'll get some sort of invalid layer source in the zoom_export function

  • Ok big help thank you. Two questions: what do the variables inside the parentheses get replaced with? Is there a python IDE that you would suggest? – Luca E Mar 24 '17 at 14:38
  • variables inside which parantheses? the mxd path? where ever your mxd is saved on your computer. I personally use pyscripter, i love it. you probably need to download the 32 bit version – ziggy Mar 24 '17 at 14:40
  • ok and as for things such as pdfname, I'm assuming I just insert whatever I want the pdf to be named? Is there a way I can format it so it uses the name of the layer it is iterating? – Luca E Mar 24 '17 at 14:42
  • the way I set it in the code is whatever the layers name is + "PDF" – ziggy Mar 24 '17 at 14:45
  • Ok thanks. What would I have to do to make it work with 400 .png rasters rather than just normal shapefiles? – Luca E Mar 24 '17 at 16:21

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.