In rasterio
<= 1.0.7 this is not possible. You have to write and close, then read. If you want to write again, just overwrite the memfile
object.
For example:
from rasterio.io import MemoryFile
import rasterio
import numpy as np
def create_memory_file(data, west_bound, north_bound, cellsize, driver='GTIFF'):
#data is a numpy array
if data.ndim ==2: # Handle 2 or 3D input arrays
data = np.expand_dims(data, axis=0)
dtype = data.dtype
shape = data.shape
transform = rasterio.transform.from_origin(west_bound, north_bound, cellsize, cellsize)
with MemoryFile() as memfile:
with memfile.open(
driver=driver, width= shape[2], height = shape[1],
transform=transform, count=shape[0], dtype=dtype) as dataset:
dataset.write(data)
return memfile.open() # <==== Re-open the memfile
data = np.array([[1,2,3], [4,5,6], [7,8,9]]).astype(np.int32)
memfile = create_memory_file(data, 0, 2, 0.5)
print(memfile.read())
data = np.array([[4,5,6], [7,8,9], [10,11, 12]]).astype(np.int32)
memfile = create_memory_file(data, 0, 2, 0.5)
print(memfile.read())
Output:
[[[1 2 3]
[4 5 6]
[7 8 9]]]
[[[ 4 5 6]
[ 7 8 9]
[10 11 12]]]
As of rasterio
1.0.8 you can write and read as datasets in a MemoryFile
are opened in r+
/w+
mode. Note that the underlying GDAL driver needs to support creation of a new dataset from scratch not just creation of a copy of an existing dataset.
For example:
from rasterio.io import MemoryFile
import rasterio
import numpy as np
def create_memory_file(data, west_bound, north_bound, cellsize, driver='GTIFF'):
#data is a numpy array
if data.ndim ==2: # Handle 2 or 3D input arrays
data = np.expand_dims(data, axis=0)
dtype = data.dtype
shape = data.shape
transform = rasterio.transform.from_origin(west_bound, north_bound, cellsize, cellsize)
with MemoryFile() as memfile:
dataset = memfile.open(
driver=driver, width= shape[2], height = shape[1],
transform=transform, count=shape[0], dtype=dtype)
dataset.write(data)
return dataset
data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]).astype(np.int32)
memfile = create_memory_file(data, 0, 2, 0.5)
print(memfile.read())
data = np.array([[4, 5, 6], [7, 8, 9], [10, 11, 12]]).astype(np.int32)
memfile.write(data, 1)
print(memfile.read())
Output:
[[[1 2 3]
[4 5 6]
[7 8 9]]]
[[[ 4 5 6]
[ 7 8 9]
[10 11 12]]]
show(stack[0])
show(stack[1])
will work fine.
If you choose tuple, you have to choose the channel to output. Multi-band output is possible only when the raster dataset is loaded.
The same is true for the creation of raster files.
dst.write(stack, 1)
will change
dst.write(stack[0], 1)
dst.write(stack[1], 2)
UPDATE
The cause of the error is that the width and height of the image are incorrectly specified.
stack.shape
return (band, height, width).
so, it will change
height=stack.shape[1],
width=stack.shape[2]
You can also use this.
height=img.height,
width=img.width
Best Answer
You're trying to write three bands into the first (and only) band of a single band output raster and you need to reshape your rgb array from (rows, cols, bands) to (bands, rows, cols).
You probably want something like:
Note that you may need to rescale your rgb data to fit the 0-255 uint8 range.