Unable to save a 3 dimensional array to stacked raster .img file.
I have this array:
arr.shape (10, 148, 160)
Now when I try to save this into raster .img format:
out_ras = arcpy.NumPyArrayToRaster(arr)
out_ras.save(r"C:\Users\Gurminder\Documents\study\IWMI\HomeTools\Dump\Converted_arr\stack.img")
I get the following error:
Runtime error Traceback (most recent call last): File "<string>", line 1, in <module> RuntimeError:
ERROR 010240: Could not save raster dataset to C:\Users\Gurminder\Documents\study\IWMI\HomeTools\Dump\
Converted_arr\stack.img with output format IMAGINE Image.
Although a file named stack.img is created in the given directory but all it contains is garbage values.
I have been facing this issue since I have upgraded from ArcMap 10.3 to 10.4, before upgrade everything was working just fine.
As Dan has said, try a much shorter path to save.
The small path that tried was up to three directories, still I get the error. Is it short enough. I have printed the output to Dan's reply you have check the code there.
Xander Bakker can you try the code? I can't flag or redirect elsewhere since you are owner of the group
Neil Ayres or Peter Wilson can one of you guys on older Arc* installs, give it a shot, since I know you have worked with arrays
I have moved the thread to the Python space and tested the code in PyScripter with Python 2.7.10 and ArcGIS 10.4.1. Writing to TIF, IMG and FGDB raster all resulted in the same error message. In case of the IMG format the raster was created with all bands filled with NoData, in case of the TIFF all bands had value 0 and in case of the FGDB raster the output was not created.
Reducing the number of bands to 3 did not yield a different result (same error), but when using a single band it will be created without problem. You could write each band separately and use arcpy.CompositeBands_management to create the multiband output, but I'm pretty sure that's not what you're after.
When you try to create a composite bands using the bands of the multi band temp output, it will create the raster without error message, but it will contain all NoData values:
import numpy as np import arcpy import os np.set_printoptions(edgeitems=2,linewidth=80,precision=2, suppress=True,threshold=20, formatter={'float': '{: 0.3f}'.format}) a = np.arange(10*5*6).reshape(10,5,6) out_ras = arcpy.NumPyArrayToRaster(a) desc = arcpy.Describe(out_ras) lst_ras = [child.catalogPath for child in desc.children] in_ras = ';'.join(lst_ras) arcpy.CompositeBands_management(in_rasters=in_ras, out_raster=r"D:\Temp\ras10bands.tif")
Bizarre... so the key seems to be having Unicode (with 2.7) or just use python 3.4.x since it sure has worked for me, regardless of the output format (*.img or *.tiff. I get a 10 band raster. I suppose another reason to upgrade python.
Some time later... I new I saw it somewhere!
This solves the dilemma of where to write temporary tif files... The describe object was the key
Nice... Thanks Xander Bakker
Except when you bring the stack back in with RasterToNumPyArray, only the first three bands have data, the rest are zero...
Very strange
Just moved to 10.4.1 last week.
Seems that Xander's work around will do the job. But perhaps this is an undocumented feature in 10.4.1
Neil... Xander is using 10.4.1 but python 2.7.x
It is either the python version (3.4.x ergo Unicode enabled) or the user
I can't narrow it down further... I normally use *.npy files to store multiband rasters/arrays, they are way smaller and I rarely need to map something.
These three are the same array in npy, img and tif formats
WELLLL
Python version....
>>> import numpy as np
>>> import arcpy
>>> a = np.arange(10*5*6).reshape(10,5,6)
>>> import sys; print(sys.version)
2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)]
>>>
>>> out_ras = arcpy.NumPyArrayToRaster(a)
RuntimeError: ERROR 010240: Could not save raster dataset to C:\Users\Dan\Documents\ArcGIS\Default.gdb\numpy with output format FGDBR.
>>>
Install ArcGIS PRO and you will get Python 3.4.3 (or you can install it yourself and fiddle with associations)
Anaconda is coming soon.
If you don't need to 'see your stuff' use the *.npy format
EDIT
python 2.7 still being distributed with arc 10.5, so the PRO install will do until 3.x is distributed with arcmap