import os import tempfile import py7zr def ConstruitMNT(nom, listeLambert, dossier, reduction=1): """ Construit un fichier RCL simple à partir de tuiles Lambert. Format RCL : ligne 1 : facteur ligne 2 : largeur ligne 3 : hauteur ligne 4 : liste des altitudes reduction : 1 = résolution originale 2 = moyenne sur blocs 2x2 5 = moyenne sur blocs 5x5 """ dossier = dossier + "MNT" if reduction < 1: raise ValueError("La réduction doit être >= 1") taille = int(len(listeLambert) ** 0.5) if taille * taille != len(listeLambert): raise ValueError("La liste ne forme pas un carré complet") terrain = [] with tempfile.TemporaryDirectory() as tmp: for ligne_tuile in range(taille): ligne_fichiers = listeLambert[ ligne_tuile * taille: (ligne_tuile + 1) * taille ] fichiers = [] for lambert in ligne_fichiers: archive_path = os.path.join( dossier, f"RGEALTI_FXX_{lambert.replace(';','_')}_MNT_LAMB93_IGN69.asc.7z" ) with py7zr.SevenZipFile( archive_path, "r" ) as archive: noms = archive.getnames() archive.extractall(tmp) nom_asc = None for fichier in noms: if fichier.endswith(".asc"): nom_asc = os.path.join(tmp, fichier) break f = open(nom_asc, "r") for _ in range(6): next(f) fichiers.append(f) for _ in range(1000): ligne = [] for f in fichiers: ligne.extend( float(x) for x in f.readline().split() ) terrain.append(ligne) for f in fichiers: f.close() # ------------------------------- # Réduction par moyenne # ------------------------------- if reduction > 1: nouvelle_hauteur = len(terrain) // reduction nouvelle_largeur = len(terrain[0]) // reduction nouveau = [] for y in range(nouvelle_hauteur): ligne = [] for x in range(nouvelle_largeur): somme = 0 nombre = 0 for yy in range( y * reduction, (y + 1) * reduction ): for xx in range( x * reduction, (x + 1) * reduction ): somme += terrain[yy][xx] nombre += 1 ligne.append( somme / nombre ) nouveau.append(ligne) terrain = nouveau hauteur = len(terrain) largeur = len(terrain[0]) donnees = [] for ligne in terrain: donnees.extend(ligne) with open(nom + ".rcl", "w") as f: f.write("1\n") f.write(str(largeur) + "\n") f.write(str(hauteur) + "\n") f.write(str(donnees)) print( f"RCL créé : {nom}.rcl " f"({largeur} x {hauteur})" )