mapbox / rio-mbtiles

A plugin command for the Rasterio CLI that exports a raster dataset to an MBTiles 1.1 SQLite file
MIT License
125 stars 36 forks source link

4 ... out of range for dataset #76

Closed przemyslawpluta closed 3 years ago

przemyslawpluta commented 3 years ago

Running:

rio mbtiles -o file.mbtiles -f WEBP --rgba --zoom-levels 6..15 file.tif

results with error:

Traceback (most recent call last):
  File "/usr/local/bin/rio", line 8, in <module>
    sys.exit(main_group())
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1137, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1062, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1668, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.9/site-packages/click/core.py", line 763, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/mbtiles/scripts/cli.py", line 575, in mbtiles
    process_tiles(
  File "/usr/local/lib/python3.9/site-packages/mbtiles/cf.py", line 53, in process_tiles
    tile, contents = future.result()
  File "/usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/_base.py", line 438, in result
    return self.__get_result()
  File "/usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/concurrent/futures/_base.py", line 390, in __get_result
    raise self._exception
rasterio._err.CPLE_IllegalArgError: panSrcBands[3] = 4 ... out of range for dataset
przemyslawpluta commented 3 years ago

Running WEBP or PNG without --rgba does not fail but produces image with black tiles:

rio mbtiles -o file.mbtiles -f WEBP --zoom-levels 6..15 file.tif
png
przemyslawpluta commented 3 years ago

It looks like rio mbtiles fails as there is no band 4 alpha in the raster. Solution is to create a RGBA dataset from a RGB dataset with an alpha mask before running command but it would be great for rio mbtiles to do it automatically if band 4 alpha is not present.

gdal_translate file.tif file-withalphamask.tif -b 1 -b 2 -b 3 -b mask
sgillies commented 3 years ago

@przemyslawpluta would you be willing to try the 1.6a1 pre-release? It has two changes that you might profit from. To get it: python -m pip install --pre rio-mbtiles.

rio mbtiles -o file.mbtiles -f WEBP --zoom-levels 6..15 --include-empty-tiles --rgba file.tif will automatically create an alpha channel and include empty transparent tiles in file.mbtiles instead of excluding them (the default).

przemyslawpluta commented 3 years ago

@sgillies thanks for an update ... it's great to see how efficient it is but ...

80vcpu

... just run another test against pre-release version and outcome seems to produce tiles with which seems to indicate that alpha mask is not covering entire raster ... places which should be black appears to be cut out. Unfortunately mapbox still returns 404 even with --include-empty-tiles.

rio mbtiles -o file.mbtiles -f WEBP --zoom-levels 6..15 --co QUALITY=95 --include-empty-tiles --rgba file.tif

artefacts

sgillies commented 3 years ago

@przemyslawpluta thanks for trying it out! I'll come up with a fix for the holes.

Version 1.5 of rio-mbtiles would exclude empty tiles from the mbtiles database. For example, if your source dataset had an L-shaped region of valid pixels within a rectangular raster, tiles that were entirely outside the valid data region would be excluded from the mbtiles database. And GL JS would report an error, which could be surprising. However, there's nothing rio-mbtiles can do about GL JS views beyond the extent of the tileset, you have to expect to see 404 errors in that case.

przemyslawpluta commented 3 years ago

@sgillies sounds good

sgillies commented 3 years ago

Version 1.6a2 on PyPI now exports images with no holes.

przemyslawpluta commented 3 years ago

@sgillies just tested 1.6a2 and it looks good. Thanks.