Skip to content

Commit

Permalink
Use rasterio MemoryFile to read GeoTIFF in-memory
Browse files Browse the repository at this point in the history
  • Loading branch information
BSchilperoort committed Aug 16, 2023
1 parent 498f178 commit d191415
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/zampy/datasets/prism_dem.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Union
import numpy as np
import xarray as xr
from rasterio.io import MemoryFile
from zampy.datasets import converter
from zampy.datasets import utils
from zampy.datasets import validation
Expand Down Expand Up @@ -231,9 +232,9 @@ def read_raw_dem(file: Path) -> xr.Dataset:
if tfdata is None:
raise ValueError(f"File {file} contains no data")

# rasterio can read bytestream (IO[bytes] type), but typing of open_dataarray is
# incorrect.
da = xr.open_dataarray(tfdata, engine="rasterio") # type: ignore
# Reading bytestream is flakey. rasterio has a MemoryFile module to allow reading
# in-memory GeoTIFF file data:
da = xr.open_dataarray(MemoryFile(tfdata), engine="rasterio") # type: ignore

da = da.sortby(["x", "y"]) # sort the dims ascending
da = da.isel(band=0) # get rid of band dim
Expand Down

0 comments on commit d191415

Please sign in to comment.