4

Is there a good way to produce a cloud-optimized GeoTIFF (https://www.cogeo.org) directly from an xarray.DataArray? At the moment it takes me a couple of steps to produce an intermediate regular GeoTIFF first, and run it through a converter like rio-cogeo to turn it into a Cloud-Optimized GeoTIFF. Something like this:

Step 1: Produce regular non-cloud optimized GeoTIFF

# https://corteva.github.io/rioxarray/stable/examples/COG.html
import rioxarray
import xarray as xr

# Load Cloud-Optimized GeoTIFF into an xarray.DataArray
rds: xr.DataArray = rioxarray.open_rasterio(
    filename="https://oin-hotosm.s3.amazonaws.com/5d7dad0becaf880008a9bc88/0/5d7dad0becaf880008a9bc89.tif",
    masked=True,
    overview_level=4,
)

# Save output to a regular GeoTIFF
rds.rio.to_raster(raster_path="non-cogeo.tif")

Step 2: Convert regular GeoTIFF to cloud-optimized GeoTIFF

rio cogeo create --cog-profile=zstd --forward-band-tags non-cogeo.tif cogeo.tif

rio cogeo info cogeo.tif

produces

Driver: GTiff
File: /home/username/projects/cogeo.tif
COG: True
Compression: ZSTD
ColorSpace: None

Profile
    Width:            688
    Height:           312
    Bands:            3
    Tiled:            True
    Dtype:            uint8
    NoData:           None
    Alpha Band:       False
    Internal Mask:    False
    Interleave:       PIXEL
    ColorMap:         False
    ColorInterp:      ('red', 'green', 'blue')
    Scales:           (1.0, 1.0, 1.0)
    Offsets:          (0.0, 0.0, 0.0)

Geo
    Crs:              EPSG:3857
    Origin:           (-13334019.180693429, 4339530.742948703)
    Resolution:       (1.1943285669562078, -1.194328566956247)
    BoundingBox:      (-13334019.180693429, 4339158.112435812, -13333197.482639363, 4339530.742948703)
    MinZoom:          16
    MaxZoom:          17

Image Metadata
    AREA_OR_POINT: Area
    OVR_RESAMPLING_ALG: NEAREST

Image Structure
    COMPRESSION: ZSTD
    INTERLEAVE: PIXEL
    LAYOUT: COG

Band 1
    ColorInterp: red
    Metadata:
        STATISTICS_MAXIMUM: 255
        STATISTICS_MEAN: 85.125740720036
        STATISTICS_MINIMUM: 0
        STATISTICS_STDDEV: 52.508740750754
        STATISTICS_VALID_PERCENT: 100

Band 2
    ColorInterp: green
    Metadata:
        STATISTICS_MAXIMUM: 255
        STATISTICS_MEAN: 86.283914728682
        STATISTICS_MINIMUM: 0
        STATISTICS_STDDEV: 49.498808560978
        STATISTICS_VALID_PERCENT: 100

Band 3
    ColorInterp: blue
    Metadata:
        STATISTICS_MAXIMUM: 255
        STATISTICS_MEAN: 76.663461538462
        STATISTICS_MINIMUM: 0
        STATISTICS_STDDEV: 47.378616249492
        STATISTICS_VALID_PERCENT: 100

IFD
    Id      Size           BlockSize     Decimation           
    0       688x312        256x256       0
    1       344x156        128x128       2

Is it possible to combine Steps 1 and 2 into one? I.e. not have to save to an intermediate file and just produce the cloud-optimized GeoTIFF directly?

1 Answer 1

9
rds.rio.to_raster(raster_path="cogeo.tif", driver="COG")

https://gdal.org/drivers/raster/cog.html

1
  • Awesome, knew there had to be a simple way! Thanks @snowman2!
    – weiji14
    Commented Apr 14, 2022 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.