3

I am trying to create a loop so that each layer is produced in its own map.

This is my code:

import arcpy
path = "C:\Users\Et\Documents\ArcGIS\Default.gdb"
mxd=arcpy.mapping.MapDocument(path +"\WinterGritting.mxd")
df=arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyrList =arcpy.mapping.ListLayers(mxd,"",df)[0].name
elements=arcpy.mapping.ListLayoutElements (mxd)
for eachlyr in lyrList:
    arcpy.SelectLayerByAttribute_management(eachlyr,'NEW_SELECTION')
    df.zoomToSelectedFeatures() 
    arcpy.SelectLayerByAttribute_management(eachlyr,'CLEAR_SELECTION')
    mxd.title=eachlyr.name
    arcpy.RefreshActiveView()
    arcpy.mapping.ExportToJPEG(mxd, path + "\Routes" + eachlyr.name + "jpg")

del mxd

The Error I get is:

Traceback (most recent call last): File "C:/Users/Et/Documents/Python", line 9, in arcpy.SelectLayerByAttribute_management(eachlyr,'NEW_SELECTION') File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 6494, in SelectLayerByAttribute raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000732: Layer Name or Table View: Dataset L does not exist or is not supported Failed to execute (SelectLayerByAttribute).

1 Answer 1

4

Here's a different way to do this by setting the dataframe equal to the current layer's extent. It a lot more simple. Also, you'd better check your path because it shows the MXD being inside the geodatabase.

import arcpy
import os

path = "C:\\Users\\Et\\Documents\\ArcGIS" #need to use two backslashes
mxd = arcpy.mapping.MapDocument(os.path.join(path, "WinterGritting.mxd")) #use os.path.join
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
layer_list = arcpy.mapping.ListLayers(mxd, "", df) #don't use indexing here

for layer in layer_list:
    layer_name = str(layer.name)

    for layer_check in layer_list: #turns on/off other layers
        if layer_check != layer:
           layer_check.visible = False
           layer.visible = True

    mxd.title = layer_name    
    layer_extent = layer.getExtent() #get the extent of the current layer
    df.extent = layer_extent #set the dataframe to the layer's extent
    arcpy.RefreshActiveView()
    arcpy.mapping.ExportToJPEG(mxd, os.path.join(path, "Routes", layer_name + ".jpg"))

del mxd

Updated for comment.

Add the names of whatever layers you don't want to turn off into the landbase list.

import arcpy
import os

path = "C:\\Users\\Et\\Documents\\ArcGIS" #need to use two backslashes
mxd = arcpy.mapping.MapDocument(os.path.join(path, "WinterGritting.mxd")) #use os.path.join
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
layer_list = arcpy.mapping.ListLayers(mxd, "", df) #don't use indexing here

landbase = ["Roads", "Lakes"] #add layer names you want always visible here

for layer in layer_list:
    layer_name = str(layer.name)

    if layer_name not in landbase:

        for layer_check in layer_list: #turns on/off other layers
            if layer_check != layer and layer_check.name not in landbase:
                layer_check.visible = False
            else:
                layer_check.visible = True

        mxd.title = layer_name
        layer_extent = layer.getExtent() #get the extent of the current layer
        df.extent = layer_extent #set the dataframe to the layer's extent
        arcpy.RefreshActiveView()
        arcpy.mapping.ExportToJPEG(mxd, os.path.join(path, "Routes", layer_name + ".jpg"))

del mxd
2
  • 1
    Hi Thank you very much that is working perfectly now. I just have another query. How do i set a layer (e.g roads) to feature behind all my other layers instead of being produced as a single JPEG. Oct 29, 2014 at 13:34
  • @WizTownley I updated my answer.
    – ianbroad
    Oct 29, 2014 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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