rioxarray icon indicating copy to clipboard operation
rioxarray copied to clipboard

Does rioxarray support generating overviews and exporting them?

Open andhuang-CLGX opened this issue 4 years ago • 10 comments

I see reading, but I couldn't find any export. https://corteva.github.io/rioxarray/html/examples/COG.html

andhuang-CLGX avatar Sep 17 '21 16:09 andhuang-CLGX

With GDAL 3.1+: https://gdal.org/drivers/raster/cog.html#raster-cog

xds.rio.to_raster("raster.tif", driver="COG")

See also: https://corteva.github.io/rioxarray/stable/examples/convert_to_raster.html

snowman2 avatar Sep 17 '21 16:09 snowman2

Thanks! Is there an equivalent of:

dst.build_overviews(factors, Resampling.average)

from https://rasterio.readthedocs.io/en/latest/topics/overviews.html

but using xarray + dask?

andhuang-CLGX avatar Sep 17 '21 16:09 andhuang-CLGX

Oh! It's built into COG! I just tried:

import rioxarray as rio

path = "something.tif"
ds = rio.open_rasterio(path, overview_level=0)
ds.rio.to_raster("raster.tif", driver="COG")

From

OVERVIEWS=[AUTO/IGNORE_EXISTING/FORCE_USE_EXISTING/NONE]: Describe the behavior regarding overview generation and use of source overviews.

AUTO (default): source overviews will be used if present (even if the dimension of the smallest level is not < 512 pixels), and, if not present, overviews will be automatically generated in the output file.

IGNORE_EXISTING: potential existing overviews on the source dataset will be ignored and new overviews will be automatically generated.

FORCE_USE_EXISTING: potential existing overviews on the source will be used (even if the dimension of the smallest level is not < 512 pixels). If there is no source overview, this is equivalent to specifying NONE.

NONE: potential source overviews will be ignored, and no overview will be generated.

But is there a way to specify with overview levels?

andhuang-CLGX avatar Sep 17 '21 16:09 andhuang-CLGX

Did you try ALIGNED_LEVELS?

snowman2 avatar Sep 17 '21 16:09 snowman2

How do I specify ALIGNED_LEVELS

import rioxarray
fp = 'something.tif'
ds = rioxarray.open_rasterio(fp, overview_level=1)
ds.rio.to_raster("test.tif", driver="COG", ALIGNED_LEVELS=1)

andhuang-CLGX avatar Sep 17 '21 17:09 andhuang-CLGX

Looks like it is a number between 1-10 and requires setting TILING_SCHEME as well.

snowman2 avatar Sep 17 '21 17:09 snowman2

ds.rio.to_raster("test.tif", driver="COG", ALIGNED_LEVELS=1, TILING_SCHEME="GoogleMapsCompatible") seems to output a bunch of levels

for level in range(0, 7):
    print(rioxarray.open_rasterio("test.tif", overview_level=level))

Up to 7

andhuang-CLGX avatar Sep 17 '21 18:09 andhuang-CLGX

The "COG" driver seems to be a Copy-only driver (in GDAL terminology), without a Create method, so will rioxarray be able to use it without having to make a complete in-memory copy of the dataset?

howff avatar Dec 06 '21 15:12 howff

I think this is what you are referring to: ref

The best way to find out is to give it a try and see what happens.

snowman2 avatar Dec 06 '21 15:12 snowman2