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

Improve error message for missing 'bidx' parameter #1031

Closed
drnextgis opened this issue Nov 16, 2024 · 4 comments
Closed

Improve error message for missing 'bidx' parameter #1031

drnextgis opened this issue Nov 16, 2024 · 4 comments

Comments

@drnextgis
Copy link
Contributor

It appears that for rasters with 3 bands, specifying bidx is not mandatory. However, when dealing with rasters that have more than 3 bands, such as the following example:

$ gdal_create -of GTiff -ot Byte -bands 5 -outsize 256 256 -a_srs epsg:4326 -a_ullr 2 50 3 49 dummy_256x256_5bands.tif

If you query a tile without specifying the bidx parameter:

http://localhost:8000/cog/tiles/WebMercatorQuad/0/0/0?scale=1&url=http://0.0.0.0:9000/dummy_256x256_5bands.tif

titiler returns an error that is difficult to interpret:

{ "detail": "Pointer 'hSrcDS' is NULL in 'GDALCreateCopy'." }

This error message does not clearly indicate what went wrong. It would be more helpful if the error explicitly mentioned that the bidx parameter must be specified when working with rasters containing more than 3 bands.

@vincentsarago
Copy link
Member

It might be pretty hard to surface better error message, It's Sad that this doesn't come from GDAL directly.

That's said we can maybe aad a set of if test in rio_tiler.utils.render to check the band number for each format 😬

from rio_tiler.models import ImageData
import numpy

arr = numpy.ma.zeros((5,266,256), dtype="uint8")
img = ImageData(arr)
img.render()
---------------------------------------------------------------------------
ObjectNullError                           Traceback (most recent call last)
<ipython-input-5-1ac762dfbb8f> in <cell line: 1>()
----> 1 img.render()

~/Dev/CogeoTiff/rio-tiler/rio_tiler/models.py in render(self, add_mask, img_format, colormap, **kwargs)
    705 
    706         if add_mask:
--> 707             return render(
    708                 array.data,
    709                 self.mask,  # We use dataset mask for rendering

~/Dev/CogeoTiff/rio-tiler/rio_tiler/utils.py in render(data, mask, img_format, colormap, **creation_options)
    606                     dst.write(mask.astype(data.dtype), indexes=count + 1)
    607 
--> 608             return memfile.read()
    609 
    610 

~/Dev/venv/py39/lib/python3.9/site-packages/rasterio/io.py in __exit__(self, *args)
    166 
    167     def __exit__(self, *args):
--> 168         self._env.close()
    169         self.close()
    170 

/opt/homebrew/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/contextlib.py in close(self)
    538     def close(self):
    539         """Immediately unwind the context stack."""
--> 540         self.__exit__(None, None, None)
    541 
    542 

/opt/homebrew/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/contextlib.py in __exit__(self, *exc_details)
    530                 # set-up context
    531                 fixed_ctx = exc_details[1].__context__
--> 532                 raise exc_details[1]
    533             except BaseException:
    534                 exc_details[1].__context__ = fixed_ctx

/opt/homebrew/Cellar/python@3.9/3.9.19/Frameworks/Python.framework/Versions/3.9/lib/python3.9/contextlib.py in __exit__(self, *exc_details)
    515             assert is_sync
    516             try:
--> 517                 if cb(*exc_details):
    518                     suppressed_exc = True
    519                     pending_raise = False

rasterio/_base.pyx in rasterio._base.DatasetBase.__exit__()
rasterio/_base.pyx in rasterio._base.DatasetBase.close()
rasterio/_io.pyx in rasterio._io.BufferedDatasetWriterBase.stop()
rasterio/_err.pyx in rasterio._err.exc_wrap_pointer()

ObjectNullError: Pointer 'hSrcDS' is NULL in 'GDALCreateCopy'. 

@vincentsarago
Copy link
Member

@drnextgis I've update rio-tiler to make the error a bit more verbose 🙏

@drnextgis
Copy link
Contributor Author

drnextgis commented Nov 19, 2024

Thank you @vincentsarago! The error message is now slightly clearer, although it's still challenging to pinpoint the exact issue just by looking at it:

{detail: "Could not encode array of shape (5,256,256) and of datatype `uint8` using PNG driver"}

That said, having this ticket is good - if anyone encounters the same problem, it could be a useful reference.

@vincentsarago
Copy link
Member

although it's still challenging to pinpoint the exact issue just by looking at it

Yeah I'm just a bit worry to go further, I would hope GDAL/Rasterio were raising better error messages

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

No branches or pull requests

2 participants