<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Iterate through two feature classes and export to PDF in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/iterate-through-two-feature-classes-and-export-to/m-p/1560837#M73213</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    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)&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 20 Nov 2024 17:19:23 GMT</pubDate>
    <dc:creator>CayerA</dc:creator>
    <dc:date>2024-11-20T17:19:23Z</dc:date>
    <item>
      <title>Iterate through two feature classes and export to PDF</title>
      <link>https://community.esri.com/t5/python-questions/iterate-through-two-feature-classes-and-export-to/m-p/1560837#M73213</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    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)&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 20 Nov 2024 17:19:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-through-two-feature-classes-and-export-to/m-p/1560837#M73213</guid>
      <dc:creator>CayerA</dc:creator>
      <dc:date>2024-11-20T17:19:23Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate through two feature classes and export to PDF</title>
      <link>https://community.esri.com/t5/python-questions/iterate-through-two-feature-classes-and-export-to/m-p/1561277#M73221</link>
      <description>&lt;P&gt;If I am iterating through a lot of pdf's, I normally build functions, like below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;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")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Nov 2024 19:36:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterate-through-two-feature-classes-and-export-to/m-p/1561277#M73221</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2024-11-21T19:36:05Z</dc:date>
    </item>
  </channel>
</rss>

