コード例 #1
0
def tile(
    scene: str,
    z: int,
    x: int,
    y: int,
    scale: int = 1,
    ext: str = "png",
    bands: str = None,
    percents: str = "",
    expr: str = None,
    rescale: str = None,
    color_formula: str = None,
    color_map: str = None,
) -> Tuple[str, str, BinaryIO]:
    """Handle tile requests."""
    driver = "jpeg" if ext == "jpg" else ext

    if bands and expr:
        raise CbersTilerError("Cannot pass bands and expression")

    tilesize = scale * 256

    if expr is not None:
        tile, mask = expression(scene, x, y, z, expr=expr, tilesize=tilesize)
    elif bands is not None:
        tile, mask = cbers_tile(scene,
                                x,
                                y,
                                z,
                                bands=tuple(bands.split(",")),
                                tilesize=tilesize,
                                percents=percents)
    else:
        raise CbersTilerError("No bands nor expression given")

    if tile is None or mask is None:
        return (
            "OK",
            f"image/png",
            b'',
        )

    rtile, rmask = _postprocess(tile,
                                mask,
                                rescale=None,
                                color_formula=color_formula)

    if color_map:
        color_map = get_colormap(color_map, format="gdal")

    options = img_profiles.get(driver, {})
    return (
        "OK",
        f"image/{ext}",
        array_to_image(rtile,
                       rmask,
                       img_format=driver,
                       color_map=color_map,
                       **options),
    )
コード例 #2
0
def test_array_to_image_valid_colormapDict():
    """Create 'colormaped' PNG image buffer from one band array using discrete cmap."""
    arr = np.random.randint(0, 255, size=(1, 512, 512), dtype=np.uint8)
    cmap = {
        1: [255, 255, 255],
        50: [255, 255, 0],
        100: [255, 0, 0],
        150: [0, 0, 255]
    }
    assert utils.array_to_image(arr, color_map=cmap)
コード例 #3
0
def tile(
    scene,
    z,
    x,
    y,
    scale=1,
    ext="png",
    bands=None,
    expr=None,
    rescale=None,
    color_formula=None,
    color_map=None,
):
    """Handle tile requests."""
    if ext == "jpg":
        driver = "jpeg"
    elif ext == "jp2":
        driver = "JP2OpenJPEG"
    else:
        driver = ext

    if bands and expr:
        raise CbersTilerError("Cannot pass bands and expression")
    if not bands and not expr:
        raise CbersTilerError("Need bands or expression")

    if bands:
        bands = tuple(bands.split(","))

    tilesize = scale * 256

    if expr is not None:
        tile, mask = expression(scene, x, y, z, expr, tilesize=tilesize)
    elif bands is not None:
        tile, mask = cbers.tile(scene, x, y, z, bands=bands, tilesize=tilesize)

    rtile, rmask = _postprocess(tile,
                                mask,
                                tilesize,
                                rescale=rescale,
                                color_formula=color_formula)

    if color_map:
        color_map = get_colormap(color_map, format="gdal")

    options = img_profiles.get(driver, {})
    return (
        "OK",
        f"image/{ext}",
        array_to_image(rtile,
                       rmask,
                       img_format=driver,
                       color_map=color_map,
                       **options),
    )
コード例 #4
0
def tile(
    z: int,
    x: int,
    y: int,
    scale: int = 1,
    ext: str = None,
    url: str = None,
    indexes: Union[str, Tuple[int]] = None,
    expr: str = None,
    nodata: Union[str, int, float] = None,
    rescale: str = None,
    color_formula: str = None,
    color_map: str = None,
) -> Tuple[str, str, BinaryIO]:
    """Handle tile requests."""
    driver = "jpeg" if ext == "jpg" else ext

    if indexes and expr:
        raise TilerError("Cannot pass indexes and expression")

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

    if isinstance(indexes, str):
        indexes = tuple(int(s) for s in re.findall(r"\d+", indexes))

    if nodata is not None:
        nodata = numpy.nan if nodata == "nan" else float(nodata)

    tilesize = scale * 256

    if expr is not None:
        tile, mask = expression(
            url, x, y, z, expr=expr, tilesize=tilesize, nodata=nodata
        )
    else:
        tile, mask = main.tile(
            url, x, y, z, indexes=indexes, tilesize=tilesize, nodata=nodata
        )

    rtile, rmask = _postprocess(
        tile, mask, rescale=rescale, color_formula=color_formula
    )

    if color_map:
        color_map = get_colormap(color_map, format="gdal")

    options = img_profiles.get(driver, {})
    return (
        "OK",
        f"image/{ext}",
        array_to_image(rtile, rmask, img_format=driver, color_map=color_map, **options),
    )
コード例 #5
0
ファイル: tile.py プロジェクト: AusBOM/serverless-tile-poc
def create_tile(src, x, y, z, style, bands=None):
    """Create the tile for x,y,z

    Parameters
    ----------
    src : string
        The connection to the source data.
        Accepted protocols are s3, https and local files.
    x : int
        Mercator tile X index.
    y : int
        Mercator tile Y index.
    z : int
        Mecator tile zoom level.
    style : Style object
        Used to style the tile.
    bands : int or list of type 'int'
        When querying a source with multiple bands, selects the band to use.

    Returns
    -------
    bytes
        The png image as bytes.
    """

    try:
        # returns the tile at the point as well as a mask to for the undefined values
        indexes = bands
        resample = style.get_resampling_method()
        tile, alpha = main.tile(src,
                                x,
                                y,
                                z,
                                indexes=indexes,
                                resampling_method=resample)
        alpha = style.style_tile_mask(tile, alpha)
        png_tile = style.normalise_tile(tile)

    except Exception as exception:
        print('tile generation exception')
        print(exception)
        traceback.print_exc(file=sys.stdout)
        # Return empty tile
        png_tile = np.full(256, 3)

    colour_map = style.get_colour_map_array()

    return array_to_image(png_tile, color_map=colour_map, mask=alpha)
コード例 #6
0
def tile(b64url, x, y, z):
    url = str(base64.b64decode(b64url).decode('utf-8'))
    app.logger.debug(f"Getting tile z {z} x {x} y {y} at url {url}")
    try:
        tile, mask = main.tile(url, int(x), int(y), int(z), tilesize=256)
    except TileOutsideBounds as e:
        app.logger.debug("Tile out of bounds")
        return abort(404)

    _buffer = array_to_image(tile,
                             mask=mask,
                             img_format="png",
                             **image_options)
    del tile, mask
    return send_file(io.BytesIO(_buffer),
                     mimetype='image/png',
                     as_attachment=False,
                     attachment_filename='tile.jpg')
コード例 #7
0
def s1tile(
    scene: str,
    z: int,
    x: int,
    y: int,
    scale: int = 1,
    ext: str = "png",
    bands: str = None,
    rescale: str = None,
    color_formula: str = None,
    color_map: str = None,
) -> Tuple[str, str, BinaryIO]:
    """Handle tile requests."""
    if not bands:
        raise Exception("bands is required")

    tilesize = scale * 256

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

    rtile, rmask = _postprocess(tile,
                                mask,
                                rescale=rescale,
                                color_formula=color_formula)

    if color_map:
        color_map = get_colormap(color_map, format="gdal")

    driver = "jpeg" if ext == "jpg" else ext
    options = img_profiles.get(driver, {})
    return (
        "OK",
        f"image/{ext}",
        array_to_image(rtile,
                       rmask,
                       img_format=driver,
                       color_map=color_map,
                       **options),
    )
コード例 #8
0
    def _get_tile(self, z, x, y, tileformat, color_ops=None):
        if tileformat == "jpg":
            tileformat = "jpeg"

        if not self.raster.tile_exists(z, x, y):
            raise web.HTTPError(404)

        data, mask = self.raster.read_tile(z, x, y)

        if len(data.shape) == 2:
            data = numpy.expand_dims(data, axis=0)

        if self.scale:
            nbands = data.shape[0]
            scale = self.scale
            if len(scale) != nbands:
                scale = scale * nbands

            for bdx in range(nbands):
                data[bdx] = numpy.where(
                    mask,
                    linear_rescale(data[bdx],
                                   in_range=scale[bdx],
                                   out_range=[0, 255]),
                    0,
                )

            data = data.astype(numpy.uint8)

        if color_ops:
            data = self._apply_color_operations(data, color_ops)

        options = img_profiles.get(tileformat, {})

        return BytesIO(
            array_to_image(data,
                           mask=mask,
                           color_map=self.colormap,
                           img_format=tileformat,
                           **options))
コード例 #9
0
ファイル: app.py プロジェクト: bekerov/rio-viz
        async def tile(
            z: int,
            x: int,
            y: int,
            ext: str = Path(..., regex="^(png)|(jpg)|(webp)$"),
            scale: int = 1,
            indexes: str = Query(None, title="Coma (',') delimited band indexes"),
            rescale: str = Query(None, title="Coma (',') delimited Min,Max bounds"),
            color_formula: str = Query(None, title="rio-color formula"),
            color_map: str = Query(None, title="rio-tiler color map names"),
            resampling_method: str = Query("bilinear", title="rasterio resampling"),
        ):
            """Handle /tiles requests."""
            if isinstance(indexes, str):
                indexes = tuple(int(s) for s in re.findall(r"\d+", indexes))

            tilesize = scale * 256
            tile, mask = self.raster.read_tile(
                z,
                x,
                y,
                tilesize=tilesize,
                indexes=indexes,
                resampling_method=resampling_method,
            )

            rtile, _ = postprocess_tile(
                tile, mask, rescale=rescale, color_formula=color_formula
            )

            if color_map:
                color_map = get_colormap(color_map, format="gdal")

            driver = "jpeg" if ext == "jpg" else ext
            options = img_profiles.get(driver, {})
            img = array_to_image(
                rtile, mask, img_format=driver, color_map=color_map, **options
            )
            return TileResponse(img, media_type=f"image/{ext}")
コード例 #10
0
def test_array_to_image_valid_colormap():
    """Creates 'colormaped' PNG image buffer from one band array."""
    arr = np.random.randint(0, 255, size=(1, 512, 512), dtype=np.uint8)
    cmap = utils.get_colormap(name="cfastie", format="gdal")
    assert utils.array_to_image(arr, color_map=cmap)
コード例 #11
0
def test_array_to_image_valid_mask():
    """Creates image buffer from 3 bands array and mask."""
    arr = np.random.randint(0, 255, size=(3, 512, 512), dtype=np.uint8)
    mask = np.zeros((512, 512), dtype=np.uint8)
    assert utils.array_to_image(arr, mask=mask)
    assert utils.array_to_image(arr, mask=mask, img_format="jpeg")
コード例 #12
0
ファイル: tiler.py プロジェクト: wwb00l/WebODM-1
    def get(self,
            request,
            pk=None,
            project_pk=None,
            tile_type="",
            z="",
            x="",
            y="",
            scale=1):
        """
        Get a tile image
        """
        task = self.get_and_check_task(request, pk)

        z = int(z)
        x = int(x)
        y = int(y)

        if x == 0 and y == 0 and z == 0:
            raise exceptions.NotFound()

        scale = int(scale)
        ext = "png"
        driver = "jpeg" if ext == "jpg" else ext

        indexes = None
        nodata = None

        formula = self.request.query_params.get('formula')
        bands = self.request.query_params.get('bands')
        rescale = self.request.query_params.get('rescale')
        color_map = self.request.query_params.get('color_map')
        hillshade = self.request.query_params.get('hillshade')

        if formula == '': formula = None
        if bands == '': bands = None
        if rescale == '': rescale = None
        if color_map == '': color_map = None
        if hillshade == '' or hillshade == '0': hillshade = None

        try:
            expr, _ = lookup_formula(formula, bands)
        except ValueError as e:
            raise exceptions.ValidationError(str(e))

        if tile_type in ['dsm', 'dtm'] and rescale is None:
            rescale = "0,1000"

        if tile_type in ['dsm', 'dtm'] and color_map is None:
            color_map = "gray"

        if tile_type == 'orthophoto' and formula is not None:
            if color_map is None:
                color_map = "gray"
            if rescale is None:
                rescale = "-1,1"

        if nodata is not None:
            nodata = np.nan if nodata == "nan" else float(nodata)
        tilesize = scale * 256

        url = get_raster_path(task, tile_type)
        if not os.path.isfile(url):
            raise exceptions.NotFound()

        try:
            if expr is not None:
                tile, mask = expression(url,
                                        x,
                                        y,
                                        z,
                                        expr=expr,
                                        tilesize=tilesize,
                                        nodata=nodata)
            else:
                tile, mask = main.tile(url,
                                       x,
                                       y,
                                       z,
                                       indexes=indexes,
                                       tilesize=tilesize,
                                       nodata=nodata)
        except TileOutsideBounds:
            raise exceptions.NotFound("Outside of bounds")

        # Use alpha channel for transparency, don't use the mask if one is provided (redundant)
        if tile.shape[0] == 4:
            mask = None

        if color_map:
            try:
                color_map = get_colormap(color_map, format="gdal")
            except FileNotFoundError:
                raise exceptions.ValidationError("Not a valid color_map value")

        intensity = None

        if hillshade is not None:
            try:
                hillshade = float(hillshade)
                if hillshade <= 0:
                    hillshade = 1.0
                print(hillshade)
            except ValueError:
                raise exceptions.ValidationError("Invalid hillshade value")

            if tile.shape[0] != 1:
                raise exceptions.ValidationError(
                    "Cannot compute hillshade of non-elevation raster (multiple bands found)"
                )

            with rasterio.open(url) as src:
                minzoom, maxzoom = get_zooms(src)
                z_value = min(maxzoom, max(z, minzoom))
                delta_scale = (maxzoom + 1 - z_value) * 4
                dx = src.meta["transform"][0] * delta_scale
                dy = -src.meta["transform"][4] * delta_scale

            ls = LightSource(azdeg=315, altdeg=45)

            # Hillshading is not a local tile operation and
            # requires neighbor tiles to be rendered seamlessly
            elevation = get_elevation_tiles(tile[0], url, x, y, z, tilesize,
                                            nodata)
            intensity = ls.hillshade(elevation,
                                     dx=dx,
                                     dy=dy,
                                     vert_exag=hillshade)
            intensity = intensity[tilesize:tilesize * 2, tilesize:tilesize * 2]

        rgb, rmask = rescale_tile(tile, mask, rescale=rescale)
        rgb = apply_colormap(rgb, color_map)

        if intensity is not None:
            # Quick check
            if rgb.shape[0] != 3:
                raise exceptions.ValidationError(
                    "Cannot process tile: intensity image provided, but no RGB data was computed."
                )

            intensity = intensity * 255.0
            rgb = hsv_blend(rgb, intensity)

        options = img_profiles.get(driver, {})
        return HttpResponse(array_to_image(rgb,
                                           rmask,
                                           img_format=driver,
                                           **options),
                            content_type="image/{}".format(ext))
コード例 #13
0
def test_array_to_image_valid_1band():
    """Creates PNG image buffer from one band array."""
    arr = np.random.randint(0, 255, size=(512, 512), dtype=np.uint8)
    assert utils.array_to_image(arr)
コード例 #14
0
ファイル: app.py プロジェクト: bekerov/cogeo-mosaic-tiler
def _img(
    mosaicid: str = None,
    z: int = None,
    x: int = None,
    y: int = None,
    scale: int = 1,
    ext: str = None,
    url: str = None,
    indexes: str = None,
    rescale: str = None,
    color_ops: str = None,
    color_map: str = None,
    pixel_selection: str = "first",
    resampling_method: str = "nearest",
) -> Tuple[str, str, BinaryIO]:
    """Handle tile 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 indexes:
        indexes = list(map(int, indexes.split(",")))

    tilesize = 256 * scale

    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,
            indexes=indexes,
            tilesize=tilesize,
            pixel_selection=pixsel_method(),
            resampling_method=resampling_method,
        )

    if tile is None:
        return ("EMPTY", "text/plain", "empty tiles")

    rtile = _postprocess(tile, mask, rescale=rescale, color_formula=color_ops)
    if color_map:
        if color_map.startswith("custom_"):
            color_map = get_custom_cmap(color_map)
        else:
            color_map = get_colormap(color_map, format="gdal")

    if not ext:
        ext = "jpg" if mask.all() else "png"

    driver = "jpeg" if ext == "jpg" else ext
    options = img_profiles.get(driver, {})

    if ext == "tif":
        ext = "tiff"
        driver = "GTiff"
        tile_bounds = mercantile.xy_bounds(mercantile.Tile(x=x, y=y, z=z))
        options = dict(
            crs={"init": "EPSG:3857"},
            transform=from_bounds(*tile_bounds, tilesize, tilesize),
        )

    return (
        "OK",
        f"image/{ext}",
        array_to_image(rtile,
                       mask,
                       img_format=driver,
                       color_map=color_map,
                       **options),
    )
コード例 #15
0
def handler(event, context, invoke_local=False):  # pylint: disable=unused-argument
    """The lambda handler for generating a tile
    
    Parameters
    ----------
    event : object
        The event passed from the API Gateway to the lambda function.
    context : object
        The context passed from the API Gateway to the lambda function.
    invoke_local : bool
        A flag to tell whether this was invoked locally.

    Returns
    -------
    object
        A lambda response to api gateway.
    """

    # Get path parameters
    data = event['pathParameters']['data']
    x_index = int(event['pathParameters']['x'])
    y_index = int(event['pathParameters']['y'].strip('.png'))
    zoom_index = int(event['pathParameters']['z'])

    bucket = os.environ['tileBucket']

    data_config = get_stac_config(data)

    if 'style' in event['pathParameters'] and event['pathParameters'][
            'style'] != 'default':
        style_id = event['pathParameters']['style']
        style = get_style(data, style_id, bucket, data_config['style_indexes'])
        output_file = f"{data_config['output_prefix']}/styles/{style_id}/{zoom_index}/{x_index}/{y_index}.png"
    else:
        style = get_style(data, 'default', bucket)
        output_file = f"{data_config['output_prefix']}/{zoom_index}/{x_index}/{y_index}.png"

    try:
        if 'style' in event['pathParameters'] and event['pathParameters'][
                'style'] != 'default':
            style_id = event['pathParameters']['style']
            style = get_style(data, style_id, bucket,
                              data_config['style_indexes'])
            output_file = f"{data_config['output_prefix']}/styles/{style_id}/{zoom_index}/{x_index}/{y_index}.png"
        else:
            style = get_style(data, 'default', bucket)
            output_file = f"{data_config['output_prefix']}/{zoom_index}/{x_index}/{y_index}.png"
            bands = data_config.get('band', [1])
        tile = create_tile(data_config['source'],
                           x_index,
                           y_index,
                           zoom_index,
                           style,
                           bands=bands)
    except Exception as e:
        print(e)
        # return {
        #     'statusCode': '404',
        #     'headers': {}
        # }
        # Create empty 1x1 transparent is png
        tile_init = np.full((1, 1, 1), 0, dtype=np.uint8)
        mask = np.full((1, 1), 0, dtype=np.uint8)
        tile = array_to_image(tile_init, mask=mask)

    if invoke_local:
        # If localally invoked save the tile to disk
        filepath = f"data/{output_file}"
        os.makedirs(os.path.dirname(filepath), exist_ok=True)
        with open(filepath, "wb") as binary_png:
            print(f"Generated: {filepath}")
            binary_png.write(tile)
    else:
        # save the tile to s3
        try:
            s3 = boto3.resource('s3')
            s3object = s3.Object(bucket, output_file)
            s3object.put(Body=tile)
        except Exception as e:
            # Log error but still return tile.
            print('failed to save to s3 tile cache')
            print(e)

    return {
        'statusCode': "200",
        'headers': {
            'Content-Type': 'image/png'
        },
        'body': base64.b64encode(tile).decode('utf-8'),
        'isBase64Encoded': True
    }
コード例 #16
0
    def get(self,
            request,
            pk=None,
            project_pk=None,
            tile_type="",
            z="",
            x="",
            y="",
            scale=1):
        """
        Get a tile image
        """
        task = self.get_and_check_task(request, pk)

        z = int(z)
        x = int(x)
        y = int(y)

        scale = int(scale)
        ext = "png"
        driver = "jpeg" if ext == "jpg" else ext

        indexes = None
        nodata = None

        formula = self.request.query_params.get('formula')
        bands = self.request.query_params.get('bands')
        rescale = self.request.query_params.get('rescale')
        color_map = self.request.query_params.get('color_map')
        hillshade = self.request.query_params.get('hillshade')

        if formula == '': formula = None
        if bands == '': bands = None
        if rescale == '': rescale = None
        if color_map == '': color_map = None
        if hillshade == '' or hillshade == '0': hillshade = None

        try:
            expr, _ = lookup_formula(formula, bands)
        except ValueError as e:
            raise exceptions.ValidationError(str(e))

        if tile_type in ['dsm', 'dtm'] and rescale is None:
            rescale = "0,1000"

        if tile_type in ['dsm', 'dtm'] and color_map is None:
            color_map = "gray"

        if tile_type == 'orthophoto' and formula is not None:
            if color_map is None:
                color_map = "gray"
            if rescale is None:
                rescale = "-1,1"

        if nodata is not None:
            nodata = np.nan if nodata == "nan" else float(nodata)
        tilesize = scale * 256

        url = get_raster_path(task, tile_type)

        if not os.path.isfile(url):
            raise exceptions.NotFound()

        with rasterio.open(url) as src:
            minzoom, maxzoom = get_zoom_safe(src)
            has_alpha = has_alpha_band(src)
            if z < minzoom - ZOOM_EXTRA_LEVELS or z > maxzoom + ZOOM_EXTRA_LEVELS:
                raise exceptions.NotFound()

            # Handle N-bands datasets for orthophotos (not plant health)
            if tile_type == 'orthophoto' and expr is None:
                ci = src.colorinterp

                # More than 4 bands?
                if len(ci) > 4:
                    # Try to find RGBA band order
                    if ColorInterp.red in ci and \
                        ColorInterp.green in ci and \
                        ColorInterp.blue in ci:
                        indexes = (
                            ci.index(ColorInterp.red) + 1,
                            ci.index(ColorInterp.green) + 1,
                            ci.index(ColorInterp.blue) + 1,
                        )
                    else:
                        # Fallback to first three
                        indexes = (
                            1,
                            2,
                            3,
                        )

                elif has_alpha:
                    indexes = non_alpha_indexes(src)

        resampling = "nearest"
        padding = 0
        if tile_type in ["dsm", "dtm"]:
            resampling = "bilinear"
            padding = 16

        try:
            if expr is not None:
                tile, mask = expression(url,
                                        x,
                                        y,
                                        z,
                                        expr=expr,
                                        tilesize=tilesize,
                                        nodata=nodata,
                                        tile_edge_padding=padding,
                                        resampling_method=resampling)
            else:
                tile, mask = main.tile(url,
                                       x,
                                       y,
                                       z,
                                       indexes=indexes,
                                       tilesize=tilesize,
                                       nodata=nodata,
                                       tile_edge_padding=padding,
                                       resampling_method=resampling)
        except TileOutsideBounds:
            raise exceptions.NotFound("Outside of bounds")

        if color_map:
            try:
                color_map = get_colormap(color_map, format="gdal")
            except FileNotFoundError:
                raise exceptions.ValidationError("Not a valid color_map value")

        intensity = None

        if hillshade is not None:
            try:
                hillshade = float(hillshade)
                if hillshade <= 0:
                    hillshade = 1.0
            except ValueError:
                raise exceptions.ValidationError("Invalid hillshade value")

            if tile.shape[0] != 1:
                raise exceptions.ValidationError(
                    "Cannot compute hillshade of non-elevation raster (multiple bands found)"
                )

            delta_scale = (maxzoom + ZOOM_EXTRA_LEVELS + 1 - z) * 4
            dx = src.meta["transform"][0] * delta_scale
            dy = -src.meta["transform"][4] * delta_scale

            ls = LightSource(azdeg=315, altdeg=45)

            # Hillshading is not a local tile operation and
            # requires neighbor tiles to be rendered seamlessly
            elevation = get_elevation_tiles(tile[0], url, x, y, z, tilesize,
                                            nodata, resampling, padding)
            intensity = ls.hillshade(elevation,
                                     dx=dx,
                                     dy=dy,
                                     vert_exag=hillshade)
            intensity = intensity[tilesize:tilesize * 2, tilesize:tilesize * 2]

        rgb, rmask = rescale_tile(tile, mask, rescale=rescale)
        rgb = apply_colormap(rgb, color_map)

        if intensity is not None:
            # Quick check
            if rgb.shape[0] != 3:
                raise exceptions.ValidationError(
                    "Cannot process tile: intensity image provided, but no RGB data was computed."
                )

            intensity = intensity * 255.0
            rgb = hsv_blend(rgb, intensity)

        options = img_profiles.get(driver, {})
        return HttpResponse(array_to_image(rgb,
                                           rmask,
                                           img_format=driver,
                                           **options),
                            content_type="image/{}".format(ext))
コード例 #17
0
def test_array_to_image_valid_1bandWebp():
    """Creates WEBP image buffer from 1 band array."""
    arr = np.random.randint(0, 255, size=(1, 512, 512), dtype=np.uint8)
    assert utils.array_to_image(arr, img_format="WEBP")
コード例 #18
0
def test_array_to_image_valid_options():
    """Creates image buffer with driver options."""
    arr = np.random.randint(0, 255, size=(3, 512, 512), dtype=np.uint8)
    mask = np.zeros((512, 512), dtype=np.uint8) + 255
    assert utils.array_to_image(arr, mask=mask, img_format="png", ZLEVEL=9)
コード例 #19
0
def test_array_to_image_geotiff():
    """Creates GeoTIFF image buffer from 3 bands array."""
    arr = np.random.randint(0, 255, size=(3, 512, 512), dtype=np.uint8)
    mask = np.zeros((512, 512), dtype=np.uint8) + 255
    assert utils.array_to_image(arr, mask=mask, img_format="GTiff")
コード例 #20
0
def tiles(
    z,
    x,
    y,
    scale=1,
    ext="png",
    url=None,
    nodata=None,
    indexes=None,
    rescale=None,
    color_ops=None,
    color_map=None,
    dem=None,
):
    """
    Handle Raster /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.
    scale : int
        Output scale factor (default: 1).
    ext : str
        Image format to return (default: png).
    url : str, required
        Dataset url to read from.
    indexes : str, optional, (defaults: None)
        Comma separated band index number (e.g "1,2,3").
    nodata, str, optional
        Custom nodata value if not preset in dataset.
    rescale : str, optional
        Min and Max data bounds to rescale data from.
    color_ops : str, optional
        rio-color compatible color formula
    color_map : str, optional
        Rio-tiler compatible colormap name ("cfastie" or "schwarzwald")
    dem : str, optional
        Create Mapbox or Mapzen RGBA encoded elevation image

    Returns
    -------
    status : str
        Status of the request (e.g. OK, NOK).
    MIME type : str
        response body MIME type (e.g. image/jpeg).
    body : bytes
        Image body.

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

    if indexes:
        indexes = tuple(int(s) for s in re.findall(r"\d+", indexes))

    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,
                           indexes=indexes,
                           tilesize=tilesize,
                           nodata=nodata)

    if dem:
        if dem == "mapbox":
            tile = encoders.data_to_rgb(tile, -10000, 1)
        elif dem == "mapzen":
            tile = mapzen_elevation_rgb.data_to_rgb(tile)
        else:
            return ("NOK", "text/plain", 'Invalid "dem" mode')
    else:
        rtile, rmask = _postprocess_tile(tile,
                                         mask,
                                         rescale=rescale,
                                         color_ops=color_ops)

        if color_map:
            color_map = get_colormap(color_map, format="gdal")

    driver = "jpeg" if ext == "jpg" else ext
    options = img_profiles.get(driver, {})
    return (
        "OK",
        f"image/{ext}",
        array_to_image(rtile,
                       rmask,
                       img_format=driver,
                       color_map=color_map,
                       **options),
    )
コード例 #21
0
ファイル: cogeo.py プロジェクト: freespys/remotepixel-tiler
def tile(
    z,
    x,
    y,
    scale=1,
    ext="png",
    url=None,
    indexes=None,
    expr=None,
    nodata=None,
    rescale=None,
    color_formula=None,
    color_map=None,
    dem=None,
):
    """Handle tile requests."""
    if ext == "jpg":
        driver = "jpeg"
    elif ext == "jp2":
        driver = "JP2OpenJPEG"
    else:
        driver = ext

    if indexes and expr:
        raise TilerError("Cannot pass indexes and expression")
    if not url:
        raise TilerError("Missing 'url' parameter")

    if indexes:
        indexes = tuple(int(s) for s in re.findall(r"\d+", indexes))

    tilesize = scale * 256

    if nodata is not None:
        nodata = numpy.nan if nodata == "nan" else float(nodata)

    if expr is not None:
        tile, mask = expression(url, x, y, z, expr, tilesize=tilesize)
    else:
        tile, mask = main.tile(url,
                               x,
                               y,
                               z,
                               indexes=indexes,
                               tilesize=tilesize)

    if dem:
        if dem == "mapbox":
            tile = encoders.data_to_rgb(tile, -10000, 1)
        elif dem == "mapzen":
            tile = mapzen_elevation_rgb.data_to_rgb(tile)
        else:
            return ("NOK", "text/plain", 'Invalid "dem" mode')
    else:
        rtile, rmask = _postprocess(tile,
                                    mask,
                                    tilesize,
                                    rescale=rescale,
                                    color_formula=color_formula)

        if color_map:
            color_map = get_colormap(color_map, format="gdal")

    options = img_profiles.get(driver, {})
    return (
        "OK",
        f"image/{ext}",
        array_to_image(rtile,
                       rmask,
                       img_format=driver,
                       color_map=color_map,
                       **options),
    )
コード例 #22
0
def mosaic_tiles(
    z,
    x,
    y,
    scale=1,
    ext="png",
    urls=None,
    nodata=None,
    indexes=None,
    rescale=None,
    color_ops=None,
    color_map=None,
    pixel_selection: str = "first",
    resampling_method: str = "bilinear",
):
    """
    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).
    ext : str
        Image format to return (default: png).
    urls : str, required
        Dataset urls to read from.
    indexes : str, optional, (defaults: None)
        Comma separated band index number (e.g "1,2,3").
    nodata, str, optional
        Custom nodata value if not preset in dataset.
    rescale : str, optional
        Min and Max data bounds to rescale data from.
    color_ops : str, optional
        rio-color compatible color formula
    color_map : str, optional
        Rio-tiler compatible colormap name ("cfastie" or "schwarzwald")
    pixel_selection : str, optional
        rio-tiler-mosaic pixel selection method (default: first)
    resampling_method : str, optional
        Resampling method to use (default: bilinear)

    Returns
    -------
    status : str
        Status of the request (e.g. OK, NOK).
    MIME type : str
        response body MIME type (e.g. image/jpeg).
    body : bytes
        Image body.

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

    if indexes:
        indexes = tuple(int(s) for s in re.findall(r"\d+", indexes))

    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,
        indexes=indexes,
        tilesize=tilesize,
        nodata=nodata,
        pixel_selection=pixel_selection,
        resampling_method=resampling_method,
    )
    if tile is None:
        return ("EMPTY", "text/plain", "empty tiles")

    rtile, rmask = _postprocess_tile(tile,
                                     mask,
                                     rescale=rescale,
                                     color_ops=color_ops)

    if color_map:
        color_map = get_colormap(color_map, format="gdal")

    driver = "jpeg" if ext == "jpg" else ext
    options = img_profiles.get(driver, {})
    return (
        "OK",
        f"image/{ext}",
        array_to_image(rtile,
                       rmask,
                       img_format=driver,
                       color_map=color_map,
                       **options),
    )
コード例 #23
0
def tile_handler(
    z: int,
    x: int,
    y: int,
    scale: int = 1,
    ext: str = None,
    url: str = None,
    indexes: Union[str, Tuple[int]] = None,
    expr: str = None,
    nodata: Union[str, int, float] = None,
    rescale: str = None,
    color_formula: str = None,
    color_map: str = None,
) -> Tuple[str, str, BinaryIO]:
    """Handle /tiles requests."""
    if indexes and expr:
        raise TilerError("Cannot pass indexes and expression")

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

    if isinstance(indexes, str):
        indexes = tuple(int(s) for s in re.findall(r"\d+", indexes))

    if nodata is not None:
        nodata = numpy.nan if nodata == "nan" else float(nodata)

    tilesize = scale * 256
    if expr is not None:
        tile, mask = expression(
            url, x, y, z, expr=expr, tilesize=tilesize, nodata=nodata
        )
    else:
        tile, mask = cogTiler.tile(
            url, x, y, z, indexes=indexes, tilesize=tilesize, nodata=nodata
        )

    if not ext:
        ext = "jpg" if mask.all() else "png"

    rtile, rmask = _postprocess(
        tile, mask, rescale=rescale, color_formula=color_formula
    )

    if color_map:
        color_map = get_colormap(color_map, format="gdal")

    if ext == "jpg":
        driver = "jpeg"
    elif ext == "tif":
        driver = "GTiff"
    else:
        driver = ext

    options = img_profiles.get(driver, {})
    if driver == "GTiff":
        mercator_tile = mercantile.Tile(x=x, y=y, z=z)
        bounds = mercantile.xy_bounds(mercator_tile)
        w, s, e, n = bounds
        dst_transform = from_bounds(w, s, e, n, rtile.shape[1], rtile.shape[2])
        options = dict(
            dtype=rtile.dtype, crs={"init": "EPSG:3857"}, transform=dst_transform
        )

    return (
        "OK",
        f"image/{ext}",
        array_to_image(rtile, rmask, img_format=driver, color_map=color_map, **options),
    )
コード例 #24
0
ファイル: tiles.py プロジェクト: robert-werner/awspds-mosaic
def tiles(
    mosaicid: str,
    z: int,
    x: int,
    y: int,
    scale: int = 1,
    ext: str = "png",
    bands: str = None,
    expr: str = None,
    rescale: str = None,
    color_ops: str = None,
    color_map: str = None,
    pixel_selection: str = "first",
) -> Tuple[str, str, BinaryIO]:
    """Handle tile requests."""
    bucket = os.environ["MOSAIC_DEF_BUCKET"]
    url = f"s3://{bucket}/mosaics/{mosaicid}.json.gz"
    assets = get_assets(url, x, y, z)
    if not assets:
        return ("EMPTY", "text/plain", f"No assets found for tile {z}-{x}-{y}")

    tilesize = 256 * scale

    pixel_selection = pixSel[pixel_selection]
    if expr is not None:
        tile, mask = mosaic_tiler(
            assets,
            x,
            y,
            z,
            expressionTiler,
            pixel_selection=pixel_selection(),
            expr=expr,
            tilesize=tilesize,
        )

    elif bands is not None:
        tile, mask = mosaic_tiler(
            assets,
            x,
            y,
            z,
            landsatTiler,
            pixel_selection=pixel_selection(),
            bands=tuple(bands.split(",")),
            tilesize=tilesize,
        )
    else:
        return ("NOK", "text/plain", "No bands nor expression given")

    if tile is None:
        return ("EMPTY", "text/plain", "empty tiles")

    if color_map:
        color_map = get_colormap(color_map, format="gdal")

    if ext == "gif":
        frames = []
        options = img_profiles.get("png", {})
        for i in range(len(tile)):
            img = post_process_tile(tile[i].copy(),
                                    mask[i].copy(),
                                    rescale=rescale,
                                    color_formula=color_ops)
            frames.append(
                Image.open(
                    io.BytesIO(
                        array_to_image(
                            img,
                            mask[i],
                            img_format="png",
                            color_map=color_map,
                            **options,
                        ))))
        sio = io.BytesIO()
        frames[0].save(
            sio,
            "gif",
            save_all=True,
            append_images=frames[1:],
            duration=300,
            loop=0,
            optimize=True,
        )
        sio.seek(0)
        return ("OK", f"image/{ext}", sio.getvalue())

    rtile = post_process_tile(tile,
                              mask,
                              rescale=rescale,
                              color_formula=color_ops)

    driver = "jpeg" if ext == "jpg" else ext
    options = img_profiles.get(driver, {})

    if ext == "tif":
        ext = "tiff"
        driver = "GTiff"
        tile_bounds = mercantile.xy_bounds(mercantile.Tile(x=x, y=y, z=z))
        options = dict(
            crs={"init": "EPSG:3857"},
            transform=from_bounds(*tile_bounds, tilesize, tilesize),
        )

    return (
        "OK",
        f"image/{ext}",
        array_to_image(rtile,
                       mask,
                       img_format=driver,
                       color_map=color_map,
                       **options),
    )