Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Using Python, I want to export a map series. Each series needs to be grouped based on an attribute field which is not being used in DDP settings (page num, page index etc...).

For example: my shapefile RoadName.shp has a field called RoadType. Now when I ask my DDP to export maps, it should group diff RoadTypes into one single (multipage) pdf.

Any quick solution please?

share|improve this question
    
Just to clarify, you want to group individual RoadTypes, not coalesce them together? –  lynxlynxlynx Sep 16 '13 at 18:32
    
Have you tried setting the definition query for each category of RoadType? This works manually - if you refresh the DDP after setting the features definition query. –  mwil Sep 16 '13 at 19:30
    
@M.B. If you wish, you can add your code as a new answer, instead of heavily editing the accepted answer. –  Devdatta Tengshe Sep 19 '13 at 8:43

2 Answers 2

up vote 2 down vote accepted

This code should achieve what you have asked (not tested):

import arcpy
import os

mxd = arcpy.mapping.MapDocument(r"C:\temp\mymap.mxd")
output_folder = r"C:\temp"
ddp_lyr = mxd.dataDrivenPages.indexLayer
pdf_field = "RoadType"

#Get a list of unique values to group by
pdf_list = set([row[0] for row in arcpy.da.SearchCursor(ddp_lyr,pdf_field)])

for pdfName in pdf_list:
    finalPdf = arcpy.mapping.PDFDocumentCreate(os.path.join(output_folder,pdfName + ".pdf"))
    for i in range(1, mxd.dataDrivenPages.pageCount + 1):
        mxd.dataDrivenPages.currentPageID = i
        cur = str(mxd.dataDrivenPages.pageRow.getValue(pdf_field))
        if cur == pdfName:
            tempPdf = os.path.join(output_folder,"temp_" + cur + str(i) + ".pdf")
            arcpy.mapping.ExportToPDF(mxd, tempPdf)
            finalPdf.appendPages(tempPdf)
            os.remove(tempPdf)
    finalPdf.saveAndClose()
    del finalPdf, tempPdf

The code is not optimised, so it loops over all the data driven pages for each unique value in the list. With some rearranging, it may be better to first export all the pdfs, then check the filenames for the unique value and group them accordingly.

share|improve this answer
    
I've updated the code with os.remove(tempPdf) to delete the file after it has been added to the final pdf. –  Cindy Williams Sep 17 '13 at 9:08
    
Great Arabella. You are a superstar. –  M.B Sep 17 '13 at 9:41
    
Please post your code as an alternative answer, as the comments are not the place for it. If you are going to use this script as a script tool, you need to set both the mxd and output_folder as input parameters. –  Cindy Williams Sep 18 '13 at 5:13

Here's a bit of psuedo code and a bit of code rearranged from another stack exchange answer:

roadtypes = list of the unique values in RoadType field
field = "RoadType"
#Loop through the road types
for type in roadtypes:
    queryStr = str("{0}" + " = " + '{1}').format(field, type)

    #Specify the MXD project (CURRENT), dataframe (Layers)
    mxd = arcpy.mapping.MapDocument("CURRENT")
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

    #Apply defintion query to specified layer group (Test)
    for lyr in arcpy.mapping.ListLayers(mxd, "Test", df)[0]:
        if lyr.supports("DEFINITIONQUERY"): 
            lyr.definitionQuery = queryStr
    arcpy.RefreshActiveView()      
    #Export map series
    #arcpy.exportToPDF()
share|improve this answer
    
Thanks Mwil. Your code comes with an error but it must be me doing something wrong here. Arabella, code works fine. –  M.B Sep 17 '13 at 9:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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