rio-tiler icon indicating copy to clipboard operation
rio-tiler copied to clipboard

COGReader.tile doesn't take into account mask band in some circumstances

Open drnextgis opened this issue 3 years ago • 13 comments

There is following set of files:

  • masked.tif (with PER_DATASET internal mask)
  • data.vrt built based on masked.tif
  • data.vrt.ovr (external overviews)

Let's try to render a tile out of it:

In [1]: from rio_tiler.io import COGReader

In [2]: with COGReader("/tmp/test/data.vrt") as cog:
   ...:     tile = cog.tile(2323, 3503, 13, tilesize=256)
   ...: 
GDAL: GDALClose(/tmp/test/masked.tif, this=0x3699940)
GDAL: GDALClose(/tmp/test/data.vrt, this=0x3640b10)

In [3]: with open("/tmp/test/tile-ok.png", "wb") as dst:
   ...:     dst.write(tile.render())

Expected result (screenshot from QGIS):

image

Actual result:

tile-ok

Everything is fine.

Now let's try to render tile on a lower zoom level, in this case GDAL will use existing overviews.

In [1]: from rio_tiler.io import COGReader

In [2]: with COGReader("/tmp/test/data.vrt") as cog:
   ...:     tile = cog.tile(72, 109, 8, tilesize=256)
   ...: 
GDAL: GDALClose(/tmp/test/masked.tif, this=0x2f1f7e0)
GDAL: GDALClose(/tmp/test/data.vrt, this=0x20b6740)
GDAL: GDALClose(/tmp/test/data.vrt.ovr, this=0x2e51cc0)

In [3]: with open("/tmp/test/tile-nok.png", "wb") as dst:
   ...:     dst.write(tile.render())
   ...: 

Expected result: image

Actual result: tile-nok

As you can see the mask has not been taken into account.

Files that are being used: example.zip

drnextgis avatar Jul 01 '21 22:07 drnextgis

Make tile with gdal_translate (with mask):

$ gdal_translate -projwin 119974.152936 2855615.99731 265006.012536 2717757.29023 \
-outsize 256 256 data.vrt tile-gdal-mask.tif

1

Make tile with gdal_translate (without mask):

$ gdal_translate -mask none -projwin 119974.152936 2855615.99731 265006.012536 2717757.29023 \
-outsize 256 256 data.vrt tile-gdal-nomask.tif

2

drnextgis avatar Jul 02 '21 04:07 drnextgis

@drnextgis thanks for the issue. can you tell which version of rasterio and GDAL you are using (and also which OS).

I'll investigate, but I've seen this kind of behaviour with some rasterio/gdal combination.

Note: rio-tiler doesn't do much, it might go down to rasterio/gdal

vincentsarago avatar Jul 02 '21 07:07 vincentsarago

Here is my first look: https://gist.github.com/vincentsarago/945fd7bbd34c0603b2d6a55f9dc8d030

I don't see anything wrong 🤷‍♂️

@drnextgis could you produce a bigger file?

vincentsarago avatar Jul 02 '21 08:07 vincentsarago

In [1]: import rasterio, rio_tiler, platform

In [2]: rasterio.__version__
Out[2]: '1.2.6'

In [3]: rio_tiler.__version__
Out[3]: '2.0.8'

In [4]: rasterio.gdal_version()
Out[4]: '3.3.0'

In [5]: platform.system()
Out[5]: 'Linux'

drnextgis avatar Jul 02 '21 08:07 drnextgis

Thank you @vincentsarago for your prompt reply. Maybe I'm missing something but shouldn't these masks be the same?

image

https://gist.github.com/drnextgis/5a25cf2e87b380e034a72c4a150358c6

drnextgis avatar Jul 02 '21 08:07 drnextgis

Yep you are right!

This should give the same result.

vincentsarago avatar Jul 02 '21 08:07 vincentsarago

Oh I see now your data has nodata set to 0 and a internal mask. This creates a conflict in rio-tiler here https://github.com/cogeotiff/rio-tiler/blob/master/rio_tiler/reader.py#L61-L67

vincentsarago avatar Jul 02 '21 08:07 vincentsarago

Ah, good catch! In this case can we follow the same order of precedence as it is done in rasterio?

drnextgis avatar Jul 02 '21 08:07 drnextgis

@drnextgis I think it's a bit more complexe because we are creating a WarpedVRT 🤷‍♂️

I think the least we could do is print a warning when nodata and mask or alpha is present!

vincentsarago avatar Jul 02 '21 08:07 vincentsarago

In WarpedVRT, I don't think you can have a Mask and Nodata

vincentsarago avatar Jul 02 '21 09:07 vincentsarago

There is one thing I fail to understand. If I delete NoData value from the original tif and read it using COGReader I get the correct dataset mask: image

But if I read it through WarpedVRT then I can't see it: image

nmasked.tif.zip

drnextgis avatar Jul 02 '21 10:07 drnextgis

can you try with WarpedVRT(src, add_alpha=True) ?

Screen Shot 2021-07-02 at 12 09 58 PMw

vincentsarago avatar Jul 02 '21 10:07 vincentsarago

It did the trick!

drnextgis avatar Jul 02 '21 10:07 drnextgis