[gdal-dev] building a vrt to stack overlapping COGs with a mask/alpha band

Landry Breuil breuil at craig.fr
Thu Jul 9 01:09:56 PDT 2020


On 08/07/2020 13:23, Even Rouault wrote:
> On mercredi 8 juillet 2020 10:12:31 CEST Landry Breuil wrote:
> 
>  > Hi,
> 
>  >
> 
>  > i'm converting a stock of tiled aerial pictures (from distinct
> 
>  > years/campaigns) to COG (using gdal 3.1.0), and trying to stack them via
> 
>  > a vrt. the COG arent 'full', in the sense that they have a bbox, but
> 
>  > there isnt data *everywhere*. Some of those campaigns/COGs have
> 
>  > overlapping BBOXes.
> 
>  >
> 
>  > the COG are built from vrts (assembling all the tiles for the campaign)
> 
>  > built with -addalpha, and then using gdal_translate -of COG -b 1 -b 2 -b
> 
>  > 3 -mask 4 to have the mask band in the resulting COG, ie i get 3 bands
> 
>  > with a 'per-dataset mask':
> 
>  >
> 
>  > Band 1 Block=512x512 Type=Byte, ColorInterp=Red
> 
>  > Overviews: 148000x172000, 74000x86000, 37000x43000, 18500x21500,
> 
>  > 9250x10750, 4625x5375, 2313x2688, 1157x1344, 579x672, 290x336
> 
>  > Mask Flags: PER_DATASET
> 
>  > Overviews of mask band: 148000x172000, 74000x86000, 37000x43000,
> 
>  > 18500x21500, 9250x10750, 4625x5375, 2313x2688, 1157x1344, 579x672, 
> 290x336
> 
>  >
> 
>  > i have zero issue loading the separate COGs in QGIS (locally or via
> 
>  > vsicurl), the nodata areas are properly displayed as transparent,
> 
>  > loading several COGs on top of each other properly render the
> 
>  > overlapping areas, ie if at some coords there's data in the lower COG
> 
>  > and nodata in the toplevel COG, then the data from the lower COG is
> 
>  > rendered. That can also be achieved via a mapserver GROUP, loading the
> 
>  > layers on top of each other, as seen in https://imgur.com/9BgZSTH.png
> 
>  >
> 
>  > Trying to achieve the same via a vrt file stacking the COGs fails (plain
> 
>  > gdalbuildvrt img1.tiff img2.tiff), ie for the same coords, nodata will
> 
>  > be rendered if there's nodata in the toplevel COG (even if there's data
> 
>  > in the lower COG), and that for the complete BBOX of the toplevel COG
> 
>  > (ie outside of the toplevel COG BBOX, data from the lower COGs is
> 
>  > displayed). See https://imgur.com/a/HiEfjxY for an example (here, it's
> 
>  > via WMS loading as jpeg, using png the white area would be transparent).
> 
>  >
> 
>  > I tried playing with the various gdalbuildvrt (-addalpha, etc..) options
> 
>  > but i still cant figure out what is the correct incantation to achieve
> 
>  > my goal (ie tell gdalbuildvrt that there's some kind of 'cutline' around
> 
>  > the given COGs ?), so i'm resorting to find hints on the list :)
> 
> Landry,
> 
> The behaviour you describe is indeed how VRT stacking works currently. 
> Alpha / masks bands are dealt as regular bands. No blending is done. 

So that explains why it works with the mapserver GROUP keyword, as it 
does the blending of the sub-layers.

> Long time wish / todo.

Ok, good to know.

> Your best option with VRT would be to use a NoData value instead if you can.

You mean setting a nodata value when building the COGs (ie 
gdal_translate -a_nodata 0 -of COG dataset.vrt dataset.tif) instead of 
the alpha/mask band, and then using this nodata value as -srcnodata 0 
parameter to gdalbuildvrt ?

> Or use gdalwarp to create a materialized mosaic.

That would be sub optimal (ie double storage) as i also need to be able 
to use the distinct campaigns independently.

Landry


More information about the gdal-dev mailing list