Skip to content

[Feature request] Support file-like objects in open_rasterio #4139

Closed

Description

@jhamman

With some acrobatics, it is possible to open file-like objects to rasterio. It would be useful if xarray supported this workflow, particularly for working with cloud optimized geotiffs and fs-spec.

MCVE Code Sample

with open('my_data.tif', 'rb') as f:
    da = xr.open_rasterio(f)

Expected Output

DataArray -> equivalent to xr.open_rasterio('my_data.tif')

Problem Description

We only currently allow str, rasterio.DatasetReader, or rasterio.WarpedVRT as inputs to open_rasterio.

Versions

Output of xr.show_versions()

INSTALLED VERSIONS

commit: 2a288f6
python: 3.8.2 | packaged by conda-forge | (default, Apr 24 2020, 07:56:27)
[Clang 9.0.1 ]
python-bits: 64
OS: Darwin
OS-release: 18.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
libhdf5: None
libnetcdf: None

xarray: 0.15.2.dev68+gb896a68f
pandas: 1.0.4
numpy: 1.18.5
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: None
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.1.5
cfgrib: None
iris: None
bottleneck: None
dask: 2.18.1
distributed: 2.18.0
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
pint: None
setuptools: 46.1.3.post20200325
pip: 20.1
conda: None
pytest: 5.4.3
IPython: 7.13.0
sphinx: 3.0.3

xref: pangeo-data/pangeo-datastore#109

Activity

  1. stale commented on Apr 19, 2022

    @stale

    In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity

    If this issue remains relevant, please comment here or remove the stale label; otherwise it will be marked as closed automatically

  2. andersy005 commented on Apr 19, 2022

    @andersy005
    Member

    this appears to have been fixed in the latest alpha release of rasterio v1.3a3 (which isn't available on conda-forge but is available on PyPI) via fsspec

    In [13]: import xarray as xr
    
    In [14]: import fsspec
    
    In [15]: import rasterio
    
    In [16]: rasterio.__version__
    Out[16]: '1.3a3'
    
    In [17]: with fsspec.open('2d_test.tiff') as f:
        ...:     ds  = xr.open_dataset(f, engine='rasterio')
        ...: 
    
    In [18]: ds
    Out[18]: 
    <xarray.Dataset>
    Dimensions:      (band: 1, y: 10, x: 10)
    Coordinates:
      * band         (band) int64 1
        xc           (y, x) float64 ...
        yc           (y, x) float64 ...
        spatial_ref  int64 ...
    Dimensions without coordinates: y, x
    Data variables:
        band_data    (band, y, x) float64 ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions