Skip to content

ENH: use fsspec to allow read remote single-files #246

Closed

Description

@raybellwaves

Activity

  1. added
    proposal
    on Feb 17, 2021
  2. snowman2 commented on Feb 17, 2021

    @snowman2
    Member

    Sounds like a good addition. @raybellwaves is this something you wouldn't mind working on?

  3. scottyhq commented on Mar 10, 2021

    @scottyhq
    Contributor

    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!

  4. jhamman commented on Mar 10, 2021

    @jhamman
    Contributor

    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.

  5. djhoese commented on Mar 10, 2021

    @djhoese
    Contributor

    (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?

  6. sgillies commented on Mar 11, 2021

    @sgillies

    @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.

  7. djhoese commented on Mar 15, 2021

    @djhoese
    Contributor

    @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?

  8. scottyhq commented on Mar 15, 2021

    @scottyhq
    Contributor

    @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)
    with fsspec.open('gs://pangeo-ncar-soilgrids/TAXNWRB_250m.tif', **gcp_settings) as fobj:
        with rasterio.open(fobj) as src: 
            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: badge

    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.17
    uri = "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!

  9. raybellwaves commented on Mar 25, 2021

    @raybellwaves
    ContributorAuthor

    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. @gjoseph92

  10. gjoseph92 commented on Mar 25, 2021

    @gjoseph92

    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.

    cc @TomAugspurger

  11. djhoese commented on Mar 27, 2021

    @djhoese
    Contributor

    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.

  12. martindurant commented on Mar 27, 2021

    @martindurant
  13. djhoese commented on Mar 29, 2021

    @djhoese
    Contributor

    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.

  14. djhoese commented on Mar 29, 2021

    @djhoese
    Contributor

    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.

  15. rouault commented on Apr 1, 2021

    @rouault

    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.

  16. djhoese commented on Apr 1, 2021

    @djhoese
    Contributor

    @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)

  17. rouault commented on Apr 1, 2021

    @rouault

    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.

  18. djhoese commented on Apr 2, 2021

    @djhoese
    Contributor

    See rasterio/rasterio#2141 for proof that this is possible. As I say there, this is not even close to a final version.

  19. JackKelly commented on Oct 6, 2021

    @JackKelly

    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?

  20. snowman2 commented on Oct 6, 2021

    @snowman2
    Member

    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?

    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:

    s3://
    az://
    gs://
    oss://
    

    You can also use the /vsi* prefixes:

    This:

    s3://landsat-pds/L8/139/045/LC81390452014295LGN00/LC81390452014295LGN00_B1.TIF
    

    is equivalent to:

    /vsis3/landsat-pds/L8/139/045/LC81390452014295LGN00/LC81390452014295LGN00_B1.TIF
    
  21. JackKelly commented on Oct 6, 2021

    @JackKelly

    Awesome, thank you!

  22. snowman2 commented on Nov 15, 2021

    @snowman2
    Member

    Thanks to @djhoese this has been added to rasterio and is expected on the rasterio 1.3 release. rasterio/rasterio#2141

  23. JackKelly commented on Nov 15, 2021

    @JackKelly

    Awesome work, thank you!

  24. sgillies commented on Nov 15, 2021

    @sgillies

    @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.

  25. djhoese commented on Nov 15, 2021

    @djhoese
    Contributor

    This isn't a criticism of @djhoese's work

    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.

  26. changed the title [-]ENH: use fsspec to allow read/write remote files[/-] [+]ENH: use fsspec to allow read remote single-files[/+] on Nov 15, 2021
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