Exemplo n.º 1
0
def viz(src_paths, style, port, mapbox_token, no_check):
    """Rasterio Viz cli."""
    # Check if cog
    src_paths = list(src_paths)
    with ExitStack() as ctx:
        for ii, src_path in enumerate(src_paths):
            if not no_check and not cog_validate(src_path):
                # create tmp COG
                click.echo("create temporaty COG")
                tmp_path = ctx.enter_context(TemporaryRasterFile(src_path))

                output_profile = cog_profiles.get("deflate")
                output_profile.update(dict(blockxsize="256", blockysize="256"))
                config = dict(
                    GDAL_TIFF_INTERNAL_MASK=os.environ.get(
                        "GDAL_TIFF_INTERNAL_MASK", True
                    ),
                    GDAL_TIFF_OVR_BLOCKSIZE="128",
                )
                cog_translate(src_path, tmp_path.name, output_profile, config=config)
                src_paths[ii] = tmp_path.name

        src_dst = raster.RasterTiles(src_paths)
        application = app.viz(src_dst, token=mapbox_token, port=port)
        url = application.get_template_url()
        click.echo(f"Viewer started at {url}", err=True)
        click.launch(url)
        application.start()
Exemplo n.º 2
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"
Exemplo n.º 3
0
def test_viz_custom():
    """Should work as expected (create TileServer object)."""

    src_path = cog_path
    dataset_reader = type("AsyncReader", (AsyncReader, ),
                          {"reader": COGReader})

    app = viz(src_path, reader=dataset_reader, host="0.0.0.0", port=5050)
    assert app.port == 5050
    assert app.endpoint == "http://0.0.0.0:5050"
Exemplo n.º 4
0
def test_viz_multi():
    """Should work as expected (create TileServer object)."""

    src_path = cogb1b2b3_path
    dataset_reader = type("AsyncReader", (AsyncReader, ),
                          {"reader": MultiFilesReader})

    app = viz(src_path, reader=dataset_reader)
    assert app.port == 8080
    assert app.endpoint == "http://127.0.0.1:8080"
    client = TestClient(app.app)

    response = client.get("/info")
    assert response.status_code == 200
    assert response.headers["content-type"] == "application/json"
    assert response.json()["band_descriptions"] == [
        ["file1", ""],
        ["file2", ""],
        ["file3", ""],
    ]
Exemplo n.º 5
0
def test_viz():
    """Should work as expected (create TileServer object)."""
    src_path = cog_path
    dataset_reader = type("AsyncReader", (AsyncReader, ),
                          {"reader": COGReader})

    app = viz(src_path, reader=dataset_reader)

    assert app.port == 8080
    assert app.endpoint == "http://127.0.0.1:8080"
    assert app.template_url == "http://127.0.0.1:8080/index.html"
    client = TestClient(app.app)

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

    response = client.get("/index.html")
    assert response.status_code == 200
    assert response.headers["cache-control"] == "no-cache"

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

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

    response = client.get(
        "/tiles/7/64/43.png?rescale=1,10&bidx=1&bidx=1&bidx=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"

    response = client.get("/tiles/7/64/43?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 == 500
    assert not response.headers.get("cache-control")

    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("/tiles/7/64/43.pbf?feature_type=point")
    assert response.status_code == 200
    assert response.headers["content-type"] == "application/x-protobuf"

    response = client.get("/preview?rescale=1,10&color_map=cfastie")
    assert response.status_code == 200
    assert response.headers["content-type"] == "image/jpeg"
    assert response.headers["cache-control"] == "no-cache"

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

    response = client.get(
        "/part.png?bbox=-2.00,48.5,-1,49.5&rescale=1,10&color_map=cfastie")
    assert response.status_code == 200
    assert response.headers["content-type"] == "image/png"

    response = client.get(
        "/part?bbox=-2.00,48.5,-1,49.5&rescale=1,10&color_map=cfastie")
    assert response.status_code == 200
    assert response.headers["content-type"] == "image/jpeg"

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

    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"] == 7
    assert r["maxzoom"] == 9
    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": [110]}
Exemplo n.º 6
0
def viz(
    src_path,
    nodata,
    minzoom,
    maxzoom,
    port,
    host,
    no_check,
    reader,
    layers,
    server_only,
    config,
):
    """Rasterio Viz cli."""
    if reader:
        module, classname = reader.rsplit(".", 1)
        reader = getattr(importlib.import_module(module), classname)  # noqa
        if not issubclass(
                reader,
            (BaseReader, AsyncBaseReader, MultiBandReader, MultiBaseReader)):
            warnings.warn(f"Invalid reader type: {type(reader)}")

    dataset_reader = reader or COGReader

    if issubclass(dataset_reader, (MultiBandReader)):
        reader_type = "bands"
    elif issubclass(dataset_reader, (MultiBaseReader)):
        reader_type = "assets"
    else:
        reader_type = "cog"

    # Check if cog
    with ExitStack() as ctx:
        if (src_path.lower().endswith(".tif") and not reader and not no_check
                and not cog_validate(src_path)[0]):
            # create tmp COG
            click.echo("create temporary COG")
            tmp_path = ctx.enter_context(TemporaryRasterFile())

            output_profile = cog_profiles.get("deflate")
            output_profile.update(dict(blockxsize="256", blockysize="256"))
            config = dict(GDAL_TIFF_INTERNAL_MASK=True,
                          GDAL_TIFF_OVR_BLOCKSIZE="128")
            cog_translate(src_path,
                          tmp_path.name,
                          output_profile,
                          config=config)
            src_path = tmp_path.name

        # Dynamically create an Async Dataset Reader if not a subclass of AsyncBaseReader
        if not issubclass(dataset_reader, AsyncBaseReader):
            dataset_reader = type("AsyncReader", (AsyncReader, ),
                                  {"reader": dataset_reader})

        application = app.viz(
            src_path=src_path,
            reader=dataset_reader,
            port=port,
            host=host,
            config=config,
            minzoom=minzoom,
            maxzoom=maxzoom,
            nodata=nodata,
            layers=layers,
            reader_type=reader_type,
        )
        if not server_only:
            click.echo(f"Viewer started at {application.template_url}",
                       err=True)
            click.launch(application.template_url)

        application.start()
Exemplo n.º 7
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
        }
    }