# 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."
Solved! Go to Solution.
import os,arcpy # Overwrite existing file arcpy.env.overwriteOutput = True try: # Set up variables # Location of pole map .mxd document mxdDoc = r'E:\qryDefTest\Pole_QtrSec\v10\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] ############## # Test the count of survey grids before processing countSurveyGrid = int(arcpy.GetCount_management(layer).getOutput(0)) print "Count of All Survey Grids (Survey_Grid_Bndry) = " + str(countSurveyGrid) + " pages" SelectBy = [layer, poleLyr] for lyr in SelectBy: iniCount = int(arcpy.GetCount_management(lyr).getOutput(0)) print lyr.name + ' initial count: ' + str(iniCount) if iniCount != 0: arcpy.SelectLayerByAttribute_management(lyr, 'CLEAR_SELECTION') outCount = int(arcpy.GetCount_management(lyr).getOutput(0)) print lyr.name + ' out count: ' + str(outCount) # should now be properly 'initialized' with no prior selections, i.e., full complement datasets arcpy.MakeFeatureLayer_management(layer, "surveyLyr") arcpy.MakeFeatureLayer_management(poleLyr, "poleLyr_new") countSurveyLyr = int(arcpy.GetCount_management("surveyLyr").getOutput(0)) print "Count of temp layer surveyLyr = " + str(countSurveyLyr) + " pages" # Select Layer By Location to limit to just maps with data arcpy.SelectLayerByLocation_management("surveyLyr", "INTERSECT", "poleLyr_new", "", "NEW_SELECTION") # Susan, check that this works, switching the selection... # SelectLayerByAttribute_management (in_layer_or_view, {selection_type}, {where_clause}) arcpy.SelectLayerByAttribute_management("surveyLyr", "SWITCH_SELECTION") # new code to get at OBJECTIDs to modify def query IDs = [] rows = arcpy.SearchCursor("surveyLyr") for row in rows: IDs.append(row.OBJECTID) del row, rows print "delete row, rows" subquery = '(' for each in IDs: subquery = subquery + str(each) + ',' subquery = subquery[0:-1] + ')' ################ # Output directory for the pole maps outDir = r"E:\qryDefTest\Pole_QtrSec\commondata\userdata" # Set the workspace arcpy.env.workspace = outDir # List of map grids twpList = ['\"TOWNSHIP_C\" = \'D5\' AND \"RANGE_CD\" = \'5\'',\ '\"TOWNSHIP_C\" = \'C4\' AND \"RANGE_CD\" = \'7\'',\ '\"TOWNSHIP_C\" = \'B8\' AND \"RANGE_CD\" = \'4\''] i = 1 for twpName in twpList: # modified the definitionQuery to include the subquery exclusion clause layer.definitionQuery = twpName + " AND \"OBJECTID\" NOT IN " + str(subquery) countDefQryLyr = int(arcpy.GetCount_management(layer).getOutput(0)) print '-' print "Count of temp layer surveyLyr = " + str(countDefQryLyr) + " pages" # refresh() after changing the layer def query to 'redefine' the DDP index limits mxd.dataDrivenPages.refresh() mxd.saveACopy(os.path.join(outDir, 'test' + str(i) + '.mxd')) i += 1 testTxt = twpName [16:18] + twpName [38:39] + "Pole.pdf" #finalPDFfn = outDir + "\\" + twpName [16:18] + twpName [38:39] + "Pole.pdf" finalPDFfn = os.path.join(outDir, testTxt) print finalPDFfn # just before you create it, make sure you delete the last var ref if os.path.exists(finalPDFfn): os.remove(finalPDFfn) finalPDF = arcpy.mapping.PDFDocumentCreate(finalPDFfn) tmpPDF = outDir + "\\PoleMapPages.pdf" print 'exporting ' + testTxt print '...to ' + tmpPDF if os.path.exists(tmpPDF): os.remove(tmpPDF) mxd.dataDrivenPages.exportToPDF(tmpPDF, 'ALL') print "Appending Map Pages" Cover = os.path.join(outDir, "Covers_" + testTxt[0:-8] + ".pdf") print "Appending this page 1st: " + Cover finalPDF.appendPages(Cover) print '\n...now appending the rest:' print tmpPDF finalPDF.appendPages(tmpPDF) # final closing treatment finalPDF.updateDocProperties(pdf_open_view = "USE_THUMBS", pdf_layout = "SINGLE_PAGE") finalPDF.saveAndClose() print "\nSave and Close" except: print 'error'
# 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
# Overwrite existing file
arcpy.env.overwriteOutput = True
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]
################
# moved part of code to select grids (layer) intersecting 'Pole' features (poleLyr)
# Create temporary layers to work with the Selection
arcpy.MakeFeatureLayer_management(layer, "surveyLyr")
arcpy.MakeFeatureLayer_management(poleLyr, "poleLyr_new")
arcpy.SelectLayerByAttribute_management("surveyLyr", "CLEAR_SELECTION")
# Select Layer By Location to limit to just maps with data
arcpy.SelectLayerByLocation_management("surveyLyr", "INTERSECT", "poleLyr_new", "", "NEW_SELECTION")
# Susan, check that this works, switching the selection...
arcpy.SelectLayerByAttribute_management("surveyLyr", "SWITCH_SELECTION")
# new code to get at OBJECTIDs to modify def query
IDs = []
rows = arcpy.SearchCursor("surveyLyr")
for row in rows:
newID = row.OBJECTID
if newID not in IDs:
IDs.append(newID)
del row, rows
subquery = '('
for each in IDs:
subquery = subquery + str(each) + ','
subquery = subquery[0:-1] + ')'
################
# 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:
# modified the definitionQuery to include the subquery exclusion clause
layer.definitionQuery = twpName + " AND \"OBJECTID\" NOT IN " + subquery
# 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."
layer.definitionQuery = twpName + " AND \"OBJECTID\" NOT IN " + subquery
layer.definitionQuery = twpName + " AND \"OBJECTID\" NOT IN " + str(subquery)