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_mapchete_init():
"""Raise TypeError if not MapcheteConfig object is passed."""
with pytest.raises(TypeError):
mapchete.Mapchete("wrong_type")
def test_execute_vrt(mp_tmpdir, cleantopo_br):
"""Using debug output."""
run_cli(["execute", cleantopo_br.path, "-z", "5", "--vrt"])
with mapchete.open(cleantopo_br.dict) as mp:
vrt_path = os.path.join(mp.config.output.path, "5.vrt")
with rasterio.open(vrt_path) as src:
assert src.read().any()
# run again, this time with custom output directory
run_cli(
["execute", cleantopo_br.path, "-z", "5", "--vrt", "--idx-out-dir", mp_tmpdir]
)
with mapchete.open(cleantopo_br.dict) as mp:
vrt_path = os.path.join(mp_tmpdir, "5.vrt")
with rasterio.open(vrt_path) as src:
assert src.read().any()
# run with single tile
run_cli(
["execute", cleantopo_br.path, "-t", "5", "3", "7", "--vrt"]
)
assert isinstance(hillshade.execute(user_process), np.ndarray)
# execute on empty tile
tile = mp.config.process_pyramid.tile(
zoom,
mp.config.process_pyramid.matrix_height(zoom) - 1,
mp.config.process_pyramid.matrix_width(zoom) - 1
)
user_process = mapchete.MapcheteProcess(
tile=tile,
params=mp.config.params_at_zoom(tile.zoom),
input=mp.config.get_inputs_for_tile(tile),
)
assert hillshade.execute(user_process) == "empty"
with mapchete.open(
dict(cleantopo_tl.dict, input=dict(dem=cleantopo_tl_tif, clip=landpoly))
) as mp:
zoom = max(mp.config.zoom_levels)
tile = next(mp.get_process_tiles(zoom))
user_process = mapchete.MapcheteProcess(
tile=tile,
params=mp.config.params_at_zoom(tile.zoom),
input=mp.config.get_inputs_for_tile(tile),
)
assert isinstance(hillshade.execute(user_process), np.ndarray)
# execute on empty tile
tile = mp.config.process_pyramid.tile(
zoom,
mp.config.process_pyramid.matrix_height(zoom) - 1,
mp.config.process_pyramid.matrix_width(zoom) - 1
)
def test_process_template(dummy1_tif, mp_tmpdir):
"""Template used to create an empty process."""
process_template = pkg_resources.resource_filename(
"mapchete.static", "process_template.py")
mp = mapchete.open(
dict(
process=process_template,
pyramid=dict(grid="geodetic"),
input=dict(file1=dummy1_tif),
output=dict(
format="GTiff",
path=mp_tmpdir,
bands=1,
dtype="uint8"
),
config_dir=mp_tmpdir,
zoom_levels=4
))
process_tile = next(mp.get_process_tiles(zoom=4))
# Mapchete throws a RuntimeError if process output is empty
with pytest.raises(MapcheteProcessOutputError):
def test_get_raw_output(example_mapchete):
"""Mapchete get_raw_output() errors."""
with mapchete.open(example_mapchete.path) as mp:
# wrong tile type
with pytest.raises(TypeError):
mp.get_raw_output("invalid")
# not matching CRSes
tile = BufferedTilePyramid("mercator").tile(7, 1, 1)
with pytest.raises(NotImplementedError):
mp.get_raw_output(tile)
def test_get_raw_output_readonly(mp_tmpdir, cleantopo_tl):
"""Get raw process output using readonly flag."""
tile = (5, 0, 0)
readonly_mp = mapchete.open(cleantopo_tl.path, mode="readonly")
write_mp = mapchete.open(cleantopo_tl.path, mode="continue")
# read non-existing data (returns empty)
assert readonly_mp.get_raw_output(tile).mask.all()
# try to process and save empty data
with pytest.raises(ValueError):
readonly_mp.write(tile, readonly_mp.get_raw_output(tile))
# actually process and save
write_mp.write(tile, write_mp.get_raw_output(tile))
# read written output
assert not readonly_mp.get_raw_output(tile).mask.all()
def _run_tiledir_process_raster(conf_dict, metatiling, bounds):
conf = deepcopy(conf_dict)
conf["pyramid"].update(metatiling=metatiling)
with mapchete.open(conf, mode="overwrite", bounds=bounds) as mp:
assert any([
next(iter(mp.config.input.values())).open(tile).read().any()
for tile in mp.get_process_tiles(4)
])
# read empty tile
assert not next(iter(mp.config.input.values())).open(
mp.config.process_pyramid.tile(4, 0, 0)
).read().any()
shutil.rmtree(mp.config.output.path, ignore_errors=True)
def test_process_exception(mp_tmpdir, cleantopo_br, process_error_py):
"""Assert process exception is raised."""
config = cleantopo_br.dict
config.update(process=process_error_py)
with mapchete.open(config) as mp:
with pytest.raises(errors.MapcheteProcessException):
mp.execute((5, 0, 0))
def test_get_raw_output_continue_raster(mp_tmpdir, cleantopo_tl):
"""Get raw process output using continue flag."""
with mapchete.open(cleantopo_tl.path) as mp:
assert mp.config.mode == "continue"
tile = (5, 0, 0)
# process and save
mp.write(tile, mp.get_raw_output(tile))
# read written data
assert not mp.get_raw_output(tile).mask.all()
"""Baselevel interpolation."""
conf = dict(baselevels.dict)
conf.update(
zoom_levels=[7, 8],
baselevels=dict(
min=8,
max=8
)
)
baselevel_tile = (8, 125, 260)
overview_tile = (7, 62, 130)
with mapchete.open(conf, mode="continue") as mp:
tile_bounds = mp.config.output_pyramid.tile(*baselevel_tile).bounds
# process using bounds of just one baselevel tile
with mapchete.open(conf, mode="continue", bounds=tile_bounds) as mp:
mp.batch_process()
with rasterio.open(
mp.config.output.get_path(mp.config.output_pyramid.tile(*overview_tile))
) as src:
overview_before = src.read()
assert overview_before.any()
# process full area which leaves out overview tile for baselevel tile above
with mapchete.open(conf, mode="continue") as mp:
mp.batch_process()
# delete baselevel tile
written_tile = os.path.join(*[
baselevels.dict["config_dir"],
baselevels.dict["output"]["path"],
*map(str, baselevel_tile),