Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
drnextgis opened this issue Jul 1, 2021 · 13 comments
Open

Comments

@drnextgis
Copy link

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
Copy link
Author

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

@vincentsarago
Copy link
Member

vincentsarago commented Jul 2, 2021

@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
Copy link
Member

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

I don't see anything wrong 🤷‍♂️

@drnextgis could you produce a bigger file?

@drnextgis
Copy link
Author

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
Copy link
Author

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

@vincentsarago
Copy link
Member

Yep you are right!

This should give the same result.

@vincentsarago
Copy link
Member

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

@drnextgis
Copy link
Author

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

@vincentsarago
Copy link
Member

@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
Copy link
Member

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

@drnextgis
Copy link
Author

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

@vincentsarago
Copy link
Member

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

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

@drnextgis
Copy link
Author

It did the trick!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants