Mask other Features in data driven page

Discussion created by CFortich on Feb 5, 2016

Using Python;

I was trying to mask adjacent features, or features in extent, so that PDF I was creating showed only the Feature of the Page Name.  I couldn't use the usual Mask method because I had a Raster Layer (shaded relief) on the bottom.  Usual method puts the adjacent features in the background color that would cover the raster layer.


Here is how I solved the problem. 

Background.  The feature layer source for the data driven pages has definition query on the outset.  Another identical layer with different seme-transparent symbology was used to show the adjacent features, if any.


In code a made a list of all the features that were to be used in the Data pages.

# Getting List of Features  that are driving the pages after setting the MXD

wherecl = r"Field1 = 0 AND Field2 = 1" #My data had a def query already

flds = []

lyrs = arcpy.mapping.ListLayers(mxd,"<Your Layer Name>") will find 2 layers.  1st data driven layer and "adjacent" layer

for lyr in lyrs:

    flay = arcpy.MakeFeatureLayer_management(lyr)

    sc = arcpy.SearchCursor(flay,wherecl)

    for row in sc:

        flds.append(row.getValue("<Your Field name driving pages>")) # Name is the field used to create the data driven page


flds.sort() #sorts list so will print maps in same order as data pages


Then use this list the alter the def query (or create one if your layer doesn't already have on) of you primary and adjacent layer.

# Reset Data Driven Pages to Single Page of Unit from list above (flds)

unitcount = 0

for unit in flds:

    defq = r"Field1 = 0 AND Field2 = 1 and NAME = '" + unit + "'"

    lyrs = arcpy.mapping.ListLayers(mxd)

#Alter  def query of your 2 layers; I find it by searching for the query

    for lyr in lyrs:

        if lyr.supports("DEFINITIONQUERY") and 'Field1 = 0' in lyr.definitionQuery:

            lyr.definitionQuery = defq





##Now you have a Data Driven Page with only 1 page   


    #### Create the PDF     

    arcpy.mapping.ExportToPDF(mxd, pdfpath + unit + ".pdf", resolution=300)


## Reset the Data Driven Page to all pages to return MXD to original state

defq = wherecl

lyrs = arcpy.mapping.ListLayers(mxd)

for lyr in lyrs:

    if lyr.supports("DEFINITIONQUERY") and 'Field 1 = 0' in lyr.definitionQuery:

        lyr.definitionQuery = defq