122 lines
3.3 KiB
Python
122 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
import sqlite3
|
|
import json
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
EPS = 1e-6 # tolleranza per confronti float
|
|
|
|
def almost_equal(a, b):
|
|
return abs(a - b) < EPS
|
|
|
|
def read_gdal_bounds(raster):
|
|
"""Legge i bounds W,S,E,N dal TIFF usando gdalinfo -json, arrotondati a 6 decimali."""
|
|
info = subprocess.check_output(["gdalinfo", "-json", raster])
|
|
info = json.loads(info)
|
|
|
|
if "wgs84Extent" in info:
|
|
coords = info["wgs84Extent"]["coordinates"][0]
|
|
minlon, minlat = coords[0]
|
|
maxlon, maxlat = coords[2]
|
|
else:
|
|
cc = info["cornerCoordinates"]
|
|
minlon, minlat = cc["lowerLeft"]
|
|
maxlon, maxlat = cc["upperRight"]
|
|
|
|
# Normalizzazione
|
|
if minlon > maxlon:
|
|
minlon, maxlon = maxlon, minlon
|
|
if minlat > maxlat:
|
|
minlat, maxlat = maxlat, minlat
|
|
|
|
# Arrotondamento a 6 decimali (standard MBTiles)
|
|
return (
|
|
round(minlon, 6),
|
|
round(minlat, 6),
|
|
round(maxlon, 6),
|
|
round(maxlat, 6),
|
|
)
|
|
|
|
def read_mbtiles_bounds(cur):
|
|
cur.execute("SELECT value FROM metadata WHERE name='bounds';")
|
|
row = cur.fetchone()
|
|
if not row:
|
|
return None
|
|
try:
|
|
w, s, e, n = map(float, row[0].split(","))
|
|
return (w, s, e, n)
|
|
except:
|
|
return None
|
|
|
|
def write_bounds(cur, bounds):
|
|
w, s, e, n = bounds
|
|
value = f"{w:.6f},{s:.6f},{e:.6f},{n:.6f}"
|
|
cur.execute("REPLACE INTO metadata (name,value) VALUES ('bounds', ?)", (value,))
|
|
return value
|
|
|
|
def main():
|
|
if len(sys.argv) != 3:
|
|
print("Uso: python3 verify_bounds_tileservergl.py <raster.tif> <file.mbtiles>")
|
|
sys.exit(1)
|
|
|
|
raster = Path(sys.argv[1])
|
|
mbtiles = Path(sys.argv[2])
|
|
|
|
if not raster.exists():
|
|
print(f"Raster non trovato: {raster}")
|
|
sys.exit(1)
|
|
if not mbtiles.exists():
|
|
print(f"MBTiles non trovato: {mbtiles}")
|
|
sys.exit(1)
|
|
|
|
print("== Verifica bounds per TileServer GL (W,S,E,N) ==")
|
|
|
|
# Bounds dal TIFF
|
|
tiff_bounds = read_gdal_bounds(raster)
|
|
print(f"Bounds TIFF (W,S,E,N): {tiff_bounds}")
|
|
|
|
# Apri MBTiles
|
|
conn = sqlite3.connect(mbtiles)
|
|
cur = conn.cursor()
|
|
|
|
# Assicura tabella metadata
|
|
cur.execute("""
|
|
CREATE TABLE IF NOT EXISTS metadata (
|
|
name TEXT PRIMARY KEY,
|
|
value TEXT
|
|
);
|
|
""")
|
|
|
|
# Bounds MBTiles
|
|
mb_bounds = read_mbtiles_bounds(cur)
|
|
print(f"Bounds MBTiles: {mb_bounds}")
|
|
|
|
# Se mancano → scrivili
|
|
if mb_bounds is None:
|
|
print("✘ Bounds mancanti → FIX")
|
|
new_value = write_bounds(cur, tiff_bounds)
|
|
print(f"✔ Bounds scritti: {new_value}")
|
|
conn.commit()
|
|
conn.close()
|
|
print("\n== Verifica completata ==")
|
|
return
|
|
|
|
# Confronto con tolleranza
|
|
w1, s1, e1, n1 = tiff_bounds
|
|
w2, s2, e2, n2 = mb_bounds
|
|
|
|
if not (almost_equal(w1, w2) and almost_equal(s1, s2) and almost_equal(e1, e2) and almost_equal(n1, n2)):
|
|
print("✘ Bounds NON corretti → FIX")
|
|
new_value = write_bounds(cur, tiff_bounds)
|
|
print(f"✔ Bounds aggiornati a: {new_value}")
|
|
conn.commit()
|
|
else:
|
|
print("✔ Bounds OK (nessuna correzione necessaria)")
|
|
|
|
conn.close()
|
|
print("\n== Verifica completata ==")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|