Esempio n. 1
0
def execute(bbox: BBOX, run_id: str, args: Dict[str, object] = dict()) -> None:
    xyz_result = xyz_provisioner(
        bbox,
        args["xyz_url"],
        ZOOM_MIN,
        ZOOM_MAX,
        ["image/png", "image/jpeg"],
        OUTPUT_FORMAT,
    )
    tmp_dir = get_run_data_path(run_id, (NAME, ))
    tmp_tile_paths = list()
    logging.info(
        f"Copying {len(xyz_result.tile_paths)} {NAME} tiles to tmp dir for edge clipping"
    )
    for tile_path in xyz_result.tile_paths:
        tmp_tile_path = tile_path.replace(xyz_result.tile_dir, tmp_dir)
        tmp_tile_paths.append(tmp_tile_path)
        os.makedirs(os.path.dirname(tmp_tile_path), exist_ok=True)
        copyfile(tile_path, tmp_tile_path)
    transparent_clip_to_bbox(
        [
            os.path.join(tmp_dir, tile_path)
            for tile_path in get_edge_tiles(tmp_dir)
        ],
        bbox,
    )
    add_or_update(tmp_dir, get_result_path((NAME, )))
Esempio n. 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
Esempio n. 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")
Esempio n. 4
0
def execute(bbox: BBOX, run_id: str, args: Dict[str, object] = dict()) -> None:
    layers = (canvec(
        bbox,
        run_id,
        (10000000, 4000000, 2000000, 1000000, 500000, 250000, 150000, 70000,
         35000),
    ) + bc_topo(bbox, run_id) + bc_hillshade(bbox, run_id) +
              bc_resource_roads(bbox, run_id) + trails(bbox, run_id) +
              shelters(bbox, run_id))
    tile_output_dir = generate_tiles(layers, ["common", "topo"], bbox, NAME,
                                     ZOOM_MIN, ZOOM_MAX, run_id).tile_dir
    logging.info("Transferring generated tile set to result directory")
    add_or_update(tile_output_dir, get_result_path((NAME, )))
Esempio n. 5
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
                )
            ],
        )
Esempio n. 6
0
def execute(
        bbox: BBOX,
        run_id: str,
        xyz_url: str,
        profile_name: str,
        layers: List[ProjectLayer],
        extra_styles: List[str] = list(),
) -> None:
    xyz_result = xyz_provisioner(bbox, xyz_url, ZOOM_MIN, ZOOM_MAX,
                                 ["image/png", "image/jpeg"], OUTPUT_FORMAT)
    generate_result = generate_tiles(
        layers,
        ["common", profile_name] + extra_styles,
        bbox,
        profile_name,
        ZOOM_MIN,
        ZOOM_MAX,
        run_id,
    )
    xyz_tiles_merged = list()
    logging.info(
        f"Merging {len(generate_result.tile_paths)} generated tile(s) to xyz base"
    )
    path_tuples = [(
        data_tile.replace(generate_result.tile_dir, xyz_result.tile_dir),
        data_tile,
        data_tile,
    ) for data_tile in generate_result.tile_paths]
    merge_tiles(path_tuples)
    xyz_tiles_merged = [path_tuple[0] for path_tuple in path_tuples]
    logging.info("Collecting non-generated xyz tiles")
    for xyz_tile in xyz_result.tile_paths:
        if xyz_tile not in xyz_tiles_merged:
            xyz_copy_path = xyz_tile.replace(xyz_result.tile_dir,
                                             generate_result.tile_dir)
            os.makedirs(os.path.dirname(xyz_copy_path), exist_ok=True)
            copyfile(xyz_tile, xyz_copy_path)
    transparent_clip_to_bbox(
        [
            os.path.join(generate_result.tile_dir, tile_path)
            for tile_path in get_edge_tiles(generate_result.tile_dir)
        ],
        bbox,
    )
    logging.info("Transferring combined tile set to result directory")
    add_or_update(generate_result.tile_dir, get_result_path((profile_name, )))
Esempio n. 7
0
def execute(bbox: BBOX, run_id: str, args: Dict[str, object] = dict()) -> None:
    layers = (canvec(
        bbox,
        run_id,
        (9244667, 4622334, 2311167, 1155583, 577792, 288896, 144448, 72224,
         36112),
    ) + bc_topo(bbox, run_id) + bc_hillshade(bbox, run_id) +
              bc_resource_roads(bbox, run_id) + trails(bbox, run_id) +
              shelters(bbox, run_id))
    generate_result = generate_tiles(layers, ["common", "topo"], bbox, NAME,
                                     ZOOM_MIN, ZOOM_MAX, run_id)
    transparent_clip_to_bbox(
        [
            os.path.join(generate_result.tile_dir, tile_path)
            for tile_path in get_edge_tiles(generate_result.tile_dir)
        ],
        bbox,
        False,
    )
    logging.info("Transferring generated tile set to result directory")
    add_or_update(generate_result.tile_dir, get_result_path((NAME, )), False)
def execute(bbox: BBOX, run_id: str, xyz_url: str, profile_name: str,
            layers: List[ProjectLayer]) -> None:
    xyz_result = xyz_provisioner(bbox, xyz_url, ZOOM_MIN, ZOOM_MAX,
                                 "image/jpeg", OUTPUT_FORMAT)
    generate_result = generate_tiles(layers, ["common", profile_name], bbox,
                                     profile_name, ZOOM_MIN, ZOOM_MAX, run_id)
    xyz_tiles_merged = list()
    logging.info(
        f"Merging {len(generate_result.tile_paths)} generated tile(s) to xyz base"
    )
    for data_tile in generate_result.tile_paths:
        matching_raw_xyz_tile = data_tile.replace(generate_result.tile_dir,
                                                  xyz_result.tile_dir)
        logging.debug(
            f"Base tile {matching_raw_xyz_tile}, overlay {data_tile}")
        merge_tiles(
            matching_raw_xyz_tile,
            data_tile,
            data_tile,
        )
        xyz_tiles_merged.append(matching_raw_xyz_tile)
    logging.info("Collecting non-generated xyz tiles")
    for xyz_tile in xyz_result.tile_paths:
        if xyz_tile not in xyz_tiles_merged:
            xyz_copy_path = xyz_tile.replace(xyz_result.tile_dir,
                                             generate_result.tile_dir)
            os.makedirs(os.path.dirname(xyz_copy_path), exist_ok=True)
            copyfile(xyz_tile, xyz_copy_path)
    transparent_clip_to_bbox(
        [
            os.path.join(generate_result.tile_dir, tile_path)
            for tile_path in get_edge_tiles(generate_result.tile_dir)
        ],
        bbox,
    )
    logging.info("Transferring combined tile set to result directory")
    add_or_update(generate_result.tile_dir, get_result_path((profile_name, )))