Layer exsitis help

584
3
Jump to solution
10-28-2013 10:57 AM
TonyAlmeida
Occasional Contributor II
I have a python script that i would like to turn on some layers "if they exsist", if they don't exsist i would like for the script to continue to run. The current script won't run it give me an error

  File "<string>", line 8, in <module>
IndexError: list index out of range

This script will be used on about 5 mxd for a single project.
Note: some mxd don't have the layer "URNBAN_12" OR "RURAL_12"
The mxd with these layers added to the TOB run just fine with this script, but some mxd don't have these layers on the mxd and therefore it stops at line 8..

So how can i make the script continue to run past line 8?

import arcpy import os  arcpy.env.overwriteOutput = True  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "URBAN_12")[0]  UR = "URBAN_12" if arcpy.Exists(UR):     lyr.visible = True  lyr = arcpy.mapping.ListLayers(mxd, "Rural_12")[0]  RU = "Rural_12" if arcpy.Exists(RU):     lyr.visible = True      mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0]  arcpy.env.workspace = os.path.dirname(mxd.filePath) wp = os.path.dirname(mxd.filePath)  del mxd  mxd = arcpy.mapping.MapDocument("CURRENT") lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0] lyrpath = lyr.workspacePath  arcpy.env.qualifiedFieldNames = False  SP = "SUBJECT_PROPERTY"   lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", SP, True )  df.extent = lyr.getSelectedExtent() df.scale = 2000 arcpy.RefreshActiveView() arcpy.RefreshTOC()  del df  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "1_2")[0]  S12 = "1_2"  lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", S12, True )  if arcpy.Exists(S12):     df.extent = lyr.getSelectedExtent()  df.extent = lyr.getSelectedExtent()     arcpy.RefreshActiveView() arcpy.RefreshTOC() 


I appreciate any help.
Thanks.
Tags (2)
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Tony,

You can add a try/except to your code.  Example:

import arcpy import os  arcpy.env.overwriteOutput = True  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] try:     lyr = arcpy.mapping.ListLayers(mxd, "URBAN_12")[0] except IndexError:     pass

View solution in original post

0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Tony,

You can add a try/except to your code.  Example:

import arcpy import os  arcpy.env.overwriteOutput = True  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] try:     lyr = arcpy.mapping.ListLayers(mxd, "URBAN_12")[0] except IndexError:     pass
0 Kudos
TonyAlmeida
Occasional Contributor II
Excellent thank you.
0 Kudos
TonyAlmeida
Occasional Contributor II
The current script works great for mxd that have layers in the TOC; Subject_Property, URBAN_12 and RURAL_12 and 1_2.
Mxd's that do not have 1_2 in the TOC give the following error

Runtime error
Traceback (most recent call last):
  File "<string>", line 29, in <module>
NameError: name 'lyr' is not defined

If i don't add line 19 "del mxd, df, lyr". The Subject_property will be updated by 1_2 feature class.

 import arcpy
import os

arcpy.env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "SUBJECT_PROPERTY")[0]
lyrpath = lyr.workspacePath

arcpy.env.qualifiedFieldNames = False
arcpy.env.workspace = os.path.dirname(mxd.filePath)
wp = os.path.dirname(mxd.filePath)

SP = "SUBJECT_PROPERTY" 

lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", SP, True )

del mxd, df, lyr

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
try:
    lyr = arcpy.mapping.ListLayers(mxd, "1_2")[0]
except IndexError:
    pass
S12 = "1_2" 
lyr.replaceDataSource(wp, "SHAPEFILE_WORKSPACE", S12, True )



if arcpy.Exists(S12):
    df.extent = lyr.getSelectedExtent()


del mxd, df

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
try:
    lyr = arcpy.mapping.ListLayers(mxd, "URBAN_12")[0]
except IndexError:
    pass

UR = "URBAN_12"
if arcpy.Exists(UR):
    lyr.visible = True

try:
    lyr = arcpy.mapping.ListLayers(mxd, "Rural_12")[0]
except IndexError:
    pass

RU = "Rural_12"
if arcpy.Exists(RU):
    lyr.visible = True

df.extent = lyr.getSelectedExtent()
df.scale = 2000
arcpy.RefreshActiveView()
arcpy.RefreshTOC()   
 
0 Kudos