+ Reply to Thread
Results 1 to 10 of 10

Thread: Python to select page name and export current page using data driven pages

  1. #1
    Molly Molter
    Join Date
    Aug 2011
    Posts
    22
    Points
    0
    Answers Provided
    0


    0

    Default Python to select page name and export current page using data driven pages

    I am creating a thematic mapbook in Python using some arcpy.mapping functions. One map in the mapbook is a section map which utilizes data driven pages. I would like the user defined parameter string of the section, township, range to be used to get to the correct data driven page. Then I want to export only the current page to PDF. The index field in the data driven pages is called SEC_FULL. This is also the field used to name the pages. I am getting stuck on how to write this in Python. Here's what I have so far which does export the map as a PDF, but it is the wrong page. It is not finding the correct page from the user defined parameter.

    #Import arcpy module
    import string
    import arcpy
    import os
    from arcpy import mapping
    from os import sep as bs

    # Script arguments
    STR = arcpy.GetParameterAsText(0)

    #Export Section Map
    mxd = arcpy.mapping.MapDocument(r"V:\gislu\Planning\DeveloperKit\SectionMap.mxd")
    df = mapping.ListDataFrames(mxd, "STR Map")[0]
    ddp = mxd.dataDrivenPages
    Layer = mapping.ListLayers(mxd, "Section Lines", df)[0]
    clause = "\"SEC_FULL\" = '" + STR + "'"
    arcpy.AddMessage("SELECTING: " + clause)
    arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION", clause)
    arcpy.AddMessage(arcpy.GetCount_management(Layer).getOutput(0))
    df.zoomToSelectedFeatures()
    df.scale = 9300
    arcpy.RefreshActiveView()
    pageID = ddp.getPageIDFromName("SEC_FULL")
    ddp.currentPageID = pageID
    tmpPdf = r"V:\gislu\Planning\DeveloperKit\output\Section_temp.pdf"
    ddp.exportToPDF(tmpPdf, 'CURRENT', resolution = 175)
    finalPdf.appendPages(tmpPdf)
    del mxd, tmpPdf

  2. #2
    Jeff Ward
    Join Date
    May 2010
    Posts
    141
    Points
    8
    Answers Provided
    1


    0

    Default Re: Python to select page name and export current page using data driven pages

    I think your problem might be your clause string. I tried typing the same text in the Python window and got a runtime error. What do you want the clause value to be?
    Jeff Ward
    Summit County, Utah

  3. #3
    Jeff Ward
    Join Date
    May 2010
    Posts
    141
    Points
    8
    Answers Provided
    1


    0

    Default Re: Python to select page name and export current page using data driven pages

    Scratch that, I needed to create the STR variable.
    Jeff Ward
    Summit County, Utah

  4. #4
    Jeff Ward
    Join Date
    May 2010
    Posts
    141
    Points
    8
    Answers Provided
    1


    0

    Default Re: Python to select page name and export current page using data driven pages

    I don't think you need to go through all of the selection code. If you replace "SEC_FULL" with the STR variable, it should change to the section entered by the user.

    Code:
    pageID = ddp.getPageIDFromName(STR)
    ddp.currentPageID = pageID
    Then you can get rid of all of the selectLayerByAttribute_management code.

    As long as the user enters the section correctly it should work. You might want to put some error handling code in to make sure they type the section correctly.
    Jeff Ward
    Summit County, Utah

  5. #5
    Jeff Ward
    Join Date
    May 2010
    Posts
    141
    Points
    8
    Answers Provided
    1


    0

    Default Re: Python to select page name and export current page using data driven pages

    You will also want to use os.remove(tmpPdf) to delete the temporary pdf file. The del command is for object cleanup which you have a bunch of - ddp, df, Layer, and mxd. Where you don't need to do the layer selection, I think the only objects you need are the mxd and ddp objects.
    Jeff Ward
    Summit County, Utah

  6. #6
    Jeff Ward
    Join Date
    May 2010
    Posts
    141
    Points
    8
    Answers Provided
    1


    0

    Default Re: Python to select page name and export current page using data driven pages

    Code:
    #Import arcpy module
    import string
    import arcpy
    import os
    from arcpy import mapping
    from os import sep as bs
    
    
    # Script arguments
    STR = arcpy.GetParameterAsText(0)
    
    #Export Section Map
    mxd = arcpy.mapping.MapDocument(r"V:\gislu\Planning\DeveloperKit\SectionMap.mxd")
    ddp = mxd.dataDrivenPages
    pageID = ddp.getPageIDFromName(STR)
    ddp.currentPageID = pageID
    tmpPdf = r"V:\gislu\Planning\DeveloperKit\output\Section_temp.pdf"
    ddp.exportToPDF(tmpPdf, 'CURRENT', resolution = 175)
    finalPdf.appendPages(tmpPdf)
    os.remove(tmpPdf)
    del mxd, ddp
    the line:
    Code:
    finalPdf.appendPages(tmpPdf)
    probably won't work because finalPdf isn't defined. I am assuming you have it defined in code that you didn't include in your post.
    Jeff Ward
    Summit County, Utah

  7. #7
    Molly Molter
    Join Date
    Aug 2011
    Posts
    22
    Points
    0
    Answers Provided
    0


    0

    Default Re: Python to select page name and export current page using data driven pages

    Thanks Jeff. I do have the os.remove(tmpPDF) command at the end of the script to remove all of the temporary pdfs. I also have defined the finalPDF at the beginning of the script which I did not include. I tried your suggestion of removing the selection piece and replacing the SEC_FULL with the STR parameter. This is what the new code section looks like:

    #Select and Zoom To Section Map, Export, and Append
    mxd = arcpy.mapping.MapDocument(r"V:\gislu\Planning\DeveloperKit\SectionMap.mxd")
    df = mapping.ListDataFrames(mxd, "STR Map")[0]
    ddp = mxd.dataDrivenPages
    pageID = ddp.getPageIDFromName(STR)
    ddp.currentPageID = pageID
    tmpPdf = r"V:\gislu\Planning\DeveloperKit\output\Section_temp.pdf"
    ddp.exportToPDF(tmpPdf, 'CURRENT', resolution = 175)
    finalPdf.appendPages(tmpPdf)
    del mxd, tmpPdf, ddp

    I typed the user defined parameter exactly the same way it is shown in the SEC_FULL field in the attribute table. I got the following error:

    <type 'exceptions.ValueError'>: u'T1S R70W S30'
    Failed to execute (MapbookSection).

    Any other suggestions?

  8. #8
    Jeff Ward
    Join Date
    May 2010
    Posts
    141
    Points
    8
    Answers Provided
    1


    0

    Default Re: Python to select page name and export current page using data driven pages

    Have you tried hard coding the text into the program? It may be a problem with getting the STR variable from the GetParameterAsText function.

    Code:
    pageID = ddp.getPageIDFromName('T1S R70W S30')
    If that works maybe try placing your GetParameterAsText function in the str() function.

    Code:
    STR = str(arcpy.GetParameterAsText(0))
    Jeff Ward
    Summit County, Utah

  9. #9
    Molly Molter
    Join Date
    Aug 2011
    Posts
    22
    Points
    0
    Answers Provided
    0


    0

    Default Re: Python to select page name and export current page using data driven pages

    That worked! Thank you so much! The file size of the final mapbook is too large to attach. If you would like a copy of the entire script let me know. It writes a title page, TOC, vicinity map, section map, and several thematic maps zoomed in on a particular property and appends it into 1 PDF. We got the idea from Clark County, WA here: http://gis.clark.wa.gov/gishome/WebO...id=goDevPacket

  10. #10
    Jeffrey Barrette

    Join Date
    Oct 2009
    Posts
    712
    Points
    201
    Answers Provided
    64


    0

    Default Re: Python to select page name and export current page using data driven pages

    There are a couple of already existing samples on the resource center:

    http://resources.arcgis.com/gallery/...8-3885D388EF60

    There is one called Print Data Driven Page(s).

    Jeff

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts