def test_index_gpkg(mp_tmpdir, cleantopo_br): # execute process MapcheteCLI([None, 'execute', cleantopo_br.path, '-z', '5', '--debug']) # generate index MapcheteCLI( [None, 'index', cleantopo_br.path, '-z', '5', '--gpkg', '--debug']) with mapchete.open(cleantopo_br.dict) as mp: files = os.listdir(mp.config.output.path) assert "5.gpkg" in files with fiona.open(os.path.join(mp.config.output.path, "5.gpkg")) as src: for f in src: assert "location" in f["properties"] assert len(list(src)) == 1 # write again and assert there is no new entry because there is already one MapcheteCLI( [None, 'index', cleantopo_br.path, '-z', '5', '--gpkg', '--debug']) with mapchete.open(cleantopo_br.dict) as mp: files = os.listdir(mp.config.output.path) assert "5.gpkg" in files with fiona.open(os.path.join(mp.config.output.path, "5.gpkg")) as src: for f in src: assert "location" in f["properties"] assert len(list(src)) == 1
def test_index_text(mp_tmpdir, cleantopo_br): # execute process MapcheteCLI([None, 'execute', cleantopo_br.path, '-z', '5', '--debug']) # generate index MapcheteCLI( [None, 'index', cleantopo_br.path, '-z', '5', '--txt', '--debug']) with mapchete.open(cleantopo_br.dict) as mp: files = os.listdir(mp.config.output.path) assert "5.txt" in files with open(os.path.join(mp.config.output.path, "5.txt")) as src: lines = list(src) assert len(lines) == 1 for l in lines: assert l.endswith("7.tif\n") # write again and assert there is no new entry because there is already one MapcheteCLI( [None, 'index', cleantopo_br.path, '-z', '5', '--txt', '--debug']) with mapchete.open(cleantopo_br.dict) as mp: files = os.listdir(mp.config.output.path) assert "5.txt" in files with open(os.path.join(mp.config.output.path, "5.txt")) as src: lines = list(src) assert len(lines) == 1 for l in lines: assert l.endswith("7.tif\n")
def test_pyramid_zoom(): """Automatic tile pyramid creation using a specific zoom.""" test_raster = os.path.join(SCRIPTDIR, "testdata/cleantopo_br.tif") try: MapcheteCLI([None, 'pyramid', test_raster, OUT_DIR, "-z", "3"]) for zoom, row, col in [(4, 15, 15), (2, 3, 0)]: out_file = os.path.join( *[OUT_DIR, str(zoom), str(row), str(col) + ".tif"]) assert not os.path.isfile(out_file) finally: shutil.rmtree(OUT_DIR, ignore_errors=True) try: MapcheteCLI([None, 'pyramid', test_raster, OUT_DIR, "-z", "3", "4"]) for zoom, row, col in [(2, 3, 0)]: out_file = os.path.join( *[OUT_DIR, str(zoom), str(row), str(col) + ".tif"]) assert not os.path.isfile(out_file) finally: shutil.rmtree(OUT_DIR, ignore_errors=True) try: MapcheteCLI([None, 'pyramid', test_raster, OUT_DIR, "-z", "4", "3"]) for zoom, row, col in [(2, 3, 0)]: out_file = os.path.join( *[OUT_DIR, str(zoom), str(row), str(col) + ".tif"]) assert not os.path.isfile(out_file) finally: shutil.rmtree(OUT_DIR, ignore_errors=True)
def test_execute_multiprocessing(mp_tmpdir, cleantopo_br, cleantopo_br_tif): """Run mapchete execute with multiple workers.""" temp_mapchete = os.path.join(mp_tmpdir, "temp.mapchete") temp_process = os.path.join(mp_tmpdir, "temp.py") out_format = "GTiff" # create from template args = [ None, 'create', temp_mapchete, temp_process, out_format, "--pyramid_type", "geodetic"] MapcheteCLI(args) # edit configuration with open(temp_mapchete, "r") as config_file: config = yaml.load(config_file) config["output"].update(bands=1, dtype="uint8", path=".") with open(temp_mapchete, "w") as config_file: config_file.write(yaml.dump(config, default_flow_style=False)) # run process with multiprocessing args = [ None, 'execute', temp_mapchete, '--zoom', '5', '--input_file', cleantopo_br_tif] with pytest.raises(MapcheteProcessOutputError): MapcheteCLI(args) # run example process with multiprocessing args = [None, 'execute', cleantopo_br.path, '--zoom', '5'] MapcheteCLI(args)
def test_formats(capfd): """Output of mapchete formats command.""" MapcheteCLI([None, 'formats']) err = capfd.readouterr()[1] assert not err MapcheteCLI([None, 'formats', '-i']) err = capfd.readouterr()[1] assert not err MapcheteCLI([None, 'formats', '-o']) err = capfd.readouterr()[1] assert not err
def test_create_existing(mp_tmpdir): """Run mapchete create and execute.""" temp_mapchete = os.path.join(mp_tmpdir, "temp.mapchete") temp_process = os.path.join(mp_tmpdir, "temp.py") out_format = "GTiff" # create files from template args = [ None, 'create', temp_mapchete, temp_process, out_format, "--pyramid_type", "geodetic"] MapcheteCLI(args) # try to create again with pytest.raises(IOError): MapcheteCLI(args)
def test_index_geojson(mp_tmpdir, cleantopo_br): # execute process at zoom 3 MapcheteCLI([None, 'execute', cleantopo_br.path, '-z', '3', '--debug']) # generate index for zoom 3 MapcheteCLI( [None, 'index', cleantopo_br.path, '-z', '3', '--geojson', '--debug']) with mapchete.open(cleantopo_br.dict) as mp: files = os.listdir(mp.config.output.path) assert len(files) == 2 assert "3.geojson" in files with fiona.open(os.path.join(mp.config.output.path, "3.geojson")) as src: for f in src: assert "location" in f["properties"] assert len(list(src)) == 1
def test_index_geojson_tile(mp_tmpdir, cleantopo_tl): # execute process for single tile MapcheteCLI( [None, 'execute', cleantopo_tl.path, '-t', '3', '0', '0', '--debug']) # generate index MapcheteCLI([ None, 'index', cleantopo_tl.path, '-t', '3', '0', '0', '--geojson', '--debug' ]) with mapchete.open(cleantopo_tl.dict) as mp: files = os.listdir(mp.config.output.path) assert len(files) == 2 assert "3.geojson" in files with fiona.open(os.path.join(mp.config.output.path, "3.geojson")) as src: assert len(list(src)) == 1
def test_execute_debug(mp_tmpdir, example_mapchete): """Using debug output.""" args = [ None, 'execute', example_mapchete.path, "-t", "10", "500", "1040", "--debug" ] MapcheteCLI(args)
def test_execute_verbose(mp_tmpdir, example_mapchete): """Using verbose output.""" args = [ None, 'execute', example_mapchete.path, "-t", "10", "500", "1040", "--verbose" ] MapcheteCLI(args)
def test_index_geojson_basepath(mp_tmpdir, cleantopo_br): # execute process at zoom 3 MapcheteCLI([None, 'execute', cleantopo_br.path, '-z', '3', '--debug']) basepath = 'http://localhost' # index and rename "location" to "new_fieldname" MapcheteCLI([ None, 'index', cleantopo_br.path, '-z', '3', '--geojson', '--debug', '--basepath', basepath ]) with mapchete.open(cleantopo_br.dict) as mp: files = os.listdir(mp.config.output.path) assert "3.geojson" in files with fiona.open(os.path.join(mp.config.output.path, "3.geojson")) as src: for f in src: assert f["properties"]["location"].startswith(basepath) assert len(list(src)) == 1
def test_pyramid_zoom_maxmin(cleantopo_br_tif, mp_tmpdir): """Automatic tile pyramid creation using max min zoom.""" MapcheteCLI([ None, 'pyramid', cleantopo_br_tif, mp_tmpdir, "-z", "4", "3"]) for zoom, row, col in [(2, 3, 0)]: out_file = os.path.join( *[mp_tmpdir, str(zoom), str(row), str(col)+".tif"]) assert not os.path.isfile(out_file)
def test_index_geojson_wkt_geom(mp_tmpdir, cleantopo_br, wkt_geom): # execute process at zoom 3 MapcheteCLI([ None, 'execute', cleantopo_br.path, '--debug', "--wkt_geometry", wkt_geom ]) # generate index for zoom 3 MapcheteCLI([ None, 'index', cleantopo_br.path, '--geojson', '--debug', "--wkt_geometry", wkt_geom ]) with mapchete.open(cleantopo_br.dict) as mp: files = os.listdir(mp.config.output.path) assert len(files) == 6 assert "3.geojson" in files
def test_execute_point(mp_tmpdir, example_mapchete, wkt_geom): """Using bounds from WKT.""" g = wkt.loads(wkt_geom) args = [ None, 'execute', example_mapchete.path, "--point", str(g.centroid.x), str(g.centroid.y) ] MapcheteCLI(args)
def test_serve_cli_params(cleantopo_br): """Test whether different CLI params pass.""" # assert too few arguments error with pytest.raises(SystemExit): MapcheteCLI([None, 'serve'], _test_serve=True) for args in [ [None, 'serve', cleantopo_br.path], [None, 'serve', cleantopo_br.path, "--port", "5001"], [None, 'serve', cleantopo_br.path, "--internal_cache", "512"], [None, 'serve', cleantopo_br.path, "--zoom", "5"], [None, 'serve', cleantopo_br.path, "--bounds", "-1", "-1", "1", "1"], [None, 'serve', cleantopo_br.path, "--overwrite"], [None, 'serve', cleantopo_br.path, "--readonly"], [None, 'serve', cleantopo_br.path, "--memory"], [None, 'serve', cleantopo_br.path, "--input_file", cleantopo_br.path], ]: MapcheteCLI(args, _test_serve=True)
def test_execute_multiprocessing(): """Run mapchete execute with multiple workers.""" temp_mapchete = "temp.mapchete" temp_process = "temp.py" out_format = "GTiff" try: # create from template args = [ None, 'create', temp_mapchete, temp_process, out_format, "--pyramid_type", "geodetic" ] MapcheteCLI(args) # edit configuration with open(temp_mapchete, "r") as config_file: config = yaml.load(config_file) config["output"].update(bands=1, dtype="uint8", path=".") with open(temp_mapchete, "w") as config_file: config_file.write(yaml.dump(config, default_flow_style=False)) # run process with multiprocessing input_file = os.path.join(SCRIPTDIR, "testdata/cleantopo_br.tif") args = [ None, 'execute', temp_mapchete, '--zoom', '6', '--input_file', input_file ] try: MapcheteCLI(args) except MapcheteProcessOutputError: pass # run example process with multiprocessing args = [ None, 'execute', os.path.join(SCRIPTDIR, "testdata/cleantopo_br.mapchete"), '--zoom', '8' ] MapcheteCLI(args) finally: delete_files = [temp_mapchete, temp_process, "temp.pyc", "temp.log"] for delete_file in delete_files: try: os.remove(delete_file) except OSError: pass shutil.rmtree(OUT_DIR, ignore_errors=True)
def test_execute_logfile(mp_tmpdir, example_mapchete): """Using logfile.""" logfile = os.path.join(mp_tmpdir, "temp.log") args = [ None, 'execute', example_mapchete.path, "-t", "10", "500", "1040", "--logfile", logfile ] MapcheteCLI(args) assert os.path.isfile(logfile) with open(logfile) as log: assert "DEBUG" in log.read()
def test_pyramid_png(cleantopo_br_tif, mp_tmpdir): """Automatic PNG tile pyramid creation of raster files.""" MapcheteCLI([None, 'pyramid', cleantopo_br_tif, mp_tmpdir, "-of", "PNG"]) for zoom, row, col in [(4, 15, 15), (3, 7, 7)]: out_file = os.path.join( *[mp_tmpdir, str(zoom), str(row), str(col)+".png"]) with rasterio.open(out_file, "r") as src: assert src.meta["driver"] == "PNG" assert src.meta["dtype"] == "uint8" data = src.read(masked=True) assert data.mask.any()
def test_pyramid_geodetic(cleantopo_br_tif, mp_tmpdir): """Automatic geodetic tile pyramid creation of raster files.""" MapcheteCLI([ None, 'pyramid', cleantopo_br_tif, mp_tmpdir, "-pt", "geodetic"]) for zoom, row, col in [(4, 15, 31), (3, 7, 15), (2, 3, 7), (1, 1, 3)]: out_file = os.path.join( *[mp_tmpdir, str(zoom), str(row), str(col)+".tif"]) with rasterio.open(out_file, "r") as src: assert src.meta["driver"] == "GTiff" assert src.meta["dtype"] == "uint16" data = src.read(masked=True) assert data.mask.any()
def test_pyramid_dtype(cleantopo_br_tif, mp_tmpdir): """Automatic tile pyramid creation using dtype scale.""" MapcheteCLI([ None, 'pyramid', cleantopo_br_tif, mp_tmpdir, "-s", "dtype_scale"]) for zoom, row, col in [(4, 15, 15), (3, 7, 7)]: out_file = os.path.join( *[mp_tmpdir, str(zoom), str(row), str(col)+".tif"]) with rasterio.open(out_file, "r") as src: assert src.meta["driver"] == "GTiff" assert src.meta["dtype"] == "uint16" data = src.read(masked=True) assert data.mask.any()
def test_serve_cli_params(): """Test whether different CLI params pass.""" # assert too few arguments error try: MapcheteCLI([None, 'serve'], _test_serve=True) except SystemExit as exit_code: assert exit_code.message == 2 example_process = os.path.join(SCRIPTDIR, "testdata/cleantopo_br.mapchete") for args in [ [None, 'serve', example_process], [None, 'serve', example_process, "--port", "5001"], [None, 'serve', example_process, "--internal_cache", "512"], [None, 'serve', example_process, "--zoom", "5"], [None, 'serve', example_process, "--bounds", "-1", "-1", "1", "1"], [None, 'serve', example_process, "--overwrite"], [None, 'serve', example_process, "--readonly"], [None, 'serve', example_process, "--memory"], [None, 'serve', example_process, "--input_file", example_process], ]: MapcheteCLI(args, _test_serve=True)
def test_create_existing(): """Run mapchete create and execute.""" temp_mapchete = "temp.mapchete" temp_process = "temp.py" out_format = "GTiff" # create files from template args = [ None, 'create', temp_mapchete, temp_process, out_format, "--pyramid_type", "geodetic" ] MapcheteCLI(args) # try to create again try: MapcheteCLI(args) except IOError: pass finally: delete_files = [temp_mapchete, temp_process] for delete_file in delete_files: try: os.remove(delete_file) except OSError: pass shutil.rmtree(OUT_DIR, ignore_errors=True)
def test_pyramid_png(): """Automatic PNG tile pyramid creation of raster files.""" test_raster = os.path.join(SCRIPTDIR, "testdata/cleantopo_br.tif") try: MapcheteCLI([None, 'pyramid', test_raster, OUT_DIR, "-of", "PNG"]) for zoom, row, col in [(4, 15, 15), (3, 7, 7)]: out_file = os.path.join( *[OUT_DIR, str(zoom), str(row), str(col) + ".png"]) with rasterio.open(out_file, "r") as src: assert src.meta["driver"] == "PNG" assert src.meta["dtype"] == "uint8" data = src.read(masked=True) assert data.mask.any() finally: shutil.rmtree(OUT_DIR, ignore_errors=True)
def test_pyramid_crop(): """Automatic tile pyramid creation cropping data.""" test_raster = os.path.join(SCRIPTDIR, "testdata/cleantopo_br.tif") try: MapcheteCLI([None, 'pyramid', test_raster, OUT_DIR, "-s", "crop"]) for zoom, row, col in [(4, 15, 15), (3, 7, 7)]: out_file = os.path.join( *[OUT_DIR, str(zoom), str(row), str(col) + ".tif"]) with rasterio.open(out_file, "r") as src: assert src.meta["driver"] == "GTiff" assert src.meta["dtype"] == "uint16" data = src.read(masked=True) assert data.mask.any() assert np.all(np.where(data <= 255, True, False)) finally: shutil.rmtree(OUT_DIR, ignore_errors=True)
def test_index_errors(mp_tmpdir, cleantopo_br): with pytest.raises(ValueError): MapcheteCLI([None, 'index', cleantopo_br.path, '-z', '5', '--debug'])
def test_execute_wkt_bounds(mp_tmpdir, example_mapchete, wkt_geom): """Using bounds from WKT.""" args = [None, 'execute', example_mapchete.path, "--wkt_geometry", wkt_geom] MapcheteCLI(args)