Iterate through selected attributes and export toggled layer maps

851
7
Jump to solution
08-12-2021 01:23 PM
EvanSweeney
New Contributor II

Hi All, 

I have to make about 17 to 20 maps for a given location ranging from soils, topographic, wetlands, etc. I have to make these maps for about 145 different locations. So in total i will be making  about 2500 maps. 

I have worked extensively with data driven pages and find it works great when the location varies but the layer stays the same, e.g. the wetland layer maps for all fields. I think of this as horizontal results. 

I have also made a scrip that will output all the maps for a give location. It will create the soil, topographic, wetland, and otherwise maps for a given location. I think of this as vertical results. 

import arcpy, os

# Specify output path and final output PDF
outPath = r'C:\Outputs\TEST'
pdfPath = os.path.join(outPath, '1. WetlandSupportdocs.pdf')
if os.path.exists(pdfPath):
os.remove(pdfPath)
finalPdf = arcpy.mapping.PDFDocumentCreate(pdfPath)

# Specify the map document and the data frame
mxd = arcpy.mapping.MapDocument(r'C:\Geodata\Template.mxd')
df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]

#################### ASSIGN VARIABLES ###########################
#################################################################
#################################################################
#################################################################

Farm = ("2222")
Tract = ("1111")
CLU = ("1")
District = ("District")
County = ("Wyoming")
AssistedBy = ("Bob Conservationsit")
FieldOffice = ("Hamilton")
Customer = ("Joe Landowner")

#################################################################
#################################################################
#################################################################
#################################################################

LandUnits = ('Farm ' +str(Farm) + ',' + 'Tract ' +str(Tract))

#Change County text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","District")[0]
TextElement.text = District

#Change County text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","County")[0]
TextElement.text = County

#Change AssistedBy text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","AssistedBy")[0]
TextElement.text = AssistedBy

#Change Field Office text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","FieldOffice")[0]
TextElement.text = FieldOffice

#Change Customer name
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","CustomerName")[0]
TextElement.text = Customer

#Change Landunits
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","LandUnits")[0]
TextElement.text = LandUnits

# Select a tract using the TRACNBR attribute and zoom to selected
lyr = arcpy.mapping.ListLayers(mxd, 'clu_a_053', df)[0]
if CLU == "":
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", 'TRACTNBR IN ({0}) '.format(Tract))
df.zoomToSelectedFeatures()
lyr.definitionQuery = 'TRACTNBR IN ({0})'.format(Tract)
else:
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", 'TRACTNBR IN ({0}) AND CLUNBR IN ({1})'.format(Tract, CLU))
df.zoomToSelectedFeatures()
lyr.definitionQuery = 'TRACTNBR IN ({0})AND CLUNBR IN ({1})'.format(Tract, CLU)
df.zoomToSelectedFeatures()

arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
arcpy.RefreshActiveView()

# Turn on visibility for each theme and export the page
lyrList = ['FSA Wetspots','National Wetlands Inventory','NYSDEC Wetlands','Madison County Soils','Soil Special Features','Percent Hydric Soils','Highly Erodible Land','Madison 2ft Contours','Topographic Image','DEC Unconsolidated Aquifers of NY State','EPA NYS sole source aquifers','USGS Potential Carbonate Karst Topography ','Surficial Geology','NYS Bedrock Geology ','NYS EcoRegion','NYS Important Bird Areas','Northern Long Eared Bat Location Data' ]

# Don't assume the layers are turned off...Turn them off first.
print("First, turn off all the layers")
for lyrName in lyrList:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = False
print("\tSet layer-visibility for '" + lyr.name + "' to " + str(lyr.visible))

print("Second, export to PDF")
for lyrName in lyrList:
print("---")
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = True
print("\tSet layer-visibility for '" + lyr.name + "' to " + str(lyr.visible))

#Change title text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
TextElement.text = lyrName

#Change Landowner name
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","CustomerName")[0]
TextElement.text = 'Holmes-Acre LLC'

#Change Landunits
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","FarmTractNbr")[0]
TextElement.text = 'Farm NUB Tract 2215'

#Export each theme to a temporary PDF and append to the final PDF
tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
if os.path.exists(tmpPdf):
os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
print("\tPDF Exported to following path: " + tmpPdf)
finalPdf.appendPages(tmpPdf)

#Turn off layer visibility and clean up for next pass through the loop
lyr.visible = False
print("\tSet layer-visibility for '" + lyr.name + "' to " + str(lyr.visible))

################################### Wetland Data #################################################

# Turn on visibility for each theme and export the page
lyrListWET = ['National Wetlands Inventory','NYSDEC Wetlands']

# Don't assume the layers are turned off...Turn them off first.
print("First, turn on all the layers")

for lyrName in lyrListWET:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = True

#Change title text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
TextElement.text = "Wetlands"

#Export each theme to a temporary PDF and append to the final PDF
tmpPdf = os.path.join(outPath, "Wetlands" + '_temp.pdf')
if os.path.exists(tmpPdf):
os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
print("\tPDF Exported to following path: " + tmpPdf)
finalPdf.appendPages(tmpPdf)

#Turn off layer visibility and clean up for next pass through the loop
for lyrName in lyrListWET:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = False
del lyr

################################### CIR Data #################################################

# Turn on visibility for each theme and export the page
lyrListCIR = ['CIR_doqq_flight_dates','Infrared Ortho Imagery']

# Don't assume the layers are turned off...Turn them off first.
print("First, turn on all the layers")

for lyrName in lyrListCIR:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = True

#Change title text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
TextElement.text = lyrName

#Export each theme to a temporary PDF and append to the final PDF
tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
if os.path.exists(tmpPdf):
os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
print("\tPDF Exported to following path: " + tmpPdf)
finalPdf.appendPages(tmpPdf)

#Turn off layer visibility and clean up for next pass through the loop
for lyrName in lyrListCIR:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = False
del lyr

################################### 2008 Data ##############################################

# Turn on visibility for each theme and export the page
lyrList2008 = ['ortho_1-1_1n_ny053_2008_1','2008 Ortho Imagery']

# Don't assume the layers are turned off...Turn them off first.
print("2008 data")

for lyrName in lyrList2008:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = True

#Change title text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
TextElement.text = lyrName

#Export each theme to a temporary PDF and append to the final PDF
tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
if os.path.exists(tmpPdf):
os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
print("\tPDF Exported to following path: " + lyr.name + tmpPdf)
finalPdf.appendPages(tmpPdf)

#Turn off layer visibility and clean up for next pass through the loop
for lyrName in lyrList2008:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = False
del lyr

################################### 2009 Data ##############################################

# Turn on visibility for each theme and export the page
lyrList2009 = ['ortho_1-1_1n_ny053_2009_1','2009 Ortho Imagery']

# Don't assume the layers are turned off...Turn them off first.
print("2009 data")

for lyrName in lyrList2009:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = True

#Change title text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
TextElement.text = lyrName

#Export each theme to a temporary PDF and append to the final PDF
tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
if os.path.exists(tmpPdf):
os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
print("\tPDF Exported to following path: " + lyr.name + tmpPdf)
finalPdf.appendPages(tmpPdf)

#Turn off layer visibility and clean up for next pass through the loop
for lyrName in lyrList2009:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = False
del lyr

################################### 2011 Data ##############################################

# Turn on visibility for each theme and export the page
lyrList2011 = ['ortho_1-1_1n_ny053_2011_1','2011 Ortho Imagery']

# Don't assume the layers are turned off...Turn them off first.
print("2011 Data")

for lyrName in lyrList2011:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = True

#Change title text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
TextElement.text = lyrName

#Export each theme to a temporary PDF and append to the final PDF
tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
if os.path.exists(tmpPdf):
os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
print("\tPDF Exported to following path: " + lyr.name + tmpPdf)
finalPdf.appendPages(tmpPdf)

#Turn off layer visibility and clean up for next pass through the loop
for lyrName in lyrList2011:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = False
del lyr

################################### 2013 Data ##############################################

# Turn on visibility for each theme and export the page
lyrList2013 = ['ortho_1-1_1n_s_ny053_2013_1','2013 Ortho Imagery']

# Don't assume the layers are turned off...Turn them off first.
print("2013 data")

for lyrName in lyrList2013:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = True

#Change title text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
TextElement.text = lyrName

#Export each theme to a temporary PDF and append to the final PDF
tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
if os.path.exists(tmpPdf):
os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
print("\tPDF Exported to following path: " + lyr.name + tmpPdf)
finalPdf.appendPages(tmpPdf)

#Turn off layer visibility and clean up for next pass through the loop
for lyrName in lyrList2013:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = False
del lyr

################################### 2015 Data ##############################################

# Turn on visibility for each theme and export the page
lyrList2015 = ['ortho_1-1_hn_s_ny053_2015_1','2015 Ortho Imagery']


# Don't assume the layers are turned off...Turn them off first.
print("2015 data")

for lyrName in lyrList2015:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = True

#Change title text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
TextElement.text = lyrName

#Export each theme to a temporary PDF and append to the final PDF
tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
if os.path.exists(tmpPdf):
os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
print("\tPDF Exported to following path: " + lyr.name + tmpPdf)
finalPdf.appendPages(tmpPdf)

#Turn off layer visibility and clean up for next pass through the loop
for lyrName in lyrList2015:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = False
del lyr

################################### 2019 Data ##############################################

# Turn on visibility for each theme and export the page
lyrList2019 = ['NAIP2019_otho_flight_lines_a_NY','2019 Ortho Imagery']

# Don't assume the layers are turned off...Turn them off first.
print("2019 data")

for lyrName in lyrList2019:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = True

#Change title text
TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
TextElement.text = lyrName

#Export each theme to a temporary PDF and append to the final PDF
tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
if os.path.exists(tmpPdf):
os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
print("\tPDF Exported to following path: " + lyr.name + tmpPdf)
finalPdf.appendPages(tmpPdf)

#Turn off layer visibility and clean up for next pass through the loop
for lyrName in lyrList2019:
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = False
del lyr

finalPdf.saveAndClose()
del mxd, df, finalPdf

Is there anyway to intergrate both a vertical and horizonal result? Selecting the tracts i need maps for and iterating through making maps for each selected tract?

 

Thanks

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
EvanSweeney
New Contributor II

Just an FYI, i found the easiest solution was to forgo the variables option and just create an SQL for my search parameters. i.e.

 

 

SQL =  "TRACTNBR IN (1234) AND CLUNBR IN ( 12)",
       "TRACTNBR IN (6789) AND CLUNBR IN ( 4,5,6,14,20)",
       "TRACTNBR IN (3456) AND CLUNBR IN ( 1)",]


##  Run for loop through each SQL statement  ##
for SQL in SQL:
         MAKE ALL THE MAPS!

>

It works pretty well until I run into a new problem that ill be posing to those who know better. The code gets hung up on Fields that dont exist (i.e. "TRACTNBR IN (1234) AND CLUNBR IN (17)" is the 250th loop in the code. The code stalls and eventually comes out with "RESTART: SHELL". I know i can go through and remove the offending the SQL, but i would rather add in something to help skip those errors. Ill likely be translating this for other teams to use so i dont want to bother pulling out bad fld numbers. 

View solution in original post

0 Kudos
7 Replies
JeffK
by MVP Regular Contributor
MVP Regular Contributor

If I understand your need correctly, how about getting a list of tracts and then iterating over it?

Convert your script into a function so you can pass dynamic variables into it and reassign the variables as needed.

 

 

def createMaps(tract):
    Tract = tract
    .... # rest of your code

whereclause = 'if you need one to choose specfic tracts'
createMaps(x[0]) for x in arcpy.da.SearchCursor(yourfc, 'tractfield', whereclause)

 

 

 

0 Kudos
EvanSweeney
New Contributor II

Hi Jeff, I'm not much of a coder. Actually, I'm not a coder at all. I'm a conservationist. But I have tried to incorporate this code as indicated above and keep getting a syntax error on the 'for'. 

 

EvanSweeney_0-1628895814545.png

 

I think this might be the solution i am looking for, i just dont know how to incorporate it. 

0 Kudos
JeffK
by MVP Regular Contributor
MVP Regular Contributor

No worries.  I didn't want to copy/ paste the whole code again but give this a try.  You might need to check the indents and make sure that they are correct since they get moved when the code copied/pasted from here without it being formatted. I see you also need/passed a second field, clu number so I provided the long form of the iteration to show passing the two values (check out lines 22 & 23). Hi from Wyoming the state, hope this helps.

 

import arcpy, os

def createMaps(tract, cluNmbr):
    # Specify output path and final output PDF
    outPath = r'C:\Outputs\TEST'
    pdfPath = os.path.join(outPath, '1. WetlandSupportdocs.pdf')
    if os.path.exists(pdfPath):
        os.remove(pdfPath)
    finalPdf = arcpy.mapping.PDFDocumentCreate(pdfPath)

    # Specify the map document and the data frame
    mxd = arcpy.mapping.MapDocument(r'C:\Geodata\Template.mxd')
    df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]

    #################### ASSIGN VARIABLES ###########################
    #################################################################
    #################################################################
    #################################################################

    # I assume these are the variables that you want to change dynamically so assign incoming parameters to the variable
    Farm = ("2222")
    Tract = tract
    CLU = cluNmbr
    District = ("District")
    County = ("Wyoming")
    AssistedBy = ("Bob Conservationsit")
    FieldOffice = ("Hamilton")
    Customer = ("Joe Landowner")

    #################################################################
    #################################################################
    #################################################################
    #################################################################

    LandUnits = ('Farm ' +str(Farm) + ',' + 'Tract ' +str(Tract))

    #Change County text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","District")[0]
    TextElement.text = District

    #Change County text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","County")[0]
    TextElement.text = County

    #Change AssistedBy text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","AssistedBy")[0]
    TextElement.text = AssistedBy

    #Change Field Office text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","FieldOffice")[0]
    TextElement.text = FieldOffice

    #Change Customer name
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","CustomerName")[0]
    TextElement.text = Customer

    #Change Landunits
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","LandUnits")[0]
    TextElement.text = LandUnits

    # Select a tract using the TRACNBR attribute and zoom to selected
    lyr = arcpy.mapping.ListLayers(mxd, 'clu_a_053', df)[0]
    if CLU == "":
        arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", 'TRACTNBR IN ({0}) '.format(Tract))
        df.zoomToSelectedFeatures()
        lyr.definitionQuery = 'TRACTNBR IN ({0})'.format(Tract)
    else:
        arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", 'TRACTNBR IN ({0}) AND CLUNBR IN ({1})'.format(Tract, CLU))
        df.zoomToSelectedFeatures()
        lyr.definitionQuery = 'TRACTNBR IN ({0})AND CLUNBR IN ({1})'.format(Tract, CLU)
        df.zoomToSelectedFeatures()

        arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
        arcpy.RefreshActiveView()

    # Turn on visibility for each theme and export the page
    lyrList = ['FSA Wetspots','National Wetlands Inventory','NYSDEC Wetlands','Madison County Soils','Soil Special Features','Percent Hydric Soils','Highly Erodible Land','Madison 2ft Contours','Topographic Image','DEC Unconsolidated Aquifers of NY State','EPA NYS sole source aquifers','USGS Potential Carbonate Karst Topography ','Surficial Geology','NYS Bedrock Geology ','NYS EcoRegion','NYS Important Bird Areas','Northern Long Eared Bat Location Data' ]

    # Don't assume the layers are turned off...Turn them off first.
    print("First, turn off all the layers")
    for lyrName in lyrList:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
        lyr.visible = False
        print("\tSet layer-visibility for '" + lyr.name + "' to " + str(lyr.visible))

    print("Second, export to PDF")
    for lyrName in lyrList:
        print("---")
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
        lyr.visible = True
        print("\tSet layer-visibility for '" + lyr.name + "' to " + str(lyr.visible))

    #Change title text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
    TextElement.text = lyrName

    #Change Landowner name
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","CustomerName")[0]
    TextElement.text = 'Holmes-Acre LLC'

    #Change Landunits
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","FarmTractNbr")[0]
    TextElement.text = 'Farm NUB Tract 2215'

    #Export each theme to a temporary PDF and append to the final PDF
    tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
    if os.path.exists(tmpPdf):
        os.remove(tmpPdf)

    arcpy.mapping.ExportToPDF(mxd, tmpPdf)
    print("\tPDF Exported to following path: " + tmpPdf)
    finalPdf.appendPages(tmpPdf)

    #Turn off layer visibility and clean up for next pass through the loop
    lyr.visible = False
    print("\tSet layer-visibility for '" + lyr.name + "' to " + str(lyr.visible))

    ################################### Wetland Data #################################################

    # Turn on visibility for each theme and export the page
    lyrListWET = ['National Wetlands Inventory','NYSDEC Wetlands']

    # Don't assume the layers are turned off...Turn them off first.
    print("First, turn on all the layers")

    for lyrName in lyrListWET:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = True

    #Change title text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
    TextElement.text = "Wetlands"

    #Export each theme to a temporary PDF and append to the final PDF
    tmpPdf = os.path.join(outPath, "Wetlands" + '_temp.pdf')
    if os.path.exists(tmpPdf):
        os.remove(tmpPdf)
    arcpy.mapping.ExportToPDF(mxd, tmpPdf)
    print("\tPDF Exported to following path: " + tmpPdf)
    finalPdf.appendPages(tmpPdf)

    #Turn off layer visibility and clean up for next pass through the loop
    for lyrName in lyrListWET:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = False
    del lyr

    ################################### CIR Data #################################################

    # Turn on visibility for each theme and export the page
    lyrListCIR = ['CIR_doqq_flight_dates','Infrared Ortho Imagery']

    # Don't assume the layers are turned off...Turn them off first.
    print("First, turn on all the layers")

    for lyrName in lyrListCIR:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = True

    #Change title text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
    TextElement.text = lyrName

    #Export each theme to a temporary PDF and append to the final PDF
    tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
    if os.path.exists(tmpPdf):
        os.remove(tmpPdf)
    arcpy.mapping.ExportToPDF(mxd, tmpPdf)
    print("\tPDF Exported to following path: " + tmpPdf)
    finalPdf.appendPages(tmpPdf)

    #Turn off layer visibility and clean up for next pass through the loop
    for lyrName in lyrListCIR:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = False
    del lyr

    ################################### 2008 Data ##############################################

    # Turn on visibility for each theme and export the page
    lyrList2008 = ['ortho_1-1_1n_ny053_2008_1','2008 Ortho Imagery']

    # Don't assume the layers are turned off...Turn them off first.
    print("2008 data")

    for lyrName in lyrList2008:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = True

    #Change title text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
    TextElement.text = lyrName

    #Export each theme to a temporary PDF and append to the final PDF
    tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
    if os.path.exists(tmpPdf):
        os.remove(tmpPdf)
    arcpy.mapping.ExportToPDF(mxd, tmpPdf)
    print("\tPDF Exported to following path: " + lyr.name + tmpPdf)
    finalPdf.appendPages(tmpPdf)

    #Turn off layer visibility and clean up for next pass through the loop
    for lyrName in lyrList2008:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = False
    del lyr

    ################################### 2009 Data ##############################################

    # Turn on visibility for each theme and export the page
    lyrList2009 = ['ortho_1-1_1n_ny053_2009_1','2009 Ortho Imagery']

    # Don't assume the layers are turned off...Turn them off first.
    print("2009 data")

    for lyrName in lyrList2009:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = True

    #Change title text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
    TextElement.text = lyrName

    #Export each theme to a temporary PDF and append to the final PDF
    tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
    if os.path.exists(tmpPdf):
        os.remove(tmpPdf)
    arcpy.mapping.ExportToPDF(mxd, tmpPdf)
    print("\tPDF Exported to following path: " + lyr.name + tmpPdf)
    finalPdf.appendPages(tmpPdf)

    #Turn off layer visibility and clean up for next pass through the loop
    for lyrName in lyrList2009:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = False
    del lyr

    ################################### 2011 Data ##############################################

    # Turn on visibility for each theme and export the page
    lyrList2011 = ['ortho_1-1_1n_ny053_2011_1','2011 Ortho Imagery']

    # Don't assume the layers are turned off...Turn them off first.
    print("2011 Data")

    for lyrName in lyrList2011:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = True

    #Change title text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
    TextElement.text = lyrName

    #Export each theme to a temporary PDF and append to the final PDF
    tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
    if os.path.exists(tmpPdf):
        os.remove(tmpPdf)
    arcpy.mapping.ExportToPDF(mxd, tmpPdf)
    print("\tPDF Exported to following path: " + lyr.name + tmpPdf)
    finalPdf.appendPages(tmpPdf)

    #Turn off layer visibility and clean up for next pass through the loop
    for lyrName in lyrList2011:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = False
    del lyr

    ################################### 2013 Data ##############################################

    # Turn on visibility for each theme and export the page
    lyrList2013 = ['ortho_1-1_1n_s_ny053_2013_1','2013 Ortho Imagery']

    # Don't assume the layers are turned off...Turn them off first.
    print("2013 data")

    for lyrName in lyrList2013:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = True

    #Change title text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
    TextElement.text = lyrName

    #Export each theme to a temporary PDF and append to the final PDF
    tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
    if os.path.exists(tmpPdf):
        os.remove(tmpPdf)
    arcpy.mapping.ExportToPDF(mxd, tmpPdf)
    print("\tPDF Exported to following path: " + lyr.name + tmpPdf)
    finalPdf.appendPages(tmpPdf)

    #Turn off layer visibility and clean up for next pass through the loop
    for lyrName in lyrList2013:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = False
    del lyr

    ################################### 2015 Data ##############################################

    # Turn on visibility for each theme and export the page
    lyrList2015 = ['ortho_1-1_hn_s_ny053_2015_1','2015 Ortho Imagery']


    # Don't assume the layers are turned off...Turn them off first.
    print("2015 data")

    for lyrName in lyrList2015:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = True

    #Change title text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
    TextElement.text = lyrName

    #Export each theme to a temporary PDF and append to the final PDF
    tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
    if os.path.exists(tmpPdf):
        os.remove(tmpPdf)
    arcpy.mapping.ExportToPDF(mxd, tmpPdf)
    print("\tPDF Exported to following path: " + lyr.name + tmpPdf)
    finalPdf.appendPages(tmpPdf)

    #Turn off layer visibility and clean up for next pass through the loop
    for lyrName in lyrList2015:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = False
    del lyr

    ################################### 2019 Data ##############################################

    # Turn on visibility for each theme and export the page
    lyrList2019 = ['NAIP2019_otho_flight_lines_a_NY','2019 Ortho Imagery']

    # Don't assume the layers are turned off...Turn them off first.
    print("2019 data")

    for lyrName in lyrList2019:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = True

    #Change title text
    TextElement = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT","TextTitle")[0]
    TextElement.text = lyrName

    #Export each theme to a temporary PDF and append to the final PDF
    tmpPdf = os.path.join(outPath, lyrName + '_temp.pdf')
    if os.path.exists(tmpPdf):
        os.remove(tmpPdf)
    arcpy.mapping.ExportToPDF(mxd, tmpPdf)
    print("\tPDF Exported to following path: " + lyr.name + tmpPdf)
    finalPdf.appendPages(tmpPdf)

    #Turn off layer visibility and clean up for next pass through the loop
    for lyrName in lyrList2019:
        lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
    lyr.visible = False
    del lyr

    finalPdf.saveAndClose()
    del mxd, df, finalPdf

with arcpy.da.SearchCursor('clu_layer', ['tractfield', 'cluNmbr'], 'whereclause') as sCur:
    for row in sCur:
        createMaps(row[0], row[1])

 

 

0 Kudos
EvanSweeney
New Contributor II

Hi From NY,

That is closer. I keep getting a runtime error "RuntimeError: cannot open 'clu_layer"

For this forum, this wasn't the actual layer name, but the correct layer name still comes up with the error. 

EvanSweeney_0-1628899617592.png

 

0 Kudos
JeffK
by MVP Regular Contributor
MVP Regular Contributor

There are a few ways to solve that-  You can use arcmap (copy from the location at the top of the catalog) /pro (click on the fc in the catalog and look for the Copy Path in the menu ribbon) to copy the path of the featureclass that you want and use the path, assign it to a variable and use the variable, or you can use arcpy.env.workspace and pass the fc name.

 

clu_layer = r'path to the fc'

with arcpy.da.SearchCursor(clu_layer, [fields]...)

# or 
with arcpy.da.SearchCursor(r'path to the fc', [fields]...)

# or using the workspace
arcpy.env.workspace = your directory or geodatabase containing the fc
with arcpy.da.SearchCursor('fcname in quotes', [fields]...)

 

 

 

0 Kudos
EvanSweeney
New Contributor II

Tried all three. Still getting the same error of not being able to open. But it's8:30 pm here so i think i have put in a solid 14 hour day. Thanks Jeff. Ill try again on monday. 

0 Kudos
EvanSweeney
New Contributor II

Just an FYI, i found the easiest solution was to forgo the variables option and just create an SQL for my search parameters. i.e.

 

 

SQL =  "TRACTNBR IN (1234) AND CLUNBR IN ( 12)",
       "TRACTNBR IN (6789) AND CLUNBR IN ( 4,5,6,14,20)",
       "TRACTNBR IN (3456) AND CLUNBR IN ( 1)",]


##  Run for loop through each SQL statement  ##
for SQL in SQL:
         MAKE ALL THE MAPS!

>

It works pretty well until I run into a new problem that ill be posing to those who know better. The code gets hung up on Fields that dont exist (i.e. "TRACTNBR IN (1234) AND CLUNBR IN (17)" is the 250th loop in the code. The code stalls and eventually comes out with "RESTART: SHELL". I know i can go through and remove the offending the SQL, but i would rather add in something to help skip those errors. Ill likely be translating this for other teams to use so i dont want to bother pulling out bad fld numbers. 

0 Kudos