Exemple #1
0
def get_tile(x,
             y,
             z,
             model,
             scenario,
             year,
             style,
             indicator,
             layer,
             compare_year=None,
             dset_b=None):
    logging.info(f'Getting tile for {x} {y} {z}')
    logging.debug(compare_year)
    bbox = TileService.get_bbox(z, x, y)
    logging.debug(f"bbox: {bbox}")
    bounds = ColoringHelper.get_data_bounds(style)
    logging.debug(bounds)
    if compare_year:
        logging.debug(f"[rout] compare_year: {compare_year}")
        if not dset_b:
            dset_b = f"{scenario}_{model}_processed"
        rasterfile = QueryService.get_tile_diff_query(bbox, year, model,
                                                      scenario, indicator,
                                                      bounds, compare_year,
                                                      dset_b)
    else:
        rasterfile = QueryService.get_tile_query(bbox, year, model, scenario,
                                                 indicator, bounds)
    colored_response = ColoringHelper.colorize(rasterfile, style=style)

    # Saving file in cache
    logging.debug(f'Requested path is: {request.path}')

    # Uploading file to storage
    # Beware of side effects!
    # ColoringHelper.colorize stores the color-coded file in the same input file
    # Uploading file to storage.
    StorageService.upload_file(rasterfile, layer, str(z), str(x), str(y), year,
                               compare_year, dset_b)

    return colored_response, 200
def get_tile(z,
             x,
             y,
             model,
             scenario,
             year,
             style,
             indicator,
             layer,
             compare_year=None,
             dset_b=None,
             no_data=None):
    logging.info(f'Getting tile for {z} {x} {y}')
    logging.debug(compare_year)
    bbox = TileService.get_bbox(x, y, z)
    logging.debug(f"bbox: {bbox}")
    bounds = ColoringHelper.get_data_bounds(style)
    logging.debug(bounds)
    if compare_year:
        logging.debug(f"[rout] compare_year: {compare_year}")
        if not dset_b:
            dset_b = f"{scenario}_{model}_processed"
        rasterfile = QueryService.get_tile_diff_query(bbox, year, model,
                                                      scenario, indicator,
                                                      bounds, compare_year,
                                                      dset_b)
    else:
        rasterfile = QueryService.get_tile_query(bbox, year, model, scenario,
                                                 indicator, bounds)

    try:
        colored_response = ColoringHelper.colorize(rasterfile, style=style)
    except CoverageNotFound as e:
        return error(status=404, detail=e.message)

    logging.debug(f"colored_response: {colored_response}")
    # logging.debug(f"colored_response.shape: {colored_response.shape}")

    if no_data is not None:
        logging.debug("Creating mask")
        maskfile = QueryService.get_tile_mask_query(bbox, year, model,
                                                    scenario, indicator,
                                                    no_data)
        ColoringHelper.blend_alpha(colored_response, maskfile)
        os.remove(maskfile)
    else:
        logging.debug("No nodata values")

    # Saving file in cache
    logging.debug(f'Requested path is: {request.path}')

    # Uploading file to storage.
    StorageService.upload_file(colored_response, layer, str(z), str(x), str(y),
                               year, compare_year, dset_b)

    tile_response = send_file(io.BytesIO(open(colored_response, 'rb').read()),
                              attachment_filename='tile.png',
                              mimetype='image/png')

    os.remove(colored_response)
    return tile_response, 200