ListLayers error

4835
13
02-09-2015 10:13 AM
PROBERT68
Frequent Contributor

I am having an syntax error from Pyscripter here and not sure why but I have a shapefile at the top of the TOC list which is the first and should be number zero  when writing a listlayers as a zero  but I still get the error .

The error is :

Traceback (most recent call last):

  File "C:\Student\python\GeoTIFF.py", line 16, in <module>

    gridlayer = arcpy.mapping.ListLayers(mxd,'JERGRIDINDEX', df,)[0]

IndexError: list index out of range

Take a look here the sytanx:: gridlayer = arcpy.mapping.ListLayers(mxd,'JERGRIDINDEX', df,)[0]

0 Kudos
13 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Robert,

Would you be able to post all of your code?  This will help troubleshoot this faster.

0 Kudos
PROBERT68
Frequent Contributor

Here you go:

mport arcpy.mapping, os

mxd = arcpy.mapping.MapDocument (r"E:\workspace\Test.mxd")

arcpy.env.workspace = r"E:/workspace/images/"

df = arcpy.mapping.ListDataFrames(mxd)[0]

df.scale = 40000

gridlayer = arcpy.mapping.ListLayers(mxd,'JERGRIDINDEX', df,)[0]

arcpy.SelectLayerByAttribute_management(gridlayer, "NEW_SELECTION")

for row in arcpy.SearchCursor(gridlayer):

  df.panToExtent(row.shape.extent)

  arcpy.RefreshActiveView()

  filename = "E:/workspace/images/" + \

    str(row.getValue("PageNumber")).zfill(4) + ".tif"

  arcpy.mapping.ExportToTIFF(mxd, filename, df, df_export_width= 65000, df_export_height=30000,geoTIFF_tags=True)

##  arcpy.mapping.ExportToTIFF(mxd, filename, df, df_export_width= 1280, \

##    df_export_height=440, geoTIFF_tags=True)

print arcpy.GetMessages()

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Everything looks correct, unless you are mis-spelling the layer name.  However, since you want the first layer, you will not have to specify a name.  Try the following:

gridlayer = arcpy.mapping.ListLayers(mxd,'', df,)[0]

One thing I did just notice is the arcpy.env.workspace.  Since you are specifying an 'r' before the path, you will want to change the forward slashes to backslashes.  Ex:

arcpy.env.workspace = r"E:\workspace\images"

0 Kudos
PROBERT68
Frequent Contributor

I did the same thing what you suggest. I can try and run it from Python window but Pyscripter should be able to recongize it.  Can I run this script from Pyscripter rather than in python window ?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Yes, you should be able to run this in Pyscripter.

0 Kudos
PROBERT68
Frequent Contributor

How can I sync this Pyscripter to Arcmap ?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

I'm not quite sure what you mean.  When the code is executed in Pyscripter, you will not see the map's extent updated.  However, you should still have the TIFF file created.

0 Kudos
PROBERT68
Frequent Contributor

I am thinking maybe it is not working.How can Pyscripter communicate with ArcMap if I write these codes ?

0 Kudos
GusMartinka
Occasional Contributor

Check out the Execute and debug your tool section for help attaching pyscripter to ArcMap for debugging.

0 Kudos