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

Styling issues with nodata from #892 #911

Closed
banesullivan opened this issue Aug 4, 2022 · 2 comments · Fixed by #914
Closed

Styling issues with nodata from #892 #911

banesullivan opened this issue Aug 4, 2022 · 2 comments · Fixed by #914

Comments

@banesullivan
Copy link
Contributor

banesullivan commented Aug 4, 2022

#892 broke a test in django-large-image where nodata is used in the styling

import large_image
path = ...
src = large_image.open(path, projection="EPSG:3857", 
    style={'bands': [{'band': 1, 'max': '100', 'min': '5', 'nodata': '0'}]}
)
src.getThumbnail()
/Users/bane/Software/kw/large_image/large_image/tilesource/base.py:1186: FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
  keep = band != nodata
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [4], in <cell line: 1>()
----> 1 src.getThumbnail()

File ~/Software/kw/large_image/large_image/cache_util/cache.py:89, in methodcache.<locals>.decorator.<locals>.wrapper(self, *args, **kwargs)
     86 except ValueError:
     87     # this can happen if a different version of python wrote the record
     88     pass
---> 89 v = func(self, *args, **kwargs)
     90 try:
     91     if lock:

File ~/Software/kw/large_image/sources/gdal/large_image_source_gdal/__init__.py:1006, in GDALFileTileSource.getThumbnail(self, width, height, **kwargs)
   1004     params['output'] = {'maxWidth': width, 'maxHeight': height}
   1005     params['region'] = {'units': 'projection'}
-> 1006     return self.getRegion(**params)
   1007 return super().getThumbnail(width, height, **kwargs)

File ~/Software/kw/large_image/sources/gdal/large_image_source_gdal/__init__.py:1166, in GDALFileTileSource.getRegion(self, format, **kwargs)
   1160 useGDALWarp = (
   1161     iterInfo and
   1162     not self._jsonstyle and
   1163     TILE_FORMAT_IMAGE in format and
   1164     kwargs.get('encoding') == 'TILED')
   1165 if not useGDALWarp:
-> 1166     return super().getRegion(format, **kwargs)
   1167 srs = self.projection or self.getProj4String()
   1168 tl = self.pixelToProjection(
   1169     iterInfo['region']['left'], iterInfo['region']['top'], iterInfo['level'])

File ~/Software/kw/large_image/large_image/tilesource/base.py:1754, in TileSource.getRegion(self, format, **kwargs)
   1751 image = None
   1752 for tile in self._tileIterator(iterInfo):
   1753     # Add each tile to the image
-> 1754     subimage, _ = _imageToNumpy(tile['tile'])
   1755     x0, y0 = tile['x'] - left, tile['y'] - top
   1756     if x0 < 0:

File ~/Software/kw/large_image/large_image/tilesource/tiledict.py:164, in LazyTileDict.__getitem__(self, key, *args, **kwargs)
    161 self.loaded = True
    163 if not self.retile:
--> 164     tileData = self.source.getTile(
    165         self.x, self.y, self.level,
    166         pilImageAllowed=True, numpyAllowed=True,
    167         sparseFallback=True, frame=self.frame)
    168     if self.crop:
    169         tileData, _ = _imageToNumpy(tileData)

File ~/Software/kw/large_image/large_image/cache_util/cache.py:89, in methodcache.<locals>.decorator.<locals>.wrapper(self, *args, **kwargs)
     86 except ValueError:
     87     # this can happen if a different version of python wrote the record
     88     pass
---> 89 v = func(self, *args, **kwargs)
     90 try:
     91     if lock:

File ~/Software/kw/large_image/sources/gdal/large_image_source_gdal/__init__.py:797, in GDALFileTileSource.getTile(self, x, y, z, pilImageAllowed, numpyAllowed, **kwargs)
    795 if len(tile.shape) == 3:
    796     tile = numpy.rollaxis(tile, 0, 3)
--> 797 return self._outputTile(tile, TILE_FORMAT_NUMPY, x, y, z,
    798                         pilImageAllowed, numpyAllowed, **kwargs)

File ~/Software/kw/large_image/large_image/tilesource/base.py:1296, in TileSource._outputTile(self, tile, tileEncoding, x, y, z, pilImageAllowed, numpyAllowed, applyStyle, **kwargs)
   1293 mode = None
   1294 if (numpyAllowed == 'always' or tileEncoding == TILE_FORMAT_NUMPY or
   1295         (applyStyle and getattr(self, 'style', None)) or isEdge):
-> 1296     tile, mode = self._outputTileNumpyStyle(
   1297         tile, applyStyle, x, y, z, self._getFrame(**kwargs))
   1298 if isEdge:
   1299     contentWidth = min(self.tileWidth,
   1300                        sizeX - (maxX - self.tileWidth))

File ~/Software/kw/large_image/large_image/tilesource/base.py:1254, in TileSource._outputTileNumpyStyle(self, tile, applyStyle, x, y, z, frame)
   1252     with self._styleLock:
   1253         if not getattr(self, '_skipStyle', False):
-> 1254             tile = self._applyStyle(tile, self.style, x, y, z, frame)
   1255 if tile.shape[0] != self.tileHeight or tile.shape[1] != self.tileWidth:
   1256     extend = numpy.zeros(
   1257         (self.tileHeight, self.tileWidth, tile.shape[2]),
   1258         dtype=tile.dtype)

File ~/Software/kw/large_image/large_image/tilesource/base.py:1224, in TileSource._applyStyle(self, image, style, x, y, z, frame)
   1222 else:
   1223     if not eidx:
-> 1224         output[:keep.shape[0], :keep.shape[1], channel] = numpy.where(keep, clrs, 0)
   1225     else:
   1226         output[:keep.shape[0], :keep.shape[1], channel] = numpy.maximum(
   1227             output[:keep.shape[0], :keep.shape[1], channel],
   1228             numpy.where(keep, clrs, 0))

AttributeError: 'bool' object has no attribute 'shape'
@manthey
Copy link
Member

manthey commented Aug 5, 2022

This occurs when nodata is passed as a string rather than a number. It is fixed in #914

@banesullivan
Copy link
Contributor Author

Thanks for figuring this out. I remember in the past having issues with sending styling params as strings vs numeric types and strings seemed the most stable a while back.

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

Successfully merging a pull request may close this issue.

2 participants