Skip to content

How to open a in-memory tmp rasterio object? #695

Answered by snowman2
SongshGeo asked this question in Q&A
Discussion options

You must be logged in to vote

The main issue in your code is that you are opening the raster outside the with block. Once you do that, the memory file ceases to exist. If you open it inside the with block, it works:

import affine
import numpy
import pyproj.crs
import rasterio
import rioxarray

data = numpy.random.random((1, 4, 4))
crs = pyproj.crs.CRS.from_user_input("epsg:4326")
transform = affine.Affine(*numpy.random.random(6))

with rasterio.MemoryFile() as mem_file:
    with mem_file.open(
        driver="GTiff",
        height=data.shape[1],
        width=data.shape[2],
        count=data.shape[0],  # number of bands
        dtype=str(data.dtype),
        crs=crs,
        transform=transform,
    ) as dataset:
  …

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by snowman2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants