Skip to content

Instantly share code, notes, and snippets.

@cindygis
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cindygis/6a440c1e5bc07ea05821 to your computer and use it in GitHub Desktop.
Save cindygis/6a440c1e5bc07ea05821 to your computer and use it in GitHub Desktop.
Intersects feature layers with the index layer for selected data driven export to a multi-page PDF with embedded layers.
#
# @date 13/07/2015
# @author Cindy Williams
#
# Intersects feature layers with the index layer
# for selected data driven export to a multi-page
# PDF with embedded layers.
#
# For use as a standalone script.
#
import arcpy
import os
map_folder = r"C:\Some\Arb\Folder\mxd
pdf_folder = r"C:\Some\Arb\Folder\pdf"
kml_folder = r"C:\Some\Arb\Folder\kml"
# Get all the mxds in the map folder
(_,_, mapdocs) = os.walk(map_folder).next()
for md in mapdocs:
mxd = arcpy.mapping.MapDocument(os.path.join(map_folder, md))
df = arcpy.mapping.ListDataFrames(mxd)[0] # First data frame
ddp = mxd.dataDrivenPages
ddp_lyr = ddp.indexLayer
# Only get the layers in the Data group
data_lyrs = [lyr for lyr in arcpy.mapping.ListLayers(mxd) if lyr.isFeatureLayer and lyr.longName.split("\\")[0] == "Data"]
for lyr in data_lyrs:
# Selects the data driven pages that intersects all feature layers
# in the Data group
arcpy.management.SelectLayerByLocation(in_layer=ddp_lyr,
overlap_type="CONTAINS",
select_features=lyr,
selection_type="ADD_TO_SELECTION")
print("Data driven pages selected: {}.".format(len(ddp.selectedPages)))
pdf_name = os.path.join(pdf_folder, md[:-4] + ".pdf")
# Uses the PDF export function from the DataDrivenPagesClass
ddp.exportToPDF(out_pdf=pdf_name,
page_range_type="SELECTED",
multiple_files="PDF_SINGLE_FILE",
resolution=100,
layers_attributes="LAYERS_AND_ATTRIBUTES",
georef_info="True")
print("Exported " + md)
# Cannot export to KML with basemap in mxd. Remove layer
# first and add back in
kmz_name = os.path.join(kml_folder, md[:-4] + ".kmz")
# Converts the mxd to KML. Only the Data group layers are
# switched on, so only they will be exported. Can enforce
# the layer visibility by lyr.visible = True
arcpy.conversion.MapToKML(in_map_document=mxd,
data_frame=df.name,
out_kmz_file=kmz_name,
map_output_scale=10000)
print("KML conversion complete.")
print("Script complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment