0

I'm new to Python and just created a hard coded script for the process I want, and it works. Right now it calls on the one of three shapefile I have in this map document. I want it to do the same thing, but for each shapefile in this folder. I have no idea how to go about this! I always use ModelBuilder, so I know that is should be an iteration.

Does anyone have any idea how to go about this, or how I am supposed to change this code?

enter image description here

import arcpy

#Declaring Variables  
ID = arcpy.GetParameterAsText(0)  
OutputLocation = arcpy.GetParameterAsText(1)  

mxd = arcpy.mapping.MapDocument(r"C:\Users\m3rexkac\Desktop\Rename_Test\DOE_auto.mxd")  

df = arcpy.mapping.ListDataFrames(mxd,"*")[0]  

#Now to Create a Layer Object, the line of code below returns a list of all the layers in the dataframe, with this name.  The [0] returns the first layer with that name instead of the whole list  
Layer = arcpy.mapping.ListLayers(mxd, "Mary_Creek_Co_Parcels", df)[0]    

#This zooms the dataframe to the selected feature  
lyr = arcpy.mapping.ListLayers(mxd,"Mary_Creek_Co_Parcels", df)[0]
extent = lyr.getExtent()

df.extent = extent
df.scale = scale  


#Exporting map dataframe view to pdf, I have it set up to save  
arcpy.mapping.ExportToPDF(mxd, OutputLocation + "2.pdf", "Page_Layout", 640, 480)  

#Cleaning out memory by deleting the python objects  
del mxd
del df
del Layer  
  • 2
    Welcome! Please confirm what you are trying to do with the script - it looks like you are attempting to export a PDF of a map, but you want to do this for a number of shapefiles that do not exist as a map. Do you wish to iteratively add each shapefile to the map, save it, export it, and remove it before moving on to the next one? – smiller Nov 13 '18 at 14:25
  • Yes that's exactly what I'm trying to do. – Kelsey Ciarrocca Nov 15 '18 at 18:28
  • I have about 300 shapefiles that I need to produce a pdf of and am trying to find the best way to do that. – Kelsey Ciarrocca Nov 15 '18 at 18:29
  • Check out arcpy.mapping.Layer and arcpy.mapping.AddLayer, such as in this thread: gis.stackexchange.com/questions/4882 and this documentation page: desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/…. What have you tried to identify the shapefiles? I use glob, but you can also use arcpy.ListFeatureClasses or arcpy.da.Walk. – smiller Nov 15 '18 at 18:44
0

First, you need to get a list of the shapefiles in the folder to iterate through. I like to use the Python library glob for this as it's pretty fast even when filtering for specific file naming conventions, but arcpy.ListFeatureClasses() or arcpy.da.Walk() would also work (note glob works for file system access and does well with shapefiles, but wouldn't work in a file geodatabase).

Documentation:

An example using glob and os:

shplist = glob.glob(os.path.join(pathtoshapefiles, "*.shp"))
shplist.sort() # glob returns an unsorted list

Esri's example using ListFeatureClasses:

import os
import arcpy

# Set the workspace for ListFeatureClasses
arcpy.env.workspace = "c:/base"

# Use the ListFeatureClasses function to return a list of
#  shapefiles.
featureclasses = arcpy.ListFeatureClasses()

Once you have the list of shapefiles, iterate through them. You will add the new layer, zoom to the desired extent, export the PDF, then remove the new layer. I've assumed based on your responses above that you will be using the same MXD each time, and used your code above as a base. I don't have a big stack of shapefiles to test so it may need some additional tweaking.

ID = arcpy.GetParameterAsText(0)  
OutputLocation = arcpy.GetParameterAsText(1)  

mxd = arcpy.mapping.MapDocument(r"C:\Users\m3rexkac\Desktop\Rename_Test\DOE_auto.mxd")  

df = arcpy.mapping.ListDataFrames(mxd,"*")[0]  

for shppath in shplist:
    # create a new layer
    newlayer = arcpy.mapping.Layer(shppath)
    # Add the new layer to the MXD
    arcpy.mapping.AddLayer(df, newlayer,"BOTTOM")

    #This zooms the dataframe to the selected feature  
    lyr = arcpy.mapping.ListLayers(mxd,"Mary_Creek_Co_Parcels", df)[0] # May need to change this line to reflec the correct layer name.
    extent = lyr.getExtent()

    df.extent = extent
    df.scale = scale  

    #Exporting map dataframe view to pdf
    mxd.save() 
    arcpy.mapping.ExportToPDF(mxd, OutputLocation + "_" + str(n) + ".pdf", "Page_Layout", 640, 480)  
    arcpy.mapping.RemoveLayer(df, lyr)
del mxd

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.