0

I am processing geotiff files with GDAL and rasterio. Every process of my processing chain generates a file in output. What I want is saving every file in memory instead of creating files in the disk. I know how to do that with GDAL using the "MEM" argument. However, how can I use the produced file (here 'dataset') in rasterio as rasterio.open needs a filename. Is there a way to convert 'dataset' into a string or something like that?

My code:

import os
from osgeo import gdal
import rasterio
import numpy as np

# path
path = 'file.tif'

# open with GDAL
file = gdal.Open(path)
band = raster.GetRasterBand(1)
array = band.ReadAsArray()
rotation = np.rot90(array)

# create memory file
x_pixels = array.shape[1]
y_pixels = array.shape[0]
driver = gdal.GetDriverByName("MEM")
dataset = driver.Create("", x_pixels, y_pixels, 1)
dataset.GetRasterBand(1).WriteArray(array)

# open the result with rasterio
with rasterio.open(dataset) as src:
    src_transform = src.transform
    src_crs = {'init': 'EPSG:3945'}
    dst_crs = {'init': 'EPSG:3945'}
    dst_transform, dst_width, dst_height = 
    calculate_default_transform(src_crs, dst_crs, x_pixels, y_pixels,                                                       
    gcps=gcps)

    kwargs = src.meta.copy()
    kwargs.update({
        'crs': dst_crs,
        'transform': dst_transform,
        'width': dst_width,
        'height': dst_height
    })

My error:

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\envs\python36\lib\site-packages\IPython\core\interactiveshell.py", line 3326, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-2770ecde2207>", line 65, in <module>
    with rasterio.open(dataset) as src:
  File "C:\ProgramData\Anaconda3\envs\python36\lib\site-packages\rasterio\env.py", line 423, in wrapper
    return f(*args, **kwds)
  File "C:\ProgramData\Anaconda3\envs\python36\lib\site-packages\rasterio\__init__.py", line 153, in open
    raise TypeError("invalid path or file: {0!r}".format(fp))
TypeError: invalid path or file: <osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow *' at 0x00000131C7385A80> >

1 Answer 1

1

One option would be to open the geotiff file trough Rasterio and create the Rasterio's MemoryFile using something like:

import rasterio
from rasterio.io import MemoryFile
import numpy as np

# path
path = 'file.tif'

with rasterio.open(path) as src, MemoryFile() as memfile:
    array = src.read(1)
    rotation = np.rot90(array)
    x_pixels = array.shape[1]
    y_pixels = array.shape[0]
    profile = dict(src.profile)
    profile.update({'width': x_pixels, 'height': y_pixels})
    with memfile.open(**profile) as dataset:
        dataset.write(np.array([rotation]))
        # Remaining code
1
  • Thanks Joonala. However, every step of my processing chain (not shown here) finish by a georeferenced raster, which means I get everytime a GDAL dataset in ouput of the georeferencing (in memory). So I really need to process a GDAL dataset first instead of using rasterio for all the process.
    – rdmato33
    Dec 9, 2019 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.