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
Solved! Go to Solution.
I think your code snippet pretty clearly shows there is a bug, but good luck getting Esri to fix it. I don't mean to come off as cynical, but I have opened so many bugs with Esri over the years that I know luck and happenstance both play a strong role in what eventually gets addressed. Soapbox aside, you should be able to workaround this bug by inserting the following line before line 14 of your code snippet:
arcpy.MakeRasterLayer_management(NewRaster, 'NewRaster')
I'm not sure if this is causing your problems, but i have had similar errors when my next line runs before the previous line has completed the process.
I think your code snippet pretty clearly shows there is a bug, but good luck getting Esri to fix it. I don't mean to come off as cynical, but I have opened so many bugs with Esri over the years that I know luck and happenstance both play a strong role in what eventually gets addressed. Soapbox aside, you should be able to workaround this bug by inserting the following line before line 14 of your code snippet:
arcpy.MakeRasterLayer_management(NewRaster, 'NewRaster')
Thanks, your answer works and put me on the right track.
Saving and adding the new layer also seems to work (I ended up using this one as I'm creating archives of the rasters later on):
NewRaster.save(os.path.join(<PATH>,'NewRaster'))
arcpy.mapping.AddLayer(df,arcpy.mapping.Layer(os.path.join(<PATH>,'NewRaster')))
the_raster = arcpy.mapping.ListLayers(mxd,'NewRaster',df)[0]