# 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.
# 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, # 1st need to assure no prior selections...the sel by loc syntax: # SelectLayerByLocation_management (in_layer, {overlap_type}, {select_features}, {search_distance}, {selection_type}) # in_layer is the index layer; select_features is the pole layer SelectBy = [layer, poleLyr] for lyr in SelectBy: iniCount = int(arcpy.GetCount_management(lyr).getOutput(0)) if iniCount != 0: arcpy.SelectLayerByAttribute_management(lyr, 'CLEAR_SELECTION') # 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") # 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 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\''] for twpName in twpList: # modified the definitionQuery to include the subquery exclusion clause layer.definitionQuery = twpName + " AND \"OBJECTID\" NOT IN " + str(subquery) # refresh() after changing the layer def query to 'redefine' the DDP index limits mxd.dataDrivenPages.refresh() # 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."