You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH: use fsspec to allow read remote single-files #246
Just wanted to link to some issues and explain why this could be helpful, but also why it might be somewhat difficult. We (mostly @jhamman and @martindurant) looked into this a while back b/c of trying to stream data from google requester pays buckets to xarray datasets and rasterio/gdal didn't have that implemented (rasterio/rasterio#1948) but you could do it via fsspec.open().
So this PR was an attempt to make xr.open_rasterio() accept fsspec objects pydata/xarray#4140
But, it was unclear how to actually implement this because it seemed that ultimately GDAL required that file-like objects be read entirely into MemoryFile(), sort of defeating the purpose of efficient header-only initial access. rasterio/rasterio#977 (comment)
Would definitely be nice though to have the common interface (credentials in particular) that fsspec provides for remote data!
But, it was unclear how to actually implement this because it seemed that ultimately GDAL required that file-like objects be read entirely into MemoryFile(), sort of defeating the purpose of efficient header-only initial access. mapbox/rasterio#977 (comment)
Exactly. This seems to be the crux to making this work right now. I ran out of steam after @sgillies last comment there. I suspect this is something that can be worked around but I don't know how.
(Disclaimer: coming in with very little knowledge of previous discussions on this or the internals of rasterio)
It seems like the function @sgillies points to returns a VSILFile which is supposed to be anything that implements this interface: https://gdal.org/api/cpl_cpp.html#classVSIVirtualHandle. So theoretically if rasterio could be provide a wrapper class for that (or some other type of base class) that it knew it could just pass to GDAL then wouldn't that work? Granted, this is mixing a lot of python and C++ class stuff which I'm sure gets complicated pretty fast especially when GDAL is involved.
...but its programming, we can do whatever we want, right?
Reacted by Sean Gillies, Joe Hamman and Gabe Joseph
@djhoese that's a very intriguing idea and I would be interested in looking into this with you. I admire fsspec and its chaining paradigm. GDAL's own chaining is less flexible, offers no control over caching, and due to under-specification of chaining there are some bugs in format driver implementations.
@sgillies I would be interested in looking this too. I'm not sure I have the time this month (or even next if I'm being honest), but it is interesting enough that I'll probably make time for it.
Just so I'm clear, what @scottyhq and @jhamman wanted is the ability to provide an open file-like object to rasterio and have rasterio intelligently/selectively load data as it is needed. Is that right @jhamman@scottyhq? The solution that we would be attempting is adding a new low-level class in rasterio to pass to GDAL that would allow random/byte-range access to this file-like object. Am I understanding this correctly?
@djhoese and @sgillies thanks so much for chiming in! any time you're willing to explore this would be greatly appreciated.
Just so I'm clear, what @scottyhq and @jhamman wanted is the ability to provide an open file-like object to rasterio and have rasterio intelligently/selectively load data as it is needed.
Yes. I definitely feel out of my depth when it comes to the internals, but I can provide a few code examples that I think help guide things. The first order issue (from PRs linked above if you want to follow the paper trail):
# 1st step, improve efficiency here (Wall time: 3.12 s, should be ~100ms to read header)gcp_settings=dict(token='cloud', project='pangeo-181919', requester_pays=True)
withfsspec.open('gs://pangeo-ncar-soilgrids/TAXNWRB_250m.tif', **gcp_settings) asfobj:
withrasterio.open(fobj) assrc:
print(src.profile)
# also will want to read data subsets
the above snippet will run via a pangeo binder session on GCP if that is helpful:
Ultimately (and please others chime in if there are other ideas here! don't want to highjack @raybellwaves 's issue), I think the goal would be to align with other xarray backends, in such a way that URIs are parsed and handled behind the scenes by fsspec:
# works in xarray>0.17uri="s3://era5-pds/zarr/2020/12/data/eastward_wind_at_10_metres.zarr"aws_settings=dict(anon=True)
fsspec_options=dict(storage_options=aws_options)
ds=xr.open_mfdataset(uri,
engine="zarr",
backend_kwargs=fsspec_options)
And this would be a similar syntax for rioxarray (related to #197):
# Goal (does not currently work)uri="gs://pangeo-ncar-soilgrids/TAXNWRB_250m.tif"gcp_settings=dict(token='cloud', project='pangeo-181919', requester_pays=True)
fsspec_options=dict(storage_options=gcp_settings)
ds=xr.open_dataset(uri,
engine="rioxarray",
backend_kwargs=fsspec_options)
Hope that helps!
Reacted by David Hoese, Jessica Scheick, Sean Harkins and Scott Staniewicz
Thanks @raybellwaves. I think @djhoese's idea of basically writing a translation layer from the Python file interface to the GDAL VSIFile interface is very promising, and would have pretty broad applicability. That would be really nice to have. Kind of reminds me of pyarrow.PythonFile, which could be useful to reference.
I was going to ask if anyone knew of a public S3-stored geotiff that I could test with locally, but I'm realizing that I could probably use any local geotiff opened with fsspec, right? For more benchmarking, if anyone has a public S3 geotiff that doesn't charge anyone to read, let me know.
On March 27, 2021 11:07:00 AM EDT, David Hoese ***@***.***> wrote:
I was going to ask if anyone knew of a public S3-stored geotiff that I
could test with locally, but I'm realizing that I could probably use
any local geotiff opened with fsspec, right? For more benchmarking, if
anyone has a public S3 geotiff that doesn't charge anyone to read, let
me know.
--
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub:
#246 (comment)
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
I got something that compiles! ...now I just need to make it do what it is supposed to. I'll make a PR as soon as it resembles what it should. From then on we can define the exact interfaces and functionality.
Reacted by Martin Durant, Gabe Joseph and Joe Hamman
So false alarm. This is harder than I thought. Compilation does not equal functional. I wanted to put this in the description of a new PR, but given that the code is less representative of how it will be I think I'll hold off until I can get it further. There are (as I've now learned) two parts to this whole thing in GDAL: the file handle and the file system handler. What I was excited about before was the file handle part. Basically this:
# in cython extension-land
class PyVSIFileBase:
handle_ptr = PythonVSIVirtualHandle(python_open_file_obj)
# in C++
class PythonVSIVirtualHandle(VSIVirtualHandle):
# functions that access the open python file object for things like Seek, Tell, etc.
The issue I ran into today is that although the above should all work (once implemented) for accessing parts of the python file-like object and using the file as GDAL wants to, it does not tell GDAL how to get the file from the file system. So while I can create the handles for above, when GDAL goes to open a file at this path it has no idea how to do that because it isn't registered/created with a file system handler. For in-memory files in GDAL, this is the part of GDAL that handles all paths with a "/vsimem/" prefix.
The bad news is I don't think I can slip my custom "PythonVSIVirtualHandle" into this system and trick the VSIMemFilesystemHandler into handling it as if it was any other block of memory. The main reason is that it seems this memory file system handler creates a new VSIMemHandle every time you open an in-memory file. I assume this is so each open memory file has its own pointer to where it is in the data.
The good news is I think I can create and register my own file system handler for any path prefix (ex. /vsipythonfile/). I'm hoping to getting a proof of concept together won't be that hard especially if I don't allow just about every type of file access except single threaded reading (for now). If anyone in this thread knows what I'm talking about and knows that I have the wrong idea about this let me know.
If this gets far enough this should probably just be added to GDAL directly.
For out-of-tree GDAL file systems, I'd advise to use the VSIInstallPluginHandler() (see cpl_vsi.h) mechanism that was added in GDAL 3.0 and offers a C API with ABI stability to register the various methods to implement a file system.
@rouault Thank you! I started copying the bits I needed from the "Mem" file handler and have ended up using VSIFileManager::InstallHandler. I'm a little confused by this plugin infrastructure. It seems the plugin struct is providing both the handlers for the file system (mkdir, etc) and the files themselves (read, tell, seek, etc). Is there any additional documentation beyond the API docs? Or maybe a project that you know of that is already using it?
The way I'm doing it so far seems to work, but I don't have everything working yet. I'd much rather have the final version use an official plugin system as what I'm doing definitely feel like a hack.
void VSIInstallPythonFileLikeFileHandler()
{
VSIFileManager::InstallHandler( "/vsipythonfilelike/", new VSIPythonFileLikeFilesystemHandler );
}
(I really need to move this into a rasterio issue instead of rioxarray)
Is there any additional documentation beyond the API docs? Or maybe a project that you know of that is already using it?
no doc beyond the API documentation. The callbacks are a 1:1 match with the corresponding C++ methods. I'm not aware of public use of it. It was added to allow in-house custom handlers.
Sorry if I'm getting confused but is one conclusion of this discussion that, today, if we want rioxarray to open geospatial data stored in a cloud storage bucket, then we'll have to download that data to local disk first?
@snowman2 it could be worth editing the issue title to reflect some limitations of this feature: it is read-only and only supports single-file data formats like GeoTIFF with no sidecars. GDAL's readdir, specifically, isn't implemented. This isn't a criticism of @djhoese's work, I'm just saying that GDAL's understanding of an entire file system can't be fully conveyed by a Python file object alone.
For this particular functionality, given the interfaces being connected, I wouldn't take any feedback as negative.
I'm just saying that GDAL's understanding of an entire file system can't be fully conveyed by a Python file object alone.
To add to this: I did not implement writing given the likely complication of multiple simultaneous writers and the lack of understanding/experience with these GDAL interfaces. For the sidecar stuff, I think theoretically you could tell rasterio to open each of the files (main file and all sidecars)...wait no that wouldn't work as rasterio's FilePath generates a UUID for each file-like object it is given. So GDAL wouldn't know what name to look for for the sidecar files. Anyway, that would be kind of disgusting to support for the users of that though. I'll try to help anyone who wants to try making that work though.
Activity
snowman2 commentedon Feb 17, 2021
Sounds like a good addition. @raybellwaves is this something you wouldn't mind working on?
snowman2 commentedon Feb 17, 2021
I suspect the area you would want to change is around here: https://github.com/corteva/rioxarray/blob/7684848d6944f14e58f825606161e6d2cf9a0a7f/rioxarray/_io.py#L715
scottyhq commentedon Mar 10, 2021
Just wanted to link to some issues and explain why this could be helpful, but also why it might be somewhat difficult. We (mostly @jhamman and @martindurant) looked into this a while back b/c of trying to stream data from google requester pays buckets to xarray datasets and rasterio/gdal didn't have that implemented (rasterio/rasterio#1948) but you could do it via fsspec.open().
So this PR was an attempt to make xr.open_rasterio() accept fsspec objects pydata/xarray#4140
But, it was unclear how to actually implement this because it seemed that ultimately GDAL required that file-like objects be read entirely into MemoryFile(), sort of defeating the purpose of efficient header-only initial access. rasterio/rasterio#977 (comment)
Would definitely be nice though to have the common interface (credentials in particular) that fsspec provides for remote data!
jhamman commentedon Mar 10, 2021
Exactly. This seems to be the crux to making this work right now. I ran out of steam after @sgillies last comment there. I suspect this is something that can be worked around but I don't know how.
djhoese commentedon Mar 10, 2021
(Disclaimer: coming in with very little knowledge of previous discussions on this or the internals of rasterio)
It seems like the function @sgillies points to returns a
VSILFilewhich is supposed to be anything that implements this interface: https://gdal.org/api/cpl_cpp.html#classVSIVirtualHandle. So theoretically if rasterio could be provide a wrapper class for that (or some other type of base class) that it knew it could just pass to GDAL then wouldn't that work? Granted, this is mixing a lot of python and C++ class stuff which I'm sure gets complicated pretty fast especially when GDAL is involved....but its programming, we can do whatever we want, right?
sgillies commentedon Mar 11, 2021
@djhoese that's a very intriguing idea and I would be interested in looking into this with you. I admire fsspec and its chaining paradigm. GDAL's own chaining is less flexible, offers no control over caching, and due to under-specification of chaining there are some bugs in format driver implementations.
djhoese commentedon Mar 15, 2021
@sgillies I would be interested in looking this too. I'm not sure I have the time this month (or even next if I'm being honest), but it is interesting enough that I'll probably make time for it.
Just so I'm clear, what @scottyhq and @jhamman wanted is the ability to provide an open file-like object to rasterio and have rasterio intelligently/selectively load data as it is needed. Is that right @jhamman @scottyhq? The solution that we would be attempting is adding a new low-level class in rasterio to pass to GDAL that would allow random/byte-range access to this file-like object. Am I understanding this correctly?
scottyhq commentedon Mar 15, 2021
@djhoese and @sgillies thanks so much for chiming in! any time you're willing to explore this would be greatly appreciated.
Yes. I definitely feel out of my depth when it comes to the internals, but I can provide a few code examples that I think help guide things. The first order issue (from PRs linked above if you want to follow the paper trail):
the above snippet will run via a pangeo binder session on GCP if that is helpful:
Ultimately (and please others chime in if there are other ideas here! don't want to highjack @raybellwaves 's issue), I think the goal would be to align with other xarray backends, in such a way that URIs are parsed and handled behind the scenes by fsspec:
And this would be a similar syntax for rioxarray (related to #197):
Hope that helps!
raybellwaves commentedon Mar 25, 2021
May be of interest here https://github.com/gjoseph92/stackstac#roadmap. "Easier access to s3://-style URIs (right now, you'll need to pass in
gdal_env=stackstac.DEFAULT_GDAL_ENV.updated(always=dict(session=rio.session.AWSSession(...))))" cc. @gjoseph92gjoseph92 commentedon Mar 25, 2021
Thanks @raybellwaves. I think @djhoese's idea of basically writing a translation layer from the Python file interface to the GDAL
VSIFileinterface is very promising, and would have pretty broad applicability. That would be really nice to have. Kind of reminds me ofpyarrow.PythonFile, which could be useful to reference.cc @TomAugspurger
djhoese commentedon Mar 27, 2021
I was going to ask if anyone knew of a public S3-stored geotiff that I could test with locally, but I'm realizing that I could probably use any local geotiff opened with fsspec, right? For more benchmarking, if anyone has a public S3 geotiff that doesn't charge anyone to read, let me know.
martindurant commentedon Mar 27, 2021
gjoseph92 commentedon Mar 28, 2021
@djhoese here are a few:
djhoese commentedon Mar 29, 2021
I got something that compiles! ...now I just need to make it do what it is supposed to. I'll make a PR as soon as it resembles what it should. From then on we can define the exact interfaces and functionality.
djhoese commentedon Mar 29, 2021
So false alarm. This is harder than I thought. Compilation does not equal functional. I wanted to put this in the description of a new PR, but given that the code is less representative of how it will be I think I'll hold off until I can get it further. There are (as I've now learned) two parts to this whole thing in GDAL: the file handle and the file system handler. What I was excited about before was the file handle part. Basically this:
The issue I ran into today is that although the above should all work (once implemented) for accessing parts of the python file-like object and using the file as GDAL wants to, it does not tell GDAL how to get the file from the file system. So while I can create the handles for above, when GDAL goes to open a file at this path it has no idea how to do that because it isn't registered/created with a file system handler. For in-memory files in GDAL, this is the part of GDAL that handles all paths with a "/vsimem/" prefix.
The bad news is I don't think I can slip my custom "PythonVSIVirtualHandle" into this system and trick the
VSIMemFilesystemHandlerinto handling it as if it was any other block of memory. The main reason is that it seems this memory file system handler creates a new VSIMemHandle every time you open an in-memory file. I assume this is so each open memory file has its own pointer to where it is in the data.The good news is I think I can create and register my own file system handler for any path prefix (ex.
/vsipythonfile/). I'm hoping to getting a proof of concept together won't be that hard especially if I don't allow just about every type of file access except single threaded reading (for now). If anyone in this thread knows what I'm talking about and knows that I have the wrong idea about this let me know.If this gets far enough this should probably just be added to GDAL directly.
rouault commentedon Apr 1, 2021
For out-of-tree GDAL file systems, I'd advise to use the VSIInstallPluginHandler() (see cpl_vsi.h) mechanism that was added in GDAL 3.0 and offers a C API with ABI stability to register the various methods to implement a file system.
djhoese commentedon Apr 1, 2021
@rouault Thank you! I started copying the bits I needed from the "Mem" file handler and have ended up using
VSIFileManager::InstallHandler. I'm a little confused by this plugin infrastructure. It seems the plugin struct is providing both the handlers for the file system (mkdir, etc) and the files themselves (read, tell, seek, etc). Is there any additional documentation beyond the API docs? Or maybe a project that you know of that is already using it?The way I'm doing it so far seems to work, but I don't have everything working yet. I'd much rather have the final version use an official plugin system as what I'm doing definitely feel like a hack.
(I really need to move this into a rasterio issue instead of rioxarray)
rouault commentedon Apr 1, 2021
no doc beyond the API documentation. The callbacks are a 1:1 match with the corresponding C++ methods. I'm not aware of public use of it. It was added to allow in-house custom handlers.
djhoese commentedon Apr 2, 2021
See rasterio/rasterio#2141 for proof that this is possible. As I say there, this is not even close to a final version.
JackKelly commentedon Oct 6, 2021
Sorry if I'm getting confused but is one conclusion of this discussion that, today, if we want
rioxarrayto open geospatial data stored in a cloud storage bucket, then we'll have to download that data to local disk first?snowman2 commentedon Oct 6, 2021
rioxarray uses rasterio, which supports reading from cloud storage:
rasterio is able to do so with GDAL VSI file system support: https://gdal.org/user/virtual_file_systems.html
These prefixes are supported in rasterio:
You can also use the
/vsi*prefixes:This:
is equivalent to:
JackKelly commentedon Oct 6, 2021
Awesome, thank you!
snowman2 commentedon Nov 15, 2021
Thanks to @djhoese this has been added to rasterio and is expected on the rasterio 1.3 release. rasterio/rasterio#2141
JackKelly commentedon Nov 15, 2021
Awesome work, thank you!
sgillies commentedon Nov 15, 2021
@snowman2 it could be worth editing the issue title to reflect some limitations of this feature: it is read-only and only supports single-file data formats like GeoTIFF with no sidecars. GDAL's readdir, specifically, isn't implemented. This isn't a criticism of @djhoese's work, I'm just saying that GDAL's understanding of an entire file system can't be fully conveyed by a Python file object alone.
djhoese commentedon Nov 15, 2021
For this particular functionality, given the interfaces being connected, I wouldn't take any feedback as negative.
To add to this: I did not implement writing given the likely complication of multiple simultaneous writers and the lack of understanding/experience with these GDAL interfaces. For the sidecar stuff, I think theoretically you could tell rasterio to open each of the files (main file and all sidecars)...wait no that wouldn't work as rasterio's FilePath generates a UUID for each file-like object it is given. So GDAL wouldn't know what name to look for for the sidecar files. Anyway, that would be kind of disgusting to support for the users of that though. I'll try to help anyone who wants to try making that work though.
[-]ENH: use fsspec to allow read/write remote files[/-][+]ENH: use fsspec to allow read remote single-files[/+]