I sought assistance with a portion of this code earlier. I have added an additional step, which to me would seem pretty simple, but I am still learning the ups & downs of Python.
My script creates data driven pages. I use a list to populate a layer definition query to run a specific township. Now I am trying to do a selection (SelectLayerByLocation) to only find the townships where there is data. I first just added the SelectLayerByLocation command. Did not work. With further reading, it looked like I needed to run a MakeFeatureLayer on the two sets of data involved in the selection. Still did not work. Do I need to use cursors or is the answer much simpler?
I do not have any error messages. I have added try/except to see if I can find additional information on what is happening with my script. The data exports just fine, but there are maps that do not have any data.
Thank you for any guidance.
My script creates data driven pages. I use a list to populate a layer definition query to run a specific township. Now I am trying to do a selection (SelectLayerByLocation) to only find the townships where there is data. I first just added the SelectLayerByLocation command. Did not work. With further reading, it looked like I needed to run a MakeFeatureLayer on the two sets of data involved in the selection. Still did not work. Do I need to use cursors or is the answer much simpler?
I do not have any error messages. I have added try/except to see if I can find additional information on what is happening with my script. The data exports just fine, but there are maps that do not have any data.
Thank you for any guidance.
Code:
# PoleDDpages.py
# Author: slw
# Date: XXXXX
# Revisions: XXXXX
# Description: Create Pole map book series using Data Driven Pages and
# insert a cover page
# Import ArcPy
import os,arcpy
try:
# Set up variables
# Location of pole map .mxd document
mxdDoc = r"G:\GEOSPATIAL\Publishing\Pole\Pole_QtrSec.mxd"
# Create the MapDocument object
mxd = arcpy.mapping.MapDocument(mxdDoc)
df = arcpy.mapping.ListDataFrames(mxd)[0]
layer = arcpy.mapping.ListLayers (mxd, "SURVEY_GRID_BNDRY",df)[0]
poleLyr = arcpy.mapping.ListLayers (mxd, "Pole",df)[0]
# Overwrite existing file
arcpy.env.overwriteOutput = True
# Output directory for the pole maps
outDir = r"G:\GEOSPATIAL\Publishing\Pole"
# Set the workspace
arcpy.env.workspace = outDir
# List of map grids
twpList = ['\"TOWNSHIP_C\" = \'D5\' AND \"RANGE_CD\" = \'5\'',\
'\"TOWNSHIP_C\" = \'D6\' AND \"RANGE_CD\" = \'4\'']
i=1
for twpName in twpList:
layer.definitionQuery = twpName
# refresh() after changing the layer def query to 'redefine' the DDP index limits
mxd.dataDrivenPages.refresh()
# Create temporary layers to work with the Selection
arcpy.MakeFeatureLayer_management(layer, "surveyLyr")
arcpy.MakeFeatureLayer_management(poleLyr, "poleLyr_new")
# Clear the Selection before starting
#arcpy.SelectLayerByAttribute_management("surveyLyr", "CLEAR_SELECTION", "")
#print "The selection has been cleared"
# Select Layer By Location to limit to just maps with data
arcpy.SelectLayerByLocation_management("surveyLyr", "INTERSECT", "poleLyr_new", "", "NEW_SELECTION")
# refresh() after changing the layer def query to 'redefine' the DDP index limits
mxd.dataDrivenPages.refresh()
mxd.saveACopy(os.path.splitext(mxdDoc) [0] + str(i) + os.path.splitext(mxdDoc)[1])
i += 1
# The final mapbook name taken from the list
finalPDFfn = outDir + "\\" + twpName [16:18] + twpName [38:39] + "Pole.pdf"
# Create the final PDF -- which is just an empty shell right now
finalPDF = arcpy.mapping.PDFDocumentCreate(finalPDFfn)
# A temporary pdf file for processing
tmpPDF = outDir + "\\PoleMapPages.pdf"
# Let the user know what is happening!
print "Exporting " + twpName [16:18] + twpName [38:39]
# Export the data driven pages in the mxd to a temporary PDF
print "Exporting map pages to the temporary PDF"
ddp = mxd.dataDrivenPages.exportToPDF(tmpPDF)
# Append the temporary pdf to the final pdf
# Cover, map pages, back cover
print "Appending Map Pages"
finalPDF.appendPages (r"G:\GEOSPATIAL\Publishing\Pole\PoleCovers\Covers_"\
+ twpName [16:18] + twpName [38:39] + ".pdf")
finalPDF.appendPages(tmpPDF)
finalPDF.appendPages(r"G:\GEOSPATIAL\Publishing\TwpGrid_Color8x11.pdf")
# Set properties for Adobe Reader and save PDF.
finalPDF.updateDocProperties(pdf_open_view = "USE_THUMBS", pdf_layout = "SINGLE_PAGE")
finalPDF.saveAndClose()
# Deleting temporary layers
arcpy.Delete_management("surveyLyr")
arcpy.Delete_management("poleLyr_new")
except Exception as e:
print e.message
print arcpy.GetMessages(2)
# Clean up
print "Cleaning up"
# Delete the temporary PDF using the ArcPy function
if arcpy.Exists(tmpPDF):
arcpy.Delete_management(tmpPDF)
# Delete objects
del mxd, tmpPDF, ddp
# Finished message
print "Map compilation completed. Please review the final output."