Ejemplo n.º 1
0
def point(scene, coordinates, expression):
    """Point handler."""
    bands = tuple(set(re.findall(r"b(?P<bands>[0-9]{1,2})", expression)))

    scene_params = cbers_parse_scene_id(scene)
    cbers_address = f'{CBERS_BUCKET}/{scene_params["key"]}'
    addresses = [
        "{}/{}_BAND{}.tif".format(cbers_address, scene, band) for band in bands
    ]

    def worker(address):
        """Point worker."""
        with rasterio.open(address) as band:
            lon_srs, lat_srs = warp.transform("EPSG:4326", band.crs,
                                              [coordinates[0]],
                                              [coordinates[1]])
            point = list(band.sample([(lon_srs[0], lat_srs[0])]))[0]
        return point[0]

    try:
        with futures.ThreadPoolExecutor(max_workers=3) as executor:
            data = list(executor.map(worker, addresses))

            ctx = {}
            for bdx, b in enumerate(bands):
                ctx["b{}".format(b)] = data[bdx]
            ratio = np.nan_to_num(ne.evaluate(expression, local_dict=ctx))
    except IndexError:
        ratio = 0.0

    date = (scene_params["acquisitionYear"] + "-" +
            scene_params["acquisitionMonth"] + "-" +
            scene_params["acquisitionDay"])

    return {"ndvi": ratio, "scene": scene, "date": date}
Ejemplo n.º 2
0
def area(
    scene,
    bbox,
    expression,
    expression_range=[-1, 1],
    bbox_crs="epsg:4326",
    out_crs="epsg:3857",
):
    """Area handler."""
    max_img_size = 512

    bands = tuple(set(re.findall(r"b(?P<bands>[0-9]{1,2})", expression)))

    scene_params = cbers_parse_scene_id(scene)
    cbers_address = f'{CBERS_BUCKET}/{scene_params["key"]}'
    addresses = [
        "{}/{}_BAND{}.tif".format(cbers_address, scene, band) for band in bands
    ]

    _worker = partial(
        get_area,
        bbox=bbox,
        max_img_size=max_img_size,
        bbox_crs=bbox_crs,
        out_crs=out_crs,
    )
    with futures.ThreadPoolExecutor(max_workers=3) as executor:
        data = np.concatenate(list(executor.map(_worker, addresses)))
        if not np.any(data):
            raise Exception("No valid data in array")

        mask = np.all(data != 0, axis=0).astype(np.uint8) * 255

        ctx = {}
        for bdx, b in enumerate(bands):
            ctx["b{}".format(b)] = data[bdx]
        ratio = np.nan_to_num(ne.evaluate(expression, local_dict=ctx))

    ratio = np.where(
        mask,
        linear_rescale(ratio, in_range=expression_range, out_range=[0, 255]),
        0).astype(np.uint8)

    img = array_to_img(ratio, mask, get_colormap(name="cfastie"))
    ndvi = b64_encode_img(img, "jpeg")

    date = (scene_params["acquisitionYear"] + "-" +
            scene_params["acquisitionMonth"] + "-" +
            scene_params["acquisitionDay"])

    return {"ndvi": ndvi, "scene": scene, "date": date}
Ejemplo n.º 3
0
def test_cbers_id_valid():
    """Should work as expected (parse cbers scene id)."""
    scene = "CBERS_4_MUX_20171121_057_094_L2"
    expected_content = {
        "acquisitionDay": "21",
        "acquisitionMonth": "11",
        "acquisitionYear": "2017",
        "intrument": "MUX",
        "key": "CBERS4/MUX/057/094/CBERS_4_MUX_20171121_057_094_L2",
        "path": "057",
        "processingCorrectionLevel": "L2",
        "row": "094",
        "satellite": "4",
        "scene": "CBERS_4_MUX_20171121_057_094_L2",
        "sensor": "CBERS",
    }
    assert utils.cbers_parse_scene_id(scene) == expected_content
Ejemplo n.º 4
0
def create(
    scene,
    bands=None,
    expression=None,
    expression_range=[-1, 1],
    img_format="jpeg",
    ovrSize=512,
):
    """Handler."""
    if img_format not in ["png", "jpeg"]:
        raise UserWarning(f"Invalid {img_format} extension")

    if not expression and not bands:
        raise Exception("Expression or Bands must be provided")

    if bands:
        nb_bands = len(bands)
        if nb_bands != 3:
            raise Exception("RGB combination only")

    if expression:
        bands = tuple(set(re.findall(r"b(?P<bands>[0-9]{1,2})", expression)))
        rgb = expression.split(",")
        nb_bands = len(rgb)

    scene_params = cbers_parse_scene_id(scene)
    cbers_address = f'{CBERS_BUCKET}/{scene_params["key"]}'
    addresses = [
        "{}/{}_BAND{}.tif".format(cbers_address, scene, band) for band in bands
    ]

    worker = partial(get_overview, ovrSize=ovrSize)
    with futures.ThreadPoolExecutor(max_workers=3) as executor:
        data = np.concatenate(list(executor.map(worker, addresses)))
        mask = np.all(data != 0, axis=0).astype(np.uint8) * 255

        if expression:
            ctx = {}
            for bdx, b in enumerate(bands):
                ctx["b{}".format(b)] = data[bdx]
            data = np.array([
                np.nan_to_num(ne.evaluate(bloc.strip(), local_dict=ctx))
                for bloc in rgb
            ])

        for band in range(data.shape[0]):
            imgRange = (expression_range if expression else np.percentile(
                data[band][mask > 0], (2, 98)).tolist())
            data[band] = np.where(
                mask,
                linear_rescale(data[band],
                               in_range=imgRange,
                               out_range=[0, 255]),
                0,
            )

    data = data.squeeze()
    colormap = None if len(data.shape) >= 3 else get_colormap(name="cfastie")

    img = array_to_img(data, mask, colormap)
    return b64_encode_img(img, img_format)
Ejemplo n.º 5
0
def test_cbers_id_invalid():
    """Should raise an error with invalid sceneid."""
    scene = "CBERS_4_MUX_20171121_057_094"
    with pytest.raises(ValueError):
        utils.cbers_parse_scene_id(scene)
Ejemplo n.º 6
0
def create(scene, bands=None, expression=None):
    """Handler."""
    scene_params = cbers_parse_scene_id(scene)
    cbers_address = f'{CBERS_BUCKET}/{scene_params["key"]}'

    if not expression and not bands:
        raise Exception("Expression or Bands must be provided")

    if bands:
        nb_bands = len(bands)
        data_type = np.uint8
        if nb_bands != 3:
            raise Exception("RGB combination only")

    if expression:
        bands = tuple(set(re.findall(r"b(?P<bands>[0-9]{1,2})", expression)))
        rgb = expression.split(",")
        data_type = np.float32
        nb_bands = len(rgb)

    bqa = f"{cbers_address}/{scene}_BAND6.tif"
    with rasterio.open(bqa) as src:
        meta = src.meta
        wind = [w for ij, w in src.block_windows(1)]

        meta.update(
            nodata=0,
            count=nb_bands,
            interleave="pixel",
            compress="DEFLATE",
            photometric="MINISBLACK" if nb_bands == 1 else "RGB",
            dtype=data_type,
        )

    memfile = MemoryFile()
    with memfile.open(**meta) as dataset:
        with contextlib.ExitStack() as stack:
            srcs = [
                stack.enter_context(
                    rasterio.open(f"{cbers_address}/{scene}_BAND{band}.tif")
                )
                for band in bands
            ]

            def get_window(idx, window):
                return srcs[idx].read(window=window, boundless=True, indexes=(1))

            for window in wind:
                _worker = partial(get_window, window=window)
                with futures.ThreadPoolExecutor(max_workers=3) as executor:
                    data = np.stack(list(executor.map(_worker, range(len(bands)))))
                    if expression:
                        ctx = {}
                        for bdx, b in enumerate(bands):
                            ctx["b{}".format(b)] = data[bdx]
                        data = np.array(
                            [
                                np.nan_to_num(ne.evaluate(bloc.strip(), local_dict=ctx))
                                for bloc in rgb
                            ]
                        )

                    dataset.write(data.astype(data_type), window=window)

    return memfile