Exemplo n.º 1
0
def MosaicBackend(input: str, *args: Any, **kwargs: Any) -> BaseBackend:
    """Select mosaic backend for input."""
    parsed = urlparse(input)

    if not input or input == ":memory:":
        return MemoryBackend(*args, **kwargs)

    # `stac+https//{hostname}/{path}`
    elif parsed.scheme and parsed.scheme.startswith("stac+"):
        input = input.replace("stac+", "")
        return STACBackend(input, *args, **kwargs)

    # `s3:///{bucket}{key}`
    elif parsed.scheme == "s3":
        return S3Backend(input, *args, **kwargs)

    # `gs://{bucket}/{key}`
    elif parsed.scheme == "gs":
        return GCSBackend(input, *args, **kwargs)

    # `dynamodb://{region}/{table}:{mosaic}`
    elif parsed.scheme == "dynamodb":
        return DynamoDBBackend(input, *args, **kwargs)

    # `sqlite:///{path.db}:{mosaic}`
    elif parsed.scheme == "sqlite":
        return SQLiteBackend(input, *args, **kwargs)

    # https://{hostname}/{path}
    elif parsed.scheme in ["https", "http"]:
        return HttpBackend(input, *args, **kwargs)

    # file:///{path}
    elif parsed.scheme == "file":
        return FileBackend(parsed.path, *args, **kwargs)

    # Invalid Scheme
    elif parsed.scheme:
        raise ValueError(f"'{parsed.scheme}' is not supported")

    # fallback to FileBackend
    else:
        return FileBackend(input, *args, **kwargs)
Exemplo n.º 2
0
def MosaicBackend(url: str, *args: Any, **kwargs: Any) -> BaseBackend:
    """Select mosaic backend for url."""
    parsed = urlparse(url)

    if parsed.scheme and parsed.scheme.startswith("stac+"):
        url = url.replace("stac+", "")
        return STACBackend(url, *args, **kwargs)

    if parsed.scheme == "s3":
        return S3Backend(url, *args, **kwargs)

    if parsed.scheme == "dynamodb":
        return DynamoDBBackend(url, *args, **kwargs)

    if parsed.scheme in ["https", "http"]:
        return HttpBackend(url, *args, **kwargs)

    if parsed.scheme == "file":
        path = parsed.path
        return FileBackend(path, *args, **kwargs)

    return FileBackend(url, *args, **kwargs)
Exemplo n.º 3
0
def test_stac_backend(post):
    """Test STAC backend."""
    with open(stac_page1, "r") as f1, open(stac_page2, "r") as f2:
        post.side_effect = [
            STACMockResponse(json.loads(f1.read())),
            STACMockResponse(json.loads(f2.read())),
        ]

    with STACBackend("https://a_stac.api/search", {},
                     8,
                     14,
                     backend_options={"max_items": 8}) as mosaic:
        assert mosaic._backend_name == "STAC"
        assert isinstance(mosaic, STACBackend)
        assert post.call_count == 1
        assert mosaic.quadkey_zoom == 8
        assert list(mosaic.metadata.dict(exclude_none=True).keys()) == [
            "mosaicjson",
            "version",
            "minzoom",
            "maxzoom",
            "quadkey_zoom",
            "bounds",
            "center",
        ]
        assert mosaic.assets_for_tile(210, 90, 10) == [
            "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a/items/S2A_12XWR_20200621_0_L2A",
            "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a/items/S2A_13XDL_20200621_0_L2A",
        ]
        assert mosaic.assets_for_point(-106.050, 81.43) == [
            "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a/items/S2A_12XWR_20200621_0_L2A",
            "https://earth-search.aws.element84.com/v0/collections/sentinel-s2-l2a/items/S2A_13XDL_20200621_0_L2A",
        ]
    post.reset_mock()

    with open(stac_page1, "r") as f1, open(stac_page2, "r") as f2:
        post.side_effect = [
            STACMockResponse(json.loads(f1.read())),
            STACMockResponse(json.loads(f2.read())),
        ]

    with STACBackend("https://a_stac.api/search", {},
                     8,
                     14,
                     backend_options={"max_items": 15}) as mosaic:
        assert mosaic._backend_name == "STAC"
        assert isinstance(mosaic, STACBackend)
        assert post.call_count == 2
        assert mosaic.quadkey_zoom == 8
        assert list(mosaic.metadata.dict(exclude_none=True).keys()) == [
            "mosaicjson",
            "version",
            "minzoom",
            "maxzoom",
            "quadkey_zoom",
            "bounds",
            "center",
        ]
    post.reset_mock()

    with open(stac_page1, "r") as f1, open(stac_page2, "r") as f2:
        post.side_effect = [
            STACMockResponse(json.loads(f1.read())),
            STACMockResponse(json.loads(f2.read())),
        ]

    with STACBackend("https://a_stac.api/search", {},
                     8,
                     14,
                     backend_options={"max_items": 15}) as mosaic:
        with pytest.raises(NotImplementedError):
            mosaic.write()

        with pytest.raises(NotImplementedError):
            mosaic.update()