AnsweredAssumed Answered

arcpy.mapping.ListLayers does not find layer

Question asked by Willem_M on May 11, 2015
Latest reply on Jun 1, 2015 by Willem_M

Hello,

 

I'm doing some processing on raster images using the python module in ArcMap 10.3.0.4322. Therefore I first convert the rasters to numpy arrays, do the processing and convert them back to rasters. This all works fine, but when I try to get hold of the resulting raster layer for some further layout (application of symbology) using arcpy.mapping.ListLayers, the function returns an empty list, while the layer is clearly there.

 

This code reproduces the error:

import numpy as np
arcpy.CheckOutExtension("Spatial")
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,'')[0]
# create random raster and get its properties
TestRaster = arcpy.sa.CreateRandomRaster("", "1", "0 0 250 250")
lowerLeft = arcpy.Point(TestRaster.extent.XMin,TestRaster.extent.YMin)
cellSize = TestRaster.meanCellWidth
# create array from raster, do some processing (not relevant here)
array = arcpy.RasterToNumPyArray(TestRaster,nodata_to_value=np.nan)
# turn array in to a raster again
NewRaster = arcpy.NumPyArrayToRaster(array,lowerLeft,cellSize,value_to_nodata=TestRaster.noDataValue)
# get hold of the raster for the application of symbology
the_raster = arcpy.mapping.ListLayers(mxd,'NewRaster',df)[0]

 

Executing this code as a whole produces the error:

Runtime error

Traceback (most recent call last):

  File "<string>", line 13, in <module>

IndexError: list index out of range

 

Nevertheless, the raster should be there: when the last line is re-executed at the prompt after the error is returned, the_raster gives <map layer u'NewRaster'> as expected, so this seems like a bug of some sort. Is there any way to avoid having to run this code in seperate code blocks at the prompt?

 

cheers,

Willem

Outcomes