terrain2/i.py
Fabio Micheluz 9603735519 first commit
2026-02-12 17:21:04 +01:00

74 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import boto3
import os
import logging
from botocore import UNSIGNED
from botocore.config import Config
BUCKET = "copernicus-dem-30m"
OUTPUT_DIR = "./glo30_italia"
LOG_FILE = "download.log"
lat_range = range(36, 48) # N36N47
lon_range = range(6, 19) # E006E018
logging.basicConfig(
filename=LOG_FILE,
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
)
console = logging.getLogger("console")
console.setLevel(logging.INFO)
console.addHandler(logging.StreamHandler())
def log(msg):
logging.info(msg)
console.info(msg)
os.makedirs(OUTPUT_DIR, exist_ok=True)
s3 = boto3.client("s3", config=Config(signature_version=UNSIGNED))
def folder_name(lat, lon):
return f"Copernicus_DSM_COG_10_N{lat:02d}_00_E{lon:03d}_00_DEM/"
log("Inizio scansione delle directory italiane...")
found_folders = []
paginator = s3.get_paginator("list_objects_v2")
for page in paginator.paginate(Bucket=BUCKET, Delimiter="/"):
for prefix in page.get("CommonPrefixes", []):
folder = prefix["Prefix"]
# Controlla se la cartella è italiana
for lat in lat_range:
for lon in lon_range:
if folder == folder_name(lat, lon):
found_folders.append(folder)
log(f"[FOUND] {folder}")
log(f"Trovate {len(found_folders)} cartelle italiane")
# -----------------------------
# DOWNLOAD DEM PRINCIPALE
# -----------------------------
for folder in found_folders:
tif_name = folder[:-1] + ".tif" # rimuove "/" e aggiunge .tif
key = folder + tif_name.split("/")[-1]
out_path = os.path.join(OUTPUT_DIR, tif_name.split("/")[-1])
if os.path.exists(out_path):
log(f"[SKIP] {out_path} già presente")
continue
log(f"[DOWNLOAD] {key}")
try:
s3.download_file(BUCKET, key, out_path)
log(f"[OK] {key} scaricato")
except Exception as e:
log(f"[ERROR] {key}{e}")
log("Completato.")