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.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()
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")


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.

  • 1
    What line is it giving that error for? Also, you don't need to add the first point back to make a closed polygon. – Paul Jul 17 '14 at 16:00
  • See here for any easier way to create polygons from extent. – Paul Jul 17 '14 at 16:07
  • it does not specify a line, all I get is - "Runtime error <type 'exceptions.IndexError'>: list index out of range" – Geord359 Jul 17 '14 at 16:11
  • Very frustrating because, as I say, the script does everything I want in one workspace but fails in another. – Geord359 Jul 17 '14 at 16:12

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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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