cogeotiff / rio-cogeo

Cloud Optimized GeoTIFF creation and validation plugin for rasterio
https://cogeotiff.github.io/rio-cogeo/
BSD 3-Clause "New" or "Revised" License
308 stars 42 forks source link

Convert toCOG an in MemoryFile raster. #290

Closed Jap8nted closed 3 months ago

Jap8nted commented 3 months ago

Hi,

I am not sure if this is possible but tried multiple ways and failed to make it work, so decided to write it here. I am generating a raster and I have it in memory as a rioxarray RasterDataset object. I want to convert it to COG and zip it, so the intended behaviour was to translate it to COG and write it to the zip without needing to write any file to filesystem (using memfiles).

The question is, is this possible? if not, does it make sense to implement it? would by happy to do if that makes sense.

with MemoryFile() as memfile:
    with rasterio.open(
        memfile,
        driver="GTiff",
        dtype=riodata[dimension].dtype,
        count=1,
        width=riodata.rio.width,
        height=riodata.rio.height,
        crs=riodata.rio.crs.to_string(),
        transform=riodata.rio.transform(),
        nodata=riodata[dimension].rio.nodata,
        compress="LZW"
        ) as ds:
        dataset.write(riodata.values, 1)

    with MemoryFile() as cog_memfile:
        cog_translate(
                memfile,
                cog_memfile.name,
                config=dict(GDAL_NUM_THREADS="ALL_CPUS"),
                in_memory=True,
                dst_kwargs=cog_profiles.get("deflate"),
                resampling="nearest",
            )