Saturday 17 September 2016

Exporting specified layers in ArcMap using ArcPy?


I am trying to export specific layers from an ArcMap .mxd. From the TOC below I would like to export "irrig_2, citylyr, boundlyr" as a GIF. Once this is done the script would then export "irrig_1, citylyr, boundlyr" as another GIF and so on. If possible, the script would also export a legend that corresponds to the layers being exported. I'm not sure if it's a good place to start, but I'm trying to work off the code below.


enter image description here


enter image description here


import arcpy


mxd = arcpy.mapping.MapDocument(r"E:\IrrigatedLands\FC_test\irrig2.mxd")
df = arcpy.mapping.ListDataFrames(mxd, '')[0]

GIFPath = r"E:\IrrigatedLands\FC_test"

for lyr in arcpy.mapping.ListLayers(mxd, '', df):
if lyr.name == "citylyr":
lyr.visible = True
if lyr.name == "boundlyr":
lyr.visible = True

if lyr.name == lyr:
lyr.visible = True
arcpy.mapping.ExportToGIF(mxd,GIFPath+"\\" + lyr.name + ".gif")
lyr.visible = False
arcpy.RefreshActiveView()
del mxd

Answer



You need to loop through a list of your output layers so you can turn them on and off for each export.


import arcpy


projFolder = r"E:\IrrigatedLands\FC_test"

mxd = arcpy.mapping.MapDocument(r"{}\irrig2.mxd".format(projFolder))
df = arcpy.mapping.ListDataFrames(mxd, '')[0]

GIFPath = r"E:\IrrigatedLands\FC_test"

layers = ['irrig_0','irrig_1','irrig_2'] # List of your output layers

lyrlist = arcpy.mapping.ListLayers(mxd, '', df)


for layer in layers:
layerOn = "" # When required layer is turned on this will be the layer name to output
for lyr in lyrlist:
lyr.visible = False
#if lyr.name in layers and lyr.name <> layer:
# lyr.visible = False
if lyr.name == "citylyr":
lyr.visible = True
if lyr.name == "boundlyr":

lyr.visible = True
if lyr.name == layer:
lyr.visible = True
layerOn = lyr.name
if layerOn:
arcpy.mapping.ExportToGIF(mxd,r"{}\{}.gif".format(GIFPath,layerOn))

del mxd

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...