Skip to content

Support the new rasterio functionality for file-like objects #440

Closed

Description

@rsignell-usgs

Now that rasterio supports file-like objects (like those from fsspec), it would be great to have it working with open_rasterio.

This works:

import fsspec
import rasterio
import rioxarray as rxr

fs = fsspec.filesystem('s3', anon=True)
s3uri = 's3://sentinel-s1-rtc-indigo/tiles/RTC/1/IW/10/T/ET/2020/S1B_20200106_10TET_ASC/Gamma0_VV.tif'
fs.size(s3uri)

with fs.open(s3uri) as f:
    with rasterio.open(f) as dataset:
        print(dataset.shape)

but this does not:

rxr.open_rasterio(fs.open(s3uri))

producing the error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/ipykernel_86/646641491.py in <module>
----> 1 ds = xr.open_dataset(fs.open(s3uri), engine='rasterio')

/home/conda/store/acb2f024c6e8193fde99cded4bc262f3ebaa0295b7bda375655032f00b7a681f-default/lib/python3.9/site-packages/xarray/backends/api.py in open_dataset(filename_or_obj, engine, chunks, cache, decode_cf, mask_and_scale, decode_times, decode_timedelta, use_cftime, concat_characters, decode_coords, drop_variables, backend_kwargs, *args, **kwargs)
    493 
    494     overwrite_encoded_chunks = kwargs.pop("overwrite_encoded_chunks", None)
--> 495     backend_ds = backend.open_dataset(
    496         filename_or_obj,
    497         drop_variables=drop_variables,

/home/conda/store/acb2f024c6e8193fde99cded4bc262f3ebaa0295b7bda375655032f00b7a681f-default/lib/python3.9/site-packages/rioxarray/xarray_plugin.py in open_dataset(self, filename_or_obj, drop_variables, parse_coordinates, chunks, cache, lock, masked, mask_and_scale, variable, group, default_name, decode_times, decode_timedelta, open_kwargs)
     53         if open_kwargs is None:
     54             open_kwargs = {}
---> 55         rds = _io.open_rasterio(
     56             filename_or_obj,
     57             parse_coordinates=parse_coordinates,

/home/conda/store/acb2f024c6e8193fde99cded4bc262f3ebaa0295b7bda375655032f00b7a681f-default/lib/python3.9/site-packages/rioxarray/_io.py in open_rasterio(filename, parse_coordinates, chunks, cache, lock, masked, mask_and_scale, variable, group, default_name, decode_times, decode_timedelta, **open_kwargs)
    842 
    843     # open the subdatasets if they exist
--> 844     if riods.subdatasets:
    845         return _load_subdatasets(
    846             riods=riods,

AttributeError: '_GeneratorContextManager' object has no attribute 'subdatasets'
​```

Activity

  1. added
    proposal
    on Dec 3, 2021
  2. snowman2 commented on Dec 8, 2021

    @snowman2
    Member

    Looks like this is a rasterio issue:

    import fsspec
    import rasterio
    
    fs = fsspec.filesystem('s3', anon=True)
    s3uri = 's3://sentinel-s1-rtc-indigo/tiles/RTC/1/IW/10/T/ET/2020/S1B_20200106_10TET_ASC/Gamma0_VV.tif'
    
    with fs.open(s3uri) as fsh:
        riods = rasterio.open(fsh)
        print(riods)
        with rasterio.open(fsh) as riods:
            print(riods)

    Output:

    <contextlib._GeneratorContextManager object at 0x7f66e850b9a0>
    <open DatasetReader name='/vsimem/c44e696f-728c-4540-95d1-5ecea7c1fe91/c44e696f-728c-4540-95d1-5ecea7c1fe91.tif' mode='r'>
    
  3. snowman2 commented on Dec 8, 2021

    @snowman2
    Member

    Forwarding upstream: rasterio/rasterio#2360

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions