Skip to main content
deleted 1 character in body
Source Link
user2856
  • 69.6k
  • 6
  • 119
  • 203

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 andan existing dataset.

For examapleexample:

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:

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:

added 5 characters in body
Source Link
user2856
  • 69.6k
  • 6
  • 119
  • 203

I don't thinkIn rasterio <= 1.0.7 this is not possible. The usual way isYou have to write and close, then read. If you want to write again, just overwrite the memfile object.

I don't think this is possible. The usual way is to write and close, then read. If you want to write again, just overwrite the memfile object.

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.

added 1496 characters in body
Source Link
user2856
  • 69.6k
  • 6
  • 119
  • 203

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]]]

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]]]
added 110 characters in body
Source Link
user2856
  • 69.6k
  • 6
  • 119
  • 203
Loading
Source Link
user2856
  • 69.6k
  • 6
  • 119
  • 203
Loading