-
The smallest case: First, I created an in-memory
According to the document, I think
What I did:
Versions: |
Beta Was this translation helpful? Give feedback.
Answered by
snowman2
Oct 5, 2023
Replies: 1 comment
-
The main issue in your code is that you are opening the raster outside the 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:
dataset.write(data)
# Open the dataset again for reading and return
with mem_file.open() as raster:
rds = rioxarray.open_rasterio(raster, driver="GTiff") |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
snowman2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 thewith
block, it works: