1

I am trying to create a map book that show the same extent with different layers displayed in ArcGIS 10.8 and ArcPy (I'm also on Windows 10 and a government computer which I cannot add programs to). I am not a coder, I'm a soil conservationist. I am using this code from Making Map Book of pages with same area shown but different layers turned on/off but find that it creates the maps with all layers minus 1. Meaning the first map in my example code will have all three layers displayed, the second will have two layers displayed, and the last will have one layer displayed.

Since I'm making 18 maps at once, this is basically crashing my program each time and is not producing what I am looking for. I would like to toggle the layer on, export the map, and toggle the layer off. At which point it would move on to the next layer on, export map, layer off again. And so on for all 18 layers.

Any ideas how to fix my issue?

import arcpy, os

#Specify output path and final output PDF
outPath = r'C:\Users\Outputs'
finalPdf = arcpy.mapping.PDFDocumentCreate(outPath + 'WetlandSupportdocs.pdf')
 
#Specify the map document and the data frame
mxd = arcpy.mapping.MapDocument(r'C:\Users\Outputs\test2.mxd')
df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]
 
#Select a tract using the TRACTNBR attribute and zoom to selected
cluLayer = arcpy.mapping.ListLayers(mxd, 'clu_a_ny067', df)[0]
arcpy.SelectLayerByAttribute_management(cluLayer, "NEW_SELECTION", '"TRACTNBR" IN (1470)')
df.zoomToSelectedFeatures()
 
#Turn on visibility for each theme and export the page
lyrList = ['soilmu_a_ny067', 'hydric_percent_a_ny067', 'soilsf_p_ny067']
for lyrName in lyrList:
     lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
     lyr.visible = True
  
     #Export each theme to a temporary PDF and append to the final PDF
     tmpPdf = outPath + lyrName + '_temp.pdf'
     if os.path.exists(tmpPdf):
          os.remove(tmpPdf)
     arcpy.mapping.ExportToPDF(mxd, tmpPdf)
     finalPdf.appendPages(tmpPdf)
 
     #Turn off layer visibility and clean up for next pass through the loop
     lyr.visible = False
     del lyr, tmpPdf
del mxd, df, finalPdf
1
  • Please remove the three dots and a space from the beginning of of each line in your code so that any potential answerers have an option to copy/paste it to test what you’ve tried
    – PolyGeo
    Commented Jun 11, 2021 at 20:45

1 Answer 1

1

I think the problem was with your file-path strings (check the docs for more info). See os.path.join. I also added a few print statements to your script to make it a bit more readable from the ArcMap Python window:

import arcpy, os

# Specify output path and final output PDF
outPath = r'C:\Temp'
pdfPath = os.path.join(outPath, 'WetlandSupportdocs.pdf')
if os.path.exists(pdfPath):
     os.remove(pdfPath)
finalPdf = arcpy.mapping.PDFDocumentCreate(pdfPath)

# Specify the map document and the data frame
mxd = arcpy.mapping.MapDocument(r'C:\Temp\Test.mxd')
# mxd = arcpy.mapping.MapDocument(r'current')
df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]
 
# Select a tract using the TRACTNBR attribute and zoom to selected
# cluLayer = arcpy.mapping.ListLayers(mxd, 'clu_a_ny067', df)[0]
# arcpy.SelectLayerByAttribute_management(cluLayer, "NEW_SELECTION", '"TRACTNBR" IN (1470)')
# df.zoomToSelectedFeatures()
 
# Turn on visibility for each theme and export the page
lyrList = ['soilmu_a_ny067', 'hydric_percent_a_ny067', 'soilsf_p_ny067']

# Don't assume the layers are turned off...Turn them off first.
print("First, turn off all the layers")
for lyrName in lyrList:
     lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
     lyr.visible = False
     print("\tSet layer-visibility for '" + lyr.name + "' to " + str(lyr.visible))

print("Second, export to PDF")
for lyrName in lyrList:
     print("---")
     lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
     lyr.visible = True
     print("\tSet layer-visibility for '" + lyr.name + "' to " + str(lyr.visible))
 
     #Export each theme to a temporary PDF and append to the final PDF
     tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
     if os.path.exists(tmpPdf):
          os.remove(tmpPdf)
     arcpy.mapping.ExportToPDF(mxd, tmpPdf)
     print("\tPDF Exported to following path: " + tmpPdf)
     finalPdf.appendPages(tmpPdf)
 
     #Turn off layer visibility and clean up for next pass through the loop
     lyr.visible = False
     print("\tSet layer-visibility for '" + lyr.name + "' to " + str(lyr.visible))
     del lyr, tmpPdf

finalPdf.saveAndClose()
del mxd, df, finalPdf

Lastly, without more info on the arcpy.SelectLayerByAttribute_management layer, it is not possible to confirm your definiton query is correct. Here is a screenshot of completed script and the output PDF:ArcMap and PDF

Edit: After reviewing this again, I see the problem (almost certainly with the filepaths). The path inside arcpy.mapping.PDFDocumentCreate() was being built as outPath + 'WetlandSupportdocs.pdf'

e.g.

outPath = r'C:\Users\Outputs'
finalPdf = arcpy.mapping.PDFDocumentCreate(outPath + 'WetlandSupportdocs.pdf')

Which translates to C:\Users\OutputsWetlandSupportdocs.pdf as your PDF path. (Note the absence of slash between Outputs and WetlandSupportdocs.pdf).

3
  • 1
    Thank you so much! That seemed to work great! I cant seem to get it to zoom to the selected attribute, but ill see if i can get that to work with some tweeking. (fyi, i did take away the hashtags for the select by attribute. so i know its not that) Commented Jun 12, 2021 at 12:54
  • @EvanSweeney, happy that helped. Check my edit above: Do you have write access to your "C:\Users" folder?
    – Keggering
    Commented Jun 14, 2021 at 23:12
  • 1
    I have do have write access to my C:\Users folder, but anything i put in there will be cleaned up (removed) by the IT department as they have access to it as well. I did figure out the issue with the "arcpy.SelectLayerByAttribute_management" and "zoom to selected". The layer i was searching has tracts with multiple fields in them, so i dissolved the fields to the tracts and it zooms right to it. Now i am on to the next layer of map production. I need to turn on new base orthos for various years. I need to the fly over date that is stored in a separate shape file. That's another q. Commented Jun 15, 2021 at 10:03

Your Answer

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

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