Exemplo n.º 1
0
def get_map(args):
    # pylint: disable=too-many-nested-blocks, too-many-branches, too-many-statements, too-many-locals
    # Parse GET parameters
    params = GetMapParameters(args)

    with cube() as dc:
        # Tiling.
        stacker = DataStacker(params.product,
                              params.geobox,
                              params.time,
                              params.resampling,
                              style=params.style)
        datasets = stacker.datasets(dc.index)
        zoomed_out = params.zf < params.product.min_zoom
        too_many_datasets = (params.product.max_datasets_wms > 0 and
                             len(datasets) > params.product.max_datasets_wms)
        if not datasets:
            body = _write_empty(params.geobox)
        elif too_many_datasets:
            body = _write_polygon(params.geobox, params.geobox.extent,
                                  params.product.zoom_fill)
        elif zoomed_out:
            # Zoomed out to far to properly render data.
            # Construct a polygon which is the union of the extents of the matching datasets.
            extent = None
            extent_crs = None
            for ds in datasets:
                if extent:
                    new_extent = bbox_to_geom(ds.extent.boundingbox,
                                              ds.extent.crs)
                    if new_extent.crs != extent_crs:
                        new_extent = new_extent.to_crs(extent_crs)
                    extent = extent.union(new_extent)
                else:
                    extent = bbox_to_geom(ds.extent.boundingbox, ds.extent.crs)
                    extent_crs = extent.crs
            extent = extent.to_crs(params.crs)
            body = _write_polygon(params.geobox, extent,
                                  params.product.zoom_fill)
        else:
            _LOG.debug("load start %s %s",
                       datetime.now().time(), args["requestid"])
            data = stacker.data(datasets,
                                manual_merge=params.product.data_manual_merge,
                                fuse_func=params.product.fuse_func)
            _LOG.debug("load stop %s %s",
                       datetime.now().time(), args["requestid"])
            if params.style.masks:
                if params.product.pq_name == params.product.name:
                    pq_band_data = (
                        data[params.product.pq_band].dims,
                        data[params.product.pq_band].astype("uint16"))
                    pq_data = xarray.Dataset(
                        {params.product.pq_band: pq_band_data},
                        coords=data[params.product.pq_band].coords)
                    flag_def = data[params.product.pq_band].flags_definition
                    pq_data[params.product.
                            pq_band].attrs["flags_definition"] = flag_def
                else:
                    pq_datasets = stacker.datasets(
                        dc.index,
                        mask=True,
                        all_time=params.product.pq_ignore_time)
                    if pq_datasets:
                        pq_data = stacker.data(
                            pq_datasets,
                            mask=True,
                            manual_merge=params.product.pq_manual_merge,
                            fuse_func=params.product.pq_fuse_func)
                    else:
                        pq_data = None
            else:
                pq_data = None
            extent_mask = None
            if not params.product.data_manual_merge:
                for band in params.style.needed_bands:
                    for f in params.product.extent_mask_func:
                        if extent_mask is None:
                            extent_mask = f(data, band)
                        else:
                            extent_mask &= f(data, band)

            if data is None or (params.style.masks and pq_data is None):
                body = _write_empty(params.geobox)
            else:
                body = _write_png(data, pq_data, params.style, extent_mask)

    return body, 200, resp_headers({"Content-Type": "image/png"})
Exemplo n.º 2
0
def get_map(args):
    # Parse GET parameters
    params = GetMapParameters(args)

    # Tiling.
    stacker = DataStacker(params.product,
                          params.geobox,
                          params.time,
                          style=params.style)
    dc = get_cube()
    try:
        datasets = stacker.datasets(dc.index)
        if not datasets:
            body = _write_empty(params.geobox)
        elif params.zf < params.product.min_zoom or (
                params.product.max_datasets_wms > 0
                and len(datasets) > params.product.max_datasets_wms):
            # Zoomed out to far to properly render data.
            # Construct a polygon which is the union of the extents of the matching datasets.
            extent = None
            extent_crs = None
            for ds in datasets:
                if extent:
                    new_extent = ds.extent
                    if new_extent.crs != extent_crs:
                        new_extent = new_extent.to_crs(extent_crs)
                    extent = extent.union(new_extent)
                else:
                    extent = ds.extent
                    extent_crs = extent.crs
            extent = extent.to_crs(params.crs)

            body = _write_polygon(params.geobox, extent,
                                  params.product.zoom_fill)
        else:
            data = stacker.data(datasets,
                                manual_merge=params.product.data_manual_merge)
            if params.style.masks:
                if params.product.pq_name == params.product.name:
                    pq_data = xarray.Dataset(
                        {
                            params.product.pq_band:
                            (data[params.product.pq_band].dims,
                             data[params.product.pq_band].astype("uint16"))
                        },
                        coords=data[params.product.pq_band].coords)
                    pq_data[params.product.
                            pq_band].attrs["flags_definition"] = data[
                                params.product.pq_band].flags_definition
                else:
                    pq_datasets = stacker.datasets(dc.index, mask=True)
                    if pq_datasets:
                        pq_data = stacker.data(
                            pq_datasets,
                            mask=True,
                            manual_merge=params.product.pq_manual_merge)
                    else:
                        pq_data = None
            else:
                pq_data = None
            extent_mask = None
            if not params.product.data_manual_merge:
                for band in params.style.needed_bands:
                    for f in params.product.extent_mask_func:
                        if extent_mask is None:
                            extent_mask = f(data, band)
                        else:
                            extent_mask &= f(data, band)

            if data is not None:
                body = _write_png(data, pq_data, params.style, extent_mask)
            else:
                body = _write_empty(params.geobox)
        release_cube(dc)
    except Exception as e:
        release_cube(dc)
        raise e
    return body, 200, resp_headers({"Content-Type": "image/png"})