Hi Umberto,

There isn't information in your question about the size of the raster image, but as you are reading it in chunks, it probably is large. Hence my guess is that your in-memory raster is not big enough to hold the data, without giving an error message. And the subsequent gdal.Warp simply cannot handle this.

If the file-raster weren't large, you could simply load it into memory in one command.

Armin

On 21/02/2020 16:52, umbertofilippo wrote:
Sorry if this is a copy of a question I already asked on gis.stackexchange (
here
<https://gis.stackexchange.com/questions/351577/cannot-use-in-memory-raster-object-with-gdal-warp-gdal-python-api>
), but I thought I might get more chance to receive an answer here.

These are the operations I am doing in my code:

1 - reading an existing raster (tif)
2 - creating a in memory raster object
3 - reading the existing raster object in chunk as array
4 - making some calculations
5 - writing the array to my in memory object

What I want to do now is, with the in memory raster object ready to be used, feed it to gdal warp, which should also output an intermediate raster object
that I'll be use afterwards.

However, when I try to GetRasterBand() out of this latter raster object, I
receive an error because it is actually equal to None:

AttributeError: 'NoneType' object has no attribute 'GetRasterBand'

I am pretty sure it is about saving/updating/flushing the status of the
raster object after the calculations but I could not find out if this is the
actual problem.

How can I use in memory (intermediate) files in my script?
Here is the code code I am using (the error is at the very last line):

from osgeo import gdal, gdalconst
import numpy as np

in_raster = gdal.Open(r'/my/raster.tif', gdalconst.GA_ReadOnly)
in_b = in_r.GetRasterBand(1)

b1 = in_raster.GetRasterBand(1)
xsize = b1.XSize
ysize = b1.YSize
step = 10 # THIS IS THE CHUNK SIZE!
ystep = ysize / step
ystep = int(ystep)
yresidual = ysize - (ystep * step)

driver = gdal.GetDriverByName("MEM")
temp_calc = driver.Create('',
xsize,
ysize,
1,
gdal.GDT_Float32)

temp_b = temp_calc.GetRasterBand(1)

for i in range(step):
if i != step-1:
arr_part = in_b.ReadAsArray(
0, ystep * i, xsize, ystep).astype('float32')

data = np.nan_to_num(arr_part)

temp_b.WriteArray(data, 0, ystep * i)
else:
arr_part = in_b.ReadAsArray(
0, ystep * i, xsize, ystep + yresidual).astype('float32')

# replace nan with zeros
#
https://docs.scipy.org/doc/numpy/reference/generated/numpy.nan_to_num.html
data = np.nan_to_num(arr_part)

# write array chunk to (in memory) file
# https://gdal.org/python/osgeo.gdal.Band-class.html#WriteArray
temp_b.WriteArray(data, 0, ystep * i)

# get raster resolution
in_gt = in_raster.GetGeoTransform()
in_x = in_gt[1]
in_y = -in_gt[5]

kwargs = {'xRes': in_x,
'yRes': in_y,
'targetAlignedPixels': True,
'resampleAlg': 'average',
'format': 'MEM'
}

ds = gdal.Warp('',
temp_calc,
**kwargs)
b1 = ds.GetRasterBand(1)
# ds is None and the above line throws the exception



--
Sent from: http://osgeo-org.1560.x6.nabble.com/GDAL-Dev-f3742093.html
_______________________________________________
gdal-dev mailing list
[email protected]
https://lists.osgeo.org/mailman/listinfo/gdal-dev


--
__________________________________________________________________
Dr Armin Schmidt
 Dipl.-Phys., Dr. rer. nat.
GeodataWIZ: Geo Data + Visualisation
 www.GeodataWIZ.com
 www.GeodataWIZ.com/armin-schmidt
Honorary Secretary ISAP
 International Society for Archaeological Prospection
 www.archprospection.org
Honorary Visiting Research Fellow, University of Bradford
 School of Archaeological and Forensic Sciences
Honorary Fellow, Durham University
 Department of Archaeology
http://orcid.org/0000-0002-4241-5381

Email: [email protected]

Available in print:
 Guidelines for the use of Geophysics in Archaeology (2015). Namur: EAC.
 Earth Resistance for Archaeologists (2013). Lanham: AltaMira Press
 Geophysical Data in Archaeology (2013). Oxford: Oxbow Books.
http://www.amazon.co.uk/-/e/B0034QC4OW
__________________________________________________________________

Disclaimer
==========
This email and any attachments to it may be confidential and are
intended solely for the use of the individual to whom it is
addressed. If you are not the intended recipient of this email,
you must neither take any action based upon its contents, nor
copy or show it to anyone.

Please contact the sender if you believe you have received this
email in error.
__________________________________________________________________


_______________________________________________
gdal-dev mailing list
[email protected]
https://lists.osgeo.org/mailman/listinfo/gdal-dev

Reply via email to