예제 #1
0
def generate_tiles(
    layers: List[ProjectLayer],
    stylesheets: List[str],
    bbox: BBOX,
    profile_name: str,
    zoom_min: int,
    zoom_max: int,
    run_id: str,
) -> GenerateResult:
    logging.info("Generating tiles from source data")
    stylesheet_content = list()
    for stylesheet in stylesheets:
        with open(get_style_path(f"{stylesheet}.mss"), "r") as f:
            stylesheet_content.append(f.read())
    project_properties = ProjectProperties(
        bbox=bbox, zoom_min=zoom_min, zoom_max=zoom_max, name=profile_name
    )
    if len(layers) > 0:
        project_creation_properties = ProjectCreationProperties(
            layers=layers, mss=stylesheet_content, **dict(project_properties)
        )
        tilemill_url = os.environ.get("TILEMILL_URL", "http://localhost:20009")
        create_or_update_project(tilemill_url, project_creation_properties)
        export_file = request_export(tilemill_url, project_properties)
        result_dir_temp = get_result_path((run_id,))
        logging.info("Calling mb-util")
        _, stderr = subprocess.Popen(
            [
                os.environ["MBUTIL_LOCATION"],
                get_export_path((export_file,)),
                result_dir_temp,
            ],
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT,
        ).communicate()
        if stderr:
            for line in stderr.decode("ascii").split(os.linesep):
                logging.warn(line)
        logging.info("mb-util complete")
        if remove_intermediaries():
            delete_directory_contents(get_export_path())
        return GenerateResult(
            tile_dir=result_dir_temp,
            tile_paths=[
                filename
                for filename in glob.iglob(
                    os.path.join(result_dir_temp, "**", "*.png"), recursive=True
                )
            ],
        )
예제 #2
0
def provision(arg: ProvisionArg) -> ProvisionResult:
    bbox, profile_name, xyz_url = arg.bbox, arg.profile_name, arg.xyz_url
    bbox_exists = has_prior_run(get_result_path((profile_name, )), bbox)
    if bbox_exists and arg.skippable:
        logging.info(
            f"Skipping {profile_name} {bbox.min_x},{bbox.min_y} {bbox.max_x},{bbox.max_y} as it already exists"
        )
        return ProvisionResult.SKIPPED
    else:
        logging.info(
            f"Provisioning {profile_name} {bbox.min_x},{bbox.min_y} {bbox.max_x},{bbox.max_y}"
        )

    run_id = str(uuid.uuid4())
    profiles = {
        profile.NAME: {
            "execute": profile.execute,
            "zoom_min": profile.ZOOM_MIN,
            "zoom_max": profile.ZOOM_MAX,
            "format": profile.OUTPUT_FORMAT,
        }
        for profile in [xyz, topo, xyzsummer, xyzwinter, xyzhunting]
    }
    profiles[profile_name]["execute"](bbox, run_id, {"xyz_url": xyz_url})
    if int(os.environ.get("BVSAR_HEAD_VALIDATE", 0)) == 1:
        logging.info("Validating result")
        check_exists(
            xyz_check_builder(
                bbox,
                "{0}/{1}/{{z}}/{{x}}/{{y}}.png".format(
                    os.environ.get("HTTP_URL", "http://rpi/tile/file"),
                    profile_name,
                ),
                profiles[profile_name]["zoom_min"],
                profiles[profile_name]["zoom_max"],
                "image/{0}".format(profiles[profile_name]["format"]),
            ))
    record_run(get_result_path((profile_name, )), bbox)
    if remove_intermediaries():
        run_dir = get_run_data_path(run_id, None)
        result_temp_dir = get_result_path((run_id, ))
        if os.path.exists(run_dir):
            rmtree(run_dir)
        if os.path.exists(result_temp_dir):
            rmtree(result_temp_dir)
    logging.info("Finished")
    return ProvisionResult.SUCCESS
예제 #3
0
def provision(bbox: BBOX, profile_name: str, xyz_url: str) -> None:
    bbox_repeat_if_exists = int(os.environ.get("BBOX_REPEAT_IF_EXISTS",
                                               0)) == 1
    bbox_exists = has_prior_run(get_result_path((profile_name, )), bbox)
    if bbox_exists and not bbox_repeat_if_exists:
        logging.info(
            f"Skipping {profile_name} {bbox.min_x},{bbox.min_y} {bbox.max_x},{bbox.max_y} as it already exists"
        )
        return

    run_id = str(uuid.uuid4())
    profiles = {
        profile.NAME: {
            "execute": profile.execute,
            "zoom_min": profile.ZOOM_MIN,
            "zoom_max": profile.ZOOM_MAX,
            "format": profile.OUTPUT_FORMAT,
        }
        for profile in [xyz, topo, xyzsummer, xyzwinter]
    }
    profiles[profile_name]["execute"](bbox, run_id, {"xyz_url": xyz_url})
    logging.info("Validating result")
    check_exists(
        xyz_check_builder(
            bbox,
            "{0}/{1}/{{z}}/{{x}}/{{y}}.png".format(
                os.environ.get("HTTP_URL",
                               "http://localhost:9000/tiles/files"),
                profile_name,
            ),
            profiles[profile_name]["zoom_min"],
            profiles[profile_name]["zoom_max"],
            "image/{0}".format(profiles[profile_name]["format"]),
        ))
    record_run(get_result_path((profile_name, )), bbox)
    if remove_intermediaries():
        run_dir = get_run_data_path(run_id, None)
        result_temp_dir = get_result_path((run_id, ))
        if os.path.exists(run_dir):
            rmtree(run_dir)
        if os.path.exists(result_temp_dir):
            rmtree(result_temp_dir)
    logging.info("Finished")
예제 #4
0
def _convert_to_tif(
    partial_coverage_tiles: List[PartialCoverageTile], wms_crs_code: str
) -> None:
    for tile in partial_coverage_tiles:
        if os.path.exists(tile.wms_path):
            logging.debug(f"Converting {tile.wms_path} to {tile.tif_path}")
            src_file = Open(tile.wms_path, GA_ReadOnly)
            Translate(
                tile.tif_path,
                src_file,
                format="GTiff",
                noData=None,
                outputSRS=wms_crs_code,
                # Translate expects bounds in format ulX, ulY, lrX, lrY so flip minY and maxY
                outputBounds=(tile.x_min, tile.y_max, tile.x_max, tile.y_min),
            )
            if remove_intermediaries():
                os.remove(tile.wms_path)
        else:
            logging.warn(f"Expected file {tile.wms_path} does not exist")
            with zipfile.ZipFile(generation_request.path, "r") as zip_ref:
                zip_ref.extract(generation_request.tif_name,
                                get_cache_path((CACHE_DIR_NAME, )))
            Warp(
                generation_request.prj_path,
                generation_request.tif_path,
                cutlineDSName=get_data_path(("grids.gpkg", )),
                cutlineLayer="BC-20000",
                cutlineWhere=f"MAP_TILE = '{generation_request.cell_name}'",
                cropToCutline=False,
                cutlineBlend=1,
                dstNodata=-1,
                dstSRS=OUTPUT_CRS_CODE,
                resampleAlg="lanczos",
            )
            if remove_intermediaries():
                os.remove(generation_request.path)
                os.remove(generation_request.tif_path)
        except Exception as ex:
            logging.error(ex)

    for generation_request in bbox_cells:
        try:
            Warp(
                generation_request.run_path,
                generation_request.prj_path,
                cutlineDSName=get_datasource_from_bbox(
                    bbox, get_run_data_path(run_id, None)),
                cutlineLayer=BBOX_LAYER_NAME,
                cropToCutline=False,
                cutlineBlend=1,