Select to view content in your preferred language

Automatically exporting maps through layer toggle

671
3
Jump to solution
01-18-2023 01:22 AM
CFrost
by
New Contributor II

Hi, I have a list of layers that I want mapped.  the layers are a mix of raster / vector but the map extent is a global and consistent regardless of the layer.  The legend attributes are the same as well, just the legend title (name of layer) would need to change. 

Is there a way to automatically cycle through the list of layers turning one on at a time, updating legend name, and exporting this as a map?  Basically, the same as data driven pages but instead of cycling through various features and changing extents, the extent stays the same but the layers are changing.  

Any advise welcome!

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

You can do that with a little Python. A quick-and-dirty approach:

layout_name = "Layout"
locked_layers = ["World_Imagery"]  # these layers will not change visibility
export_folder = "N:/.../temp"

# get the layout, its map frame, and its legend
aprx = arcpy.mp.ArcGISProject("current")
layout = aprx.listLayouts(layout_name)[0]
mapframe = layout.listElements("MAPFRAME_ELEMENT")[0]
legend = layout.listElements("LEGEND_ELEMENT")[0]

# get the layers
layers = [
    layer
    for layer in mapframe.map.listLayers()
    if layer.name not in locked_layers
    and layer.supports("visible")
    and layer.name == layer.longName
    ]
# for each layer
for layer in layers:
    print(f"Exporting layer {layer.name}")
    # make all layers invisible
    for other_layer in layers:
            other_layer.visible = False
    # make the layer visible
    layer.visible = True
    # change the legend title
    legend.title = layer.name
    # export the layout
    layout.exportToPNG(f"{export_folder}/Export_Layer_{layer.name}.png")

Have a great day!
Johannes

View solution in original post

3 Replies
JohannesLindner
MVP Frequent Contributor

You can do that with a little Python. A quick-and-dirty approach:

layout_name = "Layout"
locked_layers = ["World_Imagery"]  # these layers will not change visibility
export_folder = "N:/.../temp"

# get the layout, its map frame, and its legend
aprx = arcpy.mp.ArcGISProject("current")
layout = aprx.listLayouts(layout_name)[0]
mapframe = layout.listElements("MAPFRAME_ELEMENT")[0]
legend = layout.listElements("LEGEND_ELEMENT")[0]

# get the layers
layers = [
    layer
    for layer in mapframe.map.listLayers()
    if layer.name not in locked_layers
    and layer.supports("visible")
    and layer.name == layer.longName
    ]
# for each layer
for layer in layers:
    print(f"Exporting layer {layer.name}")
    # make all layers invisible
    for other_layer in layers:
            other_layer.visible = False
    # make the layer visible
    layer.visible = True
    # change the legend title
    legend.title = layer.name
    # export the layout
    layout.exportToPNG(f"{export_folder}/Export_Layer_{layer.name}.png")

Have a great day!
Johannes
CFrost
by
New Contributor II

HI Johannes, this looks really promising!  Would I simply amend the code and run in the arcpro python window?  Or outside of pro?  Thank you!

0 Kudos
JohannesLindner
MVP Frequent Contributor

Yeah, change lines 1, 2, 3, and 30 (if you want to export to a different format). Copy paste into the Pro Python window, execute.

You should be able to run that as a standalone script outside of Pro. For that, use the project path in line 6 instead of "current". I haven't tested that, though.


Have a great day!
Johannes