Search code examples
pythonvbscriptarcgisarcmap

ArcMap Data Driven Pages Dynamic Feature Labels


I am trying to work out a way to change between two sets of labels on a map. I have a map with zip codes that are labeled and I want to be able to output two maps: one with the zip code label (ZIP) and one with a value from a field I have joined to the data (called chrlabel). The goal is to have one map showing data for each zip code, and a second map giving the zip code as reference.

My initial attempt which I can't get working looks like this:
1) I added a second data frame to my map and add a new layer that contains two polygons with names "zip" and "chrlabel".
2) I use this frame to enable data driven pages and then I hide it behind the primary frame (I don't want to see those polygons, I just want to use them to control the data driven pages).
3) In the zip code labels I tried to write a VBScript expression like this pseudo-code:
test = "
If test = "zip" then
label = ZIP
else
label = CHRLABEL
endif

This does not work because the dynamic text does not resolve to the page name in the VBScript.
Is there some way to call the page name in VBScript so that I can make this work?

If not, is there another way to do this?
My other thought is to add another field to the layer that gets filled with a one or a zero. Then I could replace the if-then test condition with if NewField = 1.

Then I would just need to write a script that updates all the NewFields for the zipcode features when the data driven page advances to the second page. Is there a way to trigger a script (python or other) when a data driven page changes?

Thanks


Solution

  • 8 months too late, but for posterity...

    You're making things hard on yourself - it would be much easier to set up a duplicate layer and use different layers, then adjust layer visibility. I'm not familiar with VBScript for this sort of thing, but in Python (using ESRI's library) it would look something like so [python 2.6, ArcMap 10 - sample only, haven't debugged this but I do similar things quite often]:

    from arcpy import mapping
    
    ## Load the map from disk
    mxdFilePath = "C:\\GIS_Maps_Folder\\MyMap.mxd"
    mapDoc = mapping.MapDocument(mxdFilePath)
    
    ## Load map elements
    dataFrame = mapping.ListDataFrames(mapDoc)[0]   #assumes you want the first dataframe; you can also search by name
    mxdLayers = mapping.ListLayers(dataFrame)
    
    ## Adjust layers
    for layer in mxdLayers:
        if (layer.name == 'zip'):
            zip_lyr = layer
        elif(layer.name == 'sample_units'):
            labels_lyr = layer
    
    ## Print zip code map
    zip_lyr.visible = True
    zip_lyr.showLabels = True
    labels_lyr.visible = False
    labels_lyr.showLabels = False
    
    zip_path = "C:\\Output_Folder\\Zips.pdf"
    mapping.ExportToPDF(mapDoc, zip_path, layers_attributes="NONE", resolution=150)
    
    ## Print labels map
    zip_lyr.visible = False
    zip_lyr.showLabels = False
    labels_lyr.visible = True
    labels_lyr.showLabels = True
    
    labels_path = "C:\\Output_Folder\\Labels.pdf"
    mapping.ExportToPDF(mapDoc, labels_path, layers_attributes="NONE", resolution=150)
    
    ## Combine files (if desired)
    pdfDoc = mapping.PDFDocumentCreate("C:\\Output_Folder\\Output.pdf"")
    pdfDoc.appendPages(zip_path)
    pdfDoc.appendPages(labels_path)
    pdfDoc.saveAndClose()
    

    As far as the Data Driven Pages go, you can export them all at once or in a loop, and adjust whatever you want, although I'm not sure why you'd need to if you use something similar to the above. The ESRI documentation and examples are actually quite good on this. (You should be able to get to all the other Python documentation pretty easily from that page.)