Skip to content

support file-like objects in xarray.open_rasterio - #4140

Closed
jhamman wants to merge 3 commits into
pydata:mainfrom
jhamman:fix/rasterio_fobj
Closed

jhamman wants to merge 3 commits into
pydata:mainfrom
jhamman:fix/rasterio_fobj

Conversation

@jhamman

@jhamman jhamman commented Jun 10, 2020

Copy link
Copy Markdown
Member

cc @scottyhq and @martindurant

xref: pangeo-data/pangeo-datastore#109

@snowman2

Copy link
Copy Markdown
Contributor

Would you like to add this update to rioxarray as well? If not, mind if I port it over?

dtype=vrt.working_dtype,
warp_extras=vrt.warp_extras,
)
elif isinstance(filename_or_obj, io.IOBase):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how good a test for a file-like this is - maybe this could be just else

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I wanted to ask you about this. Suggestions welcome here.

def _file_object_opener(fobj, *args, **kwargs):
from rasterio.io import MemoryFile

return MemoryFile(fobj).open(*args, **kwargs)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loads the whole thing? Odd to have to do this...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this does not load the whole thing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything with a read() method? What things does rasterio actually call?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked into this a bit more.

https://github.com/mapbox/rasterio/blob/ce00d482faa24068f41164b79901eeec31777263/rasterio/_io.pyx#L837-L898

Here you'll see:

            if hasattr(file_or_bytes, 'read'):
                initial_bytes = file_or_bytes.read()
...
            self._vsif = VSIFileFromMemBuffer(
               self._path, buffer, len(self._initial_bytes), 0)
            self.mode = "r"

I had missed this initial read() call previously. This makes me think what I'm trying may not be possible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

se here, can we just add MemoryFile to the superclasses of fobj, so rasterio is tricked into thinking it's dealing with a memoryfile, but the implementation is actually the original code for the object? Or maybe subclass MemoryFile here with methods that defer to the fobj.

@jhamman

jhamman commented Jun 10, 2020

Copy link
Copy Markdown
Member Author

@snowman2 - let's get this sorted out first. I'm not 100% sure this is the best path forward yet. But you are welcome to port to rioxarray.

@jhamman

jhamman commented Nov 15, 2021

Copy link
Copy Markdown
Member Author

closed via rasterio/rasterio#2141

@jhamman jhamman closed this Nov 15, 2021
@rsignell-usgs

rsignell-usgs commented Dec 3, 2021 •

Copy link
Copy Markdown

I'd like to use this cool new rasterio/fspec functionality in xarray!

I must be doing something wrong here in cell [5]:
https://nbviewer.org/gist/rsignell-usgs/dbf3d8e952895ca255f300790759c60f

@snowman2

snowman2 commented Dec 3, 2021

Copy link
Copy Markdown
Contributor

Sounds like that may need changes in rioxarray. Mind opening up an issue: https://github.com/corteva/rioxarray/issues ?

@rsignell-usgs

Copy link
Copy Markdown

Thanks @snowman2 ! Done in vylor/rioxarray#440

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request] Support file-like objects in open_rasterio

5 participants