|
| 1 | +import argparse |
| 2 | +from pathlib import Path |
| 3 | +from alive_progress import alive_bar # type: ignore |
| 4 | + |
| 5 | +import yirgacheffe as yg |
| 6 | +from yirgacheffe.layers import RescaledRasterLayer |
| 7 | + |
| 8 | +def resize_cglo( |
| 9 | + projection: yg.MapProjection, |
| 10 | + cglo_path: Path, |
| 11 | + output_path: Path, |
| 12 | +) -> None: |
| 13 | + with yg.read_rasters(list(cglo_path.glob("*.tif"))) as cglo_30: |
| 14 | + rescaled = RescaledRasterLayer(cglo_30, projection, nearest_neighbour=False) |
| 15 | + with alive_bar(manual=True) as bar: |
| 16 | + rescaled.to_geotiff(output_path, parallelism=True, callback=bar) |
| 17 | + |
| 18 | +def make_hybrid_elevation_map( |
| 19 | + fabdem_path: Path, |
| 20 | + fabdem_patch_path: Path, |
| 21 | + cglo_path: Path, |
| 22 | + output_path: Path, |
| 23 | +) -> None: |
| 24 | + output_path.parent.mkdir(parents=True, exist_ok=True) |
| 25 | + |
| 26 | + tmpdir = output_path.parent |
| 27 | + |
| 28 | + # The CGLO files are at a different resolution to FABDEM, so we first |
| 29 | + # need to scale them. |
| 30 | + resized_cglo_path = tmpdir / "cglo.tif" |
| 31 | + if not resized_cglo_path.exists(): |
| 32 | + # Get the map projection and pixel scale for fabdem |
| 33 | + fabdem_example_tile = list(fabdem_path.glob("*.tif"))[0] |
| 34 | + with yg.read_raster(fabdem_example_tile) as example: |
| 35 | + projection = example.map_projection |
| 36 | + |
| 37 | + resize_cglo(projection, cglo_path, resized_cglo_path) |
| 38 | + |
| 39 | + # Now we build up a large group layer, and rely on the fact that |
| 40 | + # Yirgacheffe will render earlier layers over later layers |
| 41 | + file_list = list(fabdem_patch_path.glob("*.tif")) + \ |
| 42 | + list(fabdem_path.glob("*.tif")) + \ |
| 43 | + [resized_cglo_path] |
| 44 | + |
| 45 | + full_layer = yg.read_rasters(file_list) |
| 46 | + |
| 47 | + with alive_bar(manual=True) as bar: |
| 48 | + full_layer.to_geotiff(output_path, parallelism=256, callback=bar) |
| 49 | + |
| 50 | +def main() -> None: |
| 51 | + parser = argparse.ArgumentParser(description="Convert IUCN crosswalk to minimal common format.") |
| 52 | + parser.add_argument( |
| 53 | + '--fabdem_tiles', |
| 54 | + type=Path, |
| 55 | + help="Directory containing original FABDEM tiles", |
| 56 | + required=True, |
| 57 | + dest="fabdem_path", |
| 58 | + ) |
| 59 | + parser.add_argument( |
| 60 | + '--fabdem_patch_tiles', |
| 61 | + type=Path, |
| 62 | + help="Directory containing original FABDEM errata tiles", |
| 63 | + required=True, |
| 64 | + dest="fabdem_patch_path", |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + '--cglo_tiles', |
| 68 | + type=Path, |
| 69 | + help="Directory containing missing_cglo tiles", |
| 70 | + required=True, |
| 71 | + dest="cglo_path", |
| 72 | + ) |
| 73 | + parser.add_argument( |
| 74 | + '--output', |
| 75 | + type=Path, |
| 76 | + help="Final output raster", |
| 77 | + required=True, |
| 78 | + dest='output_path', |
| 79 | + ) |
| 80 | + args = parser.parse_args() |
| 81 | + |
| 82 | + make_hybrid_elevation_map( |
| 83 | + args.fabdem_path, |
| 84 | + args.fabdem_patch_path, |
| 85 | + args.cglo_path, |
| 86 | + args.output_path, |
| 87 | + ) |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + main() |
0 commit comments