def test_hash():
    """Should return a 56 characters long string."""
    hash = utils.get_hash(a=1)
    assert re.match(r"[0-9A-Fa-f]{56}", hash)
Exemple #2
0
 def mosaicid(self) -> str:
     """Return sha224 id of the mosaicjson document."""
     return get_hash(**self.mosaic_def.dict(exclude_none=True))
Exemple #3
0
def _create(
    body: str,
    minzoom: Union[str, int] = None,
    maxzoom: Union[str, int] = None,
    min_tile_cover: Union[str, float] = None,
    tile_cover_sort: Union[str, bool] = False,
    tile_format: str = None,
    tile_scale: Union[str, int] = 1,
    **kwargs: Any,
) -> Tuple[str, str, str]:
    minzoom = int(minzoom) if isinstance(minzoom, str) else minzoom
    maxzoom = int(maxzoom) if isinstance(maxzoom, str) else maxzoom
    min_tile_cover = (
        float(min_tile_cover) if isinstance(min_tile_cover, str) else min_tile_cover
    )

    mosaicid = get_hash(
        body=body,
        minzoom=minzoom,
        maxzoom=maxzoom,
        min_tile_cover=min_tile_cover,
        tile_cover_sort=tile_cover_sort,
        version=mosaic_version,
    )
    try:
        with MosaicBackend(_create_mosaic_path(mosaicid), client=s3_client) as mosaic:
            meta = mosaic.metadata
    except:  # noqa
        body = json.loads(body)
        with rasterio.Env(aws_session):
            mosaic_definition = MosaicJSON.from_urls(
                body,
                minzoom=minzoom,
                maxzoom=maxzoom,
                minimum_tile_cover=min_tile_cover,
                tile_cover_sort=tile_cover_sort,
            )

        with MosaicBackend(
            _create_mosaic_path(mosaicid),
            mosaic_def=mosaic_definition,
            client=s3_client,
        ) as mosaic:
            mosaic.write()
            meta = mosaic.metadata

    if tile_format in ["pbf", "mvt"]:
        tile_url = f"{app.host}/{mosaicid}/{{z}}/{{x}}/{{y}}.{tile_format}"
    elif tile_format in ["png", "jpg", "webp", "tif", "npy"]:
        tile_url = (
            f"{app.host}/{mosaicid}/{{z}}/{{x}}/{{y}}@{tile_scale}x.{tile_format}"
        )
    else:
        tile_url = f"{app.host}/{mosaicid}/{{z}}/{{x}}/{{y}}@{tile_scale}x"

    qs = urllib.parse.urlencode(list(kwargs.items()))
    if qs:
        tile_url += f"?{qs}"

    meta = {
        "bounds": meta["bounds"],
        "center": meta["center"],
        "maxzoom": meta["maxzoom"],
        "minzoom": meta["minzoom"],
        "name": mosaicid,
        "tilejson": "2.1.0",
        "tiles": [tile_url],
    }

    return ("OK", "application/json", json.dumps(meta, separators=(",", ":")))