Select to view content in your preferred language

Export multiple layers individually to image, same extent, arcgis pro

824
2
02-29-2024 02:32 AM
AA25
by
New Contributor

In arcgis pro, I have a 5 polygons layers.

I'd like to export an image of each layer alone (5 images), without the other 4 layers being visible.

I would like the map extent to be identical for each exported image.

I would like the aspect ratio of the image to be 16:9.

Is there a way automate this export process?

0 Kudos
2 Replies
AA25
by
New Contributor

The script below worked for me. Please check its safe for yourself. I accept no responsibility for its effects.

It exports layers in the "export" group based on "layout1" to images.

If there is a better way to do this, I would be interested to know. Thanks! 😊

import arcpy
import os

# Get the username of the current user to construct dynamic paths
username = os.getlogin()

# Construct the project path dynamically
project_path = f"C:\\Users\\{username}\\Desktop\\project\\project.aprx"

# Construct the export path dynamically
export_path = f"C:\\Users\\{username}\\Desktop\\project\\export"
if not os.path.exists(export_path):
os.makedirs(export_path)

# Load the ArcGIS Pro project
aprx = arcpy.mp.ArcGISProject(project_path)

# Obtain references to the specified layout and map
layout = aprx.listLayouts("layout1")[0]
map = aprx.listMaps("map1")[0]

# Get the group layer named "export" within the map
export_group_layer = None
for lyr in map.listLayers():
if lyr.isGroupLayer and lyr.name == "export":
export_group_layer = lyr
break

if export_group_layer is not None:
# Iterate through each layer in the "export" group
for layer in export_group_layer.listLayers():
# Make the current layer visible and all others in the group invisible
for lyr in export_group_layer.listLayers():
lyr.visible = (lyr == layer)

# Construct the export file name and path
export_file_name = f"{layer.name}.png"
export_file_path = os.path.join(export_path, export_file_name)

# Export the layout to image
layout.exportToPNG(export_file_path)

print(f"Exported {layer.name} to {export_file_path}")

else:
print("Group layer 'export' not found in the specified map.")

 

 

GeeteshSingh07
Frequent Contributor

Great @AA25 , you found it! I did some tests on my end, and was able to export separate images. (Extent is set through layout itself, I was unable to set it through code). Hope this helps as well.

import arcpy
import os

# Set the workspace
arcpy.env.workspace = r"C:\Users\geete\Downloads\exported"

# Set the map document
aprx = arcpy.mp.ArcGISProject("CURRENT")
map = aprx.listMaps("Map")[0]  

# Get the extent of the map
#map_extent = map.defaultCamera.getExtent()

# Create a new layout for exporting
layout = aprx.listLayouts()[0] 

# Iterate through each layer in the map
for layer in map.listLayers():
    # Turn off all layers except the current one
    for lyr in map.listLayers():
        if lyr == layer:
            lyr.visible = True
        else:
            lyr.visible = False

    # Set the extent of the layout map frame
    layout_map_frame = layout.listElements('MAPFRAME_ELEMENT')[0]

    # Define output PNG filename
    output_png = os.path.join(r"C:\Users\geete\Downloads\exported", f"{layer.name}.png")

    # Export the layout to PNG
    layout.exportToPNG(output_png, resolution=60)

    # Reset visibility of all layers
    for lyr in map.listLayers():
        lyr.visible = True