Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with driver.connect():
tile_data = get_tile_data(driver, ds_keys, tile_xyz=raster_file_xyz,
preserve_values=True, tile_size=settings.DEFAULT_TILE_SIZE)
# Get some values from the raster to use for colormap
classes = np.unique(tile_data)
classes = classes[:254]
colormap = {}
for i in range(classes.shape[0]):
val = classes[i]
color = val % 256
colormap[val] = (color, color, color, color)
colormap[nodata] = (100, 100, 100, 100)
raw_img = singleband.singleband(ds_keys, raster_file_xyz, colormap=colormap)
img_data = np.asarray(Image.open(raw_img).convert('RGBA'))
# get unstretched data to compare to
with driver.connect():
tile_data = get_tile_data(driver, ds_keys, tile_xyz=raster_file_xyz,
preserve_values=True, tile_size=img_data.shape[:2])
# check that labels are mapped to colors correctly
for cmap_label, cmap_color in colormap.items():
if cmap_label == nodata:
# make sure nodata is still transparent
assert np.all(img_data[tile_data == cmap_label, -1] == 0)
else:
assert np.all(img_data[tile_data == cmap_label] == np.asarray(cmap_color))
# check that all data outside of labels is transparent
def test_singleband_handler(use_testdb, raster_file_xyz,
resampling_method):
import terracotta
terracotta.update_settings(RESAMPLING_METHOD=resampling_method)
from terracotta.handlers import datasets, singleband
settings = terracotta.get_settings()
ds = datasets.datasets()
for keys in ds:
raw_img = singleband.singleband(keys, raster_file_xyz)
img_data = np.asarray(Image.open(raw_img))
assert img_data.shape == settings.DEFAULT_TILE_SIZE
def test_singleband_noxyz(use_testdb):
from terracotta import get_settings
from terracotta.handlers import singleband
settings = get_settings()
ds_keys = ['val21', 'x', 'val22']
raw_img = singleband.singleband(ds_keys)
img_data = np.asarray(Image.open(raw_img))
assert img_data.shape == settings.DEFAULT_TILE_SIZE
def test_singleband_out_of_bounds(use_testdb):
import terracotta
from terracotta.handlers import datasets, singleband
ds = datasets.datasets()
for keys in ds:
with pytest.raises(terracotta.exceptions.TileOutOfBoundsError):
singleband.singleband(keys, (10, 0, 0))
def test_colormap_consistency(use_testdb, testdb, raster_file_xyz,
stretch_range, cmap_name):
"""Test consistency between /colormap and images returned by /singleband"""
import terracotta
from terracotta.xyz import get_tile_data
from terracotta.handlers import singleband, colormap
ds_keys = ['val21', 'x', 'val22']
# get image with applied stretch and colormap
raw_img = singleband.singleband(ds_keys, raster_file_xyz, stretch_range=stretch_range,
colormap=cmap_name)
img_data = np.asarray(Image.open(raw_img).convert('RGBA'))
# get raw data to compare to
driver = terracotta.get_driver(testdb)
with driver.connect():
tile_data = get_tile_data(driver, ds_keys, tile_xyz=raster_file_xyz,
tile_size=img_data.shape[:2])
# make sure all pixel values are included in colormap
num_values = stretch_range[1] - stretch_range[0] + 1
# get colormap for given stretch
cmap = colormap.colormap(colormap=cmap_name, stretch_range=stretch_range,
num_values=num_values)
def _get_singleband_image(keys: str, tile_xyz: Tuple[int, int, int] = None) -> Any:
from terracotta.handlers.singleband import singleband
parsed_keys = [key for key in keys.split('/') if key]
option_schema = SinglebandOptionSchema()
options = option_schema.load(request.args)
if options.get('colormap', '') == 'explicit':
options['colormap'] = options.pop('explicit_color_map')
image = singleband(parsed_keys, tile_xyz=tile_xyz, **options)
return send_file(image, mimetype='image/png')