As of rasterio
1.0.8 you can write and read as datasets are opened in r+
mode. Note that the underlying GDAL driver needs to support creation of a new dataset from scratch not just creation of a copy of and existing dataset.
For examaple:
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]]]