Expose GDAL VRT connection string parameters in various image endpoints #960
Replies: 6 comments 4 replies
-
thanks for starting the discussion @TomAugspurger To be honest I'm not quite sure I see how and where to add those parameters, especially if this is to be used in titiler-pgstac. If it's to be used in a simple TilerFactory created endpoints (e.g. /cog) then I do think it's pretty simpler for user to create there own def DatasetPathParams(
url: Annotated[str, Query(description="Dataset URL")],
sds_name: Annotated[str, Query(description="The name of a subdataset in the source asset to load.")],
sds_bidx: Annotated[List[int], Query(description="The band numbers (between 1 and N) of the source asset to load."),
) -> str:
"""VRT URL from args""
...
app = FastAPI(title="My simple app")
cog = TilerFactory(path_dependency=DatasetPathParams)
app.include_router(cog.router) |
Beta Was this translation helpful? Give feedback.
-
This might appear like pure magic, but here is how you can customize your titiler-pgstac application and add https://gist.github.com/vincentsarago/81f84f1ef349b9ac1a3a383c4424142f The idea is to create a custom low level
@attr.s
class CustomReader(Reader):
subdataset_name: Optional[str] = attr.ib(default=None)
subdataset_bands: Optional[List[int]] = attr.ib(default=None)
def __attrs_post_init__(self):
vrt_params = {}
if self.subdataset_name:
vrt_params["sd_name"] = self.subdataset_name
if self.subdataset_bands:
vrt_params["bands"] = ",".join([str(band) for band in self.subdataset_bands])
if vrt_params:
params = urllib.parse.urlencode(vrt_params, safe=",")
self.input = f"vrt:///vsicurl/{self.input}?{params}"
super().__attrs_post_init__() those 2 args are passed using the
|
Beta Was this translation helpful? Give feedback.
-
Is there precedent for using an existing parameter like |
Beta Was this translation helpful? Give feedback.
-
FYI over https://gist.github.com/vincentsarago/43b2bf643f7f878761004c8145a5f959 I've explored another way TL&DR, the custom STACReader in the gist, accepts assets to be passed in form of note: using this in titiler, means that you'll need to encode the asset string (because of the ://, ? and & strings) |
Beta Was this translation helpful? Give feedback.
-
Thanks Vincent, let me make sure I understood what the gist is doing:
Did I understand that right? I like this solution because it just exposes the VRT URI. If GDAL adds more options in the future, titiler doesn't have to change. I think this approach would also work for NetCDF and HDF5, if it's not too onerous can you try it out with one of our NetCDF datasets from Planetary Computer? |
Beta Was this translation helpful? Give feedback.
-
yes, in addition to the code shared in the gist, I would need to account that maybe some asset could be named
depends what is the asset
Yes
Yes
absolutely 👍
sure |
Beta Was this translation helpful? Give feedback.
-
I'd like to visualize a band out of a large GRIB2 file, or perhaps a subdatset out of a NetCDF file. I can do this efficiently if I can build a GDAL VRT connection string to hand off to rasterio:
gdalinfo "vrt:///vsicurl/https://noaahrrr.blob.core.windows.net/hrrr/hrrr.20240620/conus/hrrr.t00z.wrfsfcf00.grib2?bands=123"
(without the
?bands=123
then rasterio / GDAL needs to read all the messages, which is slow)I'm using titiler-pgstac, and can override
PgSTACReader._get_asset_info
to build an AssetInfo with a URL that's actually a VRT connection string. From there things just work!I'd like to propose two new parameters to TiTiler's image endpoints, and am curious if it makes sense to "standardize" these in TiTiler itself.
For reasons that aren't entirely clear to me, the various "subdatasets" inside of the HRRR grib files are considered bands of a single dataset, while for a NetCDF file like this GOES asset, they're considered subdatasets:
Perhaps if you had a 3-dimensional array in the NetCDF file then you'd end up with both subdatasets and bands?
A couple notes:
subdataset_bands
isn't 100% technically correct. This is thebands
parameter passed to the VRT query string.vrt_bands
would be a bit more accurate, but I prefer thesubdataset
prefix. If we prefervrt_bands
then I'd recommend usingvrt_subdataset
orvrt_subdataset_name
for the other parameter.sd
parameter for selecting a subdataset by position. I've chosen not to add it since the docs noteBeta Was this translation helpful? Give feedback.
All reactions