Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_raster_cache_fail(driver_path, provider, raster_file, asynchronous):
"""Retrieve a tile that is larger than the total cache size"""
from terracotta import drivers, update_settings
update_settings(RASTER_CACHE_SIZE=1)
db = drivers.get_driver(driver_path, provider=provider)
keys = ('some', 'keynames')
db.create(keys)
db.insert(['some', 'value'], str(raster_file))
assert len(db._raster_cache) == 0
data1 = db.get_raster_tile(['some', 'value'], tile_size=(256, 256), asynchronous=asynchronous)
if asynchronous:
data1 = data1.result()
time.sleep(1) # allow callback to finish
assert len(db._raster_cache) == 0
def test_bench_rgb(benchmark, zoom, resampling, big_raster_file_nodata, benchmark_database):
from terracotta.server import create_app
from terracotta import update_settings
update_settings(
DRIVER_PATH=str(benchmark_database),
UPSAMPLING_METHOD=resampling,
DOWNSAMPLING_METHOD=resampling
)
zoom_level = ZOOM_XYZ[zoom]
flask_app = create_app()
with flask_app.test_client() as client:
if zoom_level is not None:
x, y, z = get_xyz(big_raster_file_nodata, zoom_level)
rv = benchmark(client.get, f'/rgb/nodata/{z}/{x}/{y}.png?r=1&g=2&b=3')
else:
rv = benchmark(client.get, f'/rgb/nodata/preview.png?r=1&g=2&b=3')
assert rv.status_code == 200
def test_update_config():
from terracotta import get_settings, update_settings
update_settings(DRIVER_PATH='test')
new_settings = get_settings()
assert new_settings.DRIVER_PATH == 'test'
update_settings(DEFAULT_TILE_SIZE=[50, 50])
new_settings = get_settings()
assert new_settings.DRIVER_PATH == 'test' and new_settings.DEFAULT_TILE_SIZE == (50, 50)
def test_bench_singleband(benchmark, zoom, resampling, big_raster_file_nodata, benchmark_database):
from terracotta.server import create_app
from terracotta import update_settings, get_driver
update_settings(
DRIVER_PATH=str(benchmark_database),
UPSAMPLING_METHOD=resampling,
DOWNSAMPLING_METHOD=resampling
)
zoom_level = ZOOM_XYZ[zoom]
flask_app = create_app()
with flask_app.test_client() as client:
if zoom_level is not None:
x, y, z = get_xyz(big_raster_file_nodata, zoom_level)
rv = benchmark(client.get, f'/singleband/nodata/1/{z}/{x}/{y}.png')
else:
rv = benchmark(client.get, f'/singleband/nodata/1/preview.png')
assert rv.status_code == 200
def test_app():
from terracotta import update_settings
update_settings(DEBUG=True)
from terracotta.server.app import app
assert app.debug
def benchmark_database(big_raster_file_nodata, big_raster_file_mask, tmpdir_factory):
from terracotta import get_driver, update_settings
keys = ['type', 'band']
update_settings(RASTER_CACHE_SIZE=0)
dbpath = tmpdir_factory.mktemp('db').join('db-readonly.sqlite')
driver = get_driver(dbpath, provider='sqlite')
driver.create(keys)
mtd = driver.compute_metadata(str(big_raster_file_nodata))
with driver.connect():
driver.insert(['nodata', '1'], str(big_raster_file_nodata), metadata=mtd)
driver.insert(['nodata', '2'], str(big_raster_file_nodata), metadata=mtd)
driver.insert(['nodata', '3'], str(big_raster_file_nodata), metadata=mtd)
driver.insert(['mask', '1'], str(big_raster_file_mask), metadata=mtd)
return dbpath
def test_bench_rgb_out_of_bounds(benchmark, big_raster_file_nodata, benchmark_database):
from terracotta.server import create_app
from terracotta import update_settings
update_settings(DRIVER_PATH=str(benchmark_database))
x, y, z = 0, 0, 20
flask_app = create_app()
with flask_app.test_client() as client:
rv = benchmark(client.get, f'/rgb/nodata/{z}/{x}/{y}.png?r=1&g=2&b=3')
assert rv.status_code == 200
def get_client(metadata_origins=None, tile_origins=None):
from terracotta.server import create_app
import terracotta
if metadata_origins is not None:
terracotta.update_settings(ALLOWED_ORIGINS_METADATA=metadata_origins)
if tile_origins is not None:
terracotta.update_settings(ALLOWED_ORIGINS_TILES=tile_origins)
flask_app = create_app()
return flask_app.test_client()
loglevel: str = None) -> None:
"""The command line interface for the Terracotta tile server.
All flags must be passed before specifying a subcommand.
Example:
$ terracotta -c config.toml connect localhost:5000
"""
if ctx.invoked_subcommand is None:
click.echo(ctx.get_help())
# update settings from config file
if config is not None:
update_settings(**config)
# setup logging
settings = get_settings()
if loglevel is None:
loglevel = settings.LOGLEVEL
logs.set_logger(loglevel, catch_warnings=True)
def serve(database=None, raster_pattern=None, debug=False, profile=False, no_browser=False,
config=None, database_provider=None):
from terracotta import get_driver, update_settings
from terracotta.flask_api import run_app
if config is not None:
update_settings(config)
if (database is None) == (raster_pattern is None):
raise click.UsageError('Either --database or --raster-pattern must be given')
if database is None:
dbfile = tempfile.NamedTemporaryFile(suffix='.sqlite', delete=False)
dbfile.close()
keys, raster_files = raster_pattern
driver = get_driver(dbfile.name, provider='sqlite')
with driver.connect():
driver.create(keys)
for key, filepath in raster_files.items():
driver.insert(key, filepath, compute_metadata=False)
else: