0

I have a process that aims to loop through a list of rasters (testing with only 3) covert each raster to an array, add the array to an existing 'template' array, then save the final array back to a raster. I'm not getting errors, but all of the test output is NoData Values. Thanks for your help!


#import modules
import arcpy, numpy, os, time

#set workspace
arcpy.env.workspace = r"C:/GIS/project/test_rasters"

#set snap raster environment
template = "C:/GIS/project/rast01_0"
arcpy.env.snapRaster = template

#define in raster
template_raster = arcpy.Raster(template)

#set output location
outpath = r"C:/GIS/project/out_rasters/rast01_x"

#create an array to serve as the frame for additional arrays.
template_array = arcpy.RasterToNumPyArray(template_raster)
template_array = numpy.float64(template_array)
main_array = template_array * 0

#check array type
print main_array.dtype.name
#start counter
count = 0

#get a list of rasters in workspace
rasters = arcpy.ListRasters()
#for each raster in the workspace
for raster in rasters:
    print "processing: " + raster + " " + time.asctime()
    #make each raster an array, add it to main raster array
    array = arcpy.RasterToNumPyArray(raster)
    main_array = main_array + array
    #count number of raster/arrays added to the main array
    count = count + 1
    print count

#print final count of summed arrays
print count
#save array of sums to raster
part_sum_raster = arcpy.NumPyArrayToRaster(main_array)
part_sum_raster.save(outpath)

print"script completed:  " + time.asctime()
1

Some points need to be clarified:

  • Are the rasters single-band or multi-bands ?
  • what is the shape of your main_array ?
  • What is the final result of your main_array ?

Example with numpy

Create an array filled with zeros:

import numpy as np
main_array= np.zeros((4,5),dtype=np.float64) 
# In your case:
# main_array = np.zeros(template_array.shape, dtype=np.float64)
print main_array
array([[ 0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.],
   [ 0.,  0.,  0.,  0.,  0.]])
main_array.shape
(4, 5)
main_array.size
20

Sums of arrays:

newarray = np.array([[ 1.,  2.,  3.,  4.,  5.],
   [ 1.,  2.,  3.,  4.,  5.],
   [1.,  2.,  3.,  4.,  5.],
   [1.,  2.,  3.,  4.,  5.]])

main_array = main_array + newarray
print main_array
array([[ 1.,  2.,  3.,  4.,  5.],
   [ 1.,  2.,  3.,  4.,  5.],
   [ 1.,  2.,  3.,  4.,  5.],
   [ 1.,  2.,  3.,  4.,  5.]])
main_array = main_array + newarray
print main_array
array([[  2.,   4.,   6.,   8.,  10.],
   [  2.,   4.,   6.,   8.,  10.],
   [  2.,   4.,   6.,   8.,  10.],
   [  2.,   4.,   6.,   8.,  10.]])
 etc.

I don't use ArcPy but with GDAL/OGR the result is not NULL:

import numpy as np
from osgeo import gdal
ds = gdal.Open('myraster')
array = np.array(ds.GetRasterBand(1).ReadAsArray())
array.shape
(62, 90)
main_array = np.zeros(array.shape, dtype=np.float64)
main_array.shape
(62, 90)
main_array + array == array
array([[ True,  True,  True, ...,  True,  True,  True],
   [ True,  True,  True, ...,  True,  True,  True],
   [ True,  True,  True, ...,  True,  True,  True],
   ..., 
   [ True,  True,  True, ...,  True,  True,  True],
   [ True,  True,  True, ...,  True,  True,  True],
   [ True,  True,  True, ...,  True,  True,  True]], dtype=bool)
4
  • yes, that looks like what I'd like to happen with my loop - and after printing the arrays within the loop all looks fine until the arcpy.NumPyArrayToRaster() output. single-band, shape = (1059L, 598L), raster of the sum of all arrays.
    – mwil
    Sep 24 '13 at 17:45
  • That was supposed to be: To answer your questions: single-band, shape = (1059L, 598L), raster of the sum of all arrays. Thanks for looking into this!
    – mwil
    Sep 24 '13 at 18:01
  • 1
    If the resulting array is not empty, so the problem is in arcpy.NumPyArrayToRaster(main_array)
    – gene
    Sep 24 '13 at 18:20
  • okay. i had to address all of the (optional) arguments in the arcpy.NumPyArrayToRaster() and then it worked. thanks @gene for the indirect help with printing the arrays to troubleshoot - i really appreciate it!
    – mwil
    Sep 24 '13 at 20:17

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.