58 lines
1.6 KiB
Python
58 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
import os
|
|
import sys
|
|
import mercantile
|
|
from rio_tiler.io import COGReader
|
|
from rio_tiler.utils import render
|
|
|
|
# ---------------------------------------------------------
|
|
# USO:
|
|
# python3 make_tiles_rio_tiler.py input_rgb.tif tiles_out/
|
|
#
|
|
# Genera tile PNG Terrain-RGB senza alterare i pixel.
|
|
# ---------------------------------------------------------
|
|
|
|
ZOOM_MIN = 5
|
|
ZOOM_MAX = 14
|
|
|
|
def generate_tiles(input_tif, output_dir):
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
with COGReader(input_tif) as cog:
|
|
for z in range(ZOOM_MIN, ZOOM_MAX + 1):
|
|
print(f"== Zoom {z} ==")
|
|
|
|
bounds = cog.bounds
|
|
tiles = list(mercantile.tiles(bounds[0], bounds[1], bounds[2], bounds[3], z))
|
|
|
|
for t in tiles:
|
|
out_dir_zxy = os.path.join(output_dir, str(z), str(t.x))
|
|
os.makedirs(out_dir_zxy, exist_ok=True)
|
|
out_path = os.path.join(out_dir_zxy, f"{t.y}.png")
|
|
|
|
tile, mask = cog.tile(t.x, t.y, z, resampling_method="nearest")
|
|
|
|
img = render(tile, mask=mask, img_format="PNG")
|
|
|
|
with open(out_path, "wb") as f:
|
|
f.write(img)
|
|
|
|
print(f"Tile {z}/{t.x}/{t.y} generata")
|
|
|
|
print("Completato.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 3:
|
|
print("Uso: python3 make_tiles_rio_tiler.py <input_rgb.tif> <output_dir>")
|
|
sys.exit(1)
|
|
|
|
INPUT_TIF = sys.argv[1]
|
|
OUTPUT_DIR = sys.argv[2]
|
|
|
|
if not os.path.isfile(INPUT_TIF):
|
|
print(f"Errore: file non trovato: {INPUT_TIF}")
|
|
sys.exit(1)
|
|
|
|
generate_tiles(INPUT_TIF, OUTPUT_DIR)
|
|
|