Created
June 3, 2024 13:10
-
-
Save vincentsarago/a0afb7fc6573cde4efbcff2728da0acd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import click | |
from rasterio.rio import options | |
from rio_tiler.io import Reader | |
import morecantile | |
@click.command() | |
@options.file_in_arg | |
@click.option( | |
"--tms", | |
help="Path to TileMatrixSet JSON file.", | |
type=click.Path(), | |
) | |
@click.option("--output", "-o", type=click.Path(exists=False, dir_okay=True), help="Output directory") | |
def main(input, tms, output): | |
if not os.path.exists(output): | |
os.makedirs(output) | |
tilematrixset = morecantile.tms.get("WebMercatorQuad") | |
if tms: | |
with open(tms, "r") as f: | |
tilematrixset = morecantile.TileMatrixSet(**json.load(f)) | |
with Reader(input) as src: | |
w, s, e, n = src.geographic_bounds | |
w = max(tilematrixset.bbox.left, w) | |
s = max(tilematrixset.bbox.bottom, s) | |
e = min(tilematrixset.bbox.right, e) | |
n = min(tilematrixset.bbox.top, n) | |
for zoom in range(src.minzoom, src.maxzoom + 1): | |
ul_tile = tilematrixset.tile(w, n, zoom) | |
lr_tile = tilematrixset.tile(e, s, zoom) | |
for x in range(ul_tile.x, lr_tile.x + 1): | |
for y in range(ul_tile.y, lr_tile.y + 1): | |
tile = src.tile(zoom, x, y) | |
with open(os.path.join(output, f"{z}-{x}-{y}.png"), "wb") as f: | |
f.write(tile.render(img_format="PNG")) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment