def computeSkyViewFactor(dem_input, image_output, svf_radius, svf_method, svf_dlevel, svf_ndirs, save_results_intermediate=False): if debug >= 2: print(cyan + "computeSkyViewFactor() : " + bold + "Calcul du Sky View Factor sous SAGA, à partir du fichier : " + endC + dem_input) # Constantes SUFFIX_TMP = "_tmp" SUFFIX_SAGA = "_saga" EXT_GRID_SAGA = '.sgrd' # Variables repertory_output = os.path.dirname(image_output) basename_dem_input = os.path.splitext(os.path.basename(dem_input))[0] basename_image_output = os.path.splitext(os.path.basename(image_output))[0] sub_repertory_temp_saga = repertory_output + os.sep + basename_image_output + SUFFIX_TMP + SUFFIX_SAGA dem_input_saga = sub_repertory_temp_saga + os.sep + basename_dem_input + EXT_GRID_SAGA image_output_saga = sub_repertory_temp_saga + os.sep + basename_image_output + EXT_GRID_SAGA # Nettoyage du répertoire temporaire cleanTempData(sub_repertory_temp_saga) # Import du DEM sous SAGA importGtiff2Sgrd(dem_input, dem_input_saga) # Utilisation de la fonction "Sky View Factor" de SAGA command = "saga_cmd ta_lighting 3 -DEM %s -SVF %s -RADIUS %s -METHOD %s -DLEVEL %s -NDIRS %s" % ( dem_input_saga, image_output_saga, svf_radius, svf_method, svf_dlevel, svf_ndirs) exitCode = os.system(command) if exitCode != 0: print(command) raise NameError( bold + red + "computeSkyViewFactor(): An error occured during execution SAGA (Sky View Factor) command to file '%s'. See error message above." + endC) % (str(dem_input_saga)) # Export du SVF sous SAGA exportSgrd2Gtiff(image_output_saga, image_output) # Suppression des fichiers intermédiaires if not save_results_intermediate: deleteDir(sub_repertory_temp_saga) return
def addDataBaseExo(image_input, image_classif_add_output, class_file_dico, class_buffer_dico, class_sql_dico, path_time_log, format_vector='ESRI Shapefile', extension_raster=".tif", extension_vector=".shp", save_results_intermediate=False, overwrite=True, simplifie_param=10.0): # Mise à jour du Log starting_event = "addDataBaseExo() : Add data base exogene to classification starting : " timeLine(path_time_log, starting_event) # Print if debug >= 3: print(bold + green + "Variables dans la fonction" + endC) print(cyan + "addDataBaseExo() : " + endC + "image_input : " + str(image_input) + endC) print(cyan + "addDataBaseExo() : " + endC + "image_classif_add_output : " + str(image_classif_add_output) + endC) print(cyan + "addDataBaseExo() : " + endC + "class_file_dico : " + str(class_file_dico) + endC) print(cyan + "addDataBaseExo() : " + endC + "class_buffer_dico : " + str(class_buffer_dico) + endC) print(cyan + "addDataBaseExo() : " + endC + "class_sql_dico : " + str(class_sql_dico) + endC) print(cyan + "addDataBaseExo() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "addDataBaseExo() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "addDataBaseExo() : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "addDataBaseExo() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "addDataBaseExo() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "addDataBaseExo() : " + endC + "overwrite : " + str(overwrite) + endC) # Constantes FOLDER_MASK_TEMP = 'Mask_' FOLDER_FILTERING_TEMP = 'Filt_' FOLDER_CUTTING_TEMP = 'Cut_' FOLDER_BUFF_TEMP = 'Buff_' SUFFIX_MASK_CRUDE = '_mcrude' SUFFIX_MASK = '_mask' SUFFIX_FUSION = '_info' SUFFIX_VECTOR_FILTER = "_filt" SUFFIX_VECTOR_CUT = '_decoup' SUFFIX_VECTOR_BUFF = '_buff' CODAGE = "uint16" # ETAPE 1 : NETTOYER LES DONNEES EXISTANTES if debug >= 2: print(cyan + "addDataBaseExo() : " + bold + green + "NETTOYAGE ESPACE DE TRAVAIL..." + endC) # Nom de base de l'image image_name = os.path.splitext(os.path.basename(image_input))[0] # Nettoyage d'anciennes données résultat # Si le fichier résultat existent deja et que overwrite n'est pas activé check = os.path.isfile(image_classif_add_output) if check and not overwrite: print(bold + yellow + "addDataBaseExo() : " + endC + image_classif_add_output + " has already added bd exo and will not be added again." + endC) else: if check: try: removeFile(image_classif_add_output ) # Tentative de suppression du fichier except Exception: pass # Si le fichier ne peut pas être supprimé, on suppose qu'il n'existe pas et on passe à la suite # Définition des répertoires temporaires repertory_output = os.path.dirname(image_classif_add_output) repertory_mask_temp = repertory_output + os.sep + FOLDER_MASK_TEMP + image_name repertory_samples_filtering_temp = repertory_output + os.sep + FOLDER_FILTERING_TEMP + image_name repertory_samples_cutting_temp = repertory_output + os.sep + FOLDER_CUTTING_TEMP + image_name repertory_samples_buff_temp = repertory_output + os.sep + FOLDER_BUFF_TEMP + image_name if debug >= 4: print(repertory_mask_temp) print(repertory_samples_filtering_temp) print(repertory_samples_cutting_temp) print(repertory_samples_buff_temp) # Creer les répertoires temporaire si ils n'existent pas if not os.path.isdir(repertory_output): os.makedirs(repertory_output) if not os.path.isdir(repertory_mask_temp): os.makedirs(repertory_mask_temp) if not os.path.isdir(repertory_samples_filtering_temp): os.makedirs(repertory_samples_filtering_temp) if not os.path.isdir(repertory_samples_cutting_temp): os.makedirs(repertory_samples_cutting_temp) if not os.path.isdir(repertory_samples_buff_temp): os.makedirs(repertory_samples_buff_temp) # Nettoyer les répertoires temporaire si ils ne sont pas vide cleanTempData(repertory_mask_temp) cleanTempData(repertory_samples_filtering_temp) cleanTempData(repertory_samples_cutting_temp) cleanTempData(repertory_samples_buff_temp) if debug >= 2: print(cyan + "addDataBaseExo() : " + bold + green + "... FIN NETTOYAGE" + endC) # ETAPE 2 : CREER UN SHAPE DE DECOUPE if debug >= 2: print(cyan + "addDataBaseExo() : " + bold + green + "SHAPE DE DECOUPE..." + endC) # 2.1 : Création des masques délimitant l'emprise de la zone par image vector_mask = repertory_mask_temp + os.sep + image_name + SUFFIX_MASK_CRUDE + extension_vector createVectorMask(image_input, vector_mask) # 2.2 : Simplification du masque global vector_simple_mask_cut = repertory_mask_temp + os.sep + image_name + SUFFIX_MASK + extension_vector simplifyVector(vector_mask, vector_simple_mask_cut, simplifie_param, format_vector) if debug >= 2: print(cyan + "addDataBaseExo() : " + bold + green + "...FIN SHAPE DE DECOUPEE" + endC) # ETAPE 3 : DECOUPER BUFFERISER LES VECTEURS ET FUSIONNER if debug >= 2: print(cyan + "addDataBaseExo() : " + bold + green + "MISE EN PLACE DES TAMPONS..." + endC) image_combined_list = [] # Parcours du dictionnaire associant les macroclasses aux noms de fichiers for macroclass_label in class_file_dico: vector_fusion_list = [] for index_info in range(len(class_file_dico[macroclass_label])): input_vector = class_file_dico[macroclass_label][index_info] vector_name = os.path.splitext( os.path.basename(input_vector))[0] output_vector_filtered = repertory_samples_filtering_temp + os.sep + vector_name + SUFFIX_VECTOR_FILTER + extension_vector output_vector_cut = repertory_samples_cutting_temp + os.sep + vector_name + SUFFIX_VECTOR_CUT + extension_vector output_vector_buff = repertory_samples_buff_temp + os.sep + vector_name + SUFFIX_VECTOR_BUFF + extension_vector sql_expression = class_sql_dico[macroclass_label][index_info] buffer_str = class_buffer_dico[macroclass_label][index_info] buff = 0.0 col_name_buf = "" try: buff = float(buffer_str) except: col_name_buf = buffer_str print( cyan + "addDataBaseExo() : " + bold + green + "Pas de valeur buffer mais un nom de colonne pour les valeur à bufferiser : " + endC + col_name_buf) if os.path.isfile(input_vector): if debug >= 3: print(cyan + "addDataBaseExo() : " + endC + "input_vector : " + str(input_vector) + endC) print(cyan + "addDataBaseExo() : " + endC + "output_vector_filtered : " + str(output_vector_filtered) + endC) print(cyan + "addDataBaseExo() : " + endC + "output_vector_cut : " + str(output_vector_cut) + endC) print(cyan + "addDataBaseExo() : " + endC + "output_vector_buff : " + str(output_vector_buff) + endC) print(cyan + "addDataBaseExo() : " + endC + "buff : " + str(buff) + endC) print(cyan + "addDataBaseExo() : " + endC + "sql : " + str(sql_expression) + endC) # 3.0 : Recuperer les vecteurs d'entrée et filtree selon la requete sql par ogr2ogr if sql_expression != "": names_attribut_list = getAttributeNameList( input_vector, format_vector) column = "'" for name_attribut in names_attribut_list: column += name_attribut + ", " column = column[0:len(column) - 2] column += "'" ret = filterSelectDataVector(input_vector, output_vector_filtered, column, sql_expression, format_vector) if not ret: print( cyan + "addDataBaseExo() : " + bold + yellow + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" % (sql_expression) + endC) output_vector_filtered = input_vector else: print(cyan + "addDataBaseExo() : " + bold + green + "Pas de filtrage sur le fichier du nom : " + endC + output_vector_filtered) output_vector_filtered = input_vector # 3.1 : Découper le vecteur selon l'empise de l'image d'entrée cutoutVectors(vector_simple_mask_cut, [output_vector_filtered], [output_vector_cut], format_vector) # 3.2 : Bufferiser lesvecteurs découpé avec la valeur défini dans le dico ou trouver dans la base du vecteur lui même si le nom de la colonne est passée dans le dico if os.path.isfile(output_vector_cut) and ( (buff != 0) or (col_name_buf != "")): bufferVector(output_vector_cut, output_vector_buff, buff, col_name_buf, 1.0, 10, format_vector) else: print(cyan + "addDataBaseExo() : " + bold + green + "Pas de buffer sur le fichier du nom : " + endC + output_vector_cut) output_vector_buff = output_vector_cut # 3.3 : Si un shape résulat existe l'ajouté à la liste de fusion if os.path.isfile(output_vector_buff): vector_fusion_list.append(output_vector_buff) if debug >= 3: print("file for fusion : " + output_vector_buff) else: print(bold + yellow + "pas de fichiers avec ce nom : " + endC + output_vector_buff) else: print(cyan + "addDataBaseExo() : " + bold + yellow + "Pas de fichier du nom : " + endC + input_vector) # 3.4 : Fusionner les shapes transformés d'une même classe, rasterization et labelisations des vecteurs # Si une liste de fichier shape existe if not vector_fusion_list: print(bold + yellow + "Pas de fusion sans donnee a fusionnee" + endC) else: # Rasterization et BandMath des fichiers shapes raster_list = [] for vector in vector_fusion_list: if debug >= 3: print(cyan + "addDataBaseExo() : " + endC + "Rasterization : " + vector + " label : " + macroclass_label) raster_output = os.path.splitext( vector)[0] + extension_raster # Rasterisation rasterizeBinaryVector(vector, image_input, raster_output, macroclass_label, CODAGE) raster_list.append(raster_output) if debug >= 3: print(cyan + "addDataBaseExo() : " + endC + "nombre d'images a combiner : " + str(len(raster_list))) # Liste les images raster combined and sample image_combined = repertory_output + os.sep + image_name + '_' + str( macroclass_label) + SUFFIX_FUSION + extension_raster image_combined_list.append(image_combined) # Fusion des images raster en une seule mergeListRaster(raster_list, image_combined, CODAGE) if debug >= 2: print(cyan + "addDataBaseExo() : " + bold + green + "FIN DE L AFFECTATION DES TAMPONS" + endC) # ETAPE 4 : ASSEMBLAGE DE L'IMAGE CLASSEE ET DES BD EXOS if debug >= 2: print(cyan + "addDataBaseExo() : " + bold + green + "ASSEMBLAGE..." + endC) # Ajout de l'image de classification a la liste des image bd conbinées image_combined_list.append(image_input) # Fusion les images avec la classification mergeListRaster(image_combined_list, image_classif_add_output, CODAGE) if debug >= 2: print(cyan + "addDataBaseExo() : " + bold + green + "FIN" + endC) # ETAPE 5 : SUPPRESIONS FICHIERS INTERMEDIAIRES INUTILES # Suppression des données intermédiaires if not save_results_intermediate: image_combined_list.remove(image_input) for to_delete in image_combined_list: removeFile(to_delete) # Suppression des repertoires temporaires deleteDir(repertory_mask_temp) deleteDir(repertory_samples_filtering_temp) deleteDir(repertory_samples_cutting_temp) deleteDir(repertory_samples_buff_temp) # Mise à jour du Log ending_event = "addDataBaseExo() : Add data base exogene to classification ending : " timeLine(path_time_log, ending_event) return
def soilOccupationChange(input_plot_vector, output_plot_vector, footprint_vector, input_tx_files_list, evolutions_list=['0:1:11000:10:50:and', '0:1:12000:10:50:and', '0:1:21000:10:50:and', '0:1:22000:10:50:and', '0:1:23000:10:50:and'], class_label_dico={11000:'Bati', 12000:'Route', 21000:'SolNu', 22000:'Eau', 23000:'Vegetation'}, epsg=2154, no_data_value=0, format_raster='GTiff', format_vector='ESRI Shapefile', extension_raster='.tif', extension_vector='.shp', postgis_ip_host='localhost', postgis_num_port=5432, postgis_user_name='postgres', postgis_password='******', postgis_database_name='database', postgis_schema_name='public', postgis_encoding='latin1', path_time_log='', save_results_intermediate=False, overwrite=True): if debug >= 3: print('\n' + bold + green + "Evolution de l'OCS par parcelle - Variables dans la fonction :" + endC) print(cyan + " soilOccupationChange() : " + endC + "input_plot_vector : " + str(input_plot_vector) + endC) print(cyan + " soilOccupationChange() : " + endC + "output_plot_vector : " + str(output_plot_vector) + endC) print(cyan + " soilOccupationChange() : " + endC + "footprint_vector : " + str(footprint_vector) + endC) print(cyan + " soilOccupationChange() : " + endC + "input_tx_files_list : " + str(input_tx_files_list) + endC) print(cyan + " soilOccupationChange() : " + endC + "evolutions_list : " + str(evolutions_list) + endC) print(cyan + " soilOccupationChange() : " + endC + "class_label_dico : " + str(class_label_dico) + endC) print(cyan + " soilOccupationChange() : " + endC + "epsg : " + str(epsg) + endC) print(cyan + " soilOccupationChange() : " + endC + "no_data_value : " + str(no_data_value) + endC) print(cyan + " soilOccupationChange() : " + endC + "format_raster : " + str(format_raster) + endC) print(cyan + " soilOccupationChange() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + " soilOccupationChange() : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + " soilOccupationChange() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + " soilOccupationChange() : " + endC + "postgis_ip_host : " + str(postgis_ip_host) + endC) print(cyan + " soilOccupationChange() : " + endC + "postgis_num_port : " + str(postgis_num_port) + endC) print(cyan + " soilOccupationChange() : " + endC + "postgis_user_name : " + str(postgis_user_name) + endC) print(cyan + " soilOccupationChange() : " + endC + "postgis_password : "******" soilOccupationChange() : " + endC + "postgis_database_name : " + str(postgis_database_name) + endC) print(cyan + " soilOccupationChange() : " + endC + "postgis_schema_name : " + str(postgis_schema_name) + endC) print(cyan + " soilOccupationChange() : " + endC + "postgis_encoding : " + str(postgis_encoding) + endC) print(cyan + " soilOccupationChange() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + " soilOccupationChange() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + " soilOccupationChange() : " + endC + "overwrite : " + str(overwrite) + endC + '\n') # Définition des constantes EXTENSION_TEXT = '.txt' SUFFIX_TEMP = '_temp' SUFFIX_CUT = '_cut' AREA_FIELD = 'st_area' GEOM_FIELD = 'geom' # Mise à jour du log starting_event = "soilOccupationChange() : Début du traitement : " timeLine(path_time_log, starting_event) print(cyan + "soilOccupationChange() : " + bold + green + "DEBUT DES TRAITEMENTS" + endC + '\n') # Définition des variables 'basename' output_plot_basename = os.path.splitext(os.path.basename(output_plot_vector))[0] # Définition des variables temp temp_directory = os.path.dirname(output_plot_vector) + os.sep + output_plot_basename + SUFFIX_TEMP plot_vector_cut = temp_directory + os.sep + output_plot_basename + SUFFIX_CUT + extension_vector # Définition des variables PostGIS plot_table = output_plot_basename.lower() # Fichier .txt associé au fichier vecteur de sortie, sur la liste des évolutions quantifiées output_evolution_text_file = os.path.splitext(output_plot_vector)[0] + EXTENSION_TEXT # Nettoyage des traitements précédents if debug >= 3: print(cyan + "soilOccupationChange() : " + endC + "Nettoyage des traitements précédents." + endC + '\n') removeVectorFile(output_plot_vector, format_vector=format_vector) removeFile(output_evolution_text_file) cleanTempData(temp_directory) dropDatabase(postgis_database_name, user_name=postgis_user_name, password=postgis_password, ip_host=postgis_ip_host, num_port=postgis_num_port, schema_name=postgis_schema_name) ############# # Etape 0/2 # Préparation des traitements ############# print(cyan + "soilOccupationChange() : " + bold + green + "ETAPE 0/2 - Début de la préparation des traitements." + endC + '\n') # Découpage du parcellaire à la zone d'étude cutVector(footprint_vector, input_plot_vector, plot_vector_cut, overwrite=overwrite, format_vector=format_vector) # Récupération du nom des champs dans le fichier source (pour isoler les champs nouvellement créés par la suite, et les renommer) attr_names_list_origin = getAttributeNameList(plot_vector_cut, format_vector=format_vector) new_attr_names_list_origin = attr_names_list_origin # Préparation de PostGIS createDatabase(postgis_database_name, user_name=postgis_user_name, password=postgis_password, ip_host=postgis_ip_host, num_port=postgis_num_port, schema_name=postgis_schema_name) print(cyan + "soilOccupationChange() : " + bold + green + "ETAPE 0/2 - Fin de la préparation des traitements." + endC + '\n') ############# # Etape 1/2 # Calculs des statistiques à tx ############# print(cyan + "soilOccupationChange() : " + bold + green + "ETAPE 1/2 - Début des calculs des statistiques à tx." + endC + '\n') len_tx = len(input_tx_files_list) tx = 0 # Boucle sur les fichiers d'entrés à t0+x for input_tx_file in input_tx_files_list: if debug >= 3: print(cyan + "soilOccupationChange() : " + endC + bold + "Calcul des statistiques à tx %s/%s." % (tx+1, len_tx) + endC + '\n') # Statistiques OCS par parcelle statisticsVectorRaster(input_tx_file, plot_vector_cut, "", 1, True, False, False, [], [], class_label_dico, path_time_log, clean_small_polygons=True, format_vector=format_vector, save_results_intermediate=save_results_intermediate, overwrite=overwrite) # Récupération du nom des champs dans le fichier parcellaire (avec les champs créés précédemment dans CVR) attr_names_list_tx = getAttributeNameList(plot_vector_cut, format_vector=format_vector) # Isolement des nouveaux champs issus du CVR fields_name_list = [] for attr_name in attr_names_list_tx: if attr_name not in new_attr_names_list_origin: fields_name_list.append(attr_name) # Gestion des nouveaux noms des champs issus du CVR new_fields_name_list = [] for field_name in fields_name_list: new_field_name = 't%s_' % tx + field_name new_field_name = new_field_name[:10] new_fields_name_list.append(new_field_name) new_attr_names_list_origin.append(new_field_name) # Renommage des champs issus du CVR, pour le relancer par la suite sur d'autres dates renameFieldsVector(plot_vector_cut, fields_name_list, new_fields_name_list, format_vector=format_vector) tx += 1 print(cyan + "soilOccupationChange() : " + bold + green + "ETAPE 1/2 - Fin des calculs des statistiques à tx." + endC + '\n') ############# # Etape 2/2 # Caractérisation des changements ############# print(cyan + "soilOccupationChange() : " + bold + green + "ETAPE 2/2 - Début de la caractérisation des changements." + endC + '\n') # Pré-traitements dans PostGIS plot_table = importVectorByOgr2ogr(postgis_database_name, plot_vector_cut, plot_table, user_name=postgis_user_name, password=postgis_password, ip_host=postgis_ip_host, num_port=postgis_num_port, schema_name=postgis_schema_name, epsg=epsg, codage=postgis_encoding) connection = openConnection(postgis_database_name, user_name=postgis_user_name, password=postgis_password, ip_host=postgis_ip_host, num_port=postgis_num_port, schema_name=postgis_schema_name) # Requête SQL pour le calcul de la surface des parcelles sql_query = "ALTER TABLE %s ADD COLUMN %s REAL;\n" % (plot_table, AREA_FIELD) sql_query += "UPDATE %s SET %s = ST_Area(%s);\n" % (plot_table, AREA_FIELD, GEOM_FIELD) # Boucle sur les évolutions à quantifier temp_field = 1 for evolution in evolutions_list: evolution_split = evolution.split(':') idx_bef = int(evolution_split[0]) idx_aft = int(evolution_split[1]) label = int(evolution_split[2]) evol = abs(int(evolution_split[3])) evol_s = abs(int(evolution_split[4])) combi = evolution_split[5] class_name = class_label_dico[label] def_evo_field = "def_evo_%s" % str(temp_field) if debug >= 3: print(cyan + "soilOccupationChange() : " + endC + bold + "Caractérisation des changements t%s/t%s pour la classe '%s' (%s)." % (idx_bef, idx_aft, class_name, label) + endC + '\n') if evol != 0 or evol_s != 0: # Gestion de l'évolution via le taux evol_str = str(evol) + ' %' evo_field = "evo_%s" % str(temp_field) t0_field = 't%s_' % idx_bef + class_name.lower()[:7] t1_field = 't%s_' % idx_aft + class_name.lower()[:7] # Gestion de l'évolution via la surface evol_s_str = str(evol_s) + ' m²' evo_s_field = "evo_s_%s" % str(temp_field) t0_s_field = 't%s_s_' % idx_bef + class_name.lower()[:5] t1_s_field = 't%s_s_' % idx_aft + class_name.lower()[:5] # Requête SQL pour le calcul brut de l'évolution sql_query += "ALTER TABLE %s ADD COLUMN %s REAL;\n" % (plot_table, evo_field) sql_query += "UPDATE %s SET %s = %s - %s;\n" % (plot_table, evo_field, t1_field, t0_field) sql_query += "ALTER TABLE %s ADD COLUMN %s REAL;\n" % (plot_table, evo_s_field) sql_query += "UPDATE %s SET %s = %s - %s;\n" % (plot_table, evo_s_field, t1_s_field, t0_s_field) sql_query += "ALTER TABLE %s ADD COLUMN %s VARCHAR;\n" % (plot_table, def_evo_field) sql_query += "UPDATE %s SET %s = 't%s a t%s - %s - aucune evolution';\n" % (plot_table, def_evo_field, idx_bef, idx_aft, class_name) # Si évolution à la fois via taux et via surface if evol != 0 and evol_s != 0: text_evol = "taux à %s" % evol_str if combi == 'and': text_evol += " ET " elif combi == 'or': text_evol += " OU " text_evol += "surface à %s" % evol_s_str sql_where_pos = "%s >= %s %s %s >= %s" % (evo_field, evol, combi, evo_s_field, evol_s) sql_where_neg = "%s <= -%s %s %s <= -%s" % (evo_field, evol, combi, evo_s_field, evol_s) # Si évolution uniquement via taux elif evol != 0: text_evol = "taux à %s" % evol_str sql_where_pos = "%s >= %s" % (evo_field, evol) sql_where_neg = "%s <= -%s" % (evo_field, evol) # Si évolution uniquement via surface elif evol_s != 0: text_evol = "surface à %s" % evol_s_str sql_where_pos = "%s >= %s" % (evo_s_field, evol_s) sql_where_neg = "%s <= -%s" % (evo_s_field, evol_s) sql_query += "UPDATE %s SET %s = 't%s a t%s - %s - evolution positive' WHERE %s;\n" % (plot_table, def_evo_field, idx_bef, idx_aft, class_name, sql_where_pos) sql_query += "UPDATE %s SET %s = 't%s a t%s - %s - evolution negative' WHERE %s;\n" % (plot_table, def_evo_field, idx_bef, idx_aft, class_name, sql_where_neg) # Ajout des paramètres de l'évolution quantifiée (temporalités, classe, taux/surface) au fichier texte de sortie text = "%s --> évolution entre t%s et t%s, pour la classe '%s' (label %s) :\n" % (def_evo_field, idx_bef, idx_aft, class_name, label) text += " %s --> taux d'évolution brut" % evo_field + " (%)\n" text += " %s --> surface d'évolution brute" % evo_s_field + " (m²)\n" text += "Evolution quantifiée : %s\n" % text_evol appendTextFileCR(output_evolution_text_file, text) temp_field += 1 # Traitements SQL de l'évolution des classes OCS executeQuery(connection, sql_query) closeConnection(connection) exportVectorByOgr2ogr(postgis_database_name, output_plot_vector, plot_table, user_name=postgis_user_name, password=postgis_password, ip_host=postgis_ip_host, num_port=postgis_num_port, schema_name=postgis_schema_name, format_type=format_vector) print(cyan + "soilOccupationChange() : " + bold + green + "ETAPE 2/2 - Fin de la caractérisation des changements." + endC + '\n') # Suppression des fichiers temporaires if not save_results_intermediate: if debug >= 3: print(cyan + "soilOccupationChange() : " + endC + "Suppression des fichiers temporaires." + endC + '\n') deleteDir(temp_directory) dropDatabase(postgis_database_name, user_name=postgis_user_name, password=postgis_password, ip_host=postgis_ip_host, num_port=postgis_num_port, schema_name=postgis_schema_name) print(cyan + "soilOccupationChange() : " + bold + green + "FIN DES TRAITEMENTS" + endC + '\n') # Mise à jour du log ending_event = "soilOccupationChange() : Fin du traitement : " timeLine(path_time_log, ending_event) return
def occupationIndicator(input_grid, output_grid, class_label_dico_out, input_vector_classif, field_classif_name, input_soil_occupation, input_height_model, class_build_list, class_road_list, class_baresoil_list, class_water_list, class_vegetation_list, class_high_vegetation_list, class_low_vegetation_list, epsg=2154, no_data_value=0, format_raster='GTiff', format_vector='ESRI Shapefile', extension_raster='.tif', extension_vector='.shp', path_time_log='', save_results_intermediate=False, overwrite=True): if debug >= 3: print( '\n' + bold + green + "Calcul d'indicateurs du taux de classes OCS - Variables dans la fonction :" + endC) print(cyan + " occupationIndicator() : " + endC + "input_grid : " + str(input_grid) + endC) print(cyan + " occupationIndicator() : " + endC + "output_grid : " + str(output_grid) + endC) print(cyan + " occupationIndicator() : " + endC + "class_label_dico_out : " + str(class_label_dico_out) + endC) print(cyan + " occupationIndicator() : " + endC + "input_vector_classif : " + str(input_vector_classif) + endC) print(cyan + " occupationIndicator() : " + endC + "field_classif_name : " + str(field_classif_name) + endC) print(cyan + " occupationIndicator() : " + endC + "input_soil_occupation : " + str(input_soil_occupation) + endC) print(cyan + " occupationIndicator() : " + endC + "input_height_model : " + str(input_height_model) + endC) print(cyan + " occupationIndicator() : " + endC + "class_build_list : " + str(class_build_list) + endC) print(cyan + " occupationIndicator() : " + endC + "class_road_list : " + str(class_road_list) + endC) print(cyan + " occupationIndicator() : " + endC + "class_baresoil_list : " + str(class_baresoil_list) + endC) print(cyan + " occupationIndicator() : " + endC + "class_water_list : " + str(class_water_list) + endC) print(cyan + " occupationIndicator() : " + endC + "class_vegetation_list : " + str(class_vegetation_list) + endC) print(cyan + " occupationIndicator() : " + endC + "class_high_vegetation_list : " + str(class_high_vegetation_list) + endC) print(cyan + " occupationIndicator() : " + endC + "class_low_vegetation_list : " + str(class_low_vegetation_list) + endC) print(cyan + " occupationIndicator() : " + endC + "epsg : " + str(epsg) + endC) print(cyan + " occupationIndicator() : " + endC + "no_data_value : " + str(no_data_value) + endC) print(cyan + " occupationIndicator() : " + endC + "format_raster : " + str(format_raster) + endC) print(cyan + " occupationIndicator() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + " occupationIndicator() : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + " occupationIndicator() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + " occupationIndicator() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + " occupationIndicator() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + " occupationIndicator() : " + endC + "overwrite : " + str(overwrite) + endC + '\n') # Définition des constantes CODAGE_8BITS = 'uint8' CODAGE_FLOAT = 'float' NODATA_FIELD = 'nodata' PREFIX_S = 'S_' SUFFIX_TEMP = '_temp' SUFFIX_RASTER = '_raster' SUFFIX_HEIGHT = '_height' SUFFIX_VEGETATION = '_vegetation' VEG_MEAN_FIELD = 'veg_h_mean' VEG_MAX_FIELD = 'veg_h_max' VEG_RATE_FIELD = 'veg_h_rate' MAJ_OCS_FIELD = 'class_OCS' BUILT_FIELD, BUILT_LABEL = 'built', 1 MINERAL_FIELD, MINERAL_LABEL = 'mineral', 2 BARESOIL_FIELD, BARESOIL_LABEL = 'baresoil', 3 WATER_FIELD, WATER_LABEL = 'water', 4 VEGETATION_FIELD, VEGETATION_LABEL = 'veget', 5 HIGH_VEGETATION_FIELD, HIGH_VEGETATION_LABEL = 'high_veg', 6 LOW_VEGETATION_FIELD, LOW_VEGETATION_LABEL = 'low_veg', 7 # Mise à jour du log starting_event = "occupationIndicator() : Début du traitement : " timeLine(path_time_log, starting_event) print(cyan + "occupationIndicator() : " + bold + green + "DEBUT DES TRAITEMENTS" + endC + '\n') # Définition des variables 'basename' output_grid_basename = os.path.basename(os.path.splitext(output_grid)[0]) output_grid_dirname = os.path.dirname(output_grid) soil_occupation_basename = os.path.basename( os.path.splitext(input_soil_occupation)[0]) # Définition des variables temp temp_directory = output_grid_dirname + os.sep + output_grid_basename temp_grid = temp_directory + os.sep + output_grid_basename + SUFFIX_TEMP + extension_vector temp_soil_occupation = temp_directory + os.sep + soil_occupation_basename + SUFFIX_TEMP + SUFFIX_RASTER + extension_raster temp_height_vegetation = temp_directory + os.sep + output_grid_basename + SUFFIX_HEIGHT + SUFFIX_VEGETATION + extension_raster # Nettoyage des traitements précédents if overwrite: if debug >= 3: print(cyan + "occupationIndicator() : " + endC + "Nettoyage des traitements précédents." + endC + '\n') removeFile(output_grid) cleanTempData(temp_directory) else: if os.path.exists(output_grid): raise NameError( cyan + "occupationIndicator() : " + bold + yellow + "Le fichier de sortie existe déjà et ne sera pas regénéré." + endC + '\n') pass ############# # Etape 0/3 # Préparation des traitements ############# print(cyan + "occupationIndicator() : " + bold + green + "ETAPE 0/3 - Début de la préparation des traitements." + endC + '\n') # Rasterisation de l'information de classification (OCS) si au format vecteur en entrée if input_vector_classif != "": if debug >= 3: print(cyan + "occupationIndicator() : " + endC + bold + "Rasterisation de l'OCS vecteur." + endC + '\n') reference_image = input_soil_occupation soil_occupation_vector_basename = os.path.basename( os.path.splitext(input_vector_classif)[0]) input_soil_occupation = temp_directory + os.sep + soil_occupation_vector_basename + SUFFIX_RASTER + extension_raster command = "otbcli_Rasterization -in %s -out %s %s -im %s -background 0 -mode attribute -mode.attribute.field %s" % ( input_vector_classif, input_soil_occupation, CODAGE_8BITS, reference_image, field_classif_name) if debug >= 3: print(command) exit_code = os.system(command) if exit_code != 0: raise NameError( cyan + "occupationIndicator() : " + bold + red + "Erreur lors de la rasterisation de l'OCS vecteur." + endC) # Analyse de la couche OCS raster class_other_list = identifyPixelValues(input_soil_occupation) no_data_ocs = getNodataValueImage(input_soil_occupation, 1) if no_data_ocs != None: no_data_value = no_data_ocs # Affectation de nouveaux codes de classification divide_vegetation_classes = False if class_high_vegetation_list != [] and class_low_vegetation_list != []: divide_vegetation_classes = True col_to_delete_list = [ "minority", PREFIX_S + NODATA_FIELD, PREFIX_S + BUILT_FIELD, PREFIX_S + MINERAL_FIELD, PREFIX_S + BARESOIL_FIELD, PREFIX_S + WATER_FIELD ] class_label_dico = { int(no_data_value): NODATA_FIELD, int(BUILT_LABEL): BUILT_FIELD, int(MINERAL_LABEL): MINERAL_FIELD, int(BARESOIL_LABEL): BARESOIL_FIELD, int(WATER_LABEL): WATER_FIELD } if not divide_vegetation_classes: class_label_dico[int(VEGETATION_LABEL)] = VEGETATION_FIELD col_to_delete_list.append(PREFIX_S + VEGETATION_FIELD) else: class_label_dico[int(HIGH_VEGETATION_LABEL)] = HIGH_VEGETATION_FIELD class_label_dico[int(LOW_VEGETATION_LABEL)] = LOW_VEGETATION_FIELD col_to_delete_list.append(PREFIX_S + HIGH_VEGETATION_FIELD) col_to_delete_list.append(PREFIX_S + LOW_VEGETATION_FIELD) # Gestion de la réaffectation des classes if debug >= 3: print(cyan + "occupationIndicator() : " + endC + bold + "Reaffectation du raster OCS." + endC + '\n') reaff_class_list = [] macro_reaff_class_list = [] for label in class_build_list: if label in class_other_list: class_other_list.remove(label) reaff_class_list.append(label) macro_reaff_class_list.append(BUILT_LABEL) for label in class_road_list: if label in class_other_list: class_other_list.remove(label) reaff_class_list.append(label) macro_reaff_class_list.append(MINERAL_LABEL) for label in class_baresoil_list: if label in class_other_list: class_other_list.remove(label) reaff_class_list.append(label) macro_reaff_class_list.append(BARESOIL_LABEL) for label in class_water_list: if label in class_other_list: class_other_list.remove(label) reaff_class_list.append(label) macro_reaff_class_list.append(WATER_LABEL) if not divide_vegetation_classes: for label in class_vegetation_list: if label in class_other_list: class_other_list.remove(label) reaff_class_list.append(label) macro_reaff_class_list.append(VEGETATION_LABEL) else: for label in class_high_vegetation_list: if label in class_other_list: class_other_list.remove(label) reaff_class_list.append(label) macro_reaff_class_list.append(HIGH_VEGETATION_LABEL) for label in class_low_vegetation_list: if label in class_other_list: class_other_list.remove(label) reaff_class_list.append(label) macro_reaff_class_list.append(LOW_VEGETATION_LABEL) # Reste des valeurs de pixel nom utilisé for label in class_other_list: reaff_class_list.append(label) macro_reaff_class_list.append(no_data_value) reallocateClassRaster(input_soil_occupation, temp_soil_occupation, reaff_class_list, macro_reaff_class_list, CODAGE_8BITS) print(cyan + "occupationIndicator() : " + bold + green + "ETAPE 0/3 - Fin de la préparation des traitements." + endC + '\n') ############# # Etape 1/3 # Calcul des indicateurs de taux de classes OCS ############# print( cyan + "occupationIndicator() : " + bold + green + "ETAPE 1/3 - Début du calcul des indicateurs de taux de classes OCS." + endC + '\n') if debug >= 3: print(cyan + "occupationIndicator() : " + endC + bold + "Calcul des indicateurs de taux de classes OCS." + endC + '\n') statisticsVectorRaster(temp_soil_occupation, input_grid, temp_grid, 1, True, True, False, col_to_delete_list, [], class_label_dico, path_time_log, True, format_vector, save_results_intermediate, overwrite) # Fusion des classes végétation dans le cas où haute et basse sont séparées (pour utilisation du taux de végétation dans le logigramme) if divide_vegetation_classes: temp_grid_v2 = os.path.splitext( temp_grid)[0] + "_v2" + extension_vector sql_statement = "SELECT *, (%s + %s) AS %s FROM %s" % ( HIGH_VEGETATION_FIELD, LOW_VEGETATION_FIELD, VEGETATION_FIELD, os.path.splitext(os.path.basename(temp_grid))[0]) os.system("ogr2ogr -sql '%s' -dialect SQLITE %s %s" % (sql_statement, temp_grid_v2, temp_grid)) removeVectorFile(temp_grid, format_vector=format_vector) copyVectorFile(temp_grid_v2, temp_grid, format_vector=format_vector) print(cyan + "occupationIndicator() : " + bold + green + "ETAPE 1/3 - Fin du calcul des indicateurs de taux de classes OCS." + endC + '\n') ############# # Etape 2/3 # Calcul de l'indicateur de "hauteur de végétation" ############# print( cyan + "occupationIndicator() : " + bold + green + "ETAPE 2/3 - Début du calcul de l'indicateur de \"hauteur de végétation\"." + endC + '\n') computeVegetationHeight( temp_grid, output_grid, temp_soil_occupation, input_height_model, temp_height_vegetation, divide_vegetation_classes, VEGETATION_LABEL, HIGH_VEGETATION_LABEL, LOW_VEGETATION_LABEL, HIGH_VEGETATION_FIELD, LOW_VEGETATION_FIELD, VEG_MEAN_FIELD, VEG_MAX_FIELD, VEG_RATE_FIELD, CODAGE_FLOAT, SUFFIX_TEMP, no_data_value, format_vector, path_time_log, save_results_intermediate, overwrite) print( cyan + "occupationIndicator() : " + bold + green + "ETAPE 2/3 - Fin du calcul de l'indicateur de \"hauteur de végétation\"." + endC + '\n') ############# # Etape 3/3 # Calcul de l'indicateur de classe majoritaire ############# print( cyan + "occupationIndicator() : " + bold + green + "ETAPE 3/3 - Début du calcul de l'indicateur de classe majoritaire." + endC + '\n') if input_height_model != "": computeMajorityClass(output_grid, temp_directory, NODATA_FIELD, BUILT_FIELD, MINERAL_FIELD, BARESOIL_FIELD, WATER_FIELD, VEGETATION_FIELD, HIGH_VEGETATION_FIELD, LOW_VEGETATION_FIELD, MAJ_OCS_FIELD, VEG_MEAN_FIELD, class_label_dico_out, format_vector, extension_vector, overwrite) else: print( cyan + "occupationIndicator() : " + bold + yellow + "Pas de calcul de l'indicateur de classe majoritaire demandé (pas de MNH en entrée)." + endC + '\n') print(cyan + "occupationIndicator() : " + bold + green + "ETAPE 3/3 - Fin du calcul de l'indicateur de classe majoritaire." + endC + '\n') #################################################################### # Suppression des fichiers temporaires if not save_results_intermediate: if debug >= 3: print(cyan + "occupationIndicator() : " + endC + "Suppression des fichiers temporaires." + endC + '\n') deleteDir(temp_directory) print(cyan + "occupationIndicator() : " + bold + green + "FIN DES TRAITEMENTS" + endC + '\n') # Mise à jour du log ending_event = "occupationIndicator() : Fin du traitement : " timeLine(path_time_log, ending_event) return 0
def createMnh(image_mns_input, image_mnt_input, image_threshold_input, vector_emprise_input, image_mnh_output, automatic, bd_road_vector_input_list, bd_road_buff_list, sql_road_expression_list, bd_build_vector_input_list, height_bias, threshold_bd_value, threshold_delta_h, mode_interpolation, method_interpolation, interpolation_bco_radius, simplify_vector_param, epsg, no_data_value, ram_otb, path_time_log, format_raster='GTiff', format_vector='ESRI Shapefile', extension_raster=".tif", extension_vector=".shp", save_results_intermediate=False, overwrite=True): # Mise à jour du Log starting_event = "createMnh() : MNH creation starting : " timeLine(path_time_log,starting_event) print(endC) print(bold + green + "## START : MNH CREATION" + endC) print(endC) if debug >= 2: print(bold + green + "createMnh() : Variables dans la fonction" + endC) print(cyan + "createMnh() : " + endC + "image_mns_input : " + str(image_mns_input) + endC) print(cyan + "createMnh() : " + endC + "image_mnt_input : " + str(image_mnt_input) + endC) print(cyan + "createMnh() : " + endC + "image_threshold_input : " + str(image_threshold_input) + endC) print(cyan + "createMnh() : " + endC + "vector_emprise_input : " + str(vector_emprise_input) + endC) print(cyan + "createMnh() : " + endC + "image_mnh_output : " + str(image_mnh_output) + endC) print(cyan + "createMnh() : " + endC + "automatic : " + str(automatic) + endC) print(cyan + "createMnh() : " + endC + "bd_road_vector_input_list : " + str(bd_road_vector_input_list) + endC) print(cyan + "createMnh() : " + endC + "bd_road_buff_list : " + str(bd_road_buff_list) + endC) print(cyan + "createMnh() : " + endC + "sql_road_expression_list : " + str(sql_road_expression_list) + endC) print(cyan + "createMnh() : " + endC + "bd_build_vector_input_list : " + str(bd_build_vector_input_list) + endC) print(cyan + "createMnh() : " + endC + "height_bias : " + str(height_bias) + endC) print(cyan + "createMnh() : " + endC + "threshold_bd_value : " + str(threshold_bd_value) + endC) print(cyan + "createMnh() : " + endC + "threshold_delta_h : " + str(threshold_delta_h) + endC) print(cyan + "createMnh() : " + endC + "mode_interpolation : " + str(mode_interpolation) + endC) print(cyan + "createMnh() : " + endC + "method_interpolation : " + str(method_interpolation) + endC) print(cyan + "createMnh() : " + endC + "interpolation_bco_radius : " + str(interpolation_bco_radius) + endC) print(cyan + "createMnh() : " + endC + "simplify_vector_param : " + str(simplify_vector_param) + endC) print(cyan + "createMnh() : " + endC + "epsg : " + str(epsg) + endC) print(cyan + "createMnh() : " + endC + "no_data_value : " + str(no_data_value) + endC) print(cyan + "createMnh() : " + endC + "ram_otb : " + str(ram_otb) + endC) print(cyan + "createMnh() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "createMnh() : " + endC + "format_raster : " + str(format_raster) + endC) print(cyan + "createMnh() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "createMnh() : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "createMnh() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "createMnh() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "createMnh() : " + endC + "overwrite : " + str(overwrite) + endC) # LES CONSTANTES PRECISION = 0.0000001 CODAGE_8B = "uint8" CODAGE_F = "float" SUFFIX_CUT = "_cut" SUFFIX_CLEAN = "_clean" SUFFIX_SAMPLE = "_sample" SUFFIX_MASK = "_mask" SUFFIX_TMP = "_tmp" SUFFIX_MNS = "_mns" SUFFIX_MNT = "_mnt" SUFFIX_ROAD = "_road" SUFFIX_BUILD = "_build" SUFFIX_RASTER = "_raster" SUFFIX_VECTOR = "_vector" # DEFINIR LES REPERTOIRES ET FICHIERS TEMPORAIRES repertory_output = os.path.dirname(image_mnh_output) basename_mnh = os.path.splitext(os.path.basename(image_mnh_output))[0] sub_repertory_raster_temp = repertory_output + os.sep + basename_mnh + SUFFIX_RASTER + SUFFIX_TMP sub_repertory_vector_temp = repertory_output + os.sep + basename_mnh + SUFFIX_VECTOR + SUFFIX_TMP cleanTempData(sub_repertory_raster_temp) cleanTempData(sub_repertory_vector_temp) basename_vector_emprise = os.path.splitext(os.path.basename(vector_emprise_input))[0] basename_mns_input = os.path.splitext(os.path.basename(image_mns_input))[0] basename_mnt_input = os.path.splitext(os.path.basename(image_mnt_input))[0] image_mnh_tmp = sub_repertory_raster_temp + os.sep + basename_mnh + SUFFIX_TMP + extension_raster image_mnh_road = sub_repertory_raster_temp + os.sep + basename_mnh + SUFFIX_ROAD + extension_raster vector_bd_bati_temp = sub_repertory_vector_temp + os.sep + basename_mnh + SUFFIX_BUILD + SUFFIX_TMP + extension_vector vector_bd_bati = repertory_output + os.sep + basename_mnh + SUFFIX_BUILD + extension_vector raster_bd_bati = sub_repertory_vector_temp + os.sep + basename_mnh + SUFFIX_BUILD + extension_raster removeVectorFile(vector_bd_bati) image_emprise_mnt_mask = sub_repertory_raster_temp + os.sep + basename_vector_emprise + SUFFIX_MNT + extension_raster image_mnt_cut = sub_repertory_raster_temp + os.sep + basename_mnt_input + SUFFIX_CUT + extension_raster image_mnt_clean = sub_repertory_raster_temp + os.sep + basename_mnt_input + SUFFIX_CLEAN + extension_raster image_mnt_clean_sample = sub_repertory_raster_temp + os.sep + basename_mnt_input + SUFFIX_CLEAN + SUFFIX_SAMPLE + extension_raster image_emprise_mns_mask = sub_repertory_raster_temp + os.sep + basename_vector_emprise + SUFFIX_MNS + extension_raster image_mns_cut = sub_repertory_raster_temp + os.sep + basename_mns_input + SUFFIX_CUT + extension_raster image_mns_clean = sub_repertory_raster_temp + os.sep + basename_mns_input + SUFFIX_CLEAN + extension_raster vector_bd_road_temp = sub_repertory_vector_temp + os.sep + basename_mnh + SUFFIX_ROAD + SUFFIX_TMP + extension_vector raster_bd_road_mask = sub_repertory_raster_temp + os.sep + basename_mnh + SUFFIX_ROAD + SUFFIX_MASK + extension_raster if image_threshold_input != "" : basename_threshold_input = os.path.splitext(os.path.basename(image_threshold_input))[0] image_threshold_cut = sub_repertory_raster_temp + os.sep + basename_threshold_input + SUFFIX_CUT + extension_raster image_threshold_mask = sub_repertory_raster_temp + os.sep + basename_threshold_input + SUFFIX_MASK + extension_raster # VERIFICATION SI LE FICHIER DE SORTIE EXISTE DEJA # Si un fichier de sortie avec le même nom existe déjà, et si l'option ecrasement est à false, alors on ne fait rien check = os.path.isfile(image_mnh_output) if check and not overwrite: print(bold + yellow + "createMnh() : " + endC + "Create mnh %s from %s and %s already done : no actualisation" % (image_mnh_output, image_mns_input, image_mnt_input) + endC) # Si non, ou si la fonction ecrasement est désative, alors on le calcule else: if check: try: # Suppression de l'éventuel fichier existant removeFile(image_mnh_output) except Exception: pass # Si le fichier ne peut pas être supprimé, on suppose qu'il n'existe pas et on passe à la suite # DECOUPAGE DES FICHIERS MS ET MNT D'ENTREE PAR LE FICHIER D'EMPRISE if debug >= 3: print(bold + green + "createMnh() : " + endC + "Decoupage selon l'emprise des fichiers %s et %s " %(image_mns_input, image_mnt_input) + endC) # Fonction de découpe du mns if not cutImageByVector(vector_emprise_input, image_mns_input, image_mns_cut, None, None, no_data_value, epsg, format_raster, format_vector) : raise NameError (cyan + "createMnh() : " + bold + red + "!!! Une erreur c'est produite au cours du decoupage de l'image : " + image_mns_input + ". Voir message d'erreur." + endC) # Fonction de découpe du mnt if not cutImageByVector(vector_emprise_input, image_mnt_input, image_mnt_cut, None, None, no_data_value, epsg, format_raster, format_vector) : raise NameError (cyan + "createMnh() : " + bold + red + "!!! Une erreur c'est produite au cours du decoupage de l'image : " + image_mnt_input + ". Voir message d'erreur." + endC) if debug >= 3: print(bold + green + "createMnh() : " + endC + "Decoupage des fichiers %s et %s complet" %(image_mns_cut, image_mnt_cut) + endC) # REBOUCHAGE DES TROUS DANS LE MNT D'ENTREE SI NECESSAIRE nodata_mnt = getNodataValueImage(image_mnt_cut) pixelNodataCount = countPixelsOfValue(image_mnt_cut, nodata_mnt) if pixelNodataCount > 0 : if debug >= 3: print(bold + green + "createMnh() : " + endC + "Fill the holes MNT for %s" %(image_mnt_cut) + endC) # Rasterisation du vecteur d'emprise pour creer un masque pour boucher les trous du MNT rasterizeBinaryVector(vector_emprise_input, image_mnt_cut, image_emprise_mnt_mask, 1, CODAGE_8B) # Utilisation de SAGA pour boucher les trous fillNodata(image_mnt_cut, image_emprise_mnt_mask, image_mnt_clean, save_results_intermediate) if debug >= 3: print(bold + green + "createMnh() : " + endC + "Fill the holes MNT to %s completed" %(image_mnt_clean) + endC) else : image_mnt_clean = image_mnt_cut if debug >= 3: print(bold + green + "\ncreateMnh() : " + endC + "Fill the holes not necessary MNT for %s" %(image_mnt_cut) + endC) # REBOUCHAGE DES TROUS DANS LE MNS D'ENTREE SI NECESSAIRE nodata_mns = getNodataValueImage(image_mns_cut) pixelNodataCount = countPixelsOfValue(image_mns_cut, nodata_mns) if pixelNodataCount > 0 : if debug >= 3: print(bold + green + "createMnh() : " + endC + "Fill the holes MNS for %s" %(image_mns_cut) + endC) # Rasterisation du vecteur d'emprise pour creer un masque pour boucher les trous du MNS rasterizeBinaryVector(vector_emprise_input, image_mns_cut, image_emprise_mns_mask, 1, CODAGE_8B) # Utilisation de SAGA pour boucher les trous fillNodata(image_mns_cut, image_emprise_mns_mask, image_mns_clean, save_results_intermediate) if debug >= 3: print(bold + green + "\ncreateMnh() : " + endC + "Fill the holes MNS to %s completed" %(image_mns_clean) + endC) else : image_mns_clean = image_mns_cut if debug >= 3: print(bold + green + "createMnh() : " + endC + "Fill the holes not necessary MNS for %s" %(image_mns_cut) + endC) # CALLER LE FICHIER MNT AU FORMAT DU FICHIER MNS # Commande de mise en place de la geométrie re-echantionage command = "otbcli_Superimpose -inr " + image_mns_clean + " -inm " + image_mnt_clean + " -mode " + mode_interpolation + " -interpolator " + method_interpolation + " -out " + image_mnt_clean_sample if method_interpolation.lower() == 'bco' : command += " -interpolator.bco.radius " + str(interpolation_bco_radius) if ram_otb > 0: command += " -ram %d" %(ram_otb) if debug >= 3: print(cyan + "createMnh() : " + bold + green + "Réechantillonage du fichier %s par rapport à la reference %s" %(image_mnt_clean, image_mns_clean) + endC) print(command) exit_code = os.system(command) if exit_code != 0: print(command) raise NameError (cyan + "createMnh() : " + bold + red + "!!! Une erreur c'est produite au cours du superimpose de l'image : " + image_mnt_input + ". Voir message d'erreur." + endC) # INCRUSTATION DANS LE MNH DES DONNEES VECTEURS ROUTES if debug >= 3: print(bold + green + "createMnh() : " + endC + "Use BD road to clean MNH" + endC) # Creation d'un masque de filtrage des donnes routes (exemple : le NDVI) if image_threshold_input != "" : if not cutImageByVector(vector_emprise_input, image_threshold_input, image_threshold_cut, None, None, no_data_value, epsg, format_raster, format_vector) : raise NameError (cyan + "createMnh() : " + bold + red + "!!! Une erreur c'est produite au cours du decoupage de l'image : " + image_threshold_input + ". Voir message d'erreur." + endC) createBinaryMask(image_threshold_cut, image_threshold_mask, threshold_bd_value, False, CODAGE_8B) # Execution de la fonction createMacroSamples pour une image correspondant au données routes if bd_road_vector_input_list != [] : createMacroSamples(image_mns_clean, vector_emprise_input, vector_bd_road_temp, raster_bd_road_mask, bd_road_vector_input_list, bd_road_buff_list, sql_road_expression_list, path_time_log, basename_mnh, simplify_vector_param, format_vector, extension_vector, save_results_intermediate, overwrite) if debug >= 3: print(bold + green + "\ncreateMnh() : " + endC + "File raster from BD road is create %s" %(raster_bd_road_mask) + endC) # CALCUL DU MNH # Calcul par bandMath du MNH definir l'expression qui soustrait le MNT au MNS en introduisant le biais et en mettant les valeurs à 0 à une valeur approcher de 0.0000001 delta = "" if height_bias > 0 : delta = "+%s" %(str(height_bias)) elif height_bias < 0 : delta = "-%s" %(str(abs(height_bias))) else : delta = "" # Definition de l'expression if bd_road_vector_input_list != [] : if image_threshold_input != "" : expression = "\"im3b1 > 0 and im4b1 > 0?%s:(im1b1-im2b1%s) > 0.0?im1b1-im2b1%s:%s\"" %(str(PRECISION), delta, delta, str(PRECISION)) command = "otbcli_BandMath -il %s %s %s %s -out %s %s -exp %s" %(image_mns_clean, image_mnt_clean_sample, raster_bd_road_mask, image_threshold_mask, image_mnh_tmp, CODAGE_F, expression) else : expression = "\"im3b1 > 0?%s:(im1b1-im2b1%s) > 0.0?im1b1-im2b1%s:%s\"" %(str(PRECISION), delta, delta, str(PRECISION)) command = "otbcli_BandMath -il %s %s %s -out %s %s -exp %s" %(image_mns_clean, image_mnt_clean_sample, raster_bd_road_mask, image_mnh_tmp, CODAGE_F, expression) else : expression = "\"(im1b1-im2b1%s) > 0.0?im1b1-im2b1%s:%s\"" %(delta, delta, str(PRECISION)) command = "otbcli_BandMath -il %s %s -out %s %s -exp %s" %(image_mns_clean, image_mnt_clean_sample, image_mnh_tmp, CODAGE_F, expression) if ram_otb > 0: command += " -ram %d" %(ram_otb) if debug >= 3: print(cyan + "createMnh() : " + bold + green + "Calcul du MNH %s difference du MNS : %s par le MNT :%s" %(image_mnh_tmp, image_mns_clean, image_mnt_clean_sample) + endC) print(command) exitCode = os.system(command) if exitCode != 0: print(command) raise NameError(cyan + "createMnh() : " + bold + red + "An error occured during otbcli_BandMath command to compute MNH " + image_mnh_tmp + ". See error message above." + endC) # DECOUPAGE DU MNH if bd_build_vector_input_list == []: image_mnh_road = image_mnh_output if debug >= 3: print(bold + green + "createMnh() : " + endC + "Decoupage selon l'emprise du fichier mnh %s " %(image_mnh_tmp) + endC) # Fonction de découpe du mnh if not cutImageByVector(vector_emprise_input, image_mnh_tmp, image_mnh_road, None, None, no_data_value, epsg, format_raster, format_vector) : raise NameError (cyan + "createMnh() : " + bold + red + "!!! Une erreur c'est produite au cours du decoupage de l'image : " + image_mns_input + ". Voir message d'erreur." + endC) if debug >= 3: print(bold + green + "createMnh() : " + endC + "Decoupage du fichier mnh %s complet" %(image_mnh_road) + endC) # INCRUSTATION DANS LE MNH DES DONNEES VECTEURS BATIS # Si demander => liste de fichier vecteur bati passé en donnée d'entrée if bd_build_vector_input_list != []: # Découpage des vecteurs de bd bati exogenes avec l'emprise vectors_build_cut_list = [] for vector_build_input in bd_build_vector_input_list : vector_name = os.path.splitext(os.path.basename(vector_build_input))[0] vector_build_cut = sub_repertory_vector_temp + os.sep + vector_name + SUFFIX_CUT + extension_vector vectors_build_cut_list.append(vector_build_cut) cutoutVectors(vector_emprise_input, bd_build_vector_input_list, vectors_build_cut_list, format_vector) # Fusion des vecteurs batis découpés fusionVectors (vectors_build_cut_list, vector_bd_bati_temp) # Croisement vecteur rasteur entre le vecteur fusion des batis et le MNH créé precedement statisticsVectorRaster(image_mnh_road, vector_bd_bati_temp, "", 1, False, False, True, ['PREC_PLANI','PREC_ALTI','ORIGIN_BAT','median','sum','std','unique','range'], [], {}, path_time_log, True, format_vector, save_results_intermediate, overwrite) # Calcul de la colonne delta_H entre les hauteurs des batis et la hauteur moyenne du MNH sous le bati COLUMN_ID = "ID" COLUMN_H_BUILD = "HAUTEUR" COLUMN_H_BUILD_MIN = "Z_MIN" COLUMN_H_BUILD_MAX = "Z_MAX" COLUMN_H_MNH = "mean" COLUMN_H_MNH_MIN = "min" COLUMN_H_MNH_MAX = "max" COLUMN_H_DIFF = "H_diff" field_type = ogr.OFTReal field_value = 0.0 field_width = 20 field_precision = 2 attribute_name_dico = {} attribute_name_dico[COLUMN_ID] = ogr.OFTString attribute_name_dico[COLUMN_H_BUILD] = ogr.OFTReal attribute_name_dico[COLUMN_H_MNH] = ogr.OFTReal # Ajouter la nouvelle colonne H_diff addNewFieldVector(vector_bd_bati_temp, COLUMN_H_DIFF, field_type, field_value, field_width, field_precision, format_vector) # Recuperer les valeur de hauteur du bati et du mnt dans le vecteur data_z_dico = getAttributeValues(vector_bd_bati_temp, None, None, attribute_name_dico, format_vector) # Calculer la difference des Hauteur bati et mnt field_new_values_dico = {} for index in range(len(data_z_dico[COLUMN_ID])) : index_polygon = data_z_dico[COLUMN_ID][index] delta_h = abs(data_z_dico[COLUMN_H_BUILD][index] - data_z_dico[COLUMN_H_MNH][index]) field_new_values_dico[index_polygon] = {COLUMN_H_DIFF:delta_h} # Mettre à jour la colonne H_diff dans le vecteur setAttributeIndexValuesList(vector_bd_bati_temp, COLUMN_ID, field_new_values_dico, format_vector) # Suppression de tous les polygones bati dons la valeur du delat H est inferieur à threshold_delta_h column = "'%s, %s, %s, %s, %s, %s, %s, %s'"% (COLUMN_ID, COLUMN_H_BUILD, COLUMN_H_BUILD_MIN, COLUMN_H_BUILD_MAX, COLUMN_H_MNH, COLUMN_H_MNH_MIN, COLUMN_H_MNH_MAX, COLUMN_H_DIFF) expression = "%s > %s" % (COLUMN_H_DIFF, threshold_delta_h) filterSelectDataVector(vector_bd_bati_temp, vector_bd_bati, column, expression, overwrite, format_vector) # Attention!!!! PAUSE pour trie et verification des polygones bati nom deja present dans le MNH ou non if not automatic : print(bold + blue + "Application MnhCreation => " + endC + "Vérification manuelle du vecteur bati %s pour ne concerver que les batis non présent dans le MNH courant %s" %(vector_bd_bati_temp, image_mnh_road) + endC) input(bold + red + "Appuyez sur entree pour continuer le programme..." + endC) # Creation du masque bati avec pour H la hauteur des batiments rasterizeVector(vector_bd_bati, raster_bd_bati, image_mnh_road, COLUMN_H_BUILD) # Fusion du mask des batis et du MNH temporaire expression = "\"im1b1 > 0.0?im1b1:im2b1\"" command = "otbcli_BandMath -il %s %s -out %s %s -exp %s" %(raster_bd_bati, image_mnh_road, image_mnh_output, CODAGE_F, expression) if ram_otb > 0: command += " -ram %d" %(ram_otb) if debug >= 3: print(cyan + "createMnh() : " + bold + green + "Amelioration du MNH %s ajout des hauteurs des batis %s" %(image_mnh_road, raster_bd_bati) + endC) print(command) exitCode = os.system(command) if exitCode != 0: print(command) raise NameError(cyan + "createMnh() : " + bold + red + "An error occured during otbcli_BandMath command to compute MNH Final" + image_mnh_output + ". See error message above." + endC) # SUPPRESIONS FICHIERS INTERMEDIAIRES INUTILES # Suppression des fichiers intermédiaires if not save_results_intermediate : if bd_build_vector_input_list != []: removeFile(image_mnh_road) removeFile(image_threshold_cut) removeFile(image_threshold_mask) removeFile(raster_bd_bati) removeVectorFile(vector_bd_road_temp) removeVectorFile(vector_bd_bati_temp) removeVectorFile(vector_bd_bati) # A confirmer!!! removeFile(raster_bd_road_mask) removeFile(image_mnh_tmp) deleteDir(sub_repertory_raster_temp) deleteDir(sub_repertory_vector_temp) print(endC) print(bold + green + "## END : MNH CREATION" + endC) print(endC) # Mise à jour du Log ending_event = "createMnh() : MNH creation ending : " timeLine(path_time_log,ending_event) return
def classesOfWaterHeights(input_flooded_areas_vector, input_digital_elevation_model_file, output_heights_classes_file, output_heights_classes_vector, heights_classes='0,0.5,1,1.5,2', epsg=2154, no_data_value=0, format_raster='GTiff', format_vector='ESRI Shapefile', extension_raster='.tif', extension_vector='.shp', grass_gisbase=os.environ['GISBASE'], grass_gisdb='GRASS_database', grass_location='LOCATION', grass_mapset='MAPSET', path_time_log='', save_results_intermediate=False, overwrite=True): if debug >= 3: print('\n' + bold + green + "Classes de hauteurs d'eau - Variables dans la fonction :" + endC) print(cyan + " classesOfWaterHeights() : " + endC + "input_flooded_areas_vector : " + str(input_flooded_areas_vector) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "input_digital_elevation_model_file : " + str(input_digital_elevation_model_file) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "output_heights_classes_file : " + str(output_heights_classes_file) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "output_heights_classes_vector : " + str(output_heights_classes_vector) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "heights_classes : " + str(heights_classes) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "epsg : " + str(epsg) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "no_data_value : " + str(no_data_value) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "format_raster : " + str(format_raster) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "grass_gisbase : " + str(grass_gisbase) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "grass_gisdb : " + str(grass_gisdb) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "grass_location : " + str(grass_location) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "grass_mapset : " + str(grass_mapset) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + " classesOfWaterHeights() : " + endC + "overwrite : " + str(overwrite) + endC + '\n') # Définition des constantes ENCODING_RASTER_FLOAT = 'float' ENCODING_RASTER_UINT8 = 'uint8' EXTENSION_RASTER_SAGA = '.sdat' FORMAT_VECTOR_GRASS = format_vector.replace(' ', '_') SUFFIX_TEMP = '_temp' SUFFIX_LINES = '_lines' SUFFIX_POINTS = '_points' SUFFIX_ALTI = '_altitude' SUFFIX_CUT = '_cut' SUFFIX_RAW = '_raw_heights' INDEX_FIELD = 'idx' ALTI_FIELD = 'altitude' VECTORISATION = 'GRASS' # Mise à jour du log starting_event = "classesOfWaterHeights() : Début du traitement : " timeLine(path_time_log, starting_event) print(cyan + "classesOfWaterHeights() : " + bold + green + "DEBUT DES TRAITEMENTS" + endC + '\n') # Définition des variables 'basename' flooded_areas_basename = os.path.splitext( os.path.basename(input_flooded_areas_vector))[0] digital_elevation_model_basename = os.path.splitext( os.path.basename(input_digital_elevation_model_file))[0] flooded_areas_lines_basename = flooded_areas_basename + SUFFIX_LINES flooded_areas_points_basename = flooded_areas_basename + SUFFIX_POINTS if output_heights_classes_file != "": output_heights_classes_basename = os.path.splitext( os.path.basename(output_heights_classes_file))[0] output_dirname = os.path.dirname(output_heights_classes_file) else: output_heights_classes_basename = os.path.splitext( os.path.basename(output_heights_classes_vector))[0] output_dirname = os.path.dirname(output_heights_classes_vector) # Définition des variables temp temp_directory = output_dirname + os.sep + output_heights_classes_basename + SUFFIX_TEMP flooded_areas_lines = temp_directory + os.sep + flooded_areas_lines_basename + extension_vector flooded_areas_points = temp_directory + os.sep + flooded_areas_points_basename + extension_vector altitude_points = temp_directory + os.sep + flooded_areas_points_basename + SUFFIX_ALTI + extension_vector altitude_grid = temp_directory + os.sep + flooded_areas_basename + SUFFIX_ALTI + EXTENSION_RASTER_SAGA altitude_file = temp_directory + os.sep + flooded_areas_basename + SUFFIX_ALTI + SUFFIX_CUT + extension_raster digital_elevation_model_cut = temp_directory + os.sep + digital_elevation_model_basename + SUFFIX_CUT + extension_raster raw_heights = temp_directory + os.sep + flooded_areas_basename + SUFFIX_RAW + extension_raster heights_classes_temp = temp_directory + os.sep + output_heights_classes_basename + extension_raster if output_heights_classes_file == "": output_heights_classes_file = output_dirname + os.sep + output_heights_classes_basename + extension_raster # Nettoyage des traitements précédents if debug >= 3: print(cyan + "classesOfWaterHeights() : " + endC + "Nettoyage des traitements précédents." + endC + '\n') removeFile(output_heights_classes_file) removeVectorFile(output_heights_classes_vector, format_vector=format_vector) cleanTempData(temp_directory) ############# # Etape 0/6 # Préparation des traitements ############# print(cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 0/6 - Début de la préparation des traitements." + endC + '\n') # Préparation de GRASS xmin, xmax, ymin, ymax = getEmpriseImage( input_digital_elevation_model_file) pixel_width, pixel_height = getPixelWidthXYImage( input_digital_elevation_model_file) grass_gisbase, grass_gisdb, grass_location, grass_mapset = initializeGrass( temp_directory, xmin, xmax, ymin, ymax, pixel_width, pixel_height, projection=epsg, gisbase=grass_gisbase, gisdb=grass_gisdb, location=grass_location, mapset=grass_mapset, clean_old=True, overwrite=overwrite) # Gestion des classes de hauteurs d'eau thresholds_list = heights_classes.split(',') thresholds_list_float = [float(x) for x in thresholds_list] thresholds_list_float.sort() thresholds_list_float_len = len(thresholds_list_float) print(cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 0/6 - Fin de la préparation des traitements." + endC + '\n') ############# # Etape 1/6 # Création de points sur le périmètre de l'emprise inondée ############# print( cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 1/6 - Début de la création de points sur le périmètre de l'emprise inondée." + endC + '\n') # Conversion de l'emprise inondée en polylignes convertePolygon2Polylines(input_flooded_areas_vector, flooded_areas_lines, overwrite=overwrite, format_vector=format_vector) # Création de points le long du polyligne use = 'vertex' dmax = 10 percent = False importVectorOgr2Grass(flooded_areas_lines, flooded_areas_lines_basename, overwrite=overwrite) pointsAlongPolylines(flooded_areas_lines_basename, flooded_areas_points_basename, use=use, dmax=dmax, percent=percent, overwrite=overwrite) exportVectorOgr2Grass(flooded_areas_points_basename, flooded_areas_points, format_vector=FORMAT_VECTOR_GRASS, overwrite=overwrite) # Ajout d'un index sur les points addNewFieldVector(flooded_areas_points, INDEX_FIELD, ogr.OFTInteger, field_value=None, field_width=None, field_precision=None, format_vector=format_vector) updateIndexVector(flooded_areas_points, index_name=INDEX_FIELD, format_vector=format_vector) print( cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 1/6 - Fin de la création de points sur le périmètre de l'emprise inondée." + endC + '\n') ############# # Etape 2/6 # Récupération de l'altitude sous chaque point ############# print( cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 2/6 - Début de la récupération de l'altitude sous chaque point." + endC + '\n') # Ajout d'un champ pour récupérer l'altitude addNewFieldVector(flooded_areas_points, ALTI_FIELD, ogr.OFTReal, field_value=None, field_width=None, field_precision=None, format_vector=format_vector) # Echantillonnage du MNT sous le fichier points importVectorOgr2Grass(flooded_areas_points, flooded_areas_points_basename, overwrite=overwrite) importRasterGdal2Grass(input_digital_elevation_model_file, digital_elevation_model_basename, overwrite=overwrite) sampleRasterUnderPoints(flooded_areas_points_basename, digital_elevation_model_basename, ALTI_FIELD, overwrite=overwrite) exportVectorOgr2Grass(flooded_areas_points_basename, altitude_points, format_vector=FORMAT_VECTOR_GRASS, overwrite=overwrite) print( cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 2/6 - Fin de la récupération de l'altitude sous chaque point." + endC + '\n') ############# # Etape 3/6 # Triangulation de l'altitude ############# print(cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 3/6 - Début de la triangulation de l'altitude." + endC + '\n') pixel_size = abs(min(pixel_width, pixel_height)) triangulationDelaunay(altitude_points, altitude_grid, ALTI_FIELD, cellsize=pixel_size) print(cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 3/6 - Fin de la triangulation de l'altitude." + endC + '\n') ############# # Etape 4/6 # Calcul des hauteurs brutes ############# print(cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 4/6 - Début du calcul des hauteurs brutes." + endC + '\n') # Redécoupage sur l'emprise inondée cutRasterImages([altitude_grid, input_digital_elevation_model_file], input_flooded_areas_vector, [altitude_file, digital_elevation_model_cut], 0, 0, epsg, no_data_value, "", False, path_time_log, format_raster=format_raster, format_vector=format_vector, extension_raster=extension_raster, extension_vector=extension_vector, save_results_intermediate=save_results_intermediate, overwrite=overwrite) # BandMath pour les hauteurs brutes (triangulation - MNT) expression = "im1b1 - im2b1" rasterCalculator([altitude_file, digital_elevation_model_cut], raw_heights, expression, codage=ENCODING_RASTER_FLOAT) print(cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 4/6 - Fin du calcul des hauteurs brutes." + endC + '\n') ############# # Etape 5/6 # Attribution des classes de hauteurs d'eau ############# print(cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 5/6 - Début de l'attribution des classes de hauteurs d'eau." + endC + '\n') # Génération de l'expression expression = "" for i in range(thresholds_list_float_len - 1): min_threshold = thresholds_list_float[i] max_threshold = thresholds_list_float[i + 1] expression += "im1b1>=%s and im1b1<%s ? %s : " % (min_threshold, max_threshold, i + 1) expression += "im1b1>=%s ? %s : 0" % (thresholds_list_float[ thresholds_list_float_len - 1], thresholds_list_float_len) # Calcul des classes de hauteurs d'eau rasterCalculator([raw_heights], heights_classes_temp, expression, codage=ENCODING_RASTER_UINT8) # Redécoupage propre des zones en dehors de l'emprise inondée cutImageByVector(input_flooded_areas_vector, heights_classes_temp, output_heights_classes_file, pixel_size_x=pixel_width, pixel_size_y=pixel_height, no_data_value=no_data_value, epsg=epsg, format_raster=format_raster, format_vector=format_vector) print(cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 5/6 - Fin de l'attribution des classes de hauteurs d'eau." + endC + '\n') ############# # Etape 6/6 # Vectorisation des classes de hauteurs d'eau ############# if output_heights_classes_vector != "": print( cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 6/6 - Début de la vectorisation des classes de hauteurs d'eau." + endC + '\n') name_column = 'class' umc_list = 0 if VECTORISATION == 'GRASS': vectorizeGrassClassification( output_heights_classes_file, output_heights_classes_vector, name_column, [umc_list], False, True, True, input_flooded_areas_vector, True, path_time_log, expression="", format_vector=format_vector, extension_raster=extension_raster, extension_vector=extension_vector, save_results_intermediate=save_results_intermediate, overwrite=overwrite) else: vectorizeClassification( output_heights_classes_file, output_heights_classes_vector, name_column, [umc_list], 2000, False, True, True, True, True, True, input_flooded_areas_vector, True, False, False, [0], path_time_log, expression="", format_vector=format_vector, extension_raster=extension_raster, extension_vector=extension_vector, save_results_intermediate=save_results_intermediate, overwrite=overwrite) print( cyan + "classesOfWaterHeights() : " + bold + green + "ETAPE 6/6 - Fin de la vectorisation des classes de hauteurs d'eau." + endC + '\n') else: print( cyan + "classesOfWaterHeights() : " + bold + yellow + "ETAPE 6/6 - Pas de vectorisation des classes de hauteurs d'eau demandée." + endC + '\n') # Suppression des fichiers temporaires if not save_results_intermediate: if debug >= 3: print(cyan + "classesOfWaterHeights() : " + endC + "Suppression des fichiers temporaires." + endC + '\n') deleteDir(temp_directory) print(cyan + "classesOfWaterHeights() : " + bold + green + "FIN DES TRAITEMENTS" + endC + '\n') # Mise à jour du log ending_event = "classesOfWaterHeights() : Fin du traitement : " timeLine(path_time_log, ending_event) return
def vectorsListToOcs(input_text, output_raster, footprint_vector, reference_raster, codage_raster='uint8', epsg=2154, no_data_value=0, format_raster='GTiff', format_vector='ESRI Shapefile', extension_raster='.tif', extension_vector='.shp', path_time_log='', save_results_intermediate=False, overwrite=True): if debug >= 3: print( '\n' + bold + green + "OCS raster à partir d'une liste de vecteurs - Variables dans la fonction :" + endC) print(cyan + " vectorsListToOcs() : " + endC + "input_text : " + str(input_text) + endC) print(cyan + " vectorsListToOcs() : " + endC + "output_raster : " + str(output_raster) + endC) print(cyan + " vectorsListToOcs() : " + endC + "footprint_vector : " + str(footprint_vector) + endC) print(cyan + " vectorsListToOcs() : " + endC + "reference_raster : " + str(reference_raster) + endC) print(cyan + " vectorsListToOcs() : " + endC + "codage_raster : " + str(codage_raster) + endC) print(cyan + " vectorsListToOcs() : " + endC + "epsg : " + str(epsg) + endC) print(cyan + " vectorsListToOcs() : " + endC + "no_data_value : " + str(no_data_value) + endC) print(cyan + " vectorsListToOcs() : " + endC + "format_raster : " + str(format_raster) + endC) print(cyan + " vectorsListToOcs() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + " vectorsListToOcs() : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + " vectorsListToOcs() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + " vectorsListToOcs() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + " vectorsListToOcs() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + " vectorsListToOcs() : " + endC + "overwrite : " + str(overwrite) + endC + '\n') # Définition des constantes SUFFIX_TEMP = '_temp' SUFFIX_CUT = '_cut' SUFFIX_FILTER = '_filter' SUFFIX_BUFFER = '_buffer' TEXT_SEPARATOR = ':' # Mise à jour du log starting_event = "vectorsListToOcs() : Début du traitement : " timeLine(path_time_log, starting_event) print(cyan + "vectorsListToOcs() : " + bold + green + "DEBUT DES TRAITEMENTS" + endC + '\n') # Définition des variables 'basename' output_raster_basename = os.path.basename( os.path.splitext(output_raster)[0]) output_raster_dirname = os.path.dirname(output_raster) # Définition des variables temp temp_directory = output_raster_dirname + os.sep + output_raster_basename + SUFFIX_TEMP temp_raster = temp_directory + os.sep + output_raster_basename + SUFFIX_TEMP + extension_raster # Nettoyage des traitements précédents if overwrite: if debug >= 3: print(cyan + "vectorsListToOcs() : " + endC + "Nettoyage des traitements précédents." + '\n') removeFile(output_raster) cleanTempData(temp_directory) else: if os.path.exists(output_raster): print(cyan + "vectorsListToOcs() : " + bold + yellow + "Le fichier de sortie existe déjà et ne sera pas regénéré." + endC) raise if not os.path.exixts(temp_directory): os.makedirs(temp_directory) pass # Test de l'emprise des fichiers vecteur d'emprise et raster de référence (le raster doit être de même taille ou plus grand que le vecteur) xmin_fpt, xmax_fpt, ymin_fpt, ymax_fpt = getEmpriseFile( footprint_vector, format_vector=format_vector) xmin_ref, xmax_ref, ymin_ref, ymax_ref = getEmpriseImage(reference_raster) if round(xmin_fpt, 4) < round(xmin_ref, 4) or round(xmax_fpt, 4) > round( xmax_ref, 4) or round(ymin_fpt, 4) < round(ymin_ref, 4) or round( ymax_fpt, 4) > round(ymax_ref, 4): print(cyan + "vectorsListToOcs() : " + bold + red + "xmin_fpt, xmax_fpt, ymin_fpt, ymax_fpt" + endC, xmin_fpt, xmax_fpt, ymin_fpt, ymax_fpt, file=sys.stderr) print(cyan + "vectorsListToOcs() : " + bold + red + "xmin_ref, xmax_ref, ymin_ref, ymax_ref" + endC, xmin_ref, xmax_ref, ymin_ref, ymax_ref, file=sys.stderr) raise NameError( cyan + "vectorsListToOcs() : " + bold + red + "The extend of the footprint vector (%s) is greater than the reference raster (%s)." % (footprint_vector, reference_raster) + endC) # Récupération des traitements à faire dans le fichier texte d'entrée text_list = readTextFileBySeparator(input_text, TEXT_SEPARATOR) #################################################################### print(cyan + "vectorsListToOcs() : " + bold + green + "Début de la génération de l'OCS raster à partir de vecteurs." + endC + '\n') # Boucle sur les traitements à réaliser for text in text_list: idx = text_list.index(text) + 1 class_label = int(text[0]) vector_file = text[1] if debug >= 3: print(cyan + "vectorsListToOcs() : " + endC + bold + "Génération %s/%s : " % (idx, len(text_list)) + endC + "traitement du fichier %s (label %s)." % (vector_file, str(class_label)) + '\n') # Gestion des noms des fichiers temporaires vector_file_basename = os.path.basename( os.path.splitext(vector_file)[0]) vector_file_cut = temp_directory + os.sep + vector_file_basename + SUFFIX_CUT + extension_vector vector_file_filter = temp_directory + os.sep + vector_file_basename + SUFFIX_FILTER + extension_vector vector_file_buffer = temp_directory + os.sep + vector_file_basename + SUFFIX_BUFFER + extension_vector vector_file_raster = temp_directory + os.sep + vector_file_basename + extension_raster # Gestion des variables de traitement (tampon et filtrage SQL) try: buffer_len = float(text[2]) except ValueError: buffer_len = text[2] except Exception: buffer_len = '' try: sql_filter = text[3] except Exception: sql_filter = '' # Découpage à l'emprise de la zone d'étude if debug >= 3: print(cyan + "vectorsListToOcs() : " + endC + "Découpage à l'emprise de la zone d'étude." + '\n') cutVectorAll(footprint_vector, vector_file, vector_file_cut, overwrite=overwrite, format_vector=format_vector) # Filtrage SQL (facultatif) if sql_filter != '': if debug >= 3: print(cyan + "vectorsListToOcs() : " + endC + "Application du filtrage SQL : %s." % sql_filter + '\n') attr_names_list = getAttributeNameList(vector_file_cut, format_vector=format_vector) column = "'" for attr_name in attr_names_list: column += attr_name + ", " column = column[:-2] column += "'" filterSelectDataVector(vector_file_cut, vector_file_filter, column, sql_filter, overwrite=overwrite, format_vector=format_vector) else: vector_file_filter = vector_file_cut # Application d'un tampon (facultatif) if buffer_len != '' and buffer_len != 0: if debug >= 3: print(cyan + "vectorsListToOcs() : " + endC + "Application d'un buffer : %s." % buffer_len + '\n') if type(buffer_len) is float: bufferVector(vector_file_filter, vector_file_buffer, buffer_len, col_name_buf='', fact_buf=1.0, quadsecs=10, format_vector=format_vector) else: bufferVector(vector_file_filter, vector_file_buffer, 0, col_name_buf=buffer_len, fact_buf=1.0, quadsecs=10, format_vector=format_vector) else: vector_file_buffer = vector_file_filter # Rastérisation du vecteur préparé if debug >= 3: print(cyan + "vectorsListToOcs() : " + endC + "Rastérisation du vecteur préparé." + '\n') rasterizeBinaryVector(vector_file_buffer, reference_raster, vector_file_raster, label=class_label, codage=codage_raster) # Ajout de l'information dans le raster de sortie if debug >= 3: print(cyan + "vectorsListToOcs() : " + endC + "Ajout de l'information dans le raster de sortie." + '\n') if idx == 1: shutil.copy(vector_file_raster, output_raster) else: removeFile(temp_raster) shutil.copy(output_raster, temp_raster) removeFile(output_raster) expression = "im1b1!=%s ? im1b1 : im2b1" % no_data_value rasterCalculator([temp_raster, vector_file_raster], output_raster, expression, codage=codage_raster) print(cyan + "vectorsListToOcs() : " + bold + green + "Fin de la génération de l'OCS raster à partir de vecteurs." + endC + '\n') #################################################################### # Suppression des fichiers temporaires if not save_results_intermediate: if debug >= 3: print(cyan + "vectorsListToOcs() : " + endC + "Suppression des fichiers temporaires." + '\n') deleteDir(temp_directory) print(cyan + "vectorsListToOcs() : " + bold + green + "FIN DES TRAITEMENTS" + endC + '\n') # Mise à jour du log ending_event = "vectorsListToOcs() : Fin du traitement : " timeLine(path_time_log, ending_event) return 0
def fillNodata(image_input, image_mask_input, image_output, save_results_intermediate=False): if debug >= 2: print( cyan + "fillNodata() : " + bold + "Remplissage des valeurs NoData sous SAGA, à partir du fichier : " + endC + image_input) # Constantes SUFFIX_TMP = "_tmp" SUFFIX_SAGA = "_saga" EXT_GRID_SAGA = '.sgrd' # Variables repertory_output = os.path.dirname(image_output) basename_image_input = os.path.splitext(os.path.basename(image_input))[0] basename_image_output = os.path.splitext(os.path.basename(image_output))[0] sub_repertory_temp_saga = repertory_output + os.sep + basename_image_output + SUFFIX_TMP + SUFFIX_SAGA image_input_saga = sub_repertory_temp_saga + os.sep + basename_image_input + EXT_GRID_SAGA image_output_saga = sub_repertory_temp_saga + os.sep + basename_image_output + EXT_GRID_SAGA if image_mask_input != "": basename_image_mask_input = os.path.splitext( os.path.basename(image_mask_input))[0] image_mask_input_saga = sub_repertory_temp_saga + os.sep + basename_image_mask_input + EXT_GRID_SAGA # Nettoyage du répertoire temporaire cleanTempData(sub_repertory_temp_saga) # Import de raster(s) sous SAGA importGtiff2Sgrd(image_input, image_input_saga) if image_mask_input != "": importGtiff2Sgrd(image_mask_input, image_mask_input_saga) # Utilisation de la fonction "Close Gaps" de SAGA pour boucher les trous de NoData if image_mask_input != "": command = "saga_cmd grid_tools 7 -INPUT %s -MASK %s -RESULT %s" % ( image_input_saga, image_mask_input_saga, image_output_saga) else: command = "saga_cmd grid_tools 7 -INPUT %s -RESULT %s" % ( image_input_saga, image_output_saga) exitCode = os.system(command) if exitCode != 0: print(command) raise NameError( bold + red + "fillNodata(): An error occured during execution SAGA (Close Gaps) command to file '" + str(image_input_saga) + "'. See error message above." + endC) # Export de raster sous SAGA exportSgrd2Gtiff(image_output_saga, image_output) # Suppression des fichiers intermédiaires if not save_results_intermediate: deleteDir(sub_repertory_temp_saga) return
def addCorrectionClass(image_input, image_output, class_add_data_dico, path_time_log, save_results_intermediate=False, overwrite=True) : # Mise à jour du Log starting_event = "addCorrectionClass() : Add data file treat to classification starting : " timeLine(path_time_log,starting_event) # print if debug >= 3: print(bold + green + "Variables dans la fonction" + endC) print(cyan + "addCorrectionClass() : " + endC + "image_input : " + str(image_input) + endC) print(cyan + "addCorrectionClass() : " + endC + "image_output : " + str(image_output) + endC) print(cyan + "addCorrectionClass() : " + endC + "class_add_data_dico : " + str(class_add_data_dico) + endC) print(cyan + "addCorrectionClass() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "addCorrectionClass() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "addCorrectionClass() : " + endC + "overwrite : " + str(overwrite) + endC) # Constantes FOLDER_MASK_TEMP = 'Mask_' SUFFIX_MASK = '_mask_' SUFFIX_MASK_FUSION = '_mask_fusion_' SUFFIX_CLASS_FUSION = '_macro_class_' CODAGE = "uint16" CODAGE_8B = "uint8" # Définition du répertoire temporaire name = os.path.splitext(os.path.basename(image_output))[0] repertory_samples_output = os.path.dirname(image_output) repertory_temp = repertory_samples_output + os.sep + FOLDER_MASK_TEMP + name # Création du répertoire temporaire si il n'existe pas if not os.path.isdir(repertory_temp): os.makedirs(repertory_temp) # Nettoyage du répertoire temporaire si il n'est pas vide cleanTempData(repertory_temp) # Test si le fichier résultat existent déjà et si ils doivent être écrasés check = os.path.isfile(image_output) if check and not overwrite: # Si les fichiers echantillons existent deja et que overwrite n'est pas activé print(bold + yellow + "File output : " + image_output + " already exists and will not be created again." + endC) else : if check: try: removeFile(image_output) except Exception: pass # si le fichier n'existe pas, il ne peut pas être supprimé : cette étape est ignorée image_combined_list = [] # Parcours du dictionnaire associant les macroclasses aux noms de fichiers et aux traitement associés for macroclass_label in class_add_data_dico : if debug >= 3: print("\nmacroclass_label : " + str(macroclass_label)) mask_fusion_list = [] # Parcours des fichiers à ajouter à la macro class for treatement_info_list in class_add_data_dico[macroclass_label] : input_image_to_treat = treatement_info_list[0] threshold_min = float(treatement_info_list[1]) threshold_max = float(treatement_info_list[2]) if debug >= 3: print("input_image_to_treat : " + str(input_image_to_treat)) print("threshold_min : " + str(threshold_min)) print("threshold_max : " + str(threshold_max)) # Traitement préparation file_mask_output_temp = repertory_temp + os.sep + os.path.splitext(os.path.basename(input_image_to_treat))[0] + SUFFIX_MASK + macroclass_label + os.path.splitext(input_image_to_treat)[1] if os.path.isfile(file_mask_output_temp) : removeFile(file_mask_output_temp) mask_fusion_list.append(file_mask_output_temp) # Creation masque binaire createBinaryMaskThreshold(input_image_to_treat, file_mask_output_temp, threshold_min, threshold_max) # Fusion des masques file_mask_output_fusion = repertory_temp + os.sep + os.path.splitext(os.path.basename(image_output))[0] + SUFFIX_MASK_FUSION + macroclass_label + os.path.splitext(image_output)[1] file_macroclass_output_fusion = repertory_temp + os.sep + os.path.splitext(os.path.basename(image_output))[0] + SUFFIX_CLASS_FUSION + macroclass_label + os.path.splitext(image_output)[1] image_combined_list.append(file_macroclass_output_fusion) if len(mask_fusion_list) == 1: # Pas de traitement à faire simple copie shutil.copyfile(mask_fusion_list[0], file_mask_output_fusion) else : # Fusionne les images mask en un seul masque image_mask_input = mask_fusion_list[0] for idx_image in range(1,len(mask_fusion_list)): print(idx_image) if idx_image == len(mask_fusion_list)-1: image_mask_output = file_mask_output_fusion else : image_mask_output = repertory_temp + os.sep + os.path.splitext(os.path.basename(image_output))[0] + SUFFIX_MASK_FUSION + macroclass_label + "_" + str(idx_image) + os.path.splitext(image_output)[1] applyMaskAnd(image_mask_input, mask_fusion_list[idx_image], image_mask_output, CODAGE_8B) image_mask_input = image_mask_output # Affectation du label de la macro class associé reaff_value_list =[] reaff_value_list.append(1) change_reaff_value_list = [] change_reaff_value_list.append(int(macroclass_label)) reallocateClassRaster(file_mask_output_fusion, file_macroclass_output_fusion, reaff_value_list, change_reaff_value_list, CODAGE) # Ajout de l'image de classification a la liste des image bd conbinées image_combined_list.append(image_input) # Fusionne les images raster et la classification mergeListRaster(image_combined_list, image_output, CODAGE) # Suppression des données intermédiaires if not save_results_intermediate: # Supression des .geom dans le dossier for to_delete in glob.glob(repertory_samples_output + os.sep + "*.geom"): removeFile(to_delete) # Suppression du repertoire temporaire deleteDir(repertory_temp) # Mise à jour du Log ending_event = "addCorrectionClass() : Add data file treat to classification ending : " timeLine(path_time_log,ending_event) return
def postTraitementsRaster(image_input, image_output, vector_input, enable_cutting_ask, post_treatment_raster_dico, no_data_value, path_time_log, format_raster='GTiff', format_vector='ESRI Shapefile', extension_raster=".tif", save_results_intermediate=False, overwrite=True): # Print if debug >= 3: print(bold + green + "Variables dans la fonction" + endC) print(cyan + "postTraitementsRaster() : " + endC + "image_input : " + str(image_input) + endC) print(cyan + "postTraitementsRaster() : " + endC + "image_output : " + str(image_output) + endC) print(cyan + "postTraitementsRaster() : " + endC + "vector_input : " + str(vector_input) + endC) print(cyan + "postTraitementsRaster() : " + endC + "enable_cutting_ask : " + str(enable_cutting_ask) + endC) print(cyan + "postTraitementsRaster() : " + endC + "post_treatment_raster_dico : " + str(post_treatment_raster_dico) + endC) print(cyan + "postTraitementsRaster() : " + endC + "no_data_value : " + str(no_data_value) + endC) print(cyan + "postTraitementsRaster() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "postTraitementsRaster() : " + endC + "format_raster : " + str(format_raster) + endC) print(cyan + "postTraitementsRaster() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "postTraitementsRaster() : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "postTraitementsRaster() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "postTraitementsRaster() : " + endC + "overwrite : " + str(overwrite) + endC) # Mise à jour du Log starting_event = "postTraitementsRaster() : Past traitements raster starting : " timeLine(path_time_log, starting_event) # Constantes CODAGE = "uint16" FOLDER_TEMP = "TempPtRaster_" SUFFIX_TEMP = "_tmp" SUFFIX_MASK = '_mask' SUFFIX_DILATED = '_dilated' SUFFIX_TOAPPLY = '_to_apply' # ETAPE 1 : NETTOYAGE DES DONNEES EXISTANTES print(cyan + "postTraitementsRaster() : " + bold + green + "NETTOYAGE DE L ESPACE DE TRAVAIL..." + endC) # Nom de base de l'image image_name = os.path.splitext(os.path.basename(image_input))[0] # Nettoyage d'anciennes données résultat check = os.path.isfile(image_output) if check and not overwrite: # Si le fichier résultat existe deja et que overwrite n'est pas activéptrd print(bold + yellow + "postTraitementsRaster() : " + endC + image_output + " already exists and will not be calculated again." + endC) else: # Cas où le fichier résultat n'existe pas au lancement if check: # Si overwrite est activé try: removeFile(image_output) # Tentative de suppression du fichier except Exception: pass # Si le fichier ne peut pas être supprimé, on suppose qu'il n'existe pas et on passe à la suite # Définition des répertoires temporaires repertory_output = os.path.dirname( image_output) # Ex : D2_Par_Zone\Paysage_01\Corr_3\Resultats basename_output = os.path.splitext(os.path.basename(image_output))[ 0] # Ex : Paysage_01_merged_filtred_pt extension = os.path.splitext(image_output)[1] image_output_temp = repertory_output + os.sep + basename_output + SUFFIX_TEMP + extension # Ex : D2_Par_Zone\Paysage_01\Corr_3\Resultats\Paysage_01_merged_filtred_pt_tmp.tif repertory_temp = repertory_output + os.sep + FOLDER_TEMP + image_name # Ex : D2_Par_Zone\Paysage_01\Corr_3\Resultats\Temp_pt_raster if debug >= 3: print(cyan + "postTraitementsRaster() : " + endC + "repertory_temp : " + str(repertory_temp) + endC) # Creation du repertoire de sortie s'il n'existe pas if not os.path.isdir(repertory_output): os.makedirs(repertory_output) # Nettoyage et creation du fichier temporaire: cleanTempData(repertory_temp) print(cyan + "postTraitementsRaster() : " + bold + green + "... FIN NETTOYAGE" + endC) # ETAPE 2 : TRAITEMENTS nb_treatments = len(post_treatment_raster_dico) idx = 0 key_traitement_list = list(post_treatment_raster_dico.keys()) for key_traitement in sorted(key_traitement_list): # Noms des fichiers temporaires binary_mask = repertory_temp + os.sep + basename_output + SUFFIX_MASK + key_traitement + extension_raster # Ex : D2_Par_Zone\Paysage_01\Corr_3\Resultats\Temp_pt_raster\Paysage_01_merged_filtred_pt_mask.tif dilated_binary_mask = repertory_temp + os.sep + basename_output + SUFFIX_DILATED + SUFFIX_MASK + key_traitement + extension_raster # Ex : D2_Par_Zone\Paysage_01\Corr_3\Resultats\Temp_pt_raster\Paysage_01_merged_filtred_pt_dilated_mask.tif mask_to_apply = repertory_temp + os.sep + basename_output + SUFFIX_TOAPPLY + SUFFIX_MASK + key_traitement + extension_raster # Ex : D2_Par_Zone\Paysage_01\Corr_3\Resultats\Temp_pt_raster\Paysage_01_merged_filtred_pt_to_apply_mask.tif # Extraction des informations du dictionnaire treatement_info_list = post_treatment_raster_dico[key_traitement][ 0] image_to_use = treatement_info_list[0] threshold_min = float(treatement_info_list[1]) threshold_max = float(treatement_info_list[2]) buffer_to_apply = int(treatement_info_list[3]) in_or_out = treatement_info_list[4] class_to_replace = str(treatement_info_list[5]) if class_to_replace.lower() != "all": class_to_replace = int(class_to_replace) replacement_class = int(treatement_info_list[6]) # ETAPE 2-1 CREATION DU MASQUE BINAIRE if debug >= 2: print( cyan + "postTraitementsRaster() : " + bold + green + "TRAITEMENT %s/%s - Etape 1/4 : CREATION DU MASQUE BINAIRE POUR %s " % (str(idx + 1), str(nb_treatments), image_to_use) + endC) createBinaryMaskThreshold(image_to_use, binary_mask, threshold_min, threshold_max, CODAGE) # ETAPE 2-2 CREATION DU MASQUE DILATE if buffer_to_apply == 0: # Si le buffer est nul, alors on copie juste le masque binaire if debug >= 2: print( cyan + "postTraitementsRaster() : " + bold + green + "TRAITEMENT %s/%s - Etape 2/4 : CREATION PAR RENOMMAGE DU MASQUE DILATE POUR %s" % (str(idx + 1), str(nb_treatments), image_to_use) + endC) os.rename(binary_mask, dilated_binary_mask) else: if debug >= 2: print( cyan + "postTraitementsRaster() : " + bold + green + "TRAITEMENT %s/%s - Etape 2/4 : CREATION DU MASQUE DILATE POUR %s " % (str(idx + 1), str(nb_treatments), image_to_use) + endC) # Creation d'un mask binaire bufferisé bufferBinaryRaster(binary_mask, dilated_binary_mask, buffer_to_apply, CODAGE) if not save_results_intermediate: if debug >= 3: print( cyan + "postTraitementsRaster() : " + bold + green + "TRAITEMENT %s/%s - Etape 2/4 : SUPRESSION DE %s QUI NE SERVIRA PLUS" % (str(idx + 1), str(nb_treatments), binary_mask) + endC) # Suppression du masque binaire de base removeFile(binary_mask) # ETAPE 2-3 CREATION DU MASQUE COMPLEMENTAIRE if in_or_out.lower() == "in": if debug >= 2: print( cyan + "postTraitementsRaster() : " + bold + green + "TRAITEMENT %s/%s - Etape 3/4 : CREATION PAR RENOMMAGE DU MASQUE COMPLEMENTAIRE A APPLIQUER POUR %s" % (str(idx + 1), str(nb_treatments), image_to_use) + endC) os.rename(dilated_binary_mask, mask_to_apply) else: if debug >= 2: print( cyan + "postTraitementsRaster() : " + bold + green + "TRAITEMENT %s/%s - Etape 3/4 : CREATION DU MASQUE COMPLEMENTAIRE POUR %s" % (str(idx + 1), str(nb_treatments), image_to_use) + endC) # Creation d'un mask binaire negatif createBinaryMask(dilated_binary_mask, mask_to_apply, 0.5, False, CODAGE) if not save_results_intermediate: if debug >= 3: print( cyan + "postTraitementsRaster() : " + bold + green + "TRAITEMENT %s/%s - Etape 3/4 : SUPRESSION DE %s QUI NE SERVIRA PLUS" % (str(idx + 1), str(nb_treatments), dilated_binary_mask) + endC) # Suppression du masque dilate removeFile(dilated_binary_mask) # ETAPE 2-4 APPLICATION DU MASQUE if str(class_to_replace).lower() == "all": expression = "\"(im1b1 == 1 ? %d : im2b1)\"" % ( int(replacement_class)) else: expression = "\"(im1b1 == 1 ? ( im2b1 == %d ? %d : im2b1) : im2b1)\"" % ( class_to_replace, replacement_class) if nb_treatments <= 1: input_for_command = image_input output_for_command = image_output_temp elif idx == 0: input_for_command = image_input output_for_command = repertory_temp + os.sep + basename_output + str( idx + 1) + extension_raster elif idx == nb_treatments - 1: input_for_command = repertory_temp + os.sep + basename_output + str( idx) + extension_raster output_for_command = image_output_temp else: input_for_command = repertory_temp + os.sep + basename_output + str( idx) + extension_raster output_for_command = repertory_temp + os.sep + basename_output + str( idx + 1) + extension_raster command = "otbcli_BandMath -il %s %s -exp %s -out %s %s" % ( mask_to_apply, input_for_command, expression, output_for_command, CODAGE) if debug >= 2: print( cyan + "postTraitementsRaster() : " + bold + green + "TRAITEMENT %s/%s - Etape 4/4 : APPLICATION DU POST TRAITEMENT AVEC %s" % (str(idx + 1), str(nb_treatments), mask_to_apply) + endC) print(command) exitCode = os.system(command) if exitCode != 0: print(command) raise NameError( bold + red + "postTraitementsRaster() : An error occured during otbcli_BandMath command. See error message above." + endC) if not save_results_intermediate: if debug >= 3: print( cyan + "postTraitementsRaster() : " + bold + green + "TRAITEMENT %s/%s - Etape 4/4 : SUPRESSION DE %s QUI NE SERVIRA PLUS" % (str(idx + 1), str(nb_treatments), mask_to_apply) + endC) # Suppression du masque à appliquer removeFile(mask_to_apply) # mise a jour de l'index idx += 1 # ETAPE 5 : DECOUPAGE DU RASTER DE SORTIE if enable_cutting_ask: # Decoupe du raster if not cutImageByVector(vector_input, image_output_temp, image_output, None, None, no_data_value, 0, format_raster, format_vector): print( cyan + "postTraitementsRaster() : " + bold + red + "!!! Une erreur c'est produite au cours du decoupage de l'image : " + image_output_temp + ". Voir message d'erreur." + endC, file=sys.stderr) raise if debug >= 2: print( cyan + "postTraitementsRaster() : " + bold + green + "TRAITEMENT %s/%s - Etape 5 : REDECOUPAGE DU RASTER DE SORTIE AVEC LE VECTEUR %s" % (str(idx + 1), str(nb_treatments), vector_input) + endC) if not save_results_intermediate: # Suppression du fichier intermediaire removeFile(image_output_temp) else: # Pas de redécoupage du fichier raster de sortie demander le fichier est juste renomé shutil.move(image_output_temp, image_output) # ETAPE 6 : SUPPRESSION DES FICHIERS INTERMEDIAIRES # Suppression des données intermédiaires if not save_results_intermediate: if debug >= 2: print( cyan + "postTraitementsRaster() : " + bold + green + "TRAITEMENT %s/%s - SUPRESSION DES DONNEES INTERMEDIAIRE CONTENUES DANS : %s" % (str(idx + 1), str(nb_treatments), repertory_temp) + endC) # Suppression du repertoire temporaire deleteDir(repertory_temp) # Mise à jour du Log ending_event = "postTraitementsRaster() : Add data base exogene to classification ending : " timeLine(path_time_log, ending_event) return
def skyViewFactor(grid_input, grid_output, mns_input, classif_input, class_build_list, dim_grid_x, dim_grid_y, svf_radius, svf_method, svf_dlevel, svf_ndirs, epsg, no_data_value, path_time_log, format_raster='GTiff', format_vector='ESRI Shapefile', extension_raster=".tif", extension_vector=".shp", save_results_intermediate=False, overwrite=True): print(bold + yellow + "Début du calcul de l'indicateur Sky View Factor." + endC + "\n") timeLine(path_time_log, "Début du calcul de l'indicateur Sky View Factor : ") if debug >= 3: print(bold + green + "skyViewFactor() : Variables dans la fonction" + endC) print(cyan + "skyViewFactor() : " + endC + "grid_input : " + str(grid_input) + endC) print(cyan + "skyViewFactor() : " + endC + "grid_output : " + str(grid_output) + endC) print(cyan + "skyViewFactor() : " + endC + "mns_input : " + str(mns_input) + endC) print(cyan + "skyViewFactor() : " + endC + "class_build_list : " + str(class_build_list) + endC) print(cyan + "skyViewFactor() : " + endC + "classif_input : " + str(classif_input) + endC) print(cyan + "skyViewFactor() : " + endC + "dim_grid_x : " + str(dim_grid_x) + endC) print(cyan + "skyViewFactor() : " + endC + "dim_grid_y : " + str(dim_grid_y) + endC) print(cyan + "skyViewFactor() : " + endC + "svf_radius : " + str(svf_radius) + endC) print(cyan + "skyViewFactor() : " + endC + "svf_method : " + str(svf_method) + endC) print(cyan + "skyViewFactor() : " + endC + "svf_dlevel : " + str(svf_dlevel) + endC) print(cyan + "skyViewFactor() : " + endC + "svf_ndirs : " + str(svf_ndirs) + endC) print(cyan + "skyViewFactor() : " + endC + "epsg : " + str(epsg) + endC) print(cyan + "skyViewFactor() : " + endC + "no_data_value : " + str(no_data_value) + endC) print(cyan + "skyViewFactor() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "skyViewFactor() : " + endC + "format_raster : " + str(format_raster) + endC) print(cyan + "skyViewFactor() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "skyViewFactor() : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "skyViewFactor() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "skyViewFactor() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "skyViewFactor() : " + endC + "overwrite : " + str(overwrite) + endC) # Constantes liées aux fichiers SKY_VIEW_FIELD = 'SkyView' BASE_FILE_TILE = 'tile_' BASE_FILE_POLY = 'poly_' SUFFIX_VECTOR_BUFF = '_buff' SUFFIX_VECTOR_TEMP = '_temp' # Constantes liées à l'arborescence FOLDER_SHP = 'SHP' FOLDER_SGRD = 'SGRD' FOLDER_TIF = 'TIF' SUB_FOLDER_DEM = 'DEM' SUB_FOLDER_SVF = 'SVF' SUB_SUB_FOLDER_BUF = 'BUF' if not os.path.exists(grid_output) or overwrite: ############################################ ### Préparation générale des traitements ### ############################################ if os.path.exists(grid_output): removeVectorFile(grid_output) temp_path = os.path.dirname(grid_output) + os.sep + "SkyViewFactor" sky_view_factor_raster = temp_path + os.sep + "sky_view_factor" + extension_raster cleanTempData(temp_path) os.makedirs(temp_path + os.sep + FOLDER_SHP) os.makedirs(temp_path + os.sep + FOLDER_SGRD + os.sep + SUB_FOLDER_DEM) os.makedirs(temp_path + os.sep + FOLDER_SGRD + os.sep + SUB_FOLDER_SVF) os.makedirs(temp_path + os.sep + FOLDER_TIF + os.sep + SUB_FOLDER_DEM) os.makedirs(temp_path + os.sep + FOLDER_TIF + os.sep + SUB_FOLDER_SVF) os.makedirs(temp_path + os.sep + FOLDER_TIF + os.sep + SUB_FOLDER_SVF + os.sep + SUB_SUB_FOLDER_BUF) # Récupération de la résolution du raster d'entrée pixel_size_x, pixel_size_y = getPixelWidthXYImage(mns_input) print(bold + "Taille de pixel du fichier '%s' :" % (mns_input) + endC) print(" pixel_size_x = " + str(pixel_size_x)) print(" pixel_size_y = " + str(pixel_size_y) + "\n") ############################################### ### Création des fichiers emprise et grille ### ############################################### print(bold + cyan + "Création des fichiers emprise et grille :" + endC) timeLine(path_time_log, " Création des fichiers emprise et grille : ") emprise_file = temp_path + os.sep + "emprise" + extension_vector quadrillage_file = temp_path + os.sep + "quadrillage" + extension_vector # Création du fichier d'emprise createVectorMask(mns_input, emprise_file, no_data_value, format_vector) # Création du fichier grille createGridVector(emprise_file, quadrillage_file, dim_grid_x, dim_grid_y, None, overwrite, epsg, format_vector) ############################################################# ### Extraction des carreaux de découpage et bufferisation ### ############################################################# print(bold + cyan + "Extraction des carreaux de découpage :" + endC) timeLine(path_time_log, " Extraction des carreaux de découpage : ") split_tile_vector_list = splitVector(quadrillage_file, temp_path + os.sep + FOLDER_SHP, "", epsg, format_vector, extension_vector) split_tile_vector_buff_list = [] # Boucle sur les fichiers polygones quadrillage for split_tile_vector in split_tile_vector_list: repertory_temp_output = os.path.dirname(split_tile_vector) base_name = os.path.splitext( os.path.basename(split_tile_vector))[0] split_tile_buff_vector = repertory_temp_output + os.sep + base_name + SUFFIX_VECTOR_BUFF + extension_vector split_tile_buff_vector_temp = repertory_temp_output + os.sep + base_name + SUFFIX_VECTOR_BUFF + SUFFIX_VECTOR_TEMP + extension_vector # Bufferisation bufferVector(split_tile_vector, split_tile_buff_vector_temp, svf_radius, "", 1.0, 10, format_vector) # Re-découpage avec l'emprise si la taille intersecte avec l'emprise if cutVector(emprise_file, split_tile_buff_vector_temp, split_tile_buff_vector, overwrite, format_vector): split_tile_vector_buff_list.append(split_tile_buff_vector) ########################################################## ### Découpage du MNS/MNH à l'emprise de chaque carreau ### ########################################################## print(bold + cyan + "Découpage du raster en tuiles :" + endC) timeLine(path_time_log, " Découpage du raster en tuiles : ") # Boucle sur les fichiers polygones quadrillage bufferisés for i in range(len(split_tile_vector_list)): print("Traitement de la tuile " + str(int(i) + 1) + str(len(split_tile_vector_list)) + "...") split_tile_vector = split_tile_vector_list[i] repertory_temp_output = os.path.dirname(split_tile_vector) base_name = os.path.splitext( os.path.basename(split_tile_vector))[0] split_tile_buff_vector = repertory_temp_output + os.sep + base_name + SUFFIX_VECTOR_BUFF + extension_vector dem_tif_file = temp_path + os.sep + FOLDER_TIF + os.sep + SUB_FOLDER_DEM + os.sep + BASE_FILE_TILE + str( i) + extension_raster if os.path.exists(split_tile_buff_vector): cutImageByVector(split_tile_buff_vector, mns_input, dem_tif_file, pixel_size_x, pixel_size_y, no_data_value, epsg, format_raster, format_vector) ################################################## ### Calcul du SVF pour chaque dalle du MNS/MNH ### ################################################## print(bold + cyan + "Calcul du SVF pour chaque tuile via SAGA :" + endC) timeLine(path_time_log, " Calcul du SVF pour chaque tuile via SAGA : ") svf_buf_tif_file_list = [] # Boucle sur les tuiles du raster d'entrée créées par chaque polygone quadrillage for i in range(len(split_tile_vector_list)): # Calcul de Sky View Factor par SAGA dem_tif_file = temp_path + os.sep + FOLDER_TIF + os.sep + SUB_FOLDER_DEM + os.sep + BASE_FILE_TILE + str( i) + extension_raster svf_buf_tif_file = temp_path + os.sep + FOLDER_TIF + os.sep + SUB_FOLDER_SVF + os.sep + SUB_SUB_FOLDER_BUF + os.sep + BASE_FILE_TILE + str( i) + extension_raster if os.path.exists(dem_tif_file): computeSkyViewFactor(dem_tif_file, svf_buf_tif_file, svf_radius, svf_method, svf_dlevel, svf_ndirs, save_results_intermediate) svf_buf_tif_file_list.append(svf_buf_tif_file) ################################################################### ### Re-découpage des tuiles du SVF avec les tuilles sans tampon ### ################################################################### print(bold + cyan + "Re-découpage des tuiles du SVF avec les tuilles sans tampon :" + endC) timeLine( path_time_log, " Re-découpage des tuiles du SVF avec les tuilles sans tampon : " ) folder_output_svf = temp_path + os.sep + FOLDER_TIF + os.sep + SUB_FOLDER_SVF + os.sep # Boucle sur les tuiles du SVF bufferisées for i in range(len(split_tile_vector_list)): print("Traitement de la tuile " + str(int(i) + 1) + "/" + str(len(split_tile_vector_list)) + "...") split_tile_vector = split_tile_vector_list[i] svf_buf_tif_file = temp_path + os.sep + FOLDER_TIF + os.sep + SUB_FOLDER_SVF + os.sep + SUB_SUB_FOLDER_BUF + os.sep + BASE_FILE_TILE + str( i) + extension_raster base_name = os.path.splitext(os.path.basename(svf_buf_tif_file))[0] svf_tif_file = folder_output_svf + os.sep + base_name + extension_raster if os.path.exists(svf_buf_tif_file): cutImageByVector(split_tile_vector, svf_buf_tif_file, svf_tif_file, pixel_size_x, pixel_size_y, no_data_value, epsg, format_raster, format_vector) #################################################################### ### Assemblage des tuiles du SVF et calcul de l'indicateur final ### #################################################################### print( bold + cyan + "Assemblage des tuiles du SVF et calcul de l'indicateur final :" + endC) timeLine( path_time_log, " Assemblage des tuiles du SVF et calcul de l'indicateur final : " ) classif_input_temp = temp_path + os.sep + FOLDER_TIF + os.sep + "classif_input" + SUFFIX_VECTOR_TEMP + extension_raster sky_view_factor_temp = temp_path + os.sep + FOLDER_TIF + os.sep + "sky_view_factor" + SUFFIX_VECTOR_TEMP + extension_raster # Issu de l'assemblage des dalles sky_view_factor_temp_temp = temp_path + os.sep + FOLDER_TIF + os.sep + "sky_view_factor" + SUFFIX_VECTOR_TEMP + SUFFIX_VECTOR_TEMP + extension_raster # Issu du redécoupage pour entrer correctement dans le BandMath sky_view_factor_temp_temp_temp = temp_path + os.sep + FOLDER_TIF + os.sep + "sky_view_factor" + SUFFIX_VECTOR_TEMP + SUFFIX_VECTOR_TEMP + SUFFIX_VECTOR_TEMP + extension_raster # Issu de la suppression des pixels bâti # Assemblage des tuiles du SVF créées précédemment pour ne former qu'un seul raster SVF selectAssembyImagesByHold( emprise_file, [folder_output_svf], sky_view_factor_temp, False, False, epsg, False, False, False, False, 1.0, pixel_size_x, pixel_size_y, no_data_value, "_", 2, 8, "", path_time_log, "_error", "_merge", "_clean", "_stack", format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) # Suppression des valeurs de SVF pour les bâtiments cutImageByVector(emprise_file, classif_input, classif_input_temp, pixel_size_x, pixel_size_y, no_data_value, epsg, format_raster, format_vector) cutImageByVector(emprise_file, sky_view_factor_temp, sky_view_factor_temp_temp, pixel_size_x, pixel_size_y, no_data_value, epsg, format_raster, format_vector) expression = "" for id_class in class_build_list: expression += "im1b1==%s or " % (str(id_class)) expression = expression[:-4] command = "otbcli_BandMath -il %s %s -out %s float -exp '%s ? -1 : im2b1'" % ( classif_input_temp, sky_view_factor_temp_temp, sky_view_factor_temp_temp_temp, expression) if debug >= 3: print(command) exit_code = os.system(command) if exit_code != 0: print(command) print( cyan + "skyViewFactor() : " + bold + red + "!!! Une erreur c'est produite au cours de la commande otbcli_BandMath : " + command + ". Voir message d'erreur." + endC, file=sys.stderr) raise command = "gdal_translate -a_srs EPSG:%s -a_nodata %s -of %s %s %s" % ( str(epsg), str(no_data_value), format_raster, sky_view_factor_temp_temp_temp, sky_view_factor_raster) if debug >= 3: print(command) exit_code = os.system(command) if exit_code != 0: print(command) print( cyan + "skyViewFactor() : " + bold + red + "!!! Une erreur c'est produite au cours de la commande gdal_translate : " + command + ". Voir message d'erreur." + endC, file=sys.stderr) raise # Croisement vecteur-raster pour récupérer la moyenne de SVF par polygone du fichier maillage d'entrée col_to_delete_list = [ "min", "max", "median", "sum", "std", "unique", "range" ] statisticsVectorRaster( sky_view_factor_raster, grid_input, grid_output, 1, False, False, True, col_to_delete_list, [], {}, path_time_log, True, format_vector, save_results_intermediate, overwrite ) ## Erreur NaN pour polygones entièrement bâtis (soucis pour la suite ?) renameFieldsVector(grid_output, ['mean'], [SKY_VIEW_FIELD], format_vector) ########################################## ### Nettoyage des fichiers temporaires ### ########################################## if not save_results_intermediate: deleteDir(temp_path) else: print(bold + magenta + "Le calcul du Sky View Factor a déjà eu lieu." + endC + "\n") print(bold + yellow + "Fin du calcul de l'indicateur Sky View Factor." + endC + "\n") timeLine(path_time_log, "Fin du calcul de l'indicateur Sky View Factor : ") return
def processMacroSamples(image_input, image_output, correction_images_input_list, treatment_mask_list, macro_sample_name, path_time_log, extension_raster=".tif", save_results_intermediate=False, overwrite=True): # Mise à jour du Log starting_event = "processMacroSamples() : process macro samples starting : " timeLine(path_time_log, starting_event) if debug >= 3: print(bold + green + "processMacroSamples() : Variables dans la fonction" + endC) print(cyan + "processMacroSamples() : " + endC + "image_input : " + str(image_input) + endC) print(cyan + "processMacroSamples() : " + endC + "image_output : " + str(image_output) + endC) print(cyan + "processMacroSamples() : " + endC + "correction_images_input_list : " + str(correction_images_input_list) + endC) print(cyan + "processMacroSamples() : " + endC + "treatment_mask_list : " + str(treatment_mask_list) + endC) print(cyan + "processMacroSamples() : " + endC + "macro_sample_name : " + str(macro_sample_name) + endC) print(cyan + "processMacroSamples() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "processMacroSamples() : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "processMacroSamples() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "processMacroSamples() : " + endC + "overwrite : " + str(overwrite) + endC) # Constantes FOLDER_MASK_TEMP = 'Mask_' CODAGE = "uint8" print(cyan + "processMacroSamples() : " + bold + green + "Nettoyage de l'espace de travail..." + endC) # Définition du répertoire temporaire repertory_samples_output = os.path.dirname(image_output) repertory_temp = repertory_samples_output + os.sep + FOLDER_MASK_TEMP + macro_sample_name # Création du répertoire temporaire si il n'existe pas if not os.path.isdir(repertory_temp): os.makedirs(repertory_temp) # Nettoyage du répertoire temporaire si il n'est pas vide cleanTempData(repertory_temp) # Test si le fichier résultat existent déjà et si ils doivent être écrasés check = os.path.isfile(image_output) if check and not overwrite: # Si les fichiers echantillons existent deja et que overwrite n'est pas activé print(bold + yellow + "File output : " + image_output + " already exists and will not be created again." + endC) else: if check: try: removeFile(image_output) except Exception: pass # si le fichier n'existe pas, il ne peut pas être supprimé : cette étape est ignorée # Information issue de la liste des paramètres de traitement if correction_images_input_list == []: # Pas de traitement à faire simple copie if debug >= 1: print(cyan + "processMacroSamples() : " + endC + "Copy file" + image_input + " to " + image_output) shutil.copyfile(image_input, image_output) else: # Liste de tous les traitement à faire cpt_treat = 0 sample_raster_file_output = image_output for idx_treatement in range(len(correction_images_input_list)): treatement_info_list = treatment_mask_list[idx_treatement] file_mask_input = correction_images_input_list[idx_treatement] if debug >= 3: print(cyan + "processMacroSamples() : " + endC + "Traitement parametres : " + str(treatement_info_list) + " avec l'image : " + file_mask_input) base_mask_name = treatement_info_list[0] threshold_min = float(treatement_info_list[1]) threshold_max = float(treatement_info_list[2]) if len(treatement_info_list) >= 4: filter_size_zone_0 = int(treatement_info_list[3]) else: filter_size_zone_0 = 0 if len(treatement_info_list) >= 5: filter_size_zone_1 = int(treatement_info_list[4]) else: filter_size_zone_1 = 0 if len(treatement_info_list) >= 6: mask_operator = str(treatement_info_list[5]) else: mask_operator = "and" if cpt_treat == 0: sample_raster_file_input_temp = image_input # Appel de la fonction de traitement processingMacroSample(sample_raster_file_input_temp, sample_raster_file_output, file_mask_input, threshold_min, threshold_max, filter_size_zone_0, filter_size_zone_1, mask_operator, repertory_temp, CODAGE, path_time_log) cpt_treat += 1 if cpt_treat != len(correction_images_input_list): sample_raster_file_input_temp = os.path.splitext( sample_raster_file_output)[0] + str( cpt_treat) + extension_raster os.rename(sample_raster_file_output, sample_raster_file_input_temp) # Nettoyage du traitement precedent sample_raster_file_input_temp_before = os.path.splitext( sample_raster_file_output)[0] + str(cpt_treat - 1) + extension_raster if os.path.isfile(sample_raster_file_input_temp_before): removeFile(sample_raster_file_input_temp_before) print(cyan + "processMacroSamples() : " + bold + green + "Fin des traitements" + endC) # Suppression des données intermédiaires if not save_results_intermediate: # Supression des .geom dans le dossier for to_delete in glob.glob(repertory_samples_output + os.sep + "*.geom"): removeFile(to_delete) # Suppression du repertoire temporaire deleteDir(repertory_temp) # Mise à jour du Log ending_event = "processMacroSamples() : process macro samples ending : " timeLine(path_time_log, ending_event) return
def createDifference(image_ortho_input, image_mns_input, image_mnt_input, bd_vector_input_list, zone_buffer_dico, departments_list, image_difference_output, vector_difference_output, fileld_bd_raster, simplifie_param, threshold_ndvi, threshold_difference, filter_difference_0, filter_difference_1, path_time_log, format_vector='ESRI Shapefile', extension_raster=".tif", extension_vector=".shp", save_results_intermediate=False, channel_order=['Red', 'Green', 'Blue', 'NIR'], overwrite=True): # Mise à jour du Log starting_event = "createDifference() : create macro samples starting : " timeLine(path_time_log, starting_event) # constantes CODAGE = "float" FOLDER_MASK_TEMP = 'Mask_' FOLDER_CUTTING_TEMP = 'Cut_' FOLDER_BUFF_TEMP = 'Buff_' FOLDER_RESULT_TEMP = 'Tmp_' SUFFIX_MASK_CRUDE = '_mcrude' SUFFIX_MASK = '_mask' SUFFIX_FILTERED = '_filtered' SUFFIX_VECTOR_CUT = '_decoup' SUFFIX_VECTOR_BUFF = '_buff' SUFFIX_NEW_MNS = '_new_mns' SUFFIX_DIFF_MNS = '_diff_mns' SUFFIX_NDVI = '_ndvi' # print if debug >= 3: print(bold + green + "Variables dans la fonction" + endC) print(cyan + "createDifference() : " + endC + "image_ortho_input : " + str(image_ortho_input) + endC) print(cyan + "createDifference() : " + endC + "image_mns_input : " + str(image_mns_input) + endC) print(cyan + "createDifference() : " + endC + "image_mnt_input : " + str(image_mnt_input) + endC) print(cyan + "createDifference() : " + endC + "bd_vector_input_list : " + str(bd_vector_input_list) + endC) print(cyan + "createDifference() : " + endC + "zone_buffer_dico : " + str(zone_buffer_dico) + endC) print(cyan + "createDifference() : " + endC + "departments_list : " + str(departments_list) + endC) print(cyan + "createDifference() : " + endC + "image_difference_output : " + str(image_difference_output) + endC) print(cyan + "createDifference() : " + endC + "vector_difference_output : " + str(vector_difference_output) + endC) print(cyan + "createDifference() : " + endC + "fileld_bd_raster : " + str(fileld_bd_raster) + endC) print(cyan + "createDifference() : " + endC + "simplifie_param : " + str(simplifie_param) + endC) print(cyan + "createDifference() : " + endC + "threshold_ndvi : " + str(threshold_ndvi) + endC) print(cyan + "createDifference() : " + endC + "threshold_difference : " + str(threshold_difference) + endC) print(cyan + "createDifference() : " + endC + "filter_difference_0 : " + str(filter_difference_0) + endC) print(cyan + "createDifference() : " + endC + "filter_difference_1 : " + str(filter_difference_1) + endC) print(cyan + "createDifference() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "createDifference() : " + endC + "channel_order : " + str(channel_order) + endC) print(cyan + "createDifference() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "createDifference() : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "createDifference() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "createDifference() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "createDifference() : " + endC + "overwrite : " + str(overwrite) + endC) # ETAPE 1 : NETTOYER LES DONNEES EXISTANTES print(cyan + "createDifference() : " + bold + green + "NETTOYAGE ESPACE DE TRAVAIL..." + endC) # Nom de base de l'image image_name = os.path.splitext(os.path.basename(image_ortho_input))[0] # Test si le fichier résultat différence existe déjà et si il doit être écrasés check = os.path.isfile(vector_difference_output) if check and not overwrite: # Si le fichier difference existe deja et que overwrite n'est pas activé print(cyan + "createDifference() : " + bold + yellow + "File difference " + vector_difference_output + " already exists and will not be created again." + endC) else: if check: try: removeFile(vector_difference_output) except Exception: pass # si le fichier n'existe pas, il ne peut pas être supprimé : cette étape est ignorée # Définition des répertoires temporaires repertory_output = os.path.dirname(vector_difference_output) repertory_output_temp = repertory_output + os.sep + FOLDER_RESULT_TEMP + image_name repertory_mask_temp = repertory_output + os.sep + FOLDER_MASK_TEMP + image_name repertory_samples_cutting_temp = repertory_output + os.sep + FOLDER_CUTTING_TEMP + image_name repertory_samples_buff_temp = repertory_output + os.sep + FOLDER_BUFF_TEMP + image_name print(repertory_output_temp) print(repertory_mask_temp) print(repertory_samples_cutting_temp) print(repertory_samples_buff_temp) # Création des répertoires temporaire qui n'existent pas if not os.path.isdir(repertory_output_temp): os.makedirs(repertory_output_temp) if not os.path.isdir(repertory_mask_temp): os.makedirs(repertory_mask_temp) if not os.path.isdir(repertory_samples_cutting_temp): os.makedirs(repertory_samples_cutting_temp) if not os.path.isdir(repertory_samples_buff_temp): os.makedirs(repertory_samples_buff_temp) # Nettoyage des répertoires temporaire qui ne sont pas vide cleanTempData(repertory_mask_temp) cleanTempData(repertory_samples_cutting_temp) cleanTempData(repertory_samples_buff_temp) cleanTempData(repertory_output_temp) BD_topo_layers_list = [] #zone = zone_buffer_dico.keys()[0] zone = list(zone_buffer_dico)[0] # Creation liste des couches des bd exogenes utilisées for layers_buffer in zone_buffer_dico[zone]: BD_topo_layers_list.append(layers_buffer[0]) print(cyan + "createDifference() : " + bold + green + "... FIN NETTOYAGE" + endC) # ETAPE 2 : DECOUPER LES VECTEURS print(cyan + "createDifference() : " + bold + green + "DECOUPAGE ECHANTILLONS..." + endC) # 2.1 : Création du masque délimitant l'emprise de la zone par image vector_mask = repertory_mask_temp + os.sep + image_name + SUFFIX_MASK_CRUDE + extension_vector createVectorMask(image_ortho_input, vector_mask) # 2.2 : Simplification du masque vector_simple_mask = repertory_mask_temp + os.sep + image_name + SUFFIX_MASK + extension_vector simplifyVector(vector_mask, vector_simple_mask, simplifie_param, format_vector) # 2.3 : Découpage des vecteurs copiés en local avec le masque vector_output_list = [] for vector_input in bd_vector_input_list: vector_name = os.path.splitext(os.path.basename(vector_input))[0] extension = os.path.splitext(os.path.basename(vector_input))[1] vector_output = repertory_samples_cutting_temp + os.sep + vector_name + SUFFIX_VECTOR_CUT + extension vector_output_list.append(vector_output) cutoutVectors(vector_simple_mask, bd_vector_input_list, vector_output_list, format_vector) print(cyan + "createDifference() : " + bold + green + "...FIN DECOUPAGE" + endC) # ETAPE 3 : BUFFERISER LES VECTEURS print(cyan + "createDifference() : " + bold + green + "MISE EN PLACE DES TAMPONS..." + endC) # Parcours du dictionnaire associant la zone aux noms de fichiers et aux tampons associés for elem_buff in zone_buffer_dico[zone]: # Parcours des départements for dpt in departments_list: input_shape = repertory_samples_cutting_temp + os.sep + elem_buff[ 0] + "_" + dpt + SUFFIX_VECTOR_CUT + extension_vector output_shape = repertory_samples_buff_temp + os.sep + elem_buff[ 0] + "_" + dpt + SUFFIX_VECTOR_BUFF + extension_vector buff = elem_buff[1] if os.path.isfile(input_shape): if debug >= 3: print(cyan + "createDifference() : " + endC + "input_shape : " + str(input_shape) + endC) print(cyan + "createDifference() : " + endC + "output_shape : " + str(output_shape) + endC) print(cyan + "createDifference() : " + endC + "buff : " + str(buff) + endC) bufferVector(input_shape, output_shape, buff, "", 1.0, 10, format_vector) else: print(cyan + "createDifference() : " + bold + yellow + "Pas de fichier du nom : " + endC + input_shape) print(cyan + "createDifference() : " + bold + green + "FIN DE L AFFECTATION DES TAMPONS" + endC) # ETAPE 4 : FUSION DES SHAPES DE LA BD TOPO print(cyan + "createDifference() : " + bold + green + "FUSION DATA BD..." + endC) shape_buff_list = [] # Parcours du dictionnaire associant la zone au nom du fichier for elem_buff in zone_buffer_dico[zone]: # Parcours des départements for dpt in departments_list: shape_file = repertory_samples_buff_temp + os.sep + elem_buff[ 0] + "_" + dpt + SUFFIX_VECTOR_BUFF + extension_vector if os.path.isfile(shape_file): shape_buff_list.append(shape_file) print("file for fusion : " + shape_file) else: print(bold + yellow + "pas de fichiers avec ce nom : " + endC + shape_file) # si une liste de fichier shape existe if not shape_buff_list: print(bold + yellow + "Pas de fusion sans donnee a fusionnee" + endC) else: # Fusion des fichiers shape image_zone_shape = repertory_output_temp + os.sep + image_name + '_' + zone + extension_vector fusionVectors(shape_buff_list, image_zone_shape) print("File BD : " + image_zone_shape) print(cyan + "createDifference() : " + bold + green + "FIN DE LA FUSION" + endC) # ETAPE 5 : RASTERISER LE FICHIER SHAPE DE ZONE BD print(cyan + "createDifference() : " + bold + green + "RASTERIZATION DE LA FUSION..." + endC) image_zone_raster = repertory_output_temp + os.sep + image_name + '_' + zone + extension_raster rasterizeVector(image_zone_shape, image_zone_raster, image_ortho_input, fileld_bd_raster, codage=CODAGE) print(cyan + "createDifference() : " + bold + green + "FIN DE LA RASTERIZATION" + endC) # ETAPE 6 : CREER UN NOUVEAU MMS ISSU DU MNT + DATA BD_TOPO print(cyan + "createDifference() : " + bold + green + "CREATION NOUVEAU MNS..." + endC) image_new_mns_output = repertory_output_temp + os.sep + image_name + SUFFIX_NEW_MNS + extension_raster createMNS(image_ortho_input, image_mnt_input, image_zone_raster, image_new_mns_output) print(cyan + "createDifference() : " + bold + green + "FIN DE LA CREATION MNS" + endC) # ETAPE 7 : CREER D'UN MASQUE SUR LES ZONES VEGETALES print(cyan + "createDifference() : " + bold + green + "CREATION DU NDVI..." + endC) image_ndvi_output = repertory_output_temp + os.sep + image_name + SUFFIX_NDVI + extension_raster createNDVI(image_ortho_input, image_ndvi_output, channel_order) print(cyan + "createDifference() : " + bold + green + "FIN DE LA CREATION DU NDVI" + endC) print(cyan + "createDifference() : " + bold + green + "CREATION DU MASQUE NDVI..." + endC) image_ndvi_mask_output = repertory_output_temp + os.sep + image_name + SUFFIX_NDVI + SUFFIX_MASK + extension_raster createBinaryMask(image_ndvi_output, image_ndvi_mask_output, threshold_ndvi, False) print(cyan + "createDifference() : " + bold + green + "FIN DE LA CREATION DU MASQUE NDVI" + endC) # ETAPE 8 : CREER UN FICHIER DE DIFFERENCE DES MNS AVEC MASQUAGE DES ZONES VEGETALES print(cyan + "createDifference() : " + bold + green + "CREATION DIFFERENCE MNS..." + endC) #image_diff_mns_output = repertory_output + os.sep + image_name + SUFFIX_DIFF_MNS + extension_raster image_diff_mns_output = image_difference_output createDifferenceFile(image_mns_input, image_new_mns_output, image_ndvi_mask_output, image_diff_mns_output) print(cyan + "createDifference() : " + bold + green + "FIN DE LA CREATION DE LA DIFFERENCE MNS" + endC) print(cyan + "createDifference() : " + bold + green + "CREATION DU MASQUE DE DIFFERENCE..." + endC) image_diff_mns_mask_output = repertory_output_temp + os.sep + image_name + SUFFIX_DIFF_MNS + SUFFIX_MASK + extension_raster createBinaryMask(image_diff_mns_output, image_diff_mns_mask_output, threshold_difference, True) print(cyan + "createDifference() : " + bold + green + "FIN DE LA CREATION DU MASQUE DE DIFFERENCE" + endC) print(cyan + "createDifference() : " + bold + green + "FILTRAGE DU MASQUE DE DIFFERENCE..." + endC) image_diff_mns_filtered_output = repertory_output_temp + os.sep + image_name + SUFFIX_DIFF_MNS + SUFFIX_FILTERED + extension_raster filterBinaryRaster(image_diff_mns_mask_output, image_diff_mns_filtered_output, filter_difference_0, filter_difference_1) print(cyan + "createDifference() : " + bold + green + "FIN DU FILTRAGE DU MASQUE DE DIFFERENCE" + endC) # ETAPE 9 : RASTERISER LE FICHIER DE DIFFERENCE DES MNS print(cyan + "createDifference() : " + bold + green + "VECTORISATION DU RASTER DE DIFFERENCE..." + endC) vector_diff_mns_filtered_output = repertory_output_temp + os.sep + image_name + SUFFIX_DIFF_MNS + SUFFIX_FILTERED + extension_vector polygonizeRaster(image_diff_mns_filtered_output, vector_diff_mns_filtered_output, image_name, field_name="DN") print(cyan + "createDifference() : " + bold + green + "FIN DE VECTORISATION DU RASTER DE DIFFERENCE" + endC) print(cyan + "createDifference() : " + bold + green + "SIMPLIFICATION VECTEUR DE DIFFERENCE..." + endC) simplifyVector(vector_diff_mns_filtered_output, vector_difference_output, simplifie_param, format_vector) print(cyan + "createDifference() : " + bold + green + "FIN DE SIMPLIFICATION DI VECTEUR DE DIFFERENCE" + endC) # ETAPE 10 : SUPPRESIONS FICHIERS INTERMEDIAIRES INUTILES if not save_results_intermediate: # Supression des .geom dans le dossier for to_delete in glob.glob(repertory_mask_temp + os.sep + "*.geom"): removeFile(to_delete) # Suppression des repertoires temporaires deleteDir(repertory_mask_temp) deleteDir(repertory_samples_cutting_temp) deleteDir(repertory_samples_buff_temp) deleteDir(repertory_output_temp) # Mise à jour du Log ending_event = "createDifference() : create macro samples ending : " timeLine(path_time_log, ending_event) return
def createMacroSamples(image_input, vector_to_cut_input, vector_sample_output, raster_sample_output, bd_vector_input_list, bd_buff_list, sql_expression_list, path_time_log, macro_sample_name="", simplify_vector_param=10.0, format_vector='ESRI Shapefile', extension_vector=".shp", save_results_intermediate=False, overwrite=True) : # Mise à jour du Log starting_event = "createMacroSamples() : create macro samples starting : " timeLine(path_time_log,starting_event) if debug >= 3: print(bold + green + "createMacroSamples() : Variables dans la fonction" + endC) print(cyan + "createMacroSamples() : " + endC + "image_input : " + str(image_input) + endC) print(cyan + "createMacroSamples() : " + endC + "vector_to_cut_input : " + str(vector_to_cut_input) + endC) print(cyan + "createMacroSamples() : " + endC + "vector_sample_output : " + str(vector_sample_output) + endC) print(cyan + "createMacroSamples() : " + endC + "raster_sample_output : " + str(raster_sample_output) + endC) print(cyan + "createMacroSamples() : " + endC + "bd_vector_input_list : " + str(bd_vector_input_list) + endC) print(cyan + "createMacroSamples() : " + endC + "bd_buff_list : " + str(bd_buff_list) + endC) print(cyan + "createMacroSamples() : " + endC + "sql_expression_list : " + str(sql_expression_list) + endC) print(cyan + "createMacroSamples() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "createMacroSamples() : " + endC + "macro_sample_name : " + str(macro_sample_name) + endC) print(cyan + "createMacroSamples() : " + endC + "simplify_vector_param : " + str(simplify_vector_param) + endC) print(cyan + "createMacroSamples() : " + endC + "format_vector : " + str(format_vector)) print(cyan + "createMacroSamples() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "createMacroSamples() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "createMacroSamples() : " + endC + "overwrite : " + str(overwrite) + endC) # Constantes FOLDER_MASK_TEMP = "Mask_" FOLDER_CUTTING_TEMP = "Cut_" FOLDER_FILTERING_TEMP = "Filter_" FOLDER_BUFF_TEMP = "Buff_" SUFFIX_MASK_CRUDE = "_crude" SUFFIX_MASK = "_mask" SUFFIX_VECTOR_CUT = "_cut" SUFFIX_VECTOR_FILTER = "_filt" SUFFIX_VECTOR_BUFF = "_buff" CODAGE = "uint8" # ETAPE 1 : NETTOYER LES DONNEES EXISTANTES print(cyan + "createMacroSamples() : " + bold + green + "Nettoyage de l'espace de travail..." + endC) # Nom du repertoire de calcul repertory_macrosamples_output = os.path.dirname(vector_sample_output) # Test si le vecteur echantillon existe déjà et si il doit être écrasés check = os.path.isfile(vector_sample_output) or os.path.isfile(raster_sample_output) if check and not overwrite: # Si les fichiers echantillons existent deja et que overwrite n'est pas activé print(bold + yellow + "File sample : " + vector_sample_output + " already exists and will not be created again." + endC) else : if check: try: removeVectorFile(vector_sample_output) removeFile(raster_sample_output) except Exception: pass # si le fichier n'existe pas, il ne peut pas être supprimé : cette étape est ignorée # Définition des répertoires temporaires repertory_mask_temp = repertory_macrosamples_output + os.sep + FOLDER_MASK_TEMP + macro_sample_name repertory_samples_cutting_temp = repertory_macrosamples_output + os.sep + FOLDER_CUTTING_TEMP + macro_sample_name repertory_samples_filtering_temp = repertory_macrosamples_output + os.sep + FOLDER_FILTERING_TEMP + macro_sample_name repertory_samples_buff_temp = repertory_macrosamples_output + os.sep + FOLDER_BUFF_TEMP + macro_sample_name if debug >= 4: print(cyan + "createMacroSamples() : " + endC + "Création du répertoire : " + str(repertory_mask_temp)) print(cyan + "createMacroSamples() : " + endC + "Création du répertoire : " + str(repertory_samples_cutting_temp)) print(cyan + "createMacroSamples() : " + endC + "Création du répertoire : " + str(repertory_samples_buff_temp)) # Création des répertoires temporaire qui n'existent pas if not os.path.isdir(repertory_macrosamples_output): os.makedirs(repertory_macrosamples_output) if not os.path.isdir(repertory_mask_temp): os.makedirs(repertory_mask_temp) if not os.path.isdir(repertory_samples_cutting_temp): os.makedirs(repertory_samples_cutting_temp) if not os.path.isdir(repertory_samples_filtering_temp): os.makedirs(repertory_samples_filtering_temp) if not os.path.isdir(repertory_samples_buff_temp): os.makedirs(repertory_samples_buff_temp) # Nettoyage des répertoires temporaire qui ne sont pas vide cleanTempData(repertory_mask_temp) cleanTempData(repertory_samples_cutting_temp) cleanTempData(repertory_samples_filtering_temp) cleanTempData(repertory_samples_buff_temp) print(cyan + "createMacroSamples() : " + bold + green + "... fin du nettoyage" + endC) # ETAPE 2 : DECOUPAGE DES VECTEURS print(cyan + "createMacroSamples() : " + bold + green + "Decoupage des echantillons ..." + endC) if vector_to_cut_input == None : # 2.1 : Création du masque délimitant l'emprise de la zone par image image_name = os.path.splitext(os.path.basename(image_input))[0] vector_mask = repertory_mask_temp + os.sep + image_name + SUFFIX_MASK_CRUDE + extension_vector createVectorMask(image_input, vector_mask) # 2.2 : Simplification du masque vector_simple_mask = repertory_mask_temp + os.sep + image_name + SUFFIX_MASK + extension_vector simplifyVector(vector_mask, vector_simple_mask, simplify_vector_param, format_vector) else : vector_simple_mask = vector_to_cut_input # 2.3 : Découpage des vecteurs de bd exogenes avec le masque vectors_cut_list = [] for vector_input in bd_vector_input_list : vector_name = os.path.splitext(os.path.basename(vector_input))[0] vector_cut = repertory_samples_cutting_temp + os.sep + vector_name + SUFFIX_VECTOR_CUT + extension_vector vectors_cut_list.append(vector_cut) cutoutVectors(vector_simple_mask, bd_vector_input_list, vectors_cut_list, format_vector) print(cyan + "createMacroSamples() : " + bold + green + "... fin du decoupage" + endC) # ETAPE 3 : FILTRAGE DES VECTEURS print(cyan + "createMacroSamples() : " + bold + green + "Filtrage des echantillons ..." + endC) vectors_filtered_list = [] if sql_expression_list != [] : for idx_vector in range (len(bd_vector_input_list)): vector_name = os.path.splitext(os.path.basename(bd_vector_input_list[idx_vector]))[0] vector_cut = vectors_cut_list[idx_vector] if idx_vector < len(sql_expression_list) : sql_expression = sql_expression_list[idx_vector] else : sql_expression = "" vector_filtered = repertory_samples_filtering_temp + os.sep + vector_name + SUFFIX_VECTOR_FILTER + extension_vector vectors_filtered_list.append(vector_filtered) # Filtrage par ogr2ogr if sql_expression != "": names_attribut_list = getAttributeNameList(vector_cut, format_vector) column = "'" for name_attribut in names_attribut_list : column += name_attribut + ", " column = column[0:len(column)-2] column += "'" ret = filterSelectDataVector(vector_cut, vector_filtered, column, sql_expression, format_vector) if not ret : print(cyan + "createMacroSamples() : " + bold + yellow + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" %(sql_expression) + endC) copyVectorFile(vector_cut, vector_filtered) else : print(cyan + "createMacroSamples() : " + bold + yellow + "Pas de filtrage sur le fichier du nom : " + endC + vector_filtered) copyVectorFile(vector_cut, vector_filtered) else : print(cyan + "createMacroSamples() : " + bold + yellow + "Pas de filtrage demandé" + endC) for idx_vector in range (len(bd_vector_input_list)): vector_cut = vectors_cut_list[idx_vector] vectors_filtered_list.append(vector_cut) print(cyan + "createMacroSamples() : " + bold + green + "... fin du filtrage" + endC) # ETAPE 4 : BUFFERISATION DES VECTEURS print(cyan + "createMacroSamples() : " + bold + green + "Mise en place des tampons..." + endC) vectors_buffered_list = [] if bd_buff_list != [] : # Parcours des vecteurs d'entrée for idx_vector in range (len(bd_vector_input_list)): vector_name = os.path.splitext(os.path.basename(bd_vector_input_list[idx_vector]))[0] buff = bd_buff_list[idx_vector] vector_filtered = vectors_filtered_list[idx_vector] vector_buffered = repertory_samples_buff_temp + os.sep + vector_name + SUFFIX_VECTOR_BUFF + extension_vector if buff != 0: if os.path.isfile(vector_filtered): if debug >= 3: print(cyan + "createMacroSamples() : " + endC + "vector_filtered : " + str(vector_filtered) + endC) print(cyan + "createMacroSamples() : " + endC + "vector_buffered : " + str(vector_buffered) + endC) print(cyan + "createMacroSamples() : " + endC + "buff : " + str(buff) + endC) bufferVector(vector_filtered, vector_buffered, buff, "", 1.0, 10, format_vector) else : print(cyan + "createMacroSamples() : " + bold + yellow + "Pas de fichier du nom : " + endC + vector_filtered) else : print(cyan + "createMacroSamples() : " + bold + yellow + "Pas de tampon sur le fichier du nom : " + endC + vector_filtered) copyVectorFile(vector_filtered, vector_buffered) vectors_buffered_list.append(vector_buffered) else : print(cyan + "createMacroSamples() : " + bold + yellow + "Pas de tampon demandé" + endC) for idx_vector in range (len(bd_vector_input_list)): vector_filtered = vectors_filtered_list[idx_vector] vectors_buffered_list.append(vector_filtered) print(cyan + "createMacroSamples() : " + bold + green + "... fin de la mise en place des tampons" + endC) # ETAPE 5 : FUSION DES SHAPES print(cyan + "createMacroSamples() : " + bold + green + "Fusion par macroclasse ..." + endC) # si une liste de fichier shape à fusionner existe if not vectors_buffered_list: print(cyan + "createMacroSamples() : " + bold + yellow + "Pas de fusion sans donnee à fusionner" + endC) # s'il n'y a qu'un fichier shape en entrée elif len(vectors_buffered_list) == 1: print(cyan + "createMacroSamples() : " + bold + yellow + "Pas de fusion pour une seule donnee à fusionner" + endC) copyVectorFile(vectors_buffered_list[0], vector_sample_output) else : # Fusion des fichiers shape vectors_buffered_controled_list = [] for vector_buffered in vectors_buffered_list : if os.path.isfile(vector_buffered) and (getGeometryType(vector_buffered, format_vector) in ('POLYGON', 'MULTIPOLYGON')) and (getNumberFeature(vector_buffered, format_vector) > 0): vectors_buffered_controled_list.append(vector_buffered) else : print(cyan + "createMacroSamples() : " + bold + red + "Attention fichier bufferisé est vide il ne sera pas fusionné : " + endC + vector_buffered, file=sys.stderr) fusionVectors(vectors_buffered_controled_list, vector_sample_output, format_vector) print(cyan + "createMacroSamples() : " + bold + green + "... fin de la fusion" + endC) # ETAPE 6 : CREATION DU FICHIER RASTER RESULTAT SI DEMANDE # Creation d'un masque binaire if raster_sample_output != "" and image_input != "" : repertory_output = os.path.dirname(raster_sample_output) if not os.path.isdir(repertory_output): os.makedirs(repertory_output) rasterizeBinaryVector(vector_sample_output, image_input, raster_sample_output, 1, CODAGE) # ETAPE 7 : SUPPRESIONS FICHIERS INTERMEDIAIRES INUTILES # Suppression des données intermédiaires if not save_results_intermediate: # Supression du fichier de decoupe si celui ci a été créer if vector_simple_mask != vector_to_cut_input : if os.path.isfile(vector_simple_mask) : removeVectorFile(vector_simple_mask) # Suppression des repertoires temporaires deleteDir(repertory_mask_temp) deleteDir(repertory_samples_cutting_temp) deleteDir(repertory_samples_filtering_temp) deleteDir(repertory_samples_buff_temp) # Mise à jour du Log ending_event = "createMacroSamples() : create macro samples ending : " timeLine(path_time_log,ending_event) return