[GIS] List index out of range error

arcgis-desktoparcpypython

I've been writing a script that clips large datasets based on a selected feature. It works fine when I run it in a test workspace where only related layers are active and running. However, when I run the script in my organisation's main workspace where alot of unrelated layers are active I get the error "Runtime error : list index out of range". This has baffled me as I have the same layers active in this workspace as I have in the test workspace yet I am getting this error, could it be something to do with the dataframe? or something else? Anyway, here is the script (apologies about the length):

import arcpy
from arcpy import env
from arcpy import mapping

env.workspace = "C:\MyArcGIS\Clip_tool\uFMfSW\uFMfSW_Banded_Vector_SE00.gdb"

arcpy.env.overwriteOutput = True

edrn_node = "C:\\MyArcGIS\\EDRN_Shp\\EDRN_NODE.shp"
ID = "EDRN_043066"
query = '"LCC_DRN_ID"' + " = '" + str(ID) + "'"
polygon_extent102 = "C:\\MyArcGIS\\Clip_tool\\poly_extent102"
depth_1in100 = "uFMfSW_SE00_DEPTH_1in100_BV"
depth1in100 = "C:\\MyArcGIS\\Clip_tool\\uFMfSW\\uFMfSW_Banded_Vector_SE00.gdb\\dep1in100"
depth_1in30 = "uFMfSW_SE00_DEPTH_1in30_BV"
depth1in30 = "C:\\MyArcGIS\\Clip_tool\\uFMfSW\\uFMfSW_Banded_Vector_SE00.gdb\\dep1in30"
depth_1in1000 = "uFMfSW_SE00_DEPTH_1in1000_BV"
depth1in1000 = "C:\\MyArcGIS\\Clip_tool\\uFMfSW\\uFMfSW_Banded_Vector_SE00.gdb\\dep1in1000"
extent_1in30 = "uFMfSW_SE00_EXTENT_1in30_BV"
extent1in30 = "C:\\MyArcGIS\\Clip_tool\\uFMfSW\\uFMfSW_Banded_Vector_SE00.gdb\\ext1in30"
extent_1in100 = "uFMfSW_SE00_EXTENT_1in100_BV"
extent1in100 = "C:\\MyArcGIS\\Clip_tool\\uFMfSW\\uFMfSW_Banded_Vector_SE00.gdb\\ext1in100"
extent_1in1000 = "uFMfSW_SE00_EXTENT_1in100_BV"
extent1in1000 = "C:\\MyArcGIS\\Clip_tool\\uFMfSW\\uFMfSW_Banded_Vector_SE00.gdb\\ext1in100"

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

arcpy.SelectLayerByAttribute_management ("edrn_node", "NEW_SELECTION", query)

df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
df.zoomToSelectedFeatures()
df.scale = 2000

dataframe = mapping.ListDataFrames(mxd, "*")[0]
frameExtent = dataframe.extent
XMAX = frameExtent.XMax
XMIN = frameExtent.XMin
YMAX = frameExtent.YMax
YMIN = frameExtent.YMin
pnt1 = arcpy.Point(XMIN, YMIN)
pnt2 = arcpy.Point(XMIN, YMAX)
pnt3 = arcpy.Point(XMAX, YMAX)
pnt4 = arcpy.Point(XMAX, YMIN)
array = arcpy.Array()
array.add(pnt1)
array.add(pnt2)
array.add(pnt3)
array.add(pnt4)
array.add(pnt1)
polygon = arcpy.Polygon(array)

arcpy.CopyFeatures_management(polygon, "polygon_Extent102")

arcpy.Clip_analysis(depth_1in30, "polygon_Extent102", "Depth1in30")
arcpy.Clip_analysis(depth_1in100, "polygon_Extent102", "Depth1in100")
arcpy.Clip_analysis(depth_1in1000, "polygon_Extent102", "Depth1in1000")
arcpy.Clip_analysis(extent_1in30, "polygon_Extent102", "extent1in30")
arcpy.Clip_analysis(extent_1in100, "polygon_Extent102", "extent1in100")
arcpy.Clip_analysis(extent_1in1000, "polygon_Extent102", "extent1in1000")

arcpy.Delete_management("polygon_Extent102") 

I've also noticed when I convert the above to a script tool that I have to reconnect the geodatabase files before I get the expected results.

Best Answer

Apologies guys,

The answer is fairly straight forward. I had not defined the name of the data frame properly in line 30. Where I had stated "layers" I should have put "Land Drainage GIS".