Select to view content in your preferred language

Iterate through two feature classes and export to PDF

260
1
11-20-2024 09:19 AM
CayerA
by
Occasional Contributor

Hi all,

I am working on a script to create map booklets for local fire agencies. Each fire agency covers a different area of the county, and each will get their own map booklet. Essentially what I want the script to do is iterate through the fire districts (FDs) feature class, selecting the map pages features that intersect, then iterate through those map page features, exporting a PDF for each FD. I already have a script that does something similar for the entire county, but I need help with the double loop logic here (or maybe there's an even better way?). Here's what I have as a very rough draft that I know doesn't work:

    aprx = MAP.ArcGISProject(aprx_file)
    lyt = aprx.listLayouts("AtlasPage")[0]
    mf = lyt.listElements("mapframe_element", "Atlas Map Frame")[0]
    mapX = aprx.listMaps('FireAtlas')[0]
    FDs = mapX.listLayers('Fire District Boundary')[0]
    pages = mapX.listLayers('Section Pages')[0]

    with arcpy.da.SearchCursor(FDs, 'FireAgency') as FDcursor:
        for row in FDcursor:
            FD_loop(row, aprx, gv.data_path)
            agency = row[0].replace(" ", "")
            agency_path = os.path.join(gv.output_path, "{}".format(agency))
            if arcpy.Exists(agency_path + os.sep + "OutputX.pdf"):
                os.remove(agency_path + os.sep + "OutputX.pdf")
            select = "FireDistrict = '{}'".format(row[0])
            arcpy.SelectLayerByAttribute_management(FDs, 'NEW_SELECTION', select)
            arcpy.SelectLayerByLocation_management(pages, 'INTERSECT', FDs, '', 'NEW_SELECTION')
            spaFil = row.shape
            spaRel = "INTERSECTS"
            with arcpy.da.SearchCursor(pages, 'FDC', sql_clause=(None, 'ORDER BY PageOrder'), spatial_filter=spaFil, spatial_relationship=spaRel) as pageCursor:
                for page in pageCursor:
                    atlas_page(page, aprx, agency_path, gv.data_path)
0 Kudos
1 Reply
TonyAlmeida
MVP Regular Contributor

If I am iterating through a lot of pdf's, I normally build functions, like below.

 

 

import arcpy
import os

# Define the function to process each fire district
def FD_loop(row, aprx, data_path):
    agency = row[0].replace(" ", "")
    agency_path = os.path.join(data_path, "{}".format(agency))
    if arcpy.Exists(agency_path + os.sep + "OutputX.pdf"):
        os.remove(agency_path + os.sep + "OutputX.pdf")
    select = "FireDistrict = '{}'".format(row[0])
    arcpy.SelectLayerByAttribute_management(FDs, 'NEW_SELECTION', select)
    arcpy.SelectLayerByLocation_management(pages, 'INTERSECT', FDs, '', 'NEW_SELECTION')
    spaFil = row.shape
    spaRel = "INTERSECTS"
    with arcpy.da.SearchCursor(pages, 'FDC', sql_clause=(None, 'ORDER BY PageOrder'), spatial_filter=spaFil, spatial_relationship=spaRel) as pageCursor:
        for page in pageCursor:
            atlas_page(page, aprx, agency_path, data_path)

# Define the function to export each map page to PDF
def atlas_page(page, aprx, agency_path, data_path):
    lyt = aprx.listLayouts("AtlasPage")[0]
    mf = lyt.listElements("mapframe_element", "Atlas Map Frame")[0]
    mf.camera.setExtent(page.shape.extent)
    pdf_path = os.path.join(agency_path, "OutputX.pdf")
    lyt.exportToPDF(pdf_path)
    print(f"Exported {pdf_path}")

# Main script
aprx_file = "path_to_your_aprx_file"
aprx = arcpy.mp.ArcGISProject(aprx_file)
mapX = aprx.listMaps('FireAtlas')[0]
FDs = mapX.listLayers('Fire District Boundary')[0]
pages = mapX.listLayers('Section Pages')[0]

with arcpy.da.SearchCursor(FDs, 'FireAgency') as FDcursor:
    for row in FDcursor:
        FD_loop(row, aprx, "path_to_your_output_directory")