def test_shapes_encoder(): """Test MVT encoder.""" asset = os.path.join(os.path.dirname(__file__), "fixtures", "cog_classes.tif") x, y, z = 814, 1750, 12 with COGReader(asset) as cog: tile, mask = cog.tile(x, y, z, resampling_method="nearest", tilesize=256) # test with default vt = shapes_encoder(tile[0], mask) mvt = vector_tile_base.VectorTile(vt) assert len(mvt.layers) == 1 layer = mvt.layers[0] assert layer.name == "my_layer" assert layer.extent == 4096 assert layer.version == 2 with COGReader(asset) as cog: tile, mask = cog.tile(x, y, z, resampling_method="nearest", tilesize=4096) colormap = cog.colormap classes = {0: "none", 1: "grass", 2: "urban", 18: "something"} # test with default vt = shapes_encoder(tile[0], mask) mvt = vector_tile_base.VectorTile(vt) assert len(mvt.layers) == 1 layer = mvt.layers[0] assert layer.name == "my_layer" assert layer.extent == 4096 assert layer.version == 2 feat = layer.features[0] assert feat.type == "polygon" props = feat.attributes assert len(props) == 1 assert props["value"] vt = shapes_encoder(tile[0], mask, colormap=colormap, class_names=classes) mvt = vector_tile_base.VectorTile(vt) assert len(mvt.layers) == 1 layer = mvt.layers[0] assert layer.name == "my_layer" assert layer.extent == 4096 assert layer.version == 2 feat = layer.features[0] assert feat.type == "polygon" props = feat.attributes assert len(props) == 2 assert props["color"] == "#4c70a3" assert props["name"] == "something"
def test_pixels_encoder(): """Test MVT encoder.""" asset = os.path.join(os.path.dirname(__file__), "fixtures", "test.tif") x = 72 y = 63 z = 7 with COGReader(asset) as cog: tile, mask = cog.tile(x, y, z, resampling_method="nearest") # test with default vt = pixels_encoder(tile, mask) mvt = vector_tile_base.VectorTile(vt) assert len(mvt.layers) == 1 layer = mvt.layers[0] assert layer.name == "my_layer" assert layer.extent == 4096 assert layer.version == 2 assert len(layer.features) == 75 feat = layer.features[0] assert feat.type == "point" props = feat.attributes assert len(props) == 1 assert props["band1"] == "21.08714485168457" # Test polygon vt = pixels_encoder(tile, mask, feature_type="polygon") mvt = vector_tile_base.VectorTile(vt) layer = mvt.layers[0] feat = layer.features[0] assert feat.type == "polygon" props = feat.attributes assert props["band1"] == "21.08714485168457" # Test band name vt = pixels_encoder(tile, mask, band_names=["pop"]) mvt = vector_tile_base.VectorTile(vt) props = mvt.layers[0].features[0].attributes assert props["pop"] == "21.08714485168457" # Test layer name vt = pixels_encoder(tile, mask, layer_name="facebook") mvt = vector_tile_base.VectorTile(vt) assert len(mvt.layers) == 1 layer = mvt.layers[0] assert layer.name == "facebook" with pytest.warns(DeprecationWarning): vt = encoder(tile, mask) mvt = vector_tile_base.VectorTile(vt) assert len(mvt.layers) == 1 # Test bad feature type with pytest.raises(Exception): pixels_encoder(tile, mask, feature_type="somethingelse")
def test_API_Vtiles(event): """Test /tiles.pbf route.""" # test missing url in queryString event["path"] = f"/tiles/12/2161/2047.pbf" event["httpMethod"] = "GET" res = APP(event, {}) assert res["statusCode"] == 500 headers = res["headers"] assert headers["Content-Type"] == "application/json" body = json.loads(res["body"]) assert "url" in body["errorMessage"] event["path"] = f"/tiles/12/2161/2047.pbf" event["httpMethod"] = "GET" event["queryStringParameters"] = {"url": file_lidar} res = APP(event, {}) assert res["statusCode"] == 200 headers = res["headers"] assert headers["Content-Type"] == "application/x-protobuf" assert res["body"] assert res["isBase64Encoded"] body = base64.b64decode(res["body"]) vt = vector_tile_base.VectorTile(body) props = vt.layers[0].features[0].properties assert props["min"] assert props["min"] == "11.0" event["path"] = f"/tiles/12/2161/2047.pbf" event["httpMethod"] = "GET" event["queryStringParameters"] = { "url": file_lidar, "nodata": "-9999", "feature_type": "polygon" } res = APP(event, {}) assert res["statusCode"] == 200 headers = res["headers"] assert headers["Content-Type"] == "application/x-protobuf" assert res["body"] assert res["isBase64Encoded"] body = base64.b64decode(res["body"]) vt = vector_tile_base.VectorTile(body) props = vt.layers[0].features[0].properties assert props["min"] assert props["max"] assert props["mean"] assert props["idw"]
def test_mvt_encoder(): """Test MVT encoder.""" # test with default vt = encoder(tile, mask) mvt = vector_tile_base.VectorTile(vt) assert len(mvt.layers) == 1 layer = mvt.layers[0] assert layer.name == "my_layer" assert layer.extent == 4096 assert layer.version == 2 assert len(layer.features) == 75 feat = layer.features[0] assert feat.type == "point" props = feat.properties assert len(props) == 1 assert props["band1"] == "21.08714485168457" # Test polygon vt = encoder(tile, mask, feature_type="polygon") mvt = vector_tile_base.VectorTile(vt) layer = mvt.layers[0] feat = layer.features[0] assert feat.type == "polygon" props = feat.properties assert props["band1"] == "21.08714485168457" # Test band name vt = encoder(tile, mask, band_names=["pop"]) mvt = vector_tile_base.VectorTile(vt) props = mvt.layers[0].features[0].properties assert props["pop"] == "21.08714485168457" # Test layer name vt = encoder(tile, mask, layer_name="facebook") mvt = vector_tile_base.VectorTile(vt) assert len(mvt.layers) == 1 layer = mvt.layers[0] assert layer.name == "facebook" # Test bad feature type with pytest.raises(Exception): encoder(tile, mask, feature_type="somethingelse")
def map_tile_to_parent(input_tile_list): mb_conn = sqlite3.connect(args.mbtiles_db) mb_cursor = mb_conn.cursor() output_list = [] for input_tile in input_tile_list: print mp.current_process().name, 'querying db for', input_tile z, y, x = input_tile sql = 'SELECT tile_data FROM tiles WHERE zoom_level = {} AND tile_row = {} and tile_column = {}'.format( z, y, x) mb_cursor.execute(sql) tile_data = [r[0] for r in mb_cursor.fetchall()][0] # and decode the associated GLAD point data as well decompressed = zlib.decompress(str(tile_data), zlib.MAX_WBITS | 16) vt = vector_tile_base.VectorTile(decompressed).layers[0] date_dict = {} for feat in vt.features: as_date = convert_jd(feat.properties['year'], feat.properties['julian_day']) conf = feat.properties['confidence'] # aggregating by (date, conf) tuples # can't serialize a tuples as a key, must be string instead key = '{}::{}'.format(as_date, conf) # if we already have a GLAD alert for this date, add to our count # otherwise create a new entry in the dict try: date_dict[key] += 1 except KeyError: date_dict[key] = 1 # convert TMS y to XYZ y # http://bl.ocks.org/lennepkade/b6fe9e4862668b2d19fe26f6c2d7cbef xyz_y = ((1 << z) - y - 1) # save the highest zoom level tile to our output list output_list.append(((x, xyz_y, z), date_dict)) # aggregate this tile all the way up, saving all parents to our output list as well output_list.extend(add_parent_tiles(x, xyz_y, z, date_dict)) return output_list
def mvt_to_hpgl(mvt_buffer): hpgl = ['IN;SC;PU;VS5;PU;'] # TODO: scale plotter coordinates to tile coordinates # Make sure to handle negative tile coordinates tile = vector_tile_base.VectorTile(mvt_buffer) for layer in tile.layers: for feature in layer.features: # Supports only points if feature.type != 'point': continue geometry = feature.get_geometry() hpgl.append('PU{},{};PD;'.format(geometry[0][0], geometry[0][1])) hpgl.append('PU;') return ''.join(hpgl)
x = int((x - tile_coord[1]) * w) alti = int(dic[key]['altitude']) cv2.circle(img, (y, h - x), 1, (alti, 0, alti), thickness=-1) cv2.imwrite('../data/plots_alti.png', img) if __name__ == '__main__': with open('../data/nodes.json') as f: dic = json.load(f) with open('../config.json') as f: config = json.load(f) tile_coord = (config["coord"]["zoom"], config["coord"]["t_lon"], config["coord"]["t_lat"]) img = cv2.imread('../data/img.png') img = np.rot90(img) cv2.imwrite('../data/img_r.png', img) img = cv2.imread('../data/img_r.png') with open(r'../data/test.pbf', 'rb') as f: data = f.read() vt = vector_tile_base.VectorTile(data) layer_map = {l.name: l for l in vt.layers} _show_plots(layer_map, img) img = cv2.imread('../data/img_r.png') _show_alti(dic, img, tile_coord)
import vector_tile_base import sys f = open(sys.argv[1], "rb") raw_tile = f.read() f.close() vt = vector_tile_base.VectorTile(raw_tile) for l in vt.layers: print(l.name) for f in l.features: print(f.type) print(f.properties) print(f.get_geometry())
def test_API_tiles_mosaic(event): """Test /mosaic route.""" # test missing url in queryString event["path"] = f"/mosaic/12/2156/2041.jpg" event["httpMethod"] = "GET" res = APP(event, {}) assert res["statusCode"] == 500 headers = res["headers"] assert headers["Content-Type"] == "application/json" body = json.loads(res["body"]) assert body["errorMessage"] == "Missing 'urls' parameter" # test valid jpg request with linear rescaling event["path"] = f"/mosaic/12/2156/2041.jpg" event["httpMethod"] = "GET" event["queryStringParameters"] = {"urls": mosaic_url, "rescale": "-1,1"} res = APP(event, {}) assert res["statusCode"] == 200 headers = res["headers"] assert headers["Content-Type"] == "image/jpg" assert res["body"] assert res["isBase64Encoded"] # test valid jpg request with rescaling and colormap event["path"] = f"/mosaic/12/2156/2041.jpg" event["httpMethod"] = "GET" event["queryStringParameters"] = { "urls": mosaic_url, "rescale": "-1,1", "color_map": "cfastie", } res = APP(event, {}) assert res["statusCode"] == 200 headers = res["headers"] assert headers["Content-Type"] == "image/jpg" assert res["body"] assert res["isBase64Encoded"] # test scale (512px tile size) event["path"] = f"/mosaic/12/2156/[email protected]" event["httpMethod"] = "GET" event["queryStringParameters"] = {"urls": mosaic_url, "rescale": "0,10"} res = APP(event, {}) assert res["statusCode"] == 200 headers = res["headers"] assert headers["Content-Type"] == "image/jpg" assert res["body"] assert res["isBase64Encoded"] # test with nodata == nan event["path"] = f"/mosaic/12/2156/2041.jpg" event["httpMethod"] = "GET" event["queryStringParameters"] = { "urls": mosaic_url, "rescale": "-1,1", "nodata": "nan", } res = APP(event, {}) assert res["statusCode"] == 200 headers = res["headers"] assert headers["Content-Type"] == "image/jpg" assert res["body"] assert res["isBase64Encoded"] # test indexes option event["path"] = f"/mosaic/12/2156/2041.jpg" event["httpMethod"] = "GET" event["queryStringParameters"] = { "urls": mosaic_url, "indexes": "1", "rescale": "0,10" } res = APP(event, {}) assert res["statusCode"] == 200 headers = res["headers"] assert headers["Content-Type"] == "image/jpg" assert res["body"] assert res["isBase64Encoded"] # test mvt option event["path"] = f"/mosaic/12/2156/2041.pbf" event["httpMethod"] = "GET" event["queryStringParameters"] = {"urls": mosaic_url} res = APP(event, {}) assert res["statusCode"] == 200 headers = res["headers"] assert headers["Content-Type"] == "application/x-protobuf" assert res["body"] assert res["isBase64Encoded"] body = base64.b64decode(res["body"]) vt = vector_tile_base.VectorTile(body) props = vt.layers[0].features[0].properties assert props["idw"] # test mvt option event["path"] = f"/mosaic/12/2156/2041.pbf" event["httpMethod"] = "GET" event["queryStringParameters"] = { "urls": mosaic_url, "feature_type": "polygon" } res = APP(event, {}) assert res["statusCode"] == 200 headers = res["headers"] assert headers["Content-Type"] == "application/x-protobuf" assert res["body"] assert res["isBase64Encoded"] body = base64.b64decode(res["body"]) vt = vector_tile_base.VectorTile(body) props = vt.layers[0].features[0].properties assert props["idw"]