Esempio n. 1
0
def test_rastertiles_metadata():
    """Should work as expected (create rastertiles object and get metadata)."""
    r = RasterTiles(cog_path)
    metadata = r.metadata()
    assert metadata["band_descriptions"] == [(1, "band1")]
    assert metadata["bounds"]
    assert metadata["statistics"]
    assert len(metadata["statistics"][1]["histogram"][0]) == 20

    metadata = r.metadata(histogram_bins=10)
    assert len(metadata["statistics"][1]["histogram"][0]) == 10

    pc = metadata["statistics"][1]["pc"]
    metadata = r.metadata(histogram_range=pc)
    assert metadata["statistics"][1]["histogram"]

    r = RasterTiles((cogb1_path, cogb2_path, cogb3_path))
    metadata = r.metadata()
    assert metadata["band_descriptions"] == [(1, "cogb1"), (2, "cogb2"),
                                             (3, "cogb3")]
    assert metadata["bounds"]
    assert metadata["statistics"]
    assert metadata["dtype"]
    assert len(metadata["statistics"].keys()) == 3

    metadata = r.metadata(indexes="2")
    assert metadata["band_descriptions"] == [(2, "cogb2")]
    assert metadata["bounds"]
    assert metadata["statistics"]
    assert len(metadata["statistics"].keys()) == 1
Esempio n. 2
0
def test_rastertiles_tile_exists_valid():
    """Should work as expected (create rastertiles object and check if tile exists)."""
    r = RasterTiles(cog_path)
    z = 7
    x = 64
    y = 43
    assert r.tile_exists(z, x, y)

    z = 7
    x = 4
    y = 43
    assert not r.tile_exists(z, x, y)
Esempio n. 3
0
async def test_rastertiles_read_tile():
    """Should work as expected (create rastertiles object and read tile)."""
    r = RasterTiles(cog_path)
    z = 7
    x = 63
    y = 43
    data, mask = await r.read_tile(z, x, y)
    assert data.shape == (1, 256, 256)
    assert mask.shape == (256, 256)

    data, mask = await r.read_tile(z, x, y, tilesize=512, indexes=[1])
    assert data.shape == (1, 512, 512)
    assert mask.shape == (512, 512)

    assert await r.read_tile_mvt(z, x, y)
    assert await r.read_tile_mvt(z, x, y, feature_type="polygon")

    # outside tile
    z = 7
    x = 65
    y = 49
    with pytest.raises(TileOutsideBounds):
        await r.read_tile_mvt(z, x, y)

    with pytest.raises(TileOutsideBounds):
        await r.read_tile_mvt(z, x, y, feature_type="polygon")

    with pytest.raises(TileOutsideBounds):
        await r.read_tile(z, x, y)

    # MultipleFiles
    r = RasterTiles((cogb1_path, cogb2_path, cogb3_path))
    z = 7
    x = 63
    y = 43
    data, mask = await r.read_tile(z, x, y)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)

    data, mask = await r.read_tile(z, x, y, tilesize=512, indexes=[1])
    assert data.shape == (1, 512, 512)
    assert mask.shape == (512, 512)

    assert await r.read_tile_mvt(z, x, y)
    assert await r.read_tile_mvt(z, x, y, feature_type="polygon")
Esempio n. 4
0
def test_viz_custom():
    """Should work as expected (create TileServer object)."""
    r = RasterTiles(cog_path)
    app = viz(r, host="0.0.0.0", port=5050)
    assert app.raster == r
    assert app.port == 5050
    assert app.get_bounds() == r.bounds
    assert app.get_center() == r.center
    assert app.get_endpoint_url() == "http://0.0.0.0:5050"
Esempio n. 5
0
def test_rastertiles_valid():
    """Should work as expected (create rastertiles object)."""
    r = RasterTiles(cog_path)
    assert r.path == [cog_path]
    assert list(map(int, r.bounds)) == [-3, 47, 0, 50]
    assert r.minzoom == 6
    assert r.maxzoom == 8
    assert r.band_descriptions == ["band1"]

    r = RasterTiles((cog_path, ))
    assert r.path == [cog_path]

    r = RasterTiles((cogb1_path, cogb2_path, cogb3_path))
    assert r.path == [cogb1_path, cogb2_path, cogb3_path]
    assert list(map(int, r.bounds)) == [-3, 47, 0, 50]
    assert r.minzoom == 6
    assert r.maxzoom == 8
    assert r.band_descriptions == ["cogb1", "cogb2", "cogb3"]
Esempio n. 6
0
def test_rastertiles_tile_point_valid():
    """Should work as expected (create rastertiles object and get point value)."""
    r = RasterTiles(cog_path)
    p = r.point([-2, 48])
    assert p == {"coordinates": [-2, 48], "value": {"band1": 110}}

    r = RasterTiles(cog_path)
    p = r.point([-2, 48])
    assert p == {"coordinates": [-2, 48], "value": {"band1": 110}}

    r = RasterTiles((cogb1_path, cogb2_path, cogb3_path))
    p = r.point([-2, 48])
    assert p == {
        "coordinates": [-2, 48],
        "value": {"cogb1": 110, "cogb2": 110, "cogb3": 110},
    }
Esempio n. 7
0
async def test_rastertiles_metadata():
    """Should work as expected (create rastertiles object and get metadata)."""
    r = RasterTiles(cog_path)
    metadata = await r.metadata()
    assert metadata["band_descriptions"] == [(1, "band1")]
    assert metadata["bounds"]
    assert metadata["statistics"]
    assert len(metadata["statistics"][1]["histogram"][0]) == 10

    r = RasterTiles((cogb1_path, cogb2_path, cogb3_path))
    metadata = await r.metadata()
    assert metadata["band_descriptions"] == [(1, "cogb1"), (2, "cogb2"), (3, "cogb3")]
    assert metadata["bounds"]
    assert metadata["statistics"]
    assert metadata["dtype"]
    assert len(metadata["statistics"].keys()) == 3

    metadata = await r.metadata(indexes=(2,))
    assert metadata["band_descriptions"] == [(2, "cogb2")]
    assert metadata["bounds"]
    assert metadata["statistics"]
    assert len(metadata["statistics"].keys()) == 1
Esempio n. 8
0
def test_viz():
    """Should work as expected (create TileServer object)."""
    r = RasterTiles(cog_path)
    app = viz(r)
    assert app.raster == r
    assert app.port == 8080
    assert app.get_bounds() == r.bounds
    assert app.get_center() == r.center
    assert app.get_endpoint_url() == "http://127.0.0.1:8080"
    assert app.get_template_url() == "http://127.0.0.1:8080/index.html"
    assert app.get_simple_template_url(
    ) == "http://127.0.0.1:8080/index_simple.html"
    client = TestClient(app.app)

    response = client.get("/")
    assert response.status_code == 404

    response = client.get("/tiles/7/64/43.png?rescale=1,10")
    assert response.status_code == 200
    assert response.headers["content-type"] == "image/png"

    response = client.get("/tiles/7/64/43.png?rescale=1,10&indexes=1")
    assert response.status_code == 200
    assert response.headers["content-type"] == "image/png"

    response = client.get("/tiles/7/64/43.png?rescale=1,10&color_map=cfastie")
    assert response.status_code == 200
    assert response.headers["content-type"] == "image/png"

    with pytest.raises(TileOutsideBounds):
        client.get("/tiles/18/8624/119094.png")

    with pytest.raises(TileOutsideBounds):
        client.get("/tiles/18/8624/119094.pbf")

    response = client.get("/tiles/7/64/43.pbf")
    assert response.status_code == 200
    assert response.headers["content-type"] == "application/x-protobuf"

    response = client.get("/tiles/7/64/43.pbf?feature_type=polygon")
    assert response.status_code == 200
    assert response.headers["content-type"] == "application/x-protobuf"

    response = client.get("/metadata")
    assert response.status_code == 200
    assert response.headers["content-type"] == "application/json"

    response = client.get("/tilejson.json?tile_format=png")
    assert response.status_code == 200
    assert response.headers["content-type"] == "application/json"
    r = response.json()
    assert r["bounds"]
    assert r["center"]
    assert r["minzoom"] == 6
    assert r["maxzoom"] == 8
    assert r["tiles"][0].endswith("png")

    response = client.get("/tilejson.json?tile_format=pbf")
    assert response.status_code == 200
    assert response.headers["content-type"] == "application/json"
    r = response.json()
    assert r["tiles"][0].endswith("pbf")

    response = client.get(
        "/tilejson.json?tile_format=pbf&feature_type=polygon")
    assert response.status_code == 200
    assert response.headers["content-type"] == "application/json"
    r = response.json()
    assert r["tiles"][0].endswith("pbf?feature_type=polygon")

    response = client.get("/point?coordinates=-2,48")
    assert response.status_code == 200
    assert response.headers["content-type"] == "application/json"
    assert response.json() == {
        "coordinates": [-2.0, 48.0],
        "value": {
            "band1": 110
        }
    }