Example #1
0
def test_rgb_out_of_bounds(use_testdb, raster_file):
    import terracotta
    from terracotta.handlers import rgb

    with pytest.raises(terracotta.exceptions.TileOutOfBoundsError) as excinfo:
        rgb.rgb(['val21', 'x'], ['val22', 'val23', 'val24'], (10, 0, 0))
        assert 'data covers less than' not in str(excinfo.value)
Example #2
0
def test_rgb_invalid_keys(use_testdb, raster_file_xyz):
    from terracotta import exceptions
    from terracotta.handlers import rgb

    with pytest.raises(exceptions.InvalidArgumentsError):
        rgb.rgb(['val21', 'x', 'y', 'z'], ['val22', 'val23', 'val24'],
                raster_file_xyz)
Example #3
0
def test_rgb_lowzoom(use_testdb, raster_file, raster_file_xyz_lowzoom):
    import terracotta
    from terracotta.handlers import rgb

    with pytest.raises(terracotta.exceptions.TileOutOfBoundsError) as excinfo:
        rgb.rgb(['val21', 'x'], ['val22', 'val23', 'val24'],
                raster_file_xyz_lowzoom)
        assert 'data covers less than' in str(excinfo.value)
Example #4
0
def test_rgb_invalid_stretch(use_testdb, raster_file_xyz):
    from terracotta import exceptions
    from terracotta.handlers import rgb

    stretch_range = [100, 0]
    ds_keys = ['val21', 'x', 'val22']

    with pytest.raises(exceptions.InvalidArgumentsError):
        rgb.rgb(ds_keys[:2], ['val22', 'val23', 'val24'],
                raster_file_xyz,
                stretch_ranges=[stretch_range] * 3)
Example #5
0
def test_rgb_stretch(stretch_range, use_testdb, testdb, raster_file_xyz):
    import terracotta
    from terracotta.xyz import get_tile_data
    from terracotta.handlers import rgb

    ds_keys = ['val21', 'x', 'val22']

    raw_img = rgb.rgb(ds_keys[:2], ['val22', 'val23', 'val24'],
                      raster_file_xyz,
                      stretch_ranges=[stretch_range] * 3)
    img_data = np.asarray(Image.open(raw_img))[..., 0]

    # get unstretched 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)

    # filter transparent values
    valid_mask = ~tile_data.mask
    assert np.all(img_data[~valid_mask] == 0)

    valid_img = img_data[valid_mask]
    valid_data = tile_data.compressed()

    assert np.all(valid_img[valid_data < stretch_range[0]] == 1)
    stretch_range_mask = (valid_data > stretch_range[0]) & (valid_data <
                                                            stretch_range[1])
    assert np.all(valid_img[stretch_range_mask] >= 1)
    assert np.all(valid_img[stretch_range_mask] <= 255)
    assert np.all(valid_img[valid_data > stretch_range[1]] == 255)
Example #6
0
def test_rgb_handler(use_testdb, raster_file, raster_file_xyz):
    import terracotta
    from terracotta.handlers import rgb
    raw_img = rgb.rgb(['val21', 'x'], ['val22', 'val23', 'val24'],
                      raster_file_xyz)
    img_data = np.asarray(Image.open(raw_img))
    assert img_data.shape == (*terracotta.get_settings().DEFAULT_TILE_SIZE, 3)
Example #7
0
def test_rgb_tile_size(use_testdb, raster_file, raster_file_xyz):
    from terracotta.handlers import rgb
    raw_img = rgb.rgb(['val21', 'x'], ['val22', 'val23', 'val24'],
                      raster_file_xyz,
                      tile_size=(100, 100))
    img_data = np.asarray(Image.open(raw_img))
    assert img_data.shape == (100, 100, 3)
Example #8
0
def _get_rgb_image(keys: str, tile_xyz: Tuple[int, int, int] = None) -> Any:
    from terracotta.handlers.rgb import rgb

    option_schema = RGBOptionSchema()
    options = option_schema.load(request.args)

    some_keys = [key for key in keys.split('/') if key]

    rgb_values = (options.pop('r'), options.pop('g'), options.pop('b'))
    stretch_ranges = tuple(options.pop(k) for k in ('r_range', 'g_range', 'b_range'))

    image = rgb(
        some_keys, rgb_values, stretch_ranges=stretch_ranges, tile_xyz=tile_xyz, **options
    )

    return send_file(image, mimetype='image/png')