Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a mxd file with multiple layers I have to export to PNG/JPG. I would like to create a script which turns on layers one at the time before they are exported. So far I have come up with the following:

import arcpy, string

mxd = arcpy.mapping.MapDocument("current")

#Read input parameters from script tool
LyrList = string.split.(arcpy.GetParameterAsText(0), ";")

PNGPath = arcpy.GetParameterAsText(1)

#Turn of all lyrs in list
for lyr in arcpy.mapping.ListLayers(mxd, LyrList):
    lyr.visible = False


#Turn on lyr and export as png one by one
for lyr in arcpy.mapping.ListLayers(mxd, LyrList):
    lyr.visible = True
    arcpy.mapping.ExportToPNG(mxd, PNGPath+"\\" + lyr.name + ".png")
    lyr.visible = False

del LyrList
del mxd

For the LyrList I use a dialoge box with the parameter data-type as Layer. Does anyone have an idea how to get this script working? When I use the ListLayers wildcard the script does work, but since I have a lot of layerfiles which all hace a kind of unique name that does not do the job for me.

thnx a lot!

share|improve this question
    
Did you get an error? What about the script isn't working? –  Roy Aug 14 '12 at 14:07
    
I do get an error if I execute the script like it is written here above. The error I get is: "<type 'exceptions.SyntaxError'>: invalid syntax (ExportLYRsToPNG.py, line 6)" When I change line 6 into: "LyrList = arcpy.GetParameterAsText(0) " I do not get an error, but there are no png's exported. –  Hamren Van de Werfhorst Aug 15 '12 at 11:54
    
Have you tried debugging by printing LyrList to see what the specific problem is? Is this for your own purposes or for someone else? I ask because you could do this without creating a script tool; see if it works by creating your own list as opposed to GetParameterAsText. –  Roy Aug 15 '12 at 12:48
    
I have printed the LyrList and all the lyr-files do get in the LyrList so that's not the problem. But they cannot be used in the way I am trying? –  Hamren Van de Werfhorst Aug 16 '12 at 12:26
add comment

1 Answer

This should work. Rather than identifying your layers as a wildcard, you can test that the layer name is equal to the current layer in your loop, if TRUE then you change visibility and export as a .png.

mxd = arcpy.mapping.MapDocument("CURRENT")  
df = arcpy.mapping.ListDataFrames(mxd, '')[0] # assuming there is only 1 df you're interested in

#Read input parameters from script tool
allLayers = arcpy.GetParameterAsText(0) # Must be a multivalue in script tool params
lyrList = allLayers.split(";")

PNGPath = arcpy.GetParameterAsText(1)

#Turn of all lyrs in list
for lyr in arcpy.mapping.ListLayers(mxd, '', df):
    for layer in lyrList:
        if lyr.name == layer:
            lyr.visible = False
arcpy.RefreshActiveView()

# Loop through each layer, turn it on and export map as PNG
for lyr in arcpy.mapping.ListLayers(mxd, '', df):
    for layer in lyrList:
        if lyr.name == layer:
            lyr.visible = True
            arcpy.RefreshActiveView() # May want to test without this -- ArcMap might export correctly without need for refresh active view
            arcpy.mapping.ExportToPNG(mxd, PNGPath+"\\" + lyr.name + ".png")
            lyr.visible = False
share|improve this answer
    
I have tried to make your solution work, it is indeed 1 df I'm interested in. Unfortunately I still get an error in line 13 of the script. The error which is returning in the dialog is "<type 'exceptions.SyntaxError'>: invalid syntax (ExportLYRsToPNG.py, line 13)" –  Hamren Van de Werfhorst Aug 20 '12 at 11:22
    
For line 13 I have put an extra '='between lyr.name and LyrList and then I don't get the error on line 13. But an entire new one: 'module' object has no attribute 'ListDataFrame'. –  Hamren Van de Werfhorst Aug 20 '12 at 11:50
    
it's a syntax error, should be arcpy.ListDataFrames –  Roy Aug 20 '12 at 13:30
    
I see... That was very sloppy from me I should have seen that. For now I still get an indexerror: "<type 'exceptions.IndexError'>: list index out of range" I have double-checked and have searched on the internet for it, but I cannot find a sollution. –  Hamren Van de Werfhorst Aug 20 '12 at 15:24
    
See updated script. My mistake I created a script that worked for me, but not all use cases last time. As I say in the script comment, the arcpy.RefreshActiveView may be unneccessary (try without it to see if the map exports correctly). If it's not needed things will run faster. –  Roy Aug 20 '12 at 15:46
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.