Exemplo n.º 1
0
def execute(mapchete_files,
            zoom=None,
            bounds=None,
            point=None,
            wkt_geometry=None,
            tile=None,
            overwrite=False,
            multi=None,
            input_file=None,
            logfile=None,
            verbose=False,
            no_pbar=False,
            debug=False,
            max_chunksize=None,
            vrt=False,
            idx_out_dir=None):
    """Execute a Mapchete process."""
    mode = "overwrite" if overwrite else "continue"
    # send verbose messages to /dev/null if not activated
    verbose_dst = open(os.devnull, 'w') if debug or not verbose else sys.stdout

    for mapchete_file in mapchete_files:
        tqdm.tqdm.write("preparing to process %s" % mapchete_file,
                        file=verbose_dst)
        # process single tile
        if tile:
            utils._process_single_tile(
                raw_conf_process_pyramid=raw_conf_process_pyramid,
                mapchete_config=mapchete_file,
                tile=tile,
                mode=mode,
                input_file=input_file,
                debug=debug,
                verbose_dst=verbose_dst,
                vrt=vrt,
                idx_out_dir=idx_out_dir,
                no_pbar=no_pbar)
        # process area
        else:
            utils._process_area(
                debug=debug,
                mapchete_config=mapchete_file,
                mode=mode,
                zoom=zoom,
                wkt_geometry=wkt_geometry,
                point=point,
                bounds=bounds,
                input_file=input_file,
                multi=multi or cpu_count(),
                verbose_dst=verbose_dst,
                max_chunksize=max_chunksize,
                no_pbar=no_pbar,
                vrt=vrt,
                idx_out_dir=idx_out_dir,
            )
Exemplo n.º 2
0
def convert(input_,
            output,
            zoom=None,
            bounds=None,
            bounds_crs=None,
            area=None,
            area_crs=None,
            point=None,
            point_crs=None,
            wkt_geometry=None,
            clip_geometry=None,
            bidx=None,
            output_pyramid=None,
            output_metatiling=None,
            output_format=None,
            output_dtype=None,
            output_geometry_type=None,
            creation_options=None,
            scale_ratio=None,
            scale_offset=None,
            resampling_method=None,
            overviews=False,
            overviews_resampling_method=None,
            cog=False,
            overwrite=False,
            logfile=None,
            verbose=False,
            no_pbar=False,
            debug=False,
            multi=None,
            vrt=False,
            idx_out_dir=None):
    try:
        input_info = _get_input_info(input_)
        output_info = _get_output_info(output)
    except Exception as e:
        raise click.BadArgumentUsage(e)

    # collect mapchete configuration
    mapchete_config = dict(
        process="mapchete.processes.convert",
        input=dict(inp=input_, clip=clip_geometry),
        pyramid=(dict(grid=output_pyramid,
                      metatiling=(output_metatiling or
                                  (input_info["pyramid"].get("metatiling", 1)
                                   if input_info["pyramid"] else 1)),
                      pixelbuffer=(input_info["pyramid"].get("pixelbuffer", 0)
                                   if input_info["pyramid"] else 0))
                 if output_pyramid else input_info["pyramid"]),
        output=dict(
            {
                k: v
                for k, v in input_info["output_params"].items()
                if k not in ["delimiters", "bounds", "mode"]
            },
            path=output,
            format=(output_format or output_info["driver"]
                    or input_info["output_params"]["format"]),
            dtype=output_dtype or input_info["output_params"].get("dtype"),
            **creation_options,
            **dict(overviews=True,
                   overviews_resampling=overviews_resampling_method)
            if overviews else dict(),
        ),
        config_dir=os.getcwd(),
        zoom_levels=zoom or input_info["zoom_levels"],
        scale_ratio=scale_ratio,
        scale_offset=scale_offset,
        resampling=resampling_method,
        band_indexes=bidx)

    # assert all required information is there
    if mapchete_config["output"]["format"] is None:
        # this happens if input file is e.g. JPEG2000 and output is a tile directory
        raise click.BadOptionUsage("output-format", "Output format required.")
    if mapchete_config["output"]["format"] == "GTiff":
        mapchete_config["output"].update(cog=cog)
    output_type = OUTPUT_FORMATS[mapchete_config["output"]
                                 ["format"]]["data_type"]
    if bidx is not None:
        mapchete_config["output"].update(bands=len(bidx))
    if mapchete_config["pyramid"] is None:
        raise click.BadOptionUsage("output-pyramid",
                                   "Output pyramid required.")
    elif mapchete_config["zoom_levels"] is None:
        try:
            mapchete_config.update(zoom_levels=dict(
                min=0,
                max=get_best_zoom_level(input_, mapchete_config["pyramid"]
                                        ["grid"])))
        except:
            raise click.BadOptionUsage("zoom", "Zoom levels required.")
    elif input_info["input_type"] != output_type:
        raise click.BadArgumentUsage(
            "Output format type (%s) is incompatible with input format (%s)." %
            (output_type, input_info["input_type"]))
    if output_metatiling:
        mapchete_config["output"].update(metatiling=output_metatiling)
    if input_info["output_params"].get("schema") and output_geometry_type:
        mapchete_config["output"]["schema"].update(
            geometry=output_geometry_type)

    # determine process bounds
    out_pyramid = BufferedTilePyramid.from_dict(mapchete_config["pyramid"])
    inp_bounds = (bounds or reproject_geometry(box(*input_info["bounds"]),
                                               src_crs=input_info["crs"],
                                               dst_crs=out_pyramid.crs).bounds
                  if input_info["bounds"] else out_pyramid.bounds)
    # if clip-geometry is available, intersect determined bounds with clip bounds
    if clip_geometry:
        clip_intersection = _clip_bbox(clip_geometry,
                                       dst_crs=out_pyramid.crs).intersection(
                                           box(*inp_bounds))
        if clip_intersection.is_empty:
            click.echo(
                "Process area is empty: clip bounds don't intersect with input bounds."
            )
            return
    # add process bounds and output type
    mapchete_config.update(
        bounds=(clip_intersection.bounds if clip_geometry else inp_bounds),
        bounds_crs=bounds_crs,
        clip_to_output_dtype=mapchete_config["output"].get("dtype", None))
    logger.debug("temporary config generated: %s", pformat(mapchete_config))

    utils._process_area(debug=debug,
                        mapchete_config=mapchete_config,
                        mode="overwrite" if overwrite else "continue",
                        zoom=zoom,
                        wkt_geometry=wkt_geometry,
                        point=point,
                        point_crs=point_crs,
                        bounds=bounds,
                        bounds_crs=bounds_crs,
                        area=area,
                        area_crs=area_crs,
                        multi=multi or cpu_count(),
                        verbose_dst=open(os.devnull, 'w')
                        if debug or not verbose else sys.stdout,
                        no_pbar=no_pbar,
                        vrt=vrt,
                        idx_out_dir=idx_out_dir)