def estimateQualityMns(image_input, vector_cut_input, vector_sample_input_list, vector_sample_points_input, raster_input_dico, vector_output, no_data_value, path_time_log, format_raster='GTiff', epsg=2154, format_vector='ESRI Shapefile', extension_raster=".tif", extension_vector=".shp", save_results_intermediate=False, overwrite=True): # Mise à jour du Log starting_event = "estimateQualityMns() : Masks creation starting : " timeLine(path_time_log, starting_event) print(endC) print(bold + green + "## START : CREATE HEIGHT POINTS FILE FROM MNS" + endC) print(endC) if debug >= 2: print(bold + green + "estimateQualityMns() : Variables dans la fonction" + endC) print(cyan + "estimateQualityMns() : " + endC + "image_input : " + str(image_input) + endC) print(cyan + "estimateQualityMns() : " + endC + "vector_cut_input : " + str(vector_cut_input) + endC) print(cyan + "estimateQualityMns() : " + endC + "vector_sample_input_list : " + str(vector_sample_input_list) + endC) print(cyan + "estimateQualityMns() : " + endC + "vector_sample_points_input : " + str(vector_sample_points_input) + endC) print(cyan + "estimateQualityMns() : " + endC + "raster_input_dico : " + str(raster_input_dico) + endC) print(cyan + "estimateQualityMns() : " + endC + "vector_output : " + str(vector_output) + endC) print(cyan + "estimateQualityMns() : " + endC + "no_data_value : " + str(no_data_value)) print(cyan + "estimateQualityMns() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "estimateQualityMns() : " + endC + "epsg : " + str(epsg) + endC) print(cyan + "estimateQualityMns() : " + endC + "format_raster : " + str(format_raster) + endC) print(cyan + "estimateQualityMns() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "estimateQualityMns() : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "estimateQualityMns() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "estimateQualityMns() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "estimateQualityMns() : " + endC + "overwrite : " + str(overwrite) + endC) # Définion des constantes EXT_DBF = '.dbf' EXT_CSV = '.csv' CODAGE = "uint16" SUFFIX_STUDY = '_study' SUFFIX_CUT = '_cut' SUFFIX_TEMP = '_temp' SUFFIX_CLEAN = '_clean' SUFFIX_SAMPLE = '_sample' ATTRIBUTE_ID = "ID" ATTRIBUTE_Z_INI = "Z_INI" ATTRIBUTE_Z_FIN = "Z_FIN" ATTRIBUTE_PREC_ALTI = "PREC_ALTI" ATTRIBUTE_Z_REF = "Z_Ref" ATTRIBUTE_Z_MNS = "Z_Mns" ATTRIBUTE_Z_DELTA = "Z_Delta" ERODE_EDGE_POINTS = -1.0 ERROR_VALUE = -99.0 ERROR_MIN_VALUE = -9999 ERROR_MAX_VALUE = 9999 # ETAPE 0 : PREPARATION DES FICHIERS INTERMEDIAIRES # Si le fichier de sortie existe on ecrase check = os.path.isfile(vector_output) if check and not overwrite: # Si un fichier de sortie avec le même nom existe déjà, et si l'option ecrasement est à false, alors FIN print(cyan + "estimateQualityMns() : " + bold + yellow + "Create file %s already exist : no actualisation" % (vector_output) + endC) return if os.path.isfile(os.path.splitext(vector_output)[0] + EXT_CSV): removeFile(os.path.splitext(vector_output)[0] + EXT_CSV) repertory_output = os.path.dirname(vector_output) base_name = os.path.splitext(os.path.basename(vector_output))[0] vector_output_temp = repertory_output + os.sep + base_name + SUFFIX_TEMP + extension_vector raster_study = repertory_output + os.sep + base_name + SUFFIX_STUDY + extension_raster vector_study = repertory_output + os.sep + base_name + SUFFIX_STUDY + extension_vector vector_study_clean = repertory_output + os.sep + base_name + SUFFIX_STUDY + SUFFIX_CLEAN + extension_vector image_cut = repertory_output + os.sep + base_name + SUFFIX_CUT + extension_raster vector_sample_temp = repertory_output + os.sep + base_name + SUFFIX_SAMPLE + SUFFIX_TEMP + extension_vector vector_sample_temp_clean = repertory_output + os.sep + base_name + SUFFIX_SAMPLE + SUFFIX_TEMP + SUFFIX_CLEAN + extension_vector # Utilisation des données raster externes raster_cut_dico = {} for raster_input in raster_input_dico: base_name_raster = os.path.splitext(os.path.basename(raster_input))[0] raster_cut = repertory_output + os.sep + base_name_raster + SUFFIX_CUT + extension_raster raster_cut_dico[raster_input] = raster_cut if os.path.exists(raster_cut): removeFile(raster_cut) # ETAPE 1 : DEFINIR UN SHAPE ZONE D'ETUDE if (not vector_cut_input is None) and (vector_cut_input != "") and ( os.path.isfile(vector_cut_input)): cutting_action = True vector_study = vector_cut_input else: cutting_action = False createVectorMask(image_input, vector_study) # ETAPE 2 : DECOUPAGE DU RASTEUR PAR LE VECTEUR D'ETUDE SI BESOIN ET REECHANTILLONAGE SI BESOIN if cutting_action: # Identification de la tailles de pixels en x et en y du fichier MNS de reference pixel_size_x, pixel_size_y = getPixelWidthXYImage(image_input) # Si le fichier de sortie existe deja le supprimer if os.path.exists(image_cut): removeFile(image_cut) # Commande de découpe if not cutImageByVector(vector_study, image_input, image_cut, pixel_size_x, pixel_size_y, no_data_value, 0, format_raster, format_vector): print( cyan + "estimateQualityMns() : " + bold + red + "Une erreur c'est produite au cours du decoupage de l'image : " + image_input + endC, file=sys.stderr) raise if debug >= 2: print(cyan + "estimateQualityMns() : " + bold + green + "DECOUPAGE DU RASTER %s AVEC LE VECTEUR %s" % (image_input, vector_study) + endC) else: image_cut = image_input # Definir l'emprise du fichier MNS de reference # Decoupage de chaque raster de la liste des rasters for raster_input in raster_input_dico: raster_cut = raster_cut_dico[raster_input] if not cutImageByVector(vector_study, raster_input, raster_cut, pixel_size_x, pixel_size_y, no_data_value, 0, format_raster, format_vector): raise NameError( cyan + "estimateQualityMns() : " + bold + red + "Une erreur c'est produite au cours du decoupage du raster : " + raster_input + endC) # Gémotrie de l'image pixel_size_x, pixel_size_y = getPixelWidthXYImage(image_cut) cols, rows, bands = getGeometryImage(image_cut) xmin, xmax, ymin, ymax = getEmpriseImage(image_cut) if debug >= 3: print("Geometrie Image : ") print(" cols = " + str(cols)) print(" rows = " + str(rows)) print(" xmin = " + str(xmin)) print(" xmax = " + str(xmax)) print(" ymin = " + str(ymin)) print(" ymax = " + str(ymax)) print(" pixel_size_x = " + str(pixel_size_x)) print(" pixel_size_y = " + str(pixel_size_y)) print("\n") # Création du dico coordonnées des points en systeme cartographique points_random_value_dico = {} # liste coordonnées des points au format matrice image brute points_coordonnees_image_list = [] # Selon que l'on utilise le fichier de points d'echantillons ou que l'on recréé a partir des sommets des vecteurs lignes if (vector_sample_points_input is None) or (vector_sample_points_input == ""): # ETAPE 3 : DECOUPAGES DES VECTEURS DE REFERENCE D'ENTREE PAR LE VECTEUR D'ETUDE ET LEUR FUSION ET # LECTURE D'UN VECTEUR DE LIGNES ET SAUVEGARDE DES COORDONNEES POINTS DES EXTREMITEES ET LEUR HAUTEUR # Découpage des vecteurs de bd réference avec le vecteur zone d'étude vector_sample_input_cut_list = [] for vector_sample in vector_sample_input_list: vector_name = os.path.splitext(os.path.basename(vector_sample))[0] vector_sample_cut = repertory_output + os.sep + vector_name + SUFFIX_CUT + extension_vector vector_sample_input_cut_list.append(vector_sample_cut) cutoutVectors(vector_study, vector_sample_input_list, vector_sample_input_cut_list, format_vector) # Fusion des vecteurs de bd réference découpés fusionVectors(vector_sample_input_cut_list, vector_sample_temp, format_vector) # Preparation des colonnes names_column_start_point_list = [ ATTRIBUTE_ID, ATTRIBUTE_Z_INI, ATTRIBUTE_PREC_ALTI ] names_column_end_point_list = [ ATTRIBUTE_ID, ATTRIBUTE_Z_FIN, ATTRIBUTE_PREC_ALTI ] fields_list = [ ATTRIBUTE_ID, ATTRIBUTE_PREC_ALTI, ATTRIBUTE_Z_INI, ATTRIBUTE_Z_FIN ] multigeometries2geometries(vector_sample_temp, vector_sample_temp_clean, fields_list, "MULTILINESTRING", format_vector) points_coordinates_dico = readVectorFileLinesExtractTeminalsPoints( vector_sample_temp_clean, names_column_start_point_list, names_column_end_point_list, format_vector) else: # ETAPE 3_BIS : DECOUPAGE DE VECTEURS D'ECHANTILLONS POINTS PAR LE VECTEUR D'EMPRISE ET # LECTURE DES COORDONNES D'ECHANTILLONS DURECTEMENT DANS LE FICHIER VECTEUR POINTS # Liste coordonnées des points au format matrice image brute cutVectorAll(vector_study, vector_sample_points_input, vector_sample_temp, format_vector) points_coordinates_dico = readVectorFilePoints(vector_sample_temp, format_vector) # ETAPE 4 : PREPARATION DU VECTEUR DE POINTS for index_key in points_coordinates_dico: # Recuperer les valeurs des coordonnees coord_info_list = points_coordinates_dico[index_key] coor_x = coord_info_list[0] coor_y = coord_info_list[1] attribut_dico = coord_info_list[2] # Coordonnées des points au format matrice image pos_x = int(round((coor_x - xmin) / abs(pixel_size_x)) - 1) pos_y = int(round((ymax - coor_y) / abs(pixel_size_y)) - 1) if pos_x < 0: pos_x = 0 if pos_x >= cols: pos_x = cols - 1 if pos_y < 0: pos_y = 0 if pos_y >= rows: pos_y = rows - 1 coordonnees_list = [pos_x, pos_y] points_coordonnees_image_list.append(coordonnees_list) value_ref = 0.0 if ATTRIBUTE_Z_INI in attribut_dico.keys(): value_ref = float(attribut_dico[ATTRIBUTE_Z_INI]) if ATTRIBUTE_Z_FIN in attribut_dico.keys(): value_ref = float(attribut_dico[ATTRIBUTE_Z_FIN]) precision_alti = 0.0 if ATTRIBUTE_PREC_ALTI in attribut_dico.keys(): precision_alti = float(attribut_dico[ATTRIBUTE_PREC_ALTI]) point_attr_dico = { ATTRIBUTE_ID: index_key, ATTRIBUTE_Z_REF: value_ref, ATTRIBUTE_PREC_ALTI: precision_alti, ATTRIBUTE_Z_MNS: 0.0, ATTRIBUTE_Z_DELTA: 0.0 } for raster_input in raster_input_dico: field_name = raster_input_dico[raster_input][0][0] point_attr_dico[field_name] = 0.0 points_random_value_dico[index_key] = [[coor_x, coor_y], point_attr_dico] # ETAPE 5 : LECTURE DES DONNEES DE HAUTEURS ISSU DU MNS et autre raster # Lecture dans le fichier raster des valeurs values_height_list = getPixelsValueListImage( image_cut, points_coordonnees_image_list) values_others_dico = {} for raster_input in raster_input_dico: raster_cut = raster_cut_dico[raster_input] values_list = getPixelsValueListImage(raster_cut, points_coordonnees_image_list) values_others_dico[raster_input] = values_list for i in range(len(points_random_value_dico)): value_mns = values_height_list[i] value_ref = points_random_value_dico[i][1][ATTRIBUTE_Z_REF] points_random_value_dico[i][1][ATTRIBUTE_Z_MNS] = float(value_mns) precision_alti = points_random_value_dico[i][1][ATTRIBUTE_PREC_ALTI] points_random_value_dico[i][1][ATTRIBUTE_PREC_ALTI] = float( precision_alti) value_diff = value_ref - value_mns points_random_value_dico[i][1][ATTRIBUTE_Z_DELTA] = float(value_diff) for raster_input in raster_input_dico: field_name = raster_input_dico[raster_input][0][0] value_other = values_others_dico[raster_input][i] points_random_value_dico[i][1][field_name] = float(value_other) # ETAPE 6 : CREATION D'UN VECTEUR DE POINTS AVEC DONNEE COORDONNES POINT ET HAUTEUR REFERENCE ET MNS # Suppression des points contenant des valeurs en erreur et en dehors du filtrage points_random_value_dico_clean = {} for i in range(len(points_random_value_dico)): value_ref = points_random_value_dico[i][1][ATTRIBUTE_Z_REF] if value_ref != ERROR_VALUE and value_ref > ERROR_MIN_VALUE and value_ref < ERROR_MAX_VALUE: points_is_valid = True for raster_input in raster_input_dico: if len(raster_input_dico[raster_input]) > 1 and len( raster_input_dico[raster_input][1]) > 1: threshold_min = float( raster_input_dico[raster_input][1][0]) threshold_max = float( raster_input_dico[raster_input][1][1]) field_name = raster_input_dico[raster_input][0][0] value_raster = float( points_random_value_dico[i][1][field_name]) if value_raster < threshold_min or value_raster > threshold_max: points_is_valid = False if points_is_valid: points_random_value_dico_clean[i] = points_random_value_dico[i] # Définir les attibuts du fichier résultat attribute_dico = { ATTRIBUTE_ID: ogr.OFTInteger, ATTRIBUTE_PREC_ALTI: ogr.OFTReal, ATTRIBUTE_Z_REF: ogr.OFTReal, ATTRIBUTE_Z_MNS: ogr.OFTReal, ATTRIBUTE_Z_DELTA: ogr.OFTReal } for raster_input in raster_input_dico: field_name = raster_input_dico[raster_input][0][0] attribute_dico[field_name] = ogr.OFTReal createPointsFromCoordList(attribute_dico, points_random_value_dico_clean, vector_output_temp, epsg, format_vector) # Suppression des points en bord de zone d'étude bufferVector(vector_study, vector_study_clean, ERODE_EDGE_POINTS, "", 1.0, 10, format_vector) cutVectorAll(vector_study_clean, vector_output_temp, vector_output, True, format_vector) # ETAPE 7 : TRANSFORMATION DU FICHIER .DBF EN .CSV dbf_file = repertory_output + os.sep + base_name + EXT_DBF csv_file = repertory_output + os.sep + base_name + EXT_CSV if debug >= 2: print(cyan + "estimateQualityMns() : " + bold + green + "Conversion du fichier DBF %s en fichier CSV %s" % (dbf_file, csv_file) + endC) convertDbf2Csv(dbf_file, csv_file) # ETAPE 8 : SUPPRESIONS FICHIERS INTERMEDIAIRES INUTILES # Suppression des données intermédiaires if not save_results_intermediate: if cutting_action: if os.path.isfile(image_cut): removeFile(image_cut) else: if os.path.isfile(vector_study): removeVectorFile(vector_study) for raster_input in raster_input_dico: raster_cut = raster_cut_dico[raster_input] if os.path.isfile(raster_cut): removeFile(raster_cut) if os.path.isfile(vector_output_temp): removeVectorFile(vector_output_temp) if os.path.isfile(vector_study_clean): removeVectorFile(vector_study_clean) if os.path.isfile(vector_sample_temp): removeVectorFile(vector_sample_temp) if os.path.isfile(vector_sample_temp_clean): removeVectorFile(vector_sample_temp_clean) for vector_file in vector_sample_input_cut_list: if os.path.isfile(vector_file): removeVectorFile(vector_file) print(bold + green + "## END : CREATE HEIGHT POINTS FILE FROM MNSE" + endC) # Mise à jour du Log ending_event = "estimateQualityMns() : Masks creation ending : " timeLine(path_time_log, ending_event) return
def preparationVecteurs(urbanatlas_input, ucz_output, emprise_file, mask_file, enter_with_mask, image_file, mnh_file, built_files_list, hydrography_file, roads_files_list, rpg_file, indicators_method, ucz_method, dbms_choice, threshold_ndvi, threshold_ndvi_water, threshold_ndwi2, threshold_bi_bottom, threshold_bi_top, path_time_log, temp_directory, format_vector, extension_vector): print(bold + yellow + "Début de la préparation des données vecteurs." + endC) step = " Début de la préparation des données vecteurs : " timeLine(path_time_log, step) field_name = 'ID' field_type = ogr.OFTInteger emprise_erosion = temp_directory + os.sep + os.path.splitext( os.path.basename(emprise_file))[0] + "_eroded" + extension_vector print(bold + cyan + " Érosion de '%s' pour le découpage des autres vecteurs :" % (emprise_file) + endC) bufferVector( emprise_file, emprise_erosion, -10, "", 1.0, 10, format_vector ) # Création du shape zone d'étude érodée (utile pour la fonction CrossingVectorRaster où shape < raster) - Tampon par défaut : -10 # Traitements sur l'Urban Atlas print(bold + cyan + " Traitements du fichier Urban Atlas '%s' :" % (urbanatlas_input) + endC) basename_grid = os.path.splitext(os.path.basename(urbanatlas_input))[0] grid_reproject = temp_directory + os.sep + basename_grid + "_reproject" + extension_vector grid_ready = temp_directory + os.sep + basename_grid + "_cut" + extension_vector grid_ready_cleaned = temp_directory + os.sep + basename_grid + "_cut_cleaned" + extension_vector column = "'%s, CODE2012, ITEM2012'" % (field_name) expression = "CODE2012 NOT IN ('12210', '12220', '12230', '50000')" updateProjection(urbanatlas_input, grid_reproject, 2154, format_vector) # MAJ projection addNewFieldVector(grid_reproject, field_name, field_type, 0, None, None, format_vector) # Ajout d'un champ ID updateIndexVector( grid_reproject, index_name=field_name) # Mise à jour du champs ID (incrémentation) cutVector( emprise_erosion, grid_reproject, grid_ready, format_vector ) # Découpage du fichier Urban Atlas d'entrée à l'emprise de la zone d'étude ret = filterSelectDataVector( grid_ready, grid_ready_cleaned, column, expression, format_vector ) # Suppression des polygones eau et routes (uniquement pour le calcul des indicateurs) if not ret: raise NameError( cyan + "preparationVecteurs : " + bold + red + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" % (expression) + endC) if indicators_method in ("BD_exogenes", "SI_seuillage", "SI_classif"): # Traitements sur les fichiers bâti de la BD TOPO print(bold + cyan + " Traitements des fichiers bâti '%s' :" % str(built_files_list) + endC) built_merge = temp_directory + os.sep + "bati_merged" + extension_vector built_ready = temp_directory + os.sep + "bati" + extension_vector column = "HAUTEUR" expression = "HAUTEUR > 0" built_intersect_list = [] for built_input in built_files_list: basename = os.path.splitext(os.path.basename(built_input))[0] built_reproject = temp_directory + os.sep + basename + "_reproject" + extension_vector built_intersect = temp_directory + os.sep + basename + "_intersect" + extension_vector updateProjection(built_input, built_reproject, 2154, format_vector) # MAJ projection intersectVector( emprise_file, built_reproject, built_intersect, format_vector ) # Sélection des entités bâti dans l'emprise de l'étude built_intersect_list.append(built_intersect) fusionVectors(built_intersect_list, built_merge, format_vector) # Fusion des couches bâti de la BD TOPO ret = filterSelectDataVector( built_merge, built_ready, column, expression ) # Suppression des polygones où la hauteur du bâtiment est à 0 if not ret: raise NameError( cyan + "preparationVecteurs : " + bold + red + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" % (expression) + endC) addNewFieldVector(built_ready, field_name, field_type, 0, None, None, format_vector) # Ajout d'un champ ID updateIndexVector( built_ready, index_name=field_name) # Mise à jour du champs ID (incrémentation) if indicators_method == "BD_exogenes": # Traitements sur le fichier routes de la BD TOPO print(bold + cyan + " Traitements du fichier hydrographie '%s' :" % (hydrography_file) + endC) basename_hydrography = os.path.splitext( os.path.basename(hydrography_file))[0] hydrography_reproject = temp_directory + os.sep + basename_hydrography + "_reproject" + extension_vector hydrography_intersect = temp_directory + os.sep + basename_hydrography + "_intersect" + extension_vector hydrography_ready = temp_directory + os.sep + "eau" + extension_vector column = "REGIME" expression = "REGIME LIKE 'Permanent'" updateProjection(hydrography_file, hydrography_reproject, 2154, format_vector) # MAJ projection intersectVector( emprise_file, hydrography_reproject, hydrography_intersect, format_vector ) # Sélection des entités routes dans l'emprise de l'étude ret = filterSelectDataVector( hydrography_intersect, hydrography_ready, column, expression, format_vector ) # Sélection des entités suivant le régime hydrographique (permanent) if not ret: raise NameError( cyan + "preparationVecteurs : " + bold + red + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" % (expression) + endC) addNewFieldVector(hydrography_ready, field_name, field_type, 0, None, None, format_vector) # Ajout d'un champ ID updateIndexVector(hydrography_ready, index_name=field_name ) # Mise à jour du champs ID (incrémentation) # Traitements sur le fichier RPG print(bold + cyan + " Traitements du fichier RPG '%s' :" % (rpg_file) + endC) basename_RPG = os.path.splitext(os.path.basename(rpg_file))[0] RPG_reproject = temp_directory + os.sep + basename_RPG + "_reproject" + extension_vector RPG_ready = temp_directory + os.sep + "RPG" + extension_vector updateProjection(rpg_file, RPG_reproject, 2154, format_vector) # MAJ projection intersectVector( emprise_file, RPG_reproject, RPG_ready, format_vector ) # Sélection des entités RPG dans l'emprise de l'étude addNewFieldVector(RPG_ready, field_name, field_type, 0, None, None, format_vector) # Ajout d'un champ ID updateIndexVector(RPG_ready, index_name=field_name ) # Mise à jour du champs ID (incrémentation) ######################################################################################################################################################################################################## ######################################################################## Partie restant à coder : normalement pas nécessaire puisque cette méthode n'a pas été retenue ################################# ######################################################################################################################################################################################################## #### if indicators_method == "SI_seuillage": #### # Traitements sur les fichiers routes de la BD TOPO #### print(bold + cyan + " Traitements des fichiers routes '%s' :" % str(roads_files_list) + endC) #### #### print( bold + "Le script ne peut continuer, le traitements des fichiers routes n'est pas encore entièrement codé" + endC) #### exit(0) #### #### #~ En entrée : fichier troncon_route + fichier surface_route #### #~ 1 - reprojection des fichiers en L93 #### #~ 2 - sélection des entités des fichiers compris dans la zone d'étude (intersect et non découpage) #### #~ 3 - filtrage des entités de troncon_route suivant la nature #### #~ ("NATURE IN ('Autoroute', 'Bretelle', 'Quasi-autoroute', 'Route 1 chausse', 'Route 2 chausses', #### #~ 'Route a 1 chaussee', 'Route a 2 chaussees', 'Route à 1 chaussée', 'Route à 2 chaussées')") #### #~ 4 - tampon sur les entités de troncon_route correspondant à 'LARGEUR'/2 #### #~ 5 - fusion des fichiers en un seul shape #### #~ 6 - ajout d'un nouveau champ ID dans le fichier de fusion #### #~ 7 - mise à jour de ce champ ID #### #### ######################################################################################################################################################################################################## ######################################################################################################################################################################################################## ######################################################################################################################################################################################################## step = " Fin de la préparation des données vecteurs : " timeLine(path_time_log, step) print(bold + yellow + "Fin de la préparation des données vecteurs." + endC) print("\n") return
def detectionOuvrages(input_dico, output_dir, method, im_indice_buffers, im_indice_sobel, input_cut_vector, input_sea_points, no_data_value, path_time_log, channel_order=['Red', 'Green', 'Blue', 'NIR'], epsg=2154, format_raster='GTiff', format_vector="ESRI Shapefile", extension_raster=".tif", extension_vector=".shp", save_results_intermediate=True, overwrite=True): # Mise à jour du Log starting_event = "detectionOuvrages() : Select Detection Ouvrages starting : " timeLine(path_time_log, starting_event) # Affichage des paramètres if debug >= 3: print(bold + green + "Variables dans detectionOuvrages - Variables générales" + endC) print(cyan + "detectionOuvrages() : " + endC + "input_dico : " + str(input_dico) + endC) print(cyan + "detectionOuvrages() : " + endC + "output_dir : " + str(output_dir) + endC) print(cyan + "detectionOuvrages() : " + endC + "method : " + str(method) + endC) print(cyan + "detectionOuvrages() : " + endC + "im_indice_buffers : " + str(im_indice_buffers) + endC) print(cyan + "detectionOuvrages() : " + endC + "im_indice_sobel : " + str(im_indice_sobel) + endC) print(cyan + "detectionOuvrages() : " + endC + "input_cut_vector : " + str(input_cut_vector) + endC) print(cyan + "detectionOuvrages() : " + endC + "input_sea_points : " + str(input_sea_points) + endC) print(cyan + "detectionOuvrages() : " + endC + "no_data_value : " + str(no_data_value) + endC) print(cyan + "detectionOuvrages() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "detectionOuvrages() : " + endC + "channel_order: " + str(channel_order) + endC) print(cyan + "detectionOuvrages() : " + endC + "epsg : " + str(epsg) + endC) print(cyan + "detectionOuvrages() : " + endC + "format_raster : " + str(format_raster) + endC) print(cyan + "detectionOuvrages() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "detectionOuvrages() : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "detectionOuvrages() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "detectionOuvrages() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "detectionOuvrages() : " + endC + "overwrite : " + str(overwrite) + endC) # Initialisation des constantes PREFIX_OUVRAGES = "Ouvrages_final_" # Variables type_extension = os.path.splitext(input_dico[0].split(":")[0])[1] # Création du répertoire de sortie s'il n'existe pas déjà if not os.path.exists(output_dir): os.makedirs(output_dir) while switch(method): if case("b"): if type_extension == extension_raster: if im_indice_buffers: if debug >= 2: print( cyan + "detectionOuvrages() : " + endC + green + bold + "Cas2 : méthode buffers avec imageIndice en entrée et avec calcul TDC, en entrée : -meth b -indb -dico ImageBrute:TailleBuffer+,TailleBuffer-,ImageIndice,SeuilTerreMer -mer -d -outd" + endC) for elt_dico in input_dico: elt_list = elt_dico.split(":") tdc = runTDCSeuil( elt_list[0] + ":" + elt_list[1].split(",")[2] + "," + elt_list[1].split(",")[3], output_dir, input_sea_points, input_cut_vector, "", "", "", "", "", "", "", "", 1.0, False, no_data_value, path_time_log, channel_order, epsg, format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) buffersOuvrages(tdc, output_dir, elt_list[1].split(",")[0], elt_list[1].split(",")[1], input_cut_vector, path_time_log, format_vector, extension_vector, save_results_intermediate, overwrite) else: if debug >= 2: print( cyan + "detectionOuvrages() : " + endC + green + bold + "Cas3 : méthode buffers avec image brute en entrée et avec calcul TDC, en entrée : -meth b -dico ImageBrute:TailleBuffer+,TailleBuffer-,SeuilTerreMer -mer -d -outd" + endC) for elt_dico in input_dico: elt_list = elt_dico.split(":") tdc = runTDCSeuil( elt_list[0] + ":" + elt_list[1].split(",")[2], output_dir, input_sea_points, input_cut_vector, "", "", "", "", "", "", "", "", 1.0, True, no_data_value, path_time_log, channel_order, epsg, format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) buffersOuvrages(tdc, output_dir, elt_list[1].split(",")[0], elt_list[1].split(",")[1], input_cut_vector, path_time_log, format_vector, extension_vector, save_results_intermediate, overwrite) elif type_extension == extension_vector: if debug >= 2: print( cyan + "detectionOuvrages() : " + endC + green + bold + "Cas1 : méthode buffers sans calculTDC, en entrée : -meth b -dico TDC:TailleBuffer+,TailleBuffer- -outd" + endC) for elt_dico in input_dico: elt_list = elt_dico.split(":") buffersOuvrages(elt_list[0], output_dir, elt_list[1].split(",")[0], elt_list[1].split(",")[1], input_cut_vector, path_time_log, format_vector, extension_vector, save_results_intermediate, overwrite) else: print( cyan + "detectionOuvrages() : " + bold + red + "Format de fichier non valide pour la détection des ouvrages" + endC, file=sys.stderr) sys.exit(1) break if case("s"): if im_indice_sobel: if debug >= 2: print( cyan + "detectionOuvrages() : " + endC + green + bold + "Cas4 : méthode Sobel avec image NDWI2 déjà calculée, en entrée : -meth s -inds -dico ImageBrute:ImageNDWI2,SeuilSobel -d -outd" + endC) for elt_dico in input_dico: elt_list = elt_dico.split(":") edgeExtractionOuvrages( elt_list[0] + ":" + elt_list[1].split(",")[0] + "," + elt_list[1].split(",")[1], output_dir, input_cut_vector, False, no_data_value, path_time_log, format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) else: if debug >= 2: print( cyan + "detectionOuvrages() : " + endC + green + bold + "Cas5 : méthode Sobel avec calcul NDWI2, en entrée : -meth s -dico ImageBrute:SeuilSobel -d -outd" + endC) for elt_dico in input_dico: elt_list = elt_dico.split(":") edgeExtractionOuvrages( elt_list[0] + ":" + elt_list[1], output_dir, input_cut_vector, True, no_data_value, path_time_log, format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) break if case("bs"): if im_indice_buffers and im_indice_sobel: if debug >= 2: print( cyan + "detectionOuvrages() : " + endC + green + bold + "Cas6 : méthode buffers+Sobel avec calcul TDC mais imageIndice et imageNDWI2 déjà calculées, en entrée : -meth bs -indb -inds -dico ImageBrute:TailleBuffer+,TailleBuffer-,ImageIndice,SeuilTerreMer,ImageNDWI2,SeuilSobel -d -outd -mer" + endC) for elt_dico in input_dico: elt_list = elt_dico.split(":") tdc = runTDCSeuil( elt_list[0] + ":" + elt_list[1].split(",")[2] + "," + elt_list[1].split(",")[3], output_dir, input_sea_points, input_cut_vector, "", "", "", "", "", "", "", "", 1.0, False, no_data_value, path_time_log, channel_order, epsg, format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) buffers_ouvrages_shp = buffersOuvrages( tdc, output_dir, elt_list[1].split(",")[0], elt_list[1].split(",")[1], input_cut_vector, path_time_log, format_vector, extension_vector, save_results_intermediate, overwrite) sobel_ouvrages_shp = edgeExtractionOuvrages( elt_list[0] + ":" + elt_list[1].split(",")[4] + "," + elt_list[1].split(",")[5], output_dir, input_cut_vector, False, no_data_value, path_time_log, format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) fusionVectors( [buffers_ouvrages_shp, sobel_ouvrages_shp[0]], output_dir + os.sep + PREFIX_OUVRAGES + os.path.splitext(os.path.basename(elt_list[0]))[0] + extension_vector, format_vector) elif im_indice_buffers: if debug >= 2: print( cyan + "detectionOuvrages() : " + endC + green + bold + "Cas7 : méthode buffers+Sobel avec calcul TDC, imageIndice déjà calculée mais imageNDWI2 à calculer, en entrée : -meth bs -indb -dico ImageBrute:TailleBuffer+,TailleBuffer-,ImageIndice,SeuilTerreMer,SeuilSobel -d -outd -mer" + endC) for elt_dico in input_dico: elt_list = elt_dico.split(":") tdc = runTDCSeuil( elt_list[0] + ":" + elt_list[1].split(",")[2] + "," + elt_list[1].split(",")[3], output_dir, input_sea_points, input_cut_vector, "", "", "", "", "", "", "", "", 1.0, False, no_data_value, path_time_log, channel_order, epsg, format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) buffers_ouvrages_shp = buffersOuvrages( tdc, output_dir, elt_list[1].split(",")[0], elt_list[1].split(",")[1], input_cut_vector, path_time_log, format_vector, extension_vector, save_results_intermediate, overwrite) sobel_ouvrages_shp = edgeExtractionOuvrages( elt_list[0] + ":" + elt_list[1].split(",")[4], output_dir, input_cut_vector, True, no_data_value, path_time_log, format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) fusionVectors( [buffers_ouvrages_shp, sobel_ouvrages_shp[0]], output_dir + os.sep + PREFIX_OUVRAGES + os.path.splitext(os.path.basename(elt_list[0]))[0] + extension_vector, format_vector) elif im_indice_sobel: if debug >= 2: print( cyan + "detectionOuvrages() : " + endC + green + bold + "Cas8 : méthode buffers+Sobel avec TDC et imageNDWI2 déjà calculés, en entrée : -meth bs -inds -dico TDC:TailleBuffer+,TailleBuffer-,ImageBrute,ImageNDWI2,SeuilSobel -d -outd" + endC) for elt_dico in input_dico: elt_list = elt_dico.split(":") buffers_ouvrages_shp = buffersOuvrages( elt_list[1].split(",")[2], output_dir, elt_list[1].split(",")[0], elt_list[1].split(",")[1], input_cut_vector, path_time_log, format_vector, extension_vector, save_results_intermediate, overwrite) sobel_ouvrages_shp = edgeExtractionOuvrages( elt_list[0] + ":" + elt_list[1].split(",")[3] + "," + elt_list[1].split(",")[4], output_dir, input_cut_vector, False, no_data_value, path_time_log, format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) fusionVectors( [buffers_ouvrages_shp, sobel_ouvrages_shp[0]], output_dir + os.sep + PREFIX_OUVRAGES + os.path.splitext(os.path.basename(elt_list[0]))[0] + extension_vector, format_vector) else: if type_extension == extension_vector: if debug >= 2: print( cyan + "detectionOuvrages() : " + endC + green + bold + "Cas9 : méthode buffers+Sobel avec TDC déjà calculé mais imageNDWI2 à calculer, en entrée : -meth bs -dico TDC:TailleBuffer+,TailleBuffer-,ImageBrute,SeuilSobel -d -outd" + endC) for elt_dico in input_dico: elt_list = elt_dico.split(":") buffers_ouvrages_shp = buffersOuvrages( elt_list[0], output_dir, elt_list[1].split(",")[0], elt_list[1].split(",")[1], input_cut_vector, path_time_log, format_vector, extension_vector, save_results_intermediate, overwrite) sobel_ouvrages_shp = edgeExtractionOuvrages( elt_list[1].split(",")[2] + ":" + elt_list[1].split(",")[3], output_dir, input_cut_vector, True, no_data_value, path_time_log, format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) fusionVectors( [buffers_ouvrages_shp, sobel_ouvrages_shp[0]], output_dir + os.sep + PREFIX_OUVRAGES + os.path.splitext( os.path.basename(elt_list[1].split(",")[2]))[0] + extension_vector, format_vector) elif type_extension == extension_raster: if debug >= 2: print( cyan + "detectionOuvrages() : " + endC + green + bold + "Cas10 : méthode buffers+Sobel avec calculs TDC, imageNDVI et imageNDWI2 à faire, en entrée : -meth bs -dico ImageBrute:TailleBuffer+,TailleBuffer-,SeuilTerreMer,SeuilSobel -d -outd -mer" + endC) for elt_dico in input_dico: elt_list = elt_dico.split(":") tdc = runTDCSeuil( elt_list[0] + ":" + elt_list[1].split(",")[2], output_dir, input_sea_points, input_cut_vector, "", "", "", "", "", "", "", "", 1.0, True, no_data_value, path_time_log, channel_order, epsg, format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) buffers_ouvrages_shp = buffersOuvrages( tdc, output_dir, elt_list[1].split(",")[0], elt_list[1].split(",")[1], input_cut_vector, path_time_log, format_vector, extension_vector, save_results_intermediate, overwrite) sobel_ouvrages_shp = edgeExtractionOuvrages( elt_list[0] + ":" + elt_list[1].split(",")[3], output_dir, input_cut_vector, True, no_data_value, path_time_log, format_raster, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite) fusionVectors( [buffers_ouvrages_shp, sobel_ouvrages_shp[0]], output_dir + os.sep + PREFIX_OUVRAGES + os.path.splitext(os.path.basename(elt_list[0]))[0] + extension_vector, format_vector) else: print( cyan + "detectionOuvrages() : " + bold + red + "Format de fichier non valide pour la détection des ouvrages" + endC, file=sys.stderr) sys.exit(1) break else: print( cyan + "detectionOuvrages() : " + bold + red + "Méthode de détection des ouvrages non valide : b pour buffers, s pour méthode de Sobel, bs pour combiner les 2" + endC, file=sys.stderr) sys.exit(1) # Mise à jour du Log ending_event = "detectionOuvrages() : Select Detection Ouvrages ending : " timeLine(path_time_log, ending_event) return
def computeMajorityClass(input_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): SUFFIX_CLASS = '_class' FIELD_TYPE = ogr.OFTInteger FIELD_NAME_MAJORITY = 'majority' temp_class_list = [] base_name = os.path.splitext(os.path.basename(input_grid))[0] temp_grid = temp_directory + os.sep + base_name + SUFFIX_CLASS + extension_vector temp_class0 = temp_directory + os.sep + base_name + SUFFIX_CLASS + "0" + extension_vector temp_class1 = temp_directory + os.sep + base_name + SUFFIX_CLASS + "1" + extension_vector temp_class2 = temp_directory + os.sep + base_name + SUFFIX_CLASS + "2" + extension_vector temp_class3 = temp_directory + os.sep + base_name + SUFFIX_CLASS + "3" + extension_vector temp_class4 = temp_directory + os.sep + base_name + SUFFIX_CLASS + "4" + extension_vector ### Récupération de la classe majoritaire if debug >= 3: print(cyan + "computeMajorityClass() : " + endC + bold + "Récupération de la classe majoritaire." + endC + '\n') addNewFieldVector(input_grid, maj_ocs_field, FIELD_TYPE, field_value=None, field_width=None, field_precision=None, format_vector=format_vector) attr_names_list = getAttributeNameList(input_grid, format_vector=format_vector) attr_names_list_str = "'" for attr_name in attr_names_list: attr_names_list_str += attr_name + ', ' attr_names_list_str = attr_names_list_str[:-2] + "'" expression = "%s = '%s' OR %s = '%s' OR %s = '%s' OR %s = '%s'" % ( FIELD_NAME_MAJORITY, nodata_field, FIELD_NAME_MAJORITY, built_field, FIELD_NAME_MAJORITY, mineral_field, FIELD_NAME_MAJORITY, water_field) ret = filterSelectDataVector(input_grid, temp_class0, attr_names_list_str, expression, overwrite=overwrite, format_vector=format_vector) updateFieldVector(temp_class0, field_name=maj_ocs_field, value=class_label_dico_out["MAJ_OTHERS_CLASS"], format_vector=format_vector) temp_class_list.append(temp_class0) expression = "%s = '%s'" % (FIELD_NAME_MAJORITY, baresoil_field) ret = filterSelectDataVector(input_grid, temp_class1, attr_names_list_str, expression, overwrite=overwrite, format_vector=format_vector) updateFieldVector(temp_class1, field_name=maj_ocs_field, value=class_label_dico_out["MAJ_BARESOIL_CLASS"], format_vector=format_vector) temp_class_list.append(temp_class1) expression = "(%s = '%s' OR %s = '%s' OR %s = '%s') AND (%s < 1)" % ( FIELD_NAME_MAJORITY, vegetation_field, FIELD_NAME_MAJORITY, low_vegetation_field, FIELD_NAME_MAJORITY, high_vegetation_field, veg_mean_field) ret = filterSelectDataVector(input_grid, temp_class2, attr_names_list_str, expression, overwrite=overwrite, format_vector=format_vector) updateFieldVector(temp_class2, field_name=maj_ocs_field, value=class_label_dico_out["MAJ_LOW_VEG_CLASS"], format_vector=format_vector) temp_class_list.append(temp_class2) expression = "(%s = '%s' OR %s = '%s' OR %s = '%s') AND (%s >= 1 AND %s < 5)" % ( FIELD_NAME_MAJORITY, vegetation_field, FIELD_NAME_MAJORITY, low_vegetation_field, FIELD_NAME_MAJORITY, high_vegetation_field, veg_mean_field, veg_mean_field) ret = filterSelectDataVector(input_grid, temp_class3, attr_names_list_str, expression, overwrite=overwrite, format_vector=format_vector) updateFieldVector(temp_class3, field_name=maj_ocs_field, value=class_label_dico_out["MAJ_MED_VEG_CLASS"], format_vector=format_vector) temp_class_list.append(temp_class3) expression = "(%s = '%s' OR %s = '%s' OR %s = '%s') AND (%s >= 5)" % ( FIELD_NAME_MAJORITY, vegetation_field, FIELD_NAME_MAJORITY, low_vegetation_field, FIELD_NAME_MAJORITY, high_vegetation_field, veg_mean_field) ret = filterSelectDataVector(input_grid, temp_class4, attr_names_list_str, expression, overwrite=overwrite, format_vector=format_vector) updateFieldVector(temp_class4, field_name=maj_ocs_field, value=class_label_dico_out["MAJ_HIGH_VEG_CLASS"], format_vector=format_vector) temp_class_list.append(temp_class4) fusionVectors(temp_class_list, temp_grid, format_vector=format_vector) removeVectorFile(input_grid, format_vector=format_vector) copyVectorFile(temp_grid, input_grid, format_vector=format_vector) 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 vectorsPreparation(emprise_file, classif_input, grid_input, built_input_list, roads_input_list, grid_output, grid_output_cleaned, built_output, roads_output, col_code_ua, col_item_ua, epsg, path_time_log, format_vector='ESRI Shapefile', extension_vector=".shp", save_results_intermediate=False, overwrite=True): print(bold + yellow + "Début de la préparation des fichiers vecteurs." + endC + "\n") timeLine(path_time_log, "Début de la préparation des fichiers vecteurs : ") if debug >= 3 : print(bold + green + "vectorsPreparation() : Variables dans la fonction" + endC) print(cyan + "vectorsPreparation() : " + endC + "emprise_file : " + str(emprise_file) + endC) print(cyan + "vectorsPreparation() : " + endC + "classif_input : " + str(classif_input) + endC) print(cyan + "vectorsPreparation() : " + endC + "grid_input : " + str(grid_input) + endC) print(cyan + "vectorsPreparation() : " + endC + "built_input_list : " + str(built_input_list) + endC) print(cyan + "vectorsPreparation() : " + endC + "roads_input_list : " + str(roads_input_list) + endC) print(cyan + "vectorsPreparation() : " + endC + "grid_output : " + str(grid_output) + endC) print(cyan + "vectorsPreparation() : " + endC + "grid_output_cleaned : " + str(grid_output_cleaned) + endC) print(cyan + "vectorsPreparation() : " + endC + "built_output : " + str(built_output) + endC) print(cyan + "vectorsPreparation() : " + endC + "roads_output : " + str(roads_output) + endC) print(cyan + "vectorsPreparation() : " + endC + "col_code_ua : " + str(col_code_ua) + endC) print(cyan + "vectorsPreparation() : " + endC + "col_item_ua : " + str(col_item_ua) + endC) print(cyan + "vectorsPreparation() : " + endC + "epsg : " + str(epsg)) print(cyan + "vectorsPreparation() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "vectorsPreparation() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "vectorsPreparation() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "vectorsPreparation() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "vectorsPreparation() : " + endC + "overwrite : " + str(overwrite) + endC) FOLDER_TEMP = 'TEMP' SUFFIX_VECTOR_REPROJECT = '_reproject' SUFFIX_VECTOR_INTERSECT = '_intersect' SUFFIX_VECTOR_MERGE = '_merge' SUFFIX_VECTOR_SELECT = '_select' if not os.path.exists(grid_output) or not os.path.exists(built_output) or not os.path.exists(roads_output) or overwrite: ############################################ ### Préparation générale des traitements ### ############################################ path_grid_temp = os.path.dirname(grid_output) + os.sep + FOLDER_TEMP path_built_temp = os.path.dirname(built_output) + os.sep + FOLDER_TEMP path_roads_temp = os.path.dirname(roads_output) + os.sep + FOLDER_TEMP if os.path.exists(path_grid_temp): shutil.rmtree(path_grid_temp) if os.path.exists(path_built_temp): shutil.rmtree(path_built_temp) if os.path.exists(path_roads_temp): shutil.rmtree(path_roads_temp) if not os.path.exists(path_grid_temp): os.mkdir(path_grid_temp) if not os.path.exists(path_built_temp): os.mkdir(path_built_temp) if not os.path.exists(path_roads_temp): os.mkdir(path_roads_temp) basename_grid = os.path.splitext(os.path.basename(grid_output))[0] basename_built = os.path.splitext(os.path.basename(built_output))[0] basename_roads = os.path.splitext(os.path.basename(roads_output))[0] # Variables pour ajout colonne ID field_name = 'ID' # Attention ! Nom fixé en dur dans les scripts indicateurs, pas dans le script final field_type = ogr.OFTInteger ############################################## ### Traitements sur le vecteur Urban Atlas ### ############################################## if not os.path.exists(grid_output) or overwrite : if os.path.exists(grid_output): removeVectorFile(grid_output) if os.path.exists(grid_output_cleaned): removeVectorFile(grid_output_cleaned) # MAJ projection grid_reproject = path_grid_temp + os.sep + basename_grid + SUFFIX_VECTOR_REPROJECT + extension_vector updateProjection(grid_input, grid_reproject, projection=epsg) # Découpage du fichier Urban Atlas d'entrée à l'emprise de la zone d'étude grid_output_temp = os.path.splitext(grid_output)[0] + "_temp" + extension_vector cutVector(emprise_file, grid_reproject, grid_output_temp, overwrite, format_vector) # Suppression des très petits polygones qui introduisent des valeurs NaN pixel_size = getPixelSizeImage(classif_input) min_size_area = pixel_size * 2 cleanMiniAreaPolygons(grid_output_temp, grid_output, min_size_area, '', format_vector) if not save_results_intermediate: if os.path.exists(grid_output_temp): removeVectorFile(grid_output_temp, format_vector) # Ajout d'un champ ID addNewFieldVector(grid_output, field_name, field_type, 0, None, None, format_vector) updateIndexVector(grid_output, field_name, format_vector) # Suppression des polygones eau et routes (uniquement pour le calcul des indicateurs) column = "'%s, %s, %s'" % (field_name, col_code_ua, col_item_ua) expression = "%s NOT IN ('12210', '12220', '12230', '50000')" % (col_code_ua) ret = filterSelectDataVector(grid_output, grid_output_cleaned, column, expression, format_vector) if not ret : raise NameError (cyan + "vectorsPreparation : " + bold + red + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" %(expression) + endC) ######################################### ### Traitements sur les vecteurs bâti ### ######################################### if not os.path.exists(built_output) or overwrite : if os.path.exists(built_output): removeVectorFile(built_output) # MAJ projection built_reproject_list=[] for built_input in built_input_list: built_reproject = path_built_temp + os.sep + os.path.splitext(os.path.basename(built_input))[0] + SUFFIX_VECTOR_REPROJECT + extension_vector updateProjection(built_input, built_reproject, projection=epsg) built_reproject_list.append(built_reproject) # Sélection des entités bâti dans l'emprise de l'étude built_intersect_list = [] for built_reproject in built_reproject_list: built_intersect = path_built_temp + os.sep + os.path.splitext(os.path.basename(built_reproject))[0] + SUFFIX_VECTOR_INTERSECT + extension_vector intersectVector(emprise_file, built_reproject, built_intersect, format_vector) built_intersect_list.append(built_intersect) # Fusion des couches bâti de la BD TOPO built_merge = path_built_temp + os.sep + basename_built + SUFFIX_VECTOR_MERGE + extension_vector built_select = path_built_temp + os.sep + basename_built + SUFFIX_VECTOR_SELECT + extension_vector fusionVectors(built_intersect_list, built_merge) # Suppression des polygones où la hauteur du bâtiment est à 0 column = "HAUTEUR" expression = "HAUTEUR > 0" ret = filterSelectDataVector(built_merge, built_select, column, expression, format_vector) if not ret : raise NameError (cyan + "vectorsPreparation : " + bold + red + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" %(expression) + endC) # Découpage des bati d'entrée à l'emprise de la zone d'étude cutVector(emprise_file, built_select, built_output, overwrite, format_vector) # Ajout d'un champ ID addNewFieldVector(built_output, field_name, field_type, 0, None, None, format_vector) updateIndexVector(built_output, field_name, format_vector) ########################################### ### Traitements sur les vecteurs routes ### ########################################### if not os.path.exists(roads_output) or overwrite : if os.path.exists(roads_output): removeVectorFile(roads_output) # MAJ projection roads_reproject_list=[] for roads_input in roads_input_list: roads_reproject = path_roads_temp + os.sep + os.path.splitext(os.path.basename(roads_input))[0] + SUFFIX_VECTOR_REPROJECT + extension_vector updateProjection(roads_input, roads_reproject, projection=epsg) roads_reproject_list.append(roads_reproject) # Sélection des entités routes dans l'emprise de l'étude roads_intersect_list = [] for roads_reproject in roads_reproject_list: roads_intersect = path_roads_temp + os.sep + os.path.splitext(os.path.basename(roads_reproject))[0] + SUFFIX_VECTOR_INTERSECT + extension_vector intersectVector(emprise_file, roads_reproject, roads_intersect, format_vector) roads_intersect_list.append(roads_intersect) # Fusion des couches route de la BD TOPO roads_merge = path_roads_temp + os.sep + basename_roads + SUFFIX_VECTOR_MERGE + extension_vector roads_select = path_roads_temp + os.sep + basename_roads + SUFFIX_VECTOR_SELECT + extension_vector fusionVectors(roads_intersect_list, roads_merge) # Sélection des entités suivant la nature de la route dans la couche routes de la BD TOPO column = "NATURE" expression = "NATURE IN ('Autoroute', 'Bretelle', 'Quasi-autoroute', 'Route 1 chausse', 'Route 2 chausses', 'Route a 1 chaussee', 'Route a 2 chaussees', 'Route à 1 chaussée', 'Route à 2 chaussées')" ret = filterSelectDataVector (roads_merge, roads_select, column, expression, format_vector) if not ret : raise NameError (cyan + "vectorsPreparation : " + bold + red + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" %(expression) + endC) # Découpage des routes d'entrée à l'emprise de la zone d'étude cutVectorAll(emprise_file, roads_select, roads_output, overwrite, format_vector) # Ajout d'un champ ID addNewFieldVector(roads_output, field_name, field_type, 0, None, None, format_vector) updateIndexVector(roads_output, field_name, format_vector) ########################################## ### Nettoyage des fichiers temporaires ### ########################################## if not save_results_intermediate: if os.path.exists(path_grid_temp): shutil.rmtree(path_grid_temp) if os.path.exists(path_built_temp): shutil.rmtree(path_built_temp) if os.path.exists(path_roads_temp): shutil.rmtree(path_roads_temp) else: print(bold + magenta + "La préparation des fichiers vecteurs a déjà eu lieu.\n" + endC) print(bold + yellow + "Fin de la préparation des fichiers vecteurs.\n" + endC) timeLine(path_time_log, "Fin de la préparation des fichiers vecteurs : ") return
def multiBuffersVector(input_file, output_dir, buffer_size, nb_buffers, path_time_log, epsg=2154, format_vector="ESRI Shapefile", project_encoding="UTF-8", overwrite=True): # Mise à jour du Log starting_event = "multiBuffersVector() : Select multi buffers vector starting : " timeLine(path_time_log,starting_event) # Configuration du format vecteur driver = ogr.GetDriverByName(format_vector) # Création de la référence spatiale srs = osr.SpatialReference() srs.ImportFromEPSG(epsg) result_list = [] extension_vector = os.path.splitext(os.path.split(input_file)[1])[1] nom_input_file = os.path.splitext(os.path.split(input_file)[1])[0] output_repertory = output_dir + os.sep + "multi_ring_buffers_" + nom_input_file + extension_vector for i in range(1, nb_buffers+1): # Création du buffer output_temp_buffer = output_dir + os.sep + os.path.splitext(os.path.split(input_file)[1])[0] + "_buffer_" + str(i*buffer_size) + extension_vector bufferVector(input_file, output_temp_buffer, i*buffer_size, "", 1.0, 10, format_vector) # Ajout d'un champ taille de buffer data_source_buffer = driver.Open(output_temp_buffer, 1) buffer_layer = data_source_buffer.GetLayer(0) buff_size_field = ogr.FieldDefn("size_buff", ogr.OFTReal) buffer_layer.CreateField(buff_size_field) for feature in buffer_layer: feature.SetField("size_buff",i*buffer_size) buffer_layer.SetFeature(feature) # Ajout du premier buffer à la liste finale if i == 1: result_list.append(output_temp_buffer) # Transformation des buffers en anneaux if i>1: i_preced = i-1 output_temp_buffer_preced = output_dir + os.sep + nom_input_file + "_buffer_" + str(i_preced*buffer_size) + extension_vector output_temp_ring = output_dir + os.sep + nom_input_file + "_ring_" + str(i*buffer_size) + extension_vector differenceVector(output_temp_buffer_preced, output_temp_buffer, output_temp_ring, format_vector) # Remplissage du champ taille du buffer data_source_ring = driver.Open(output_temp_ring, 1) ring_layer = data_source_ring.GetLayer(0) for ring in ring_layer: ring.SetField("size_buff",i*buffer_size) ring_layer.SetFeature(ring) # Ajout de l'anneau à la liste finale result_list.append(output_temp_ring) fusionVectors(result_list, output_repertory, format_vector) # Mise à jour du Log ending_event = "multiBuffersVector() : multi buffers vector ending : " timeLine(path_time_log,ending_event) return output_repertory
def computeQualityIndiceRateQuantity(raster_input, vector_sample_input, repertory_output, base_name, geom, size_grid, pixel_size_x, pixel_size_y, field_value_verif, field_value_other, no_data_value, epsg, format_raster, format_vector, extension_raster, extension_vector, overwrite=True, save_results_intermediate=False): # Définition des constantes EXT_TXT = '.txt' SUFFIX_STUDY = '_study' SUFFIX_CUT = '_cut' SUFFIX_BUILD = '_build' SUFFIX_OTHER = '_other' SUFFIX_LOCAL = '_local' SUFFIX_MATRIX = '_matrix' FIELD_NAME_CLASSIF = "classif" FIELD_TYPE = ogr.OFTInteger # Les variables locales vector_local_study = repertory_output + os.sep + base_name + SUFFIX_LOCAL + SUFFIX_STUDY + extension_vector vector_local_cut_study = repertory_output + os.sep + base_name + SUFFIX_LOCAL + SUFFIX_CUT + SUFFIX_STUDY + extension_vector vector_local_cut_build = repertory_output + os.sep + base_name + SUFFIX_LOCAL + SUFFIX_CUT + SUFFIX_BUILD + extension_vector vector_local_cut_other = repertory_output + os.sep + base_name + SUFFIX_LOCAL + SUFFIX_CUT + SUFFIX_OTHER + extension_vector vector_local_cut = repertory_output + os.sep + base_name + SUFFIX_LOCAL + SUFFIX_CUT + extension_vector raster_local_cut = repertory_output + os.sep + base_name + SUFFIX_LOCAL + SUFFIX_CUT + extension_raster matrix_local_file = repertory_output + os.sep + base_name + SUFFIX_LOCAL + SUFFIX_CUT + SUFFIX_MATRIX + EXT_TXT class_ref_list = None class_pro_list = None rate_quantity_list = None matrix_origine = None kappa = 0.0 overall_accuracy = 0.0 # Netoyage les fichiers de travail local if os.path.isfile(vector_local_study): removeVectorFile(vector_local_study) if os.path.isfile(vector_local_cut_study): removeVectorFile(vector_local_cut_study) if os.path.isfile(vector_local_cut): removeVectorFile(vector_local_cut) if os.path.isfile(vector_local_cut_build): removeVectorFile(vector_local_cut_build) if os.path.isfile(vector_local_cut_other): removeVectorFile(vector_local_cut_other) if os.path.isfile(raster_local_cut): removeFile(raster_local_cut) if os.path.isfile(matrix_local_file): removeFile(matrix_local_file) # Creation d'un shape file de travail local polygon_attr_geom_dico = {"1": [geom, {}]} createPolygonsFromGeometryList({}, polygon_attr_geom_dico, vector_local_study, epsg, format_vector) # Découpe sur zone local d'étude du fichier vecteur de référence cutVector(vector_local_study, vector_sample_input, vector_local_cut_build, format_vector) differenceVector(vector_local_cut_build, vector_local_study, vector_local_cut_other, format_vector) addNewFieldVector(vector_local_cut_build, FIELD_NAME_CLASSIF, FIELD_TYPE, field_value_verif, None, None, format_vector) addNewFieldVector(vector_local_cut_other, FIELD_NAME_CLASSIF, FIELD_TYPE, field_value_other, None, None, format_vector) input_shape_list = [vector_local_cut_build, vector_local_cut_other] fusionVectors(input_shape_list, vector_local_cut) # Découpe sur zone local d'étude du fichier rasteur de classification if not cutImageByVector(vector_local_study, raster_input, raster_local_cut, pixel_size_x, pixel_size_y, no_data_value, 0, format_raster, format_vector): return class_ref_list, class_pro_list, rate_quantity_list, kappa, overall_accuracy, matrix_origine # Calcul de la matrice de confusion computeConfusionMatrix(raster_local_cut, vector_local_cut, "", FIELD_NAME_CLASSIF, matrix_local_file, overwrite) # lecture de la matrice de confusion matrix, class_ref_list, class_pro_list = readConfusionMatrix( matrix_local_file) matrix_origine = copy.deepcopy(matrix) if matrix == []: print( cyan + "computeQualityIndiceRateQuantity() : " + bold + yellow + "!!! Une erreur c'est produite au cours de la lecture de la matrice de confusion : " + matrix_local_file + ". Voir message d'erreur." + endC) matrix_origine = None return class_ref_list, class_pro_list, rate_quantity_list, kappa, overall_accuracy, matrix_origine # Correction de la matrice de confusion # Dans le cas ou le nombre de microclasses des échantillons de controles # et le nombre de microclasses de la classification sont différents class_missing_list = [] if class_ref_list != class_pro_list: matrix, class_missing_list = correctMatrix(class_ref_list, class_pro_list, matrix, no_data_value) class_count = len(matrix[0]) - len(class_missing_list) # Calcul des indicateurs de qualité : rate_quantity_list precision_list, recall_list, fscore_list, performance_list, rate_false_positive_list, rate_false_negative_list, rate_quantity_list, class_list, overall_accuracy, overall_fscore, overall_performance, kappa = computeIndicators( class_count, matrix, class_ref_list, class_missing_list) # Chercher si une ligne no data existe si c'est le cas correction de la matrice if str(no_data_value) in class_pro_list: pos_col_nodata = class_pro_list.index(str(no_data_value)) for line in matrix_origine: del line[pos_col_nodata] class_pro_list.remove(str(no_data_value)) # Suppression des données temporaires locales if not save_results_intermediate: if os.path.isfile(vector_local_study): removeVectorFile(vector_local_study) if os.path.isfile(vector_local_cut_study): removeVectorFile(vector_local_cut_study) if os.path.isfile(vector_local_cut): removeVectorFile(vector_local_cut) if os.path.isfile(vector_local_cut_build): removeVectorFile(vector_local_cut_build) if os.path.isfile(vector_local_cut_other): removeVectorFile(vector_local_cut_other) if os.path.isfile(raster_local_cut): removeFile(raster_local_cut) if os.path.isfile(matrix_local_file): removeFile(matrix_local_file) return class_ref_list, class_pro_list, rate_quantity_list, kappa, overall_accuracy, matrix_origine
def processTDCfilesSmoothAndFusion(coastline_vectors_input_list, vector_rocky_input, vector_all_output, vector_withrocky_output, generalize_param_method, generalize_param_threshold, name_column_fusion, path_time_log, epsg=2154, format_vector='ESRI Shapefile', extension_vector='.shp', save_results_intermediate=False, overwrite=True): # Mise à jour du Log starting_event = "processTDCfilesSmoothAndFusion() : Create final coastline starting : " timeLine(path_time_log,starting_event) print(endC) print(bold + green + "## START : POST TRAITEMENT TDC" + endC) print(endC) if debug >= 2: print(bold + green + "processTDCfilesSmoothAndFusion() : Variables dans la fonction" + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "coastline_vectors_input_list : " + str(coastline_vectors_input_list) + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "vector_rocky_input : " + str(vector_rocky_input) + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "vector_all_output : " + str(vector_all_output) + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "vector_withrocky_output : " + str(vector_withrocky_output) + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "generalize_param_method : " + str(generalize_param_method) + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "generalize_param_threshold : " + str(generalize_param_threshold) + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "name_column_fusion : " + str(name_column_fusion) + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "epsg : " + str(epsg) + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "processTDCfilesSmoothAndFusion() : " + endC + "overwrite : " + str(overwrite) + endC) SUFFIX_SMOOTH = "_smooth" SUFFIX_TMP = "_tmp" SUFFIX_SMOOTH = "_smooth" SUFFIX_FUSION = "_fusion" repertory_output = os.path.dirname(vector_all_output) file_name = os.path.splitext(os.path.basename(vector_all_output))[0] vector_fusion = repertory_output + os.sep + file_name + SUFFIX_FUSION + extension_vector repertory_temp = repertory_output + os.sep + file_name + SUFFIX_TMP if not os.path.exists(repertory_temp): os.makedirs(repertory_temp) # Vérification de l'existence du vecteur de sortie check = os.path.isfile(vector_all_output) # Si oui et si la vérification est activée, passage à l'étape suivante if check and not overwrite : print(cyan + "processTDCfilesSmoothAndFusion() : " + bold + green + "Vector general coastline already existe : " + str(vector_all_output) + "." + endC) # Si non ou si la vérification est désactivée, application des traitements de lissage et de la fusion else: # Tentative de suppresion des fichiers try: removeVectorFile(vector_all_output) removeVectorFile(vector_withrocky_output) except Exception: # Ignore l'exception levée si le fichier n'existe pas (et ne peut donc pas être supprimé) pass # Pour tous les fichiers vecteurs d'entrée appliquer le traitement de lissage par GRASS param_generalize_dico = {"method":generalize_param_method, "threshold":generalize_param_threshold} vectors_temp_output_list = [] for input_vector in coastline_vectors_input_list : vector_name = os.path.splitext(os.path.basename(input_vector))[0] output_temp_vector = repertory_temp + os.sep + vector_name + SUFFIX_TMP + extension_vector output_smooth_vector = repertory_temp + os.sep + vector_name + SUFFIX_SMOOTH + extension_vector vectors_temp_output_list.append(output_temp_vector) xmin, xmax, ymin, ymax = getEmpriseFile(input_vector, format_vector) projection = getProjection(input_vector, format_vector) if projection is None: projection = epsg # Init GRASS if debug >= 3: print(cyan + "processTDCfilesSmoothAndFusion() : " + bold + green + "Initialisation de GRASS " + endC) initializeGrass(repertory_temp, xmin, xmax, ymin, ymax, 1, 1, projection) # Generalize GRASS if debug >= 3: print(cyan + "processTDCfilesSmoothAndFusion() : " + bold + green + "Applying smooth GRASS for vector : " + str(input_vector) + endC) smoothGeomGrass(input_vector, output_smooth_vector, param_generalize_dico, format_vector, overwrite) geometries2multigeometries(output_smooth_vector, output_temp_vector, name_column_fusion, format_vector) # Init GRASS if debug >= 3: print(cyan + "processTDCfilesSmoothAndFusion() : " + bold + green + "Cloture de GRASS " + endC) cleanGrass(repertory_temp) if debug >= 3: print(cyan + "processTDCfilesSmoothAndFusion() : " + bold + green + "Fusion de tous les vecteurs lissés : " + str(vectors_temp_output_list) + endC) # Fusion de tous les fichiers vecteurs temp fusionVectors(vectors_temp_output_list, vector_fusion, format_vector) # Suppression du champ "cat" introduit par l'application GRASS deleteFieldsVector(vector_fusion, vector_all_output, ["cat"], format_vector) # Re-met à jour le champ id avec un increment updateIndexVector(vector_all_output, "id", format_vector) # Nettoyage des zones rocheuses sur la ligne de trait de côte if vector_rocky_input != "" and vector_withrocky_output != "": if debug >= 3: print("\n" + cyan + "processTDCfilesSmoothAndFusion() : " + bold + green + "Creation d'un trait de côte generale sans les zones rocheuses : " + str(vector_withrocky_output) + endC) differenceVector(vector_rocky_input, vector_all_output, vector_withrocky_output, overwrite, format_vector) # Suppression des fichiers intermédiaires if not save_results_intermediate : if debug >= 3: print(cyan + "processTDCfilesSmoothAndFusion() : " + bold + green + "Suppression des fichiers temporaires " + endC) if os.path.exists(repertory_temp): shutil.rmtree(repertory_temp) removeVectorFile(vector_fusion) print(endC) print(bold + green + "## END : POST TRAITEMENT TDC" + endC) print(endC) # Mise à jour du Log ending_event = "processTDCfilesSmoothAndFusion() : Create final coastline ending : " timeLine(path_time_log,ending_event) return
def occupationIndicator(classif_input, mnh_input, vector_grid_input, vector_grid_output, class_label_dico, epsg, 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 = "occupationIndicator() : Calcul de l'indicateur occupation du sol/hauteur de végétation starting : " timeLine(path_time_log, starting_event) print( bold + green + "Début du calcul de l'indicateur 'occupation du sol/hauteur de végétation'." + endC + "\n") if debug >= 3: print(bold + green + "occupationIndicator() : Variables dans la fonction" + endC) print(cyan + "occupationIndicator() : " + endC + "classif_input : " + str(classif_input) + endC) print(cyan + "occupationIndicator() : " + endC + "mnh_input : " + str(mnh_input) + endC) print(cyan + "occupationIndicator() : " + endC + "vector_grid_input : " + str(vector_grid_input) + endC) print(cyan + "occupationIndicator() : " + endC + "vector_grid_output : " + str(vector_grid_output) + endC) print(cyan + "occupationIndicator() : " + endC + "class_label_dico : " + str(class_label_dico) + endC) print(cyan + "occupationIndicator() : " + endC + "epsg : " + str(epsg) + endC) print(cyan + "occupationIndicator() : " + endC + "path_time_log : " + str(path_time_log) + 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 + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "occupationIndicator() : " + endC + "overwrite : " + str(overwrite) + endC) # Constante FIELD_OCS_NAME = 'class_OCS' FIELD_OCS_TYPE = ogr.OFTInteger FIELD_MAJORITY_NAME = 'majority' # Test si le vecteur de sortie existe déjà et si il doit être écrasés check = os.path.isfile(vector_grid_output) if check and not overwrite: # Si le fichier de sortie existent deja et que overwrite n'est pas activé print( bold + yellow + "Le calcul de l'indicateur occupation du sol/hauteur de végétation a déjà eu lieu. \n" + endC) print(bold + yellow + "Grid vector output : " + vector_grid_output + " already exists and will not be created again." + endC) else: if check: try: removeVectorFile(vector_grid_output) except Exception: pass # si le fichier n'existe pas, il ne peut pas être supprimé : cette étape est ignorée ############################################ ### Préparation générale des traitements ### ############################################ # Récuperation de la projection de l'image epsg_proj = getProjectionImage(classif_input) if epsg_proj == 0: epsg_proj = epsg # Liste des classes key_class_label_list = list(class_label_dico.keys()) # Préparation des fichiers temporaires temp_path = os.path.dirname(vector_grid_output) + os.sep + "TEMP_OCS" # Nettoyage du repertoire temporaire si il existe if os.path.exists(temp_path): shutil.rmtree(temp_path) os.makedirs(temp_path) tempOCS = temp_path + os.sep + "occupation_du_sol" + extension_vector tempHveg = temp_path + os.sep + "occupation_du_sol_hauteur_de_vegetation" + extension_vector temp_class0 = temp_path + os.sep + "occupation_du_sol_hauteur_de_vegetation_class0" + extension_vector temp_class1 = temp_path + os.sep + "occupation_du_sol_hauteur_de_vegetation_class1" + extension_vector temp_class2 = temp_path + os.sep + "occupation_du_sol_hauteur_de_vegetation_class2" + extension_vector temp_class3 = temp_path + os.sep + "occupation_du_sol_hauteur_de_vegetation_class3" + extension_vector temp_class4 = temp_path + os.sep + "occupation_du_sol_hauteur_de_vegetation_class4" + extension_vector vegetation_height_temp = temp_path + os.sep + "hauteur_vegetation_temp" + extension_raster vegetation_height = temp_path + os.sep + "hauteur_vegetation" + extension_raster ############################## ### Calcul de l'indicateur ### ############################## # Récupération de l'occupation du sol de chaque maille statisticsVectorRaster(classif_input, vector_grid_input, tempOCS, 1, True, True, False, [], [], class_label_dico, path_time_log, True, format_vector, save_results_intermediate, overwrite) # Récupération de la hauteur moyenne et la hauteur max de la végétation de chaque maille command = "otbcli_BandMath -il %s %s -out %s float -exp 'im1b1==%s ? im2b1 : 0'" % ( classif_input, mnh_input, vegetation_height_temp, str(key_class_label_list[4])) exit_code = os.system(command) if exit_code != 0: print(command) print( cyan + "occupationIndicator() : " + 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 0 -of %s %s %s" % ( str(epsg_proj), format_raster, vegetation_height_temp, vegetation_height) exit_code = os.system(command) if exit_code != 0: print(command) print( cyan + "occupationIndicator() : " + bold + red + "!!! Une erreur c'est produite au cours de la comande : gdal_translate : " + command + ". Voir message d'erreur." + endC, file=sys.stderr) raise statisticsVectorRaster(vegetation_height, tempOCS, tempHveg, 1, False, False, True, [], [], {}, path_time_log, True, format_vector, save_results_intermediate, overwrite) # Définir le nom des champs temp_class_list = [] list_class_str = "" for class_str in key_class_label_list: list_class_str += "%s, " % (str(class_label_dico[class_str])) built_str = class_label_dico[key_class_label_list[0]] road_str = class_label_dico[key_class_label_list[1]] water_str = class_label_dico[key_class_label_list[2]] baresoil_str = class_label_dico[key_class_label_list[3]] vegetation_str = class_label_dico[key_class_label_list[4]] vegetation_height_medium_str = "H_moy_" + vegetation_str[0:3] vegetation_height_max_str = "H_max_" + vegetation_str[0:3] if debug >= 3: print("built_str = " + built_str) print("road_str = " + road_str) print("water_str = " + water_str) print("baresoil_str = " + baresoil_str) print("vegetation_str = " + vegetation_str) print("vegetation_height_medium_str = " + vegetation_height_medium_str) print("vegetation_height_max_str = " + vegetation_height_max_str) column = "'ID, majority, minority, %s%s, %s, %s'" % ( list_class_str, vegetation_height_max_str, vegetation_height_medium_str, FIELD_OCS_NAME) # Ajout d'un champ renvoyant la classe d'OCS attribué à chaque polygone et renomer le champ 'mean' renameFieldsVector(tempHveg, ['max'], [vegetation_height_max_str], format_vector) renameFieldsVector(tempHveg, ['mean'], [vegetation_height_medium_str], format_vector) addNewFieldVector(tempHveg, FIELD_OCS_NAME, FIELD_OCS_TYPE, 0, None, None, format_vector) # Attribution de la classe 0 => classe majoritaire de bâti ou route ou eau #expression = "(" + built_str + " >= " + baresoil_str + " AND " + built_str + " >= " + vegetation_str + ") OR (" + road_str + " >= " + baresoil_str + " AND " + road_str + " >= " + vegetation_str + ") OR (" + water_str + " >= " + baresoil_str + " AND " + water_str + " >= " + vegetation_str + ")" expression = "(" + FIELD_MAJORITY_NAME + " = '" + built_str + "') OR (" + FIELD_MAJORITY_NAME + " = '" + road_str + "') OR (" + FIELD_MAJORITY_NAME + " = '" + water_str + "')" if debug >= 3: print(expression) ret = filterSelectDataVector(tempHveg, temp_class0, column, expression, format_vector) if not ret: raise NameError( cyan + "occupationIndicator() : " + bold + red + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" % (expression) + endC) updateFieldVector(temp_class0, FIELD_OCS_NAME, 0, format_vector) temp_class_list.append(temp_class0) # Attribution de la classe 1 => classe majoritaire de sol nu #expression = "(" + baresoil_str + " > " + built_str + " AND " + baresoil_str + " > " + road_str + " AND " + baresoil_str + " > " + water_str + " AND " + baresoil_str + " >= " + vegetation_str + ")" expression = "(" + FIELD_MAJORITY_NAME + " = '" + baresoil_str + "')" if debug >= 3: print(expression) ret = filterSelectDataVector(tempHveg, temp_class1, column, expression, format_vector) if not ret: raise NameError( cyan + "occupationIndicator() : " + bold + red + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" % (expression) + endC) updateFieldVector(temp_class1, FIELD_OCS_NAME, 1, format_vector) temp_class_list.append(temp_class1) # Attribution de la classe 2 => classe majoritaire de végétation avec Hauteur inferieur à 1 #expression = "(" + vegetation_str + " > " + built_str + " AND " + vegetation_str + " > " + road_str + " AND " + vegetation_str + " > " + water_str + " AND " + vegetation_str + " > " + baresoil_str + ") AND (" + vegetation_height_medium_str + " < 1)" expression = "(" + FIELD_MAJORITY_NAME + " = '" + vegetation_str + "') AND (" + vegetation_height_medium_str + " < 1)" if debug >= 3: print(expression) ret = filterSelectDataVector(tempHveg, temp_class2, column, expression, format_vector) if not ret: raise NameError( cyan + "occupationIndicator() : " + bold + red + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" % (expression) + endC) updateFieldVector(temp_class2, FIELD_OCS_NAME, 2, format_vector) temp_class_list.append(temp_class2) # Attribution de la classe 3 => classe majoritaire de végétation avec Hauteur entre 1 et 5 #expression = "(" + vegetation_str + " > " + built_str + " AND " + vegetation_str + " > " + road_str + " AND " + vegetation_str + " > " + water_str + " AND " + vegetation_str + " > " + baresoil_str + ") AND (" + vegetation_height_medium_str + " >= 1 AND " + vegetation_height_medium_str + " < 5)" expression = "(" + FIELD_MAJORITY_NAME + " = '" + vegetation_str + "') AND (" + vegetation_height_medium_str + " >= 1 AND " + vegetation_height_medium_str + " < 5)" if debug >= 3: print(expression) ret = filterSelectDataVector(tempHveg, temp_class3, column, expression, format_vector) if not ret: raise NameError( cyan + "occupationIndicator() : " + bold + red + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" % (expression) + endC) updateFieldVector(temp_class3, FIELD_OCS_NAME, 3, format_vector) temp_class_list.append(temp_class3) # Attribution de la classe 4 => classe majoritaire de végétation avec Hauteur > 5 #expression = "(" + vegetation_str + " > " + built_str + " AND " + vegetation_str + " > " + road_str + " AND " + vegetation_str + " > " + water_str + " AND " + vegetation_str + " > " + baresoil_str + ") AND (" + vegetation_height_medium_str + " >= 5)" expression = "(" + FIELD_MAJORITY_NAME + " = '" + vegetation_str + "') AND (" + vegetation_height_medium_str + " >= 5)" if debug >= 3: print(expression) ret = filterSelectDataVector(tempHveg, temp_class4, column, expression, format_vector) if not ret: raise NameError( cyan + "occupationIndicator() : " + bold + red + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" % (expression) + endC) updateFieldVector(temp_class4, FIELD_OCS_NAME, 4, format_vector) temp_class_list.append(temp_class4) fusionVectors(temp_class_list, vector_grid_output, format_vector) ########################################## ### Nettoyage des fichiers temporaires ### ########################################## if not save_results_intermediate: if os.path.exists(temp_path): shutil.rmtree(temp_path) # Mise à jour du Log print( bold + green + "Fin du calcul de l'indicateur 'occupation du sol/hauteur de végétation'." + endC + "\n") ending_event = "occupationIndicator() : Calcul de l'indicateur occupation du sol/hauteur de végétation 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
def selectSamples(image_input_list, sample_image_input, vector_output, table_statistics_output, sampler_strategy, select_ratio_floor, ratio_per_class_dico, name_column, no_data_value, path_time_log, rand_seed=0, ram_otb=0, epsg=2154, format_vector='ESRI Shapefile', extension_vector=".shp", save_results_intermediate=False, overwrite=True) : # Mise à jour du Log starting_event = "selectSamples() : Select points in raster mask macro input starting : " timeLine(path_time_log, starting_event) if debug >= 3: print(cyan + "selectSamples() : " + endC + "image_input_list : " + str(image_input_list) + endC) print(cyan + "selectSamples() : " + endC + "sample_image_input : " + str(sample_image_input) + endC) print(cyan + "selectSamples() : " + endC + "vector_output : " + str(vector_output) + endC) print(cyan + "selectSamples() : " + endC + "table_statistics_output : " + str(table_statistics_output) + endC) print(cyan + "selectSamples() : " + endC + "sampler_strategy : " + str(sampler_strategy) + endC) print(cyan + "selectSamples() : " + endC + "select_ratio_floor : " + str(select_ratio_floor) + endC) print(cyan + "selectSamples() : " + endC + "ratio_per_class_dico : " + str(ratio_per_class_dico) + endC) print(cyan + "selectSamples() : " + endC + "name_column : " + str(name_column) + endC) print(cyan + "selectSamples() : " + endC + "no_data_value : " + str(no_data_value) + endC) print(cyan + "selectSamples() : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "selectSamples() : " + endC + "rand_seed : " + str(rand_seed) + endC) print(cyan + "selectSamples() : " + endC + "ram_otb : " + str(ram_otb) + endC) print(cyan + "selectSamples() : " + endC + "epsg : " + str(epsg) + endC) print(cyan + "selectSamples() : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "selectSamples() : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "selectSamples() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "selectSamples() : " + endC + "overwrite : " + str(overwrite) + endC) # Constantes EXT_XML = ".xml" SUFFIX_SAMPLE = "_sample" SUFFIX_STATISTICS = "_statistics" SUFFIX_POINTS = "_points" SUFFIX_VALUE = "_value" BAND_NAME = "band_" COLUMN_CLASS = "class" COLUMN_ORIGINFID = "originfid" NB_POINTS = "nb_points" AVERAGE = "average" STANDARD_DEVIATION = "st_dev" print(cyan + "selectSamples() : " + bold + green + "DEBUT DE LA SELECTION DE POINTS" + endC) # Definition variables et chemins repertory_output = os.path.dirname(vector_output) filename = os.path.splitext(os.path.basename(vector_output))[0] sample_points_output = repertory_output + os.sep + filename + SUFFIX_SAMPLE + extension_vector file_statistic_points = repertory_output + os.sep + filename + SUFFIX_STATISTICS + SUFFIX_POINTS + EXT_XML if debug >= 3: print(cyan + "selectSamples() : " + endC + "file_statistic_points : " + str(file_statistic_points) + endC) # 0. EXISTENCE DU FICHIER DE SORTIE #---------------------------------- # Si le fichier vecteur points de sortie existe deja et que overwrite n'est pas activé check = os.path.isfile(vector_output) if check and not overwrite: print(bold + yellow + "Samples points already done for file %s and will not be calculated again." %(vector_output) + endC) else: # Si non ou si la vérification est désactivée : creation du fichier d'échantillons points # Suppression de l'éventuel fichier existant if check: try: removeVectorFile(vector_output) except Exception: pass # Si le fichier ne peut pas être supprimé, on suppose qu'il n'existe pas et on passe à la suite if os.path.isfile(table_statistics_output) : try: removeFile(table_statistics_output) except Exception: pass # Si le fichier ne peut pas être supprimé, on suppose qu'il n'existe pas et on passe à la suite # 1. STATISTIQUE SUR L'IMAGE DES ECHANTILLONS RASTEUR #---------------------------------------------------- if debug >= 3: print(cyan + "selectSamples() : " + bold + green + "Start statistique sur l'image des echantillons rasteur..." + endC) id_micro_list = identifyPixelValues(sample_image_input) if 0 in id_micro_list : id_micro_list.remove(0) min_micro_class_nb_points = -1 min_micro_class_label = 0 infoStructPointSource_dico = {} writeTextFile(file_statistic_points, '<?xml version="1.0" ?>\n') appendTextFileCR(file_statistic_points, '<GeneralStatistics>') appendTextFileCR(file_statistic_points, ' <Statistic name="pointsPerClassRaw">') if debug >= 2: print("Nombre de points par micro classe :" + endC) for id_micro in id_micro_list : nb_pixels = countPixelsOfValue(sample_image_input, id_micro) if debug >= 2: print("MicroClass : " + str(id_micro) + ", nb_points = " + str(nb_pixels)) appendTextFileCR(file_statistic_points, ' <StatisticPoints class="%d" value="%d" />' %(id_micro, nb_pixels)) if min_micro_class_nb_points == -1 or min_micro_class_nb_points > nb_pixels : min_micro_class_nb_points = nb_pixels min_micro_class_label = id_micro infoStructPointSource_dico[id_micro] = StructInfoMicoClass() infoStructPointSource_dico[id_micro].label_class = id_micro infoStructPointSource_dico[id_micro].nb_points = nb_pixels infoStructPointSource_dico[id_micro].info_points_list = [] del nb_pixels if debug >= 2: print("MicroClass min points find : " + str(min_micro_class_label) + ", nb_points = " + str(min_micro_class_nb_points)) appendTextFileCR(file_statistic_points, ' </Statistic>') pending_event = cyan + "selectSamples() : " + bold + green + "End statistique sur l'image des echantillons rasteur. " + endC if debug >= 3: print(pending_event) timeLine(path_time_log,pending_event) # 2. CHARGEMENT DE L'IMAGE DES ECHANTILLONS #------------------------------------------ if debug >= 3: print(cyan + "selectSamples() : " + bold + green + "Start chargement de l'image des echantillons..." + endC) # Information image cols, rows, bands = getGeometryImage(sample_image_input) xmin, xmax, ymin, ymax = getEmpriseImage(sample_image_input) pixel_width, pixel_height = getPixelWidthXYImage(sample_image_input) projection_input = getProjectionImage(sample_image_input) if projection_input == None or projection_input == 0 : projection_input = epsg else : projection_input = int(projection_input) pixel_width = abs(pixel_width) pixel_height = abs(pixel_height) # Lecture des données raw_data = getRawDataImage(sample_image_input) if debug >= 3: print("projection = " + str(projection_input)) print("cols = " + str(cols)) print("rows = " + str(rows)) # Creation d'une structure dico contenent tous les points différents de zéro progress = 0 pass_prog = False for y_row in range(rows) : for x_col in range(cols) : value_class = raw_data[y_row][x_col] if value_class != 0 : infoStructPointSource_dico[value_class].info_points_list.append(x_col + (y_row * cols)) # Barre de progression if debug >= 4: if ((float(y_row) / rows) * 100.0 > progress) and not pass_prog : progress += 1 pass_prog = True print("Progression => " + str(progress) + "%") if ((float(y_row) / rows) * 100.0 > progress + 1) : pass_prog = False del raw_data pending_event = cyan + "selectSamples() : " + bold + green + "End chargement de l'image des echantillons. " + endC if debug >= 3: print(pending_event) timeLine(path_time_log,pending_event) # 3. SELECTION DES POINTS D'ECHANTILLON #-------------------------------------- if debug >= 3: print(cyan + "selectSamples() : " + bold + green + "Start selection des points d'echantillon..." + endC) appendTextFileCR(file_statistic_points, ' <Statistic name="pointsPerClassSelect">') # Rendre deterministe la fonction aléatoire de random.sample if rand_seed > 0: random.seed( rand_seed ) # Pour toute les micro classes for id_micro in id_micro_list : # Selon la stategie de selection nb_points_ratio = 0 while switch(sampler_strategy.lower()): if case('all'): # Le mode de selection 'all' est choisi nb_points_ratio = infoStructPointSource_dico[id_micro].nb_points infoStructPointSource_dico[id_micro].sample_points_list = range(nb_points_ratio) break if case('percent'): # Le mode de selection 'percent' est choisi id_macro_class = int(math.floor(id_micro / 100) * 100) select_ratio_class = ratio_per_class_dico[id_macro_class] nb_points_ratio = int(infoStructPointSource_dico[id_micro].nb_points * select_ratio_class / 100) infoStructPointSource_dico[id_micro].sample_points_list = random.sample(range(infoStructPointSource_dico[id_micro].nb_points), nb_points_ratio) break if case('mixte'): # Le mode de selection 'mixte' est choisi nb_points_ratio = int(infoStructPointSource_dico[id_micro].nb_points * select_ratio_floor / 100) if id_micro == min_micro_class_label : # La plus petite micro classe est concervée intégralement infoStructPointSource_dico[id_micro].sample_points_list = range(infoStructPointSource_dico[id_micro].nb_points) nb_points_ratio = min_micro_class_nb_points elif nb_points_ratio <= min_micro_class_nb_points : # Les micro classes dont le ratio de selection est inferieur au nombre de points de la plus petite classe sont égement conservées intégralement infoStructPointSource_dico[id_micro].sample_points_list = random.sample(range(infoStructPointSource_dico[id_micro].nb_points), min_micro_class_nb_points) nb_points_ratio = min_micro_class_nb_points else : # Pour toutes les autres micro classes tirage aleatoire d'un nombre de points correspondant au ratio infoStructPointSource_dico[id_micro].sample_points_list = random.sample(range(infoStructPointSource_dico[id_micro].nb_points), nb_points_ratio) break break if debug >= 2: print("MicroClass = " + str(id_micro) + ", nb_points_ratio " + str(nb_points_ratio)) appendTextFileCR(file_statistic_points, ' <StatisticPoints class="%d" value="%d" />' %(id_micro, nb_points_ratio)) appendTextFileCR(file_statistic_points, ' </Statistic>') appendTextFileCR(file_statistic_points, '</GeneralStatistics>') pending_event = cyan + "selectSamples() : " + bold + green + "End selection des points d'echantillon. " + endC if debug >= 3: print(pending_event) timeLine(path_time_log,pending_event) # 4. PREPARATION DES POINTS D'ECHANTILLON #---------------------------------------- if debug >= 3: print(cyan + "selectSamples() : " + bold + green + "Start preparation des points d'echantillon..." + endC) # Création du dico de points points_random_value_dico = {} index_dico_point = 0 for micro_class in infoStructPointSource_dico : micro_class_struct = infoStructPointSource_dico[micro_class] label_class = micro_class_struct.label_class point_attr_dico = {name_column:int(label_class), COLUMN_CLASS:int(label_class), COLUMN_ORIGINFID:0} for id_point in micro_class_struct.sample_points_list: # Recuperer les valeurs des coordonnees des points coor_x = float(xmin + (int(micro_class_struct.info_points_list[id_point] % cols) * pixel_width)) + (pixel_width / 2.0) coor_y = float(ymax - (int(micro_class_struct.info_points_list[id_point] / cols) * pixel_height)) - (pixel_height / 2.0) points_random_value_dico[index_dico_point] = [[coor_x, coor_y], point_attr_dico] del coor_x del coor_y index_dico_point += 1 del point_attr_dico del infoStructPointSource_dico pending_event = cyan + "selectSamples() : " + bold + green + "End preparation des points d'echantillon. " + endC if debug >=3: print(pending_event) timeLine(path_time_log,pending_event) # 5. CREATION DU FICHIER SHAPE DE POINTS D'ECHANTILLON #----------------------------------------------------- if debug >= 3: print(cyan + "selectSamples() : " + bold + green + "Start creation du fichier shape de points d'echantillon..." + endC) # Définir les attibuts du fichier résultat attribute_dico = {name_column:ogr.OFTInteger, COLUMN_CLASS:ogr.OFTInteger, COLUMN_ORIGINFID:ogr.OFTInteger} # Creation du fichier shape createPointsFromCoordList(attribute_dico, points_random_value_dico, sample_points_output, projection_input, format_vector) del attribute_dico del points_random_value_dico pending_event = cyan + "selectSamples() : " + bold + green + "End creation du fichier shape de points d'echantillon. " + endC if debug >=3: print(pending_event) timeLine(path_time_log,pending_event) # 6. EXTRACTION DES POINTS D'ECHANTILLONS #----------------------------------------- if debug >= 3: print(cyan + "selectSamples() : " + bold + green + "Start extraction des points d'echantillon dans l'image..." + endC) # Cas ou l'on a une seule image if len(image_input_list) == 1: # Extract sample image_input = image_input_list[0] command = "otbcli_SampleExtraction -in %s -vec %s -outfield prefix -outfield.prefix.name %s -out %s -field %s" %(image_input, sample_points_output, BAND_NAME, vector_output, name_column) if ram_otb > 0: command += " -ram %d" %(ram_otb) if debug >= 3: print(command) exitCode = os.system(command) if exitCode != 0: raise NameError(cyan + "selectSamples() : " + bold + red + "An error occured during otbcli_SampleExtraction command. See error message above." + endC) # Cas de plusieurs imagettes else : # Le repertoire de sortie repertory_output = os.path.dirname(vector_output) # Initialisation de la liste pour le multi-threading et la liste de l'ensemble des echantions locaux thread_list = [] vector_local_output_list = [] # Obtenir l'emprise des images d'entrées pour redecouper le vecteur d'echantillon d'apprentissage pour chaque image for image_input in image_input_list : # Definition des fichiers sur emprise local file_name = os.path.splitext(os.path.basename(image_input))[0] emprise_local_sample = repertory_output + os.sep + file_name + SUFFIX_SAMPLE + extension_vector vector_sample_local_output = repertory_output + os.sep + file_name + SUFFIX_VALUE + extension_vector vector_local_output_list.append(vector_sample_local_output) # Gestion sans thread... #SampleLocalExtraction(image_input, sample_points_output, emprise_local_sample, vector_sample_local_output, name_column, BAND_NAME, ram_otb, format_vector, extension_vector, save_results_intermediate) # Gestion du multi threading thread = threading.Thread(target=SampleLocalExtraction, args=(image_input, sample_points_output, emprise_local_sample, vector_sample_local_output, name_column, BAND_NAME, ram_otb, format_vector, extension_vector, save_results_intermediate)) thread.start() thread_list.append(thread) # Extraction des echantions points des images try: for thread in thread_list: thread.join() except: print(cyan + "selectSamples() : " + bold + red + "Erreur lors de l'éextaction des valeurs d'echantion : impossible de demarrer le thread" + endC, file=sys.stderr) # Fusion des multi vecteurs de points contenant les valeurs des bandes de l'image fusionVectors(vector_local_output_list, vector_output, format_vector) # Clean des vecteurs point sample local file for vector_sample_local_output in vector_local_output_list : removeVectorFile(vector_sample_local_output) if debug >= 3: print(cyan + "selectSamples() : " + bold + green + "End extraction des points d'echantillon dans l'image." + endC) # 7. CALCUL DES STATISTIQUES SUR LES VALEURS DES POINTS D'ECHANTILLONS SELECTIONNEES #----------------------------------------------------------------------------------- if debug >= 3: print(cyan + "selectSamples() : " + bold + green + "Start calcul des statistiques sur les valeurs des points d'echantillons selectionnees..." + endC) # Si le calcul des statistiques est demandé presence du fichier stat if table_statistics_output != "": # On récupère la liste de données pending_event = cyan + "selectSamples() : " + bold + green + "Encours calcul des statistiques part1... " + endC if debug >=4: print(pending_event) timeLine(path_time_log,pending_event) attribute_name_dico = {} name_field_value_list = [] names_attribut_list = getAttributeNameList(vector_output, format_vector) if debug >=4: print("names_attribut_list = " + str(names_attribut_list)) attribute_name_dico[name_column] = ogr.OFTInteger for name_attribut in names_attribut_list : if BAND_NAME in name_attribut : attribute_name_dico[name_attribut] = ogr.OFTReal name_field_value_list.append(name_attribut) name_field_value_list.sort() res_values_dico = getAttributeValues(vector_output, None, None, attribute_name_dico, format_vector) del attribute_name_dico # Trie des données par identifiant micro classes pending_event = cyan + "selectSamples() : " + bold + green + "Encours calcul des statistiques part2... " + endC if debug >=4: print(pending_event) timeLine(path_time_log,pending_event) data_value_by_micro_class_dico = {} stat_by_micro_class_dico = {} # Initilisation du dico complexe for id_micro in id_micro_list : data_value_by_micro_class_dico[id_micro] = {} stat_by_micro_class_dico[id_micro] = {} for name_field_value in res_values_dico : if name_field_value != name_column : data_value_by_micro_class_dico[id_micro][name_field_value] = [] stat_by_micro_class_dico[id_micro][name_field_value] = {} stat_by_micro_class_dico[id_micro][name_field_value][AVERAGE] = 0.0 stat_by_micro_class_dico[id_micro][name_field_value][STANDARD_DEVIATION] = 0.0 # Trie des valeurs pending_event = cyan + "selectSamples() : " + bold + green + "Encours calcul des statistiques part3... " + endC if debug >=4: print(pending_event) timeLine(path_time_log,pending_event) for index in range(len(res_values_dico[name_column])) : id_micro = res_values_dico[name_column][index] for name_field_value in name_field_value_list : data_value_by_micro_class_dico[id_micro][name_field_value].append(res_values_dico[name_field_value][index]) del res_values_dico # Calcul des statistiques pending_event = cyan + "selectSamples() : " + bold + green + "Encours calcul des statistiques part4... " + endC if debug >=4: print(pending_event) timeLine(path_time_log,pending_event) for id_micro in id_micro_list : for name_field_value in name_field_value_list : try : stat_by_micro_class_dico[id_micro][name_field_value][AVERAGE] = average(data_value_by_micro_class_dico[id_micro][name_field_value]) except: stat_by_micro_class_dico[id_micro][name_field_value][AVERAGE] = 0 try : stat_by_micro_class_dico[id_micro][name_field_value][STANDARD_DEVIATION] = standardDeviation(data_value_by_micro_class_dico[id_micro][name_field_value]) except: stat_by_micro_class_dico[id_micro][name_field_value][STANDARD_DEVIATION] = 0 try : stat_by_micro_class_dico[id_micro][name_field_value][NB_POINTS] = len(data_value_by_micro_class_dico[id_micro][name_field_value]) except: stat_by_micro_class_dico[id_micro][name_field_value][NB_POINTS] = 0 del data_value_by_micro_class_dico # Creation du fichier statistique .csv pending_event = cyan + "selectSamples() : " + bold + green + "Encours calcul des statistiques part5... " + endC if debug >= 4: print(pending_event) timeLine(path_time_log,pending_event) text_csv = " Micro classes ; Champs couche image ; Nombre de points ; Moyenne ; Ecart type \n" writeTextFile(table_statistics_output, text_csv) for id_micro in id_micro_list : for name_field_value in name_field_value_list : # Ecriture du fichier text_csv = " %d " %(id_micro) text_csv += " ; %s" %(name_field_value) text_csv += " ; %d" %(stat_by_micro_class_dico[id_micro][name_field_value][NB_POINTS]) text_csv += " ; %f" %(stat_by_micro_class_dico[id_micro][name_field_value][AVERAGE]) text_csv += " ; %f" %(stat_by_micro_class_dico[id_micro][name_field_value][STANDARD_DEVIATION]) appendTextFileCR(table_statistics_output, text_csv) del name_field_value_list else : if debug >=3: print(cyan + "selectSamples() : " + bold + green + "Pas de calcul des statistiques sur les valeurs des points demander!!!." + endC) del id_micro_list pending_event = cyan + "selectSamples() : " + bold + green + "End calcul des statistiques sur les valeurs des points d'echantillons selectionnees. " + endC if debug >= 3: print(pending_event) timeLine(path_time_log,pending_event) # 8. SUPRESSION DES FICHIERS INTERMEDIAIRES #------------------------------------------ if not save_results_intermediate: if os.path.isfile(sample_points_output) : removeVectorFile(sample_points_output) print(cyan + "selectSamples() : " + bold + green + "FIN DE LA SELECTION DE POINTS" + endC) # Mise à jour du Log ending_event = "selectSamples() : Select points in raster mask macro input ending : " timeLine(path_time_log,ending_event) return