Пример #1
0
def tiler(
    z: int,
    x: int,
    y: int,
    tilesize: int = 128,
    feature_type: str = "polygon"
) -> Tuple:
    """Handle tile requests."""
    if isinstance(tilesize, str):
        tilesize = int(tilesize)

    address = f"https://elevation-tiles-prod.s3.amazonaws.com/geotiff/{z}/{x}/{y}.tif"
    tile, mask = main.tile(address, x, y, z, tilesize=tilesize)

    token = os.environ["MAPBOX_ACCESS_TOKEN"]
    address = f"https://api.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.jpg?access_token={token}"
    imgdata = urlopen(address).read()
    with MemoryFile(imgdata) as memfile:
        with memfile.open() as dataset:
            rgb_tile = dataset.read(out_shape=(3, tilesize, tilesize))

    tile = numpy.concatenate([tile, rgb_tile], axis=0)
    return (
        "OK",
        "application/x-protobuf",
        mvtEncoder(
            tile,
            mask,
            ["elevation", "R", "G", "B"],
            "satellite-3d",
            feature_type=feature_type,
        ),
    )
Пример #2
0
def mvttiles(
    scene: str,
    z: int,
    x: int,
    y: int,
    bands: str = None,
    tile_size: Union[str, int] = 256,
    pixel_selection: str = "first",
    feature_type: str = "point",
    resampling_method: str = "nearest",
) -> Tuple[str, str, BinaryIO]:
    """Handle MVT tile requests."""
    if tile_size is not None and isinstance(tile_size, str):
        tile_size = int(tile_size)

    bands = tuple(bands.split(","))
    tile, mask = landsat8.tile(scene, x, y, z, bands=bands, tilesize=tile_size)

    band_descriptions = [f"Band_{b}" for b in bands]
    return (
        "OK",
        "application/x-protobuf",
        mvtEncoder(tile,
                   mask,
                   band_descriptions,
                   "landsat",
                   feature_type=feature_type),
    )
Пример #3
0
def _mvt(
    mosaicid: str = None,
    z: int = None,
    x: int = None,
    y: int = None,
    url: str = None,
    tile_size: Union[str, int] = 256,
    pixel_selection: str = "first",
    feature_type: str = "point",
    resampling_method: str = "nearest",
) -> Tuple:
    """Handle MVT requests."""
    from rio_tiler_mvt.mvt import encoder as mvtEncoder  # noqa

    if not mosaicid and not url:
        return ("NOK", "text/plain", "Missing 'MosaicID or URL' parameter")

    mosaic_path = _create_mosaic_path(mosaicid) if mosaicid else url
    with MosaicBackend(mosaic_path) as mosaic:
        assets = mosaic.tile(x, y, z)
        if not assets:
            return ("EMPTY", "text/plain", f"No assets found for tile {z}-{x}-{y}")

    if tile_size is not None and isinstance(tile_size, str):
        tile_size = int(tile_size)

    if pixel_selection == "last":
        pixel_selection = "first"
        assets = list(reversed(assets))

    with rasterio.Env(aws_session):
        pixsel_method = PIXSEL_METHODS[pixel_selection]
        tile, mask = mosaic_tiler(
            assets,
            x,
            y,
            z,
            cogeoTiler,
            tilesize=tile_size,
            pixel_selection=pixsel_method(),
            resampling_method=resampling_method,
        )
        if tile is None:
            return ("EMPTY", "text/plain", "empty tiles")

        with rasterio.open(assets[0]) as src_dst:
            band_descriptions = _get_layer_names(src_dst)

        return (
            "OK",
            "application/x-protobuf",
            mvtEncoder(
                tile,
                mask,
                band_descriptions,
                mosaicid or os.path.basename(url),
                feature_type=feature_type,
            ),
        )
Пример #4
0
def _mvt(
    mosaicid: str = None,
    z: int = None,
    x: int = None,
    y: int = None,
    url: str = None,
    tile_size: Union[str, int] = 256,
    pixel_selection: str = "first",
    feature_type: str = "point",
    resampling_method: str = "nearest",
) -> Tuple[str, str, BinaryIO]:
    """Handle MVT requests."""
    if mosaicid:
        url = _create_path(mosaicid)
    elif url is None:
        return ("NOK", "text/plain", "Missing 'URL' parameter")

    assets = fetch_and_find_assets(url, x, y, z)
    if not assets:
        return ("EMPTY", "text/plain", f"No assets found for tile {z}-{x}-{y}")

    if tile_size is not None and isinstance(tile_size, str):
        tile_size = int(tile_size)

    if pixel_selection == "last":
        pixel_selection = "first"
        assets = list(reversed(assets))

    with rasterio.Env(aws_session):
        pixsel_method = PIXSEL_METHODS[pixel_selection]
        tile, mask = mosaic_tiler(
            assets,
            x,
            y,
            z,
            cogeoTiler,
            tilesize=tile_size,
            pixel_selection=pixsel_method(),
            resampling_method=resampling_method,
        )
        if tile is None:
            return ("EMPTY", "text/plain", "empty tiles")

        with rasterio.open(assets[0]) as src_dst:
            band_descriptions = _get_layer_names(src_dst)

        return (
            "OK",
            "application/x-protobuf",
            mvtEncoder(
                tile,
                mask,
                band_descriptions,
                os.path.basename(url),
                feature_type=feature_type,
            ),
        )
Пример #5
0
 def read_tile_mvt(
     self,
     z: int,
     x: int,
     y: int,
     tilesize: int = 128,
     resampling_method: str = "bilinear",
     feature_type: str = "point",
 ) -> BinaryIO:
     """Read raster tile data and encode to MVT."""
     tile, mask = self.read_tile(z,
                                 x,
                                 y,
                                 tilesize=tilesize,
                                 resampling_method=resampling_method)
     return mvtEncoder(tile,
                       mask,
                       self.band_descriptions,
                       feature_type=feature_type)
Пример #6
0
def mosaic_tiles_mvt(
    z,
    x,
    y,
    scale=1,
    urls=None,
    nodata=None,
    pixel_selection: str = "first",
    resampling_method: str = "bilinear",
    feature_type="point",
):
    """
    Handle Raster /mosaics requests.

    Note: All the querystring parameters are translated to function keywords
    and passed as string value by lambda_proxy

    Attributes
    ----------
    z : int, required
        Mercator tile ZOOM level.
    x : int, required
        Mercator tile X index.
    y : int, required
        Mercator tile Y index.
    scale : int
        Output scale factor (default: 1).
    urls : str, required
        Dataset urls to read from.
    nodata, str, optional
        Custom nodata value if not preset in dataset.
    pixel_selection : str, optional
        rio-tiler-mosaic pixel selection method (default: first)
    resampling_method : str, optional
        Resampling method to use (default: bilinear)
    feature_type : str, optional
        Output feature type (default: point)

    Returns
    -------
    status : str
        Status of the request (e.g. OK, NOK).
    MIME type : str
        response body MIME type (e.g. application/x-protobuf).
    body : bytes
        VT body.

    """
    if not urls:
        raise TilerError("Missing 'urls' parameter")

    if nodata is not None and isinstance(nodata, str):
        nodata = numpy.nan if nodata == "nan" else float(nodata)

    tilesize = 256 * scale
    tile, mask = mosaic_tiler(
        urls.split(","),
        x,
        y,
        z,
        main.tile,
        tilesize=tilesize,
        nodata=nodata,
        pixel_selection=pixel_selection,
        resampling_method=resampling_method,
    )
    if tile is None:
        return ("EMPTY", "text/plain", "empty tiles")

    band_descriptions = _get_layer_names(urls.split(",")[0])

    return (
        "OK",
        "application/x-protobuf",
        mvtEncoder(
            tile,
            mask,
            band_descriptions,
            "mosaic",
            feature_type=feature_type,
        ),
    )
Пример #7
0
def mvt(
    z,
    x,
    y,
    url,
    scale=1,
    nodata=None,
    feature_type="point",
    resampling="nearest",
):
    """
    Handle MVT /tiles requests.

    Note: All the querystring parameters are translated to function keywords
    and passed as string value by lambda_proxy

    Attributes
    ----------
    z : int, required
        Mercator tile ZOOM level.
    x : int, required
        Mercator tile X index.
    y : int, required
        Mercator tile Y index.
    url : str, required
        Dataset url to read from.
    scale : int
        Output scale factor (default: 1).
    nodata : str, optional
        Custom nodata value if not preset in dataset.
    feature_type : str, optional
        Output feature type (default: point)
    resampling : str, optional
        Data resampling (default: nearest)

    Returns
    -------
    status : str
        Status of the request (e.g. OK, NOK).
    MIME type : str
        response body MIME type (e.g. application/x-protobuf).
    body : bytes
        VT body.

    """
    if nodata is not None and isinstance(nodata, str):
        nodata = numpy.nan if nodata == "nan" else float(nodata)

    tilesize = 256 * scale

    tile, mask = main.tile(
        url,
        x,
        y,
        z,
        tilesize=tilesize,
        nodata=nodata,
        resampling_method=resampling,
    )

    band_descriptions = _get_layer_names(url)

    return (
        "OK",
        "application/x-protobuf",
        mvtEncoder(
            tile,
            mask,
            band_descriptions,
            os.path.basename(url),
            feature_type=feature_type,
        ),
    )