def main(gui=False): # Définition des arguments possibles pour l'appel en ligne de commande parser = argparse.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter, prog="DataBaseSuperposition", description="\ Info : Add BD exo to classification. \n\ Objectif : Permet d'enrichir le resultat de la classification avec une superposition d'element provement de BD Exogenes a fin d'ameliorer le resultat final. \n\ Example : python DataBaseSuperposition.py -i ../ImagesTestChaine/APTV_06/Resultats/APTV_06_classif.tif \n\ -o ../ImagesTestChaine/APTV_06/Resultats/APTV_06_classif_final.tif \n\ -classBd 11000:../ImagesTestChaine/APTV_06/BD/ROUTE_74.shp:../ImagesTestChaine/APTV_06/BD/BATI_INDIFFERENCIE_74.shp \n\ 12200:../ImagesTestChaine/APTV_06/BD/SURFACE_EAU_74.shp \n\ -log ../ImagesTestChaine/APTV_06/fichierTestLog.txt" ) # Paramètres généraux parser.add_argument('-i', '--image_input', default="", help="Image classification input to add data", type=str, required=True) parser.add_argument( '-o', '--image_output', default="", help="Image classification output result additional bd data", type=str, required=True) parser.add_argument( '-classBd', '--class_file_dico', default="", nargs="+", help= "Dictionary of class containt bd, (format : classeLabel:[BDfile][..]), ex. 11000:../ImagesTestChaine/APTV_06/BD/ROUTE_74.shp,../ImagesTestChaine/APTV_06/BD/BATI_INDIFFERENCIE_74.shp 12200:../ImagesTestChaine/APTV_06/BD/SURFACE_EAU_74.shp", type=str, required=True) parser.add_argument( '-classBuf', '--class_buffer_dico', default="", nargs="+", help= "Dictionary of class containt buffer, (format : classeLabel:[sizeBuffer][..]), ex. 11000:3,5.0,,7 12200:0,", type=str, required=False) parser.add_argument( '-classSql', '--class_sql_dico', default="", nargs="+", help= "Dictionary of class containt sql request, (format : classeLabel:[SqlRequest][..]), ex. 11000:NATURE ='Autoroute' OR NATURE ='Route 2 Chausses',NATURE ='Route 2 Chausses' 12200:NATURE = 'Lac naturel'", type=str, required=False) parser.add_argument( '-simp', '--simple_param_vector', default=10.0, help="Parameter of polygons simplification. By default : 10.0", type=float, required=False) parser.add_argument('-vef', '--format_vector', default="ESRI Shapefile", help="Format of the output file.", type=str, required=False) parser.add_argument( '-rae', '--extension_raster', default=".tif", help="Option : Extension file for image raster. By default : '.tif'", type=str, required=False) parser.add_argument( '-vee', '--extension_vector', default=".shp", help="Option : Extension file for vector. By default : '.shp'", type=str, required=False) parser.add_argument('-log', '--path_time_log', default="", help="Name of log", type=str, required=False) parser.add_argument( '-sav', '--save_results_inter', action='store_true', default=False, help= "Save or delete intermediate result after the process. By default, False", required=False) parser.add_argument( '-now', '--overwrite', action='store_false', default=True, help="Overwrite files with same names. By default, True", required=False) parser.add_argument( '-debug', '--debug', default=3, help="Option : Value of level debug trace, default : 3 ", type=int, required=False) args = displayIHM(gui, parser) # RECUPERATION DES ARGUMENTS # Récupération de l'image d'entrée if args.image_input != None: image_input = args.image_input if not os.path.isfile(image_input): raise NameError(cyan + "DataBaseSuperposition : " + bold + red + "File %s not existe!" % (image_input) + endC) # Récupération de l'image de sortie if args.image_output != None: image_output = args.image_output # Creation de dictionaire table macro class contenant les fichiers dela BD class_file_dico = {} if args.class_file_dico != None: class_file_dico_tmp = extractDico(args.class_file_dico) for ident_class in class_file_dico_tmp: class_file_dico[ident_class] = class_file_dico_tmp[ident_class][0] # Creation de dictionaire table macro class contenant les buffers class_buffer_dico = {} if args.class_buffer_dico != None: class_buffer_dico_tmp = extractDico(args.class_buffer_dico) for ident_class in class_buffer_dico_tmp: class_buffer_dico[ident_class] = class_buffer_dico_tmp[ ident_class][0] # Creation de dictionaire table macro class contenant les requettes SQL if args.class_sql_dico != None: class_sql_dico_str = "" for str_dico in args.class_sql_dico: class_sql_dico_str += str_dico + " " class_sql_dico_list = class_sql_dico_str.split(":") class_sql_dico = {} idex_elem = 0 while idex_elem < len(class_sql_dico_list): class_sql_dico[class_sql_dico_list[idex_elem]] = cleanSpaceText( class_sql_dico_list[idex_elem + 1]).split(",") idex_elem += 2 # Simplifie_param param if args.simple_param_vector != None: simplifie_param = args.simple_param_vector # Récupération du format des vecteurs de sortie if args.format_vector != None: format_vector = args.format_vector # Paramètre de l'extension des images rasters if args.extension_raster != None: extension_raster = args.extension_raster # Récupération de l'extension des fichiers vecteurs if args.extension_vector != None: extension_vector = args.extension_vector # Récupération du nom du fichier log if args.path_time_log != None: path_time_log = args.path_time_log if args.save_results_inter != None: save_results_intermediate = args.save_results_inter if args.overwrite != None: overwrite = args.overwrite # Récupération de l'option niveau de debug if args.debug != None: global debug debug = args.debug if debug >= 3: print(bold + green + "Variables dans le parser" + endC) print(cyan + "DataBaseSuperposition : " + endC + "image_input : " + str(image_input) + endC) print(cyan + "DataBaseSuperposition : " + endC + "image_output : " + str(image_output) + endC) print(cyan + "DataBaseSuperposition : " + endC + "class_file_dico : " + str(class_file_dico) + endC) print(cyan + "DataBaseSuperposition : " + endC + "class_buffer_dico : " + str(class_buffer_dico) + endC) print(cyan + "DataBaseSuperposition : " + endC + "class_sql_dico : " + str(class_sql_dico) + endC) print(cyan + "DataBaseSuperposition : " + endC + "simple_param_vector : " + str(simplifie_param) + endC) print(cyan + "DataBaseSuperposition : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "DataBaseSuperposition : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "DataBaseSuperposition : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "DataBaseSuperposition : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "DataBaseSuperposition : " + endC + "save_results_inter : " + str(save_results_intermediate) + endC) print(cyan + "DataBaseSuperposition : " + endC + "overwrite: " + str(overwrite) + endC) print(cyan + "DataBaseSuperposition : " + endC + "debug: " + str(debug) + endC) # EXECUTION DE LA FONCTION # Si le dossier de sortie n'existe pas, on le crée repertory_output = os.path.dirname(image_output) if not os.path.isdir(repertory_output): os.makedirs(repertory_output) # Execution de la fonction pour une image addDataBaseExo(image_input, image_output, class_file_dico, class_buffer_dico, class_sql_dico, path_time_log, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite, simplifie_param)
def main(gui=False): # Définition des différents paramètres du parser parser = argparse.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter, prog="QualityMnsEstimation", description="\ Info : Estimating the quality of mns image by bd réference. \n\ Objectif : Estimer la qualiter d'une image de MNS en comparaison à des données issue de BD. \n\ vector_cut_input : si ce parametre est absent alors on decoupe faite sur la base de l'emprise du MNS \n\ Example : python QualityMnsEstimation.py -i /mnt/Data/gilles.fouvet/QualiteMNS/MNS/CUB_Nord_Est_MNS.tif \n\ -v /mnt/Data/gilles.fouvet/QualiteMNS/Emprise/Emprise_CUB_zone_test_NE.shp \n\ -pl /mnt/Data/gilles.fouvet/QualiteMNS/BD_Topo/ROUTE_PRIMAIRE_033.shp /mnt/Data/gilles.fouvet/QualiteMNS/BD_Topo/ROUTE_SECONDAIRE_033.shp \n\ -al /mnt/Data/gilles.fouvet/QualiteMNS/Resultats/CUB_zone_test_NE_NDVI.tif:NDVI,-1.0,0.3 /mnt/Data/gilles.fouvet/QualiteMNS/Resultats/CUB_zone_test_NE_NDWI2.tif:NDWI2 \n\ -o /mnt/Data/gilles.fouvet/QualiteMNS/Resultats/CUB_zone_test_NE_QualityMNS.shp \n\ -log /mnt/Data/gilles.fouvet/QualiteMNS/fichierTestLog.txt" ) parser.add_argument('-i', '--image_input', default="", help="Image MNS input to qualify", type=str, required=True) parser.add_argument('-v', '--vector_cut_input', default=None, help="Vector input define the sudy area.", type=str, required=False) parser.add_argument('-pl', '--vector_sample_input_list', nargs="+", default="", help="list of vector input of sample for comparaison.", type=str, required=False) parser.add_argument( '-p', '--vector_sample_points_input', default=None, help= "Vector input of sample points (Warning! replace vector_sample_input_list).", type=str, required=False) parser.add_argument( '-al', '--raster_input_dico', nargs="+", default="", help= "List of other raster input get value into vector output and colum name, threshold values min max. Exemple ndvi file and ndwi file", type=str, required=False) parser.add_argument( '-o', '--vector_output', default="", help="Vector output contain dots from the random draw into study area.", type=str, required=True) parser.add_argument( '-ndv', '--no_data_value', default=0, help="Option : Value of the pixel no data. By default : 0", type=int, required=False) parser.add_argument('-epsg', '--epsg', default=2154, help="Projection of the output file.", type=int, required=False) parser.add_argument( '-raf', '--format_raster', default="GTiff", help="Option : Format output image, by default : GTiff (GTiff, HFA...)", type=str, required=False) parser.add_argument('-vef', '--format_vector', default="ESRI Shapefile", help="Format of the output file.", type=str, required=False) parser.add_argument( '-rae', '--extension_raster', default=".tif", help="Option : Extension file for image raster. By default : '.tif'", type=str, required=False) parser.add_argument( '-vee', '--extension_vector', default=".shp", help="Option : Extension file for vector. By default : '.shp'", type=str, required=False) parser.add_argument('-log', '--path_time_log', default="", help="Name of log", type=str, required=False) parser.add_argument( '-sav', '--save_results_inter', action='store_true', default=False, help= "Save or delete intermediate result after the process. By default, False", required=False) parser.add_argument( '-now', '--overwrite', action='store_false', default=True, help="Overwrite files with same names. By default, True", required=False) parser.add_argument( '-debug', '--debug', default=3, help="Option : Value of level debug trace, default : 3 ", type=int, required=False) args = displayIHM(gui, parser) # RECUPERATION DES ARGUMENTS # Récupération de l'image d'entrée if args.image_input != None: image_input = args.image_input if not os.path.isfile(image_input): raise NameError(cyan + "QualityMnsEstimation : " + bold + red + "File %s not existe!" % (image_input) + endC) # Récupération du vecteur de decoupe vector_cut_input = None if args.vector_cut_input != None: vector_cut_input = args.vector_cut_input if vector_cut_input != "" and not os.path.isfile(vector_cut_input): raise NameError(cyan + "QualityMnsEstimation : " + bold + red + "File %s not existe!" % (vector_cut_input) + endC) # Récupération de la liste des vecteurs d'échantillonage if args.vector_sample_input_list != None: vector_sample_input_list = args.vector_sample_input_list for vector_sample_input in vector_sample_input_list: if not os.path.isfile(vector_sample_input): raise NameError(cyan + "QualityMnsEstimation : " + bold + red + "File %s not existe!" % (vector_sample_input) + endC) # Récupération du vecteur d'échantillonage points vector_sample_points_input = None if args.vector_sample_points_input != None: vector_sample_points_input = args.vector_sample_points_input if vector_sample_points_input != "" and not os.path.isfile( vector_sample_points_input): raise NameError(cyan + "QualityMnsEstimation : " + bold + red + "File %s not existe!" % (vector_sample_points_input) + endC) # Récupération des fichiers d'entrése autres et de leur valeur de seuil if args.raster_input_dico != None: raster_input_dico = extractDico(args.raster_input_dico) for raster_input in raster_input_dico: if not os.path.isfile(raster_input): raise NameError(cyan + "QualityMnsEstimation : " + bold + red + "File %s not existe!" % (raster_input) + endC) field_name = raster_input_dico[raster_input][0][0] if field_name == "" or len(field_name) > 10: raise NameError(cyan + "QualityMnsEstimation : " + bold + red + "Field name %s not valide!" % (field_name) + endC) # Récupération du fichier de sortie if args.vector_output != None: vector_output = args.vector_output # Récupération du parametre no_data_value if args.no_data_value != None: no_data_value = args.no_data_value # Récupération de la projection du fichier de sortie if args.epsg != None: epsg = args.epsg # Paramètre format des images de sortie if args.format_raster != None: format_raster = args.format_raster # Récupération du nom du format des fichiers vecteur if args.format_vector != None: format_vector = args.format_vector # Paramètre de l'extension des images rasters if args.extension_raster != None: extension_raster = args.extension_raster # Récupération de l'extension des fichiers vecteurs if args.extension_vector != None: extension_vector = args.extension_vector # Récupération du nom du fichier log if args.path_time_log != None: path_time_log = args.path_time_log # Récupération de l'option écrasement if args.save_results_inter != None: save_results_intermediate = args.save_results_inter if args.overwrite != None: overwrite = args.overwrite # Récupération de l'option niveau de debug if args.debug != None: global debug debug = args.debug if debug >= 3: print(bold + green + "QualityMnsEstimation : Variables dans le parser" + endC) print(cyan + "QualityMnsEstimation : " + endC + "image_input : " + str(image_input) + endC) print(cyan + "QualityMnsEstimation : " + endC + "vector_cut_input : " + str(vector_cut_input) + endC) print(cyan + "QualityMnsEstimation : " + endC + "vector_sample_input_list : " + str(vector_sample_input_list) + endC) print(cyan + "QualityMnsEstimation : " + endC + "vector_sample_points_input : " + str(vector_sample_points_input) + endC) print(cyan + "QualityMnsEstimation : " + endC + "raster_input_dico : " + str(raster_input_dico) + endC) print(cyan + "QualityMnsEstimation : " + endC + "vector_output : " + str(vector_output) + endC) print(cyan + "QualityMnsEstimation : " + endC + "no_data_value : " + str(no_data_value) + endC) print(cyan + "QualityMnsEstimation : " + endC + "epsg : " + str(epsg) + endC) print(cyan + "QualityMnsEstimation : " + endC + "format_raster : " + str(format_raster) + endC) print(cyan + "QualityMnsEstimation : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "QualityMnsEstimation : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "QualityMnsEstimation : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "QualityMnsEstimation : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "QualityMnsEstimation : " + endC + "save_results_inter : " + str(save_results_intermediate) + endC) print(cyan + "QualityMnsEstimation : " + endC + "overwrite : " + str(overwrite) + endC) print(cyan + "QualityMnsEstimation : " + endC + "debug : " + str(debug) + endC) # EXECUTION DE LA FONCTION # Si le dossier de sortie n'existent pas, on le crée repertory_output = os.path.dirname(vector_output) if not os.path.isdir(repertory_output): os.makedirs(repertory_output) # execution de la fonction pour une image 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, epsg, format_vector, extension_raster, extension_vector, save_results_intermediate, overwrite)
def BDToSamples(input_image, output_dir, input_bd_buff_dico, input_index_images_list, no_data_value, path_time_log, project_encoding="UTF-8", epsg=2154, format_vector="ESRI Shapefile", extension_raster=".tif", extension_vector=".shp", save_results_intermediate=True, overwrite=True): # Mise à jour du Log starting_event = "BDToSamples() : Select BDToSamples starting : " timeLine(path_time_log,starting_event) # Affichage des paramètres if debug >= 3: print(bold + green + "Variables dans le parser" + endC) print(cyan + "BDToSamples : " + endC + "input_image : " + str(input_image) + endC) print(cyan + "BDToSamples : " + endC + "output_dir : " + str(output_dir) + endC) print(cyan + "BDToSamples : " + endC + "input_bd_buff_dico : " + str(input_bd_buff_dico) + endC) print(cyan + "BDToSamples : " + endC + "input_index_images_list : " + str(input_index_images_list) + endC) print(cyan + "BDToSamples : " + endC + "project_encoding : " + str(project_encoding) + endC) print(cyan + "BDToSamples : " + endC + "epsg : " + str(epsg) + endC) print(cyan + "BDToSamples : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "BDToSamples : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "BDToSamples : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "BDToSamples() : " + endC + "no_data_value : " + str(no_data_value) + endC) print(cyan + "BDToSamples : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "BDToSamples : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "BDToSamples : " + endC + "overwrite : " + str(overwrite) + endC) # Initialisation des constantes EXTENSION_TEXT = ".txt" REP_TEMP = "temp_BDToSamples" ID_CLASS = "id_class" INDEX_NDVI = "NDVI" INDEX_NDWI2 = "NDWI2" CODAGE = "uint8" SUFFIX_MASK = "_mask" SUFFIX_KMEANS = "_kmeans" SUFFIX_CENTROID = "_centroid" SUFFIX_POLYGON = "_polygon" SUFFIX_VECTOR = "_vector" SUFFIX_OUTPUT = "_output" SUFFIX_TABLE_REALLOC = "_table_realloc" SUFFIX_SAMPLES_MERGED = "_samples_merged" SUFFIX_CORRECTED = "_corrected_" # Variables repertory_temp = output_dir + os.sep + REP_TEMP sample_masks_final_list = [] sample_kmeans_masks_list = [] macroclass_labels_list = [] nb_macroclass_samples_list = [] centroids_list = [] polygons_list = [] buffer_size_list_clean = [] buffer_approximate_list_clean = [] minimal_area_list_clean = [] simplification_tolerance_list_clean = [] # 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) # Création du répertoire de sortie temporaire s'il n'existe pas déjà if not os.path.exists(repertory_temp): os.makedirs(repertory_temp) # Initialisation des nom de fichiers input_image_name = os.path.splitext(os.path.basename(input_image))[0] #output_vector = repertory_temp + os.sep + input_image_name + SUFFIX_OUTPUT + SUFFIX_VECTOR + extension_vector output_proposal_table = repertory_temp + os.sep + input_image_name + SUFFIX_TABLE_REALLOC + SUFFIX_VECTOR + EXTENSION_TEXT output_merged = repertory_temp + os.sep + input_image_name + SUFFIX_SAMPLES_MERGED + extension_vector input_bd_buff_dico = extractDico(input_bd_buff_dico) # Traitements et remplissage des listes pour chaque macroclasse for sample, list_param in input_bd_buff_dico.items(): repertory_sample = repertory_temp + os.sep + sample # Création du répertoire de sortie par macroclasse s'il n'existe pas if not os.path.exists(repertory_sample): os.makedirs(repertory_sample) output_sample = repertory_sample + os.sep + sample + extension_vector sample_mask = repertory_sample + os.sep + sample + SUFFIX_MASK + extension_raster sample_kmeans_mask = repertory_sample + os.sep + sample + SUFFIX_KMEANS + SUFFIX_MASK_KMEANS + extension_raster sample_kmeans_masks_list.append(sample_kmeans_mask) macroclass_label = list_param[0][0] macroclass_labels_list.append(macroclass_label) nb_macroclass_sample = list_param[0][1] nb_macroclass_samples_list.append(nb_macroclass_sample) centroid = repertory_sample + os.sep + sample + SUFFIX_CENTROID + EXTENSION_TEXT centroids_list.append(centroid) polygon = repertory_sample + os.sep + sample + SUFFIX_POLYGON + extension_vector polygons_list.append(polygon) bd_shp = list_param[0][2] buffer_shp = float(list_param[0][3]) buffer_size_list_clean.append(-0.5) buffer_approximate_list_clean.append(2) minimal_area_list_clean.append(10) simplification_tolerance_list_clean.append(1) bd_shp_list = [] bd_shp_list.append(bd_shp) buffer_size_list = [] buffer_size_list.append(buffer_shp) createMacroSamples(input_image, output_sample, "", "", bd_shp_list, buffer_size_list, None, path_time_log, "", 10.0, format_vector, extension_vector, save_results_intermediate, overwrite) rasterizeBinaryVector(output_sample, input_image, sample_mask, 1, CODAGE) # Amélioration des échantillons avec seuils sur les images indices if input_index_images_list != "": for sample, list_param in input_bd_buff_dico.items(): repertory_sample = repertory_temp + os.sep + sample if len(list_param[0]) == 7 : sample_mask_to_correct = repertory_sample + os.sep + sample + SUFFIX_MASK + extension_raster seuil_bas = list_param[0][5] seuil_haut = list_param[0][6] if list_param[0][4] == INDEX_NDVI: sample_mask_corrected_NDVI = repertory_sample + os.sep + sample + SUFFIX_MASK + SUFFIX_CORRECTED + INDEX_NDVI + extension_raster sample_masks_final_list.append(sample_mask_corrected_NDVI) treatment_mask_list = [INDEX_NDVI, seuil_bas, seuil_haut, "0", "0", "and"] processMacroSamples(sample_mask_to_correct, sample_mask_corrected_NDVI, input_index_images_list[0], treatment_mask_list, INDEX_NDVI, path_time_log, extension_raster, save_results_intermediate, overwrite) elif list_param[0][4] == INDEX_NDWI2: sample_mask_corrected_NDWI2 = repertory_sample + os.sep + sample + SUFFIX_MASK + SUFFIX_CORRECTED + INDEX_NDWI2 + extension_raster sample_masks_final_list.append(sample_mask_corrected_NDWI2) treatment_mask_list = [INDEX_NDWI2, seuil_bas, seuil_haut, "0", "0", "and"] processMacroSamples(sample_mask_to_correct, sample_mask_corrected_NDWI2, input_index_images_list[1], treatment_mask_list, INDEX_NDWI2, path_time_log, save_results_intermediate, overwrite) else: sample_masks_final_list.append(repertory_sample + os.sep + sample + SUFFIX_MASK + extension_raster) if debug >= 2: print(cyan + str(sample_masks_final_list) + endC) applyKmeansMasks(input_image, sample_masks_final_list, "", "", sample_kmeans_masks_list, centroids_list, nb_macroclass_samples_list, macroclass_labels_list, no_data_value, path_time_log, extension_raster, save_results_intermediate, overwrite) polygonize_gdal(sample_kmeans_masks_list, polygons_list, path_time_log, ID_CLASS, format_vector, save_results_intermediate, overwrite) cleanMergeVectors_ogr(polygons_list, output_merged, output_proposal_table, path_time_log, buffer_size_list_clean, buffer_approximate_list_clean, minimal_area_list_clean, simplification_tolerance_list_clean, 10, ID_CLASS, 'GEOMETRY', 'POLYGON', format_vector, extension_raster, extension_vector, project_encoding, epsg, save_results_intermediate, overwrite) # Suppression du repertoire temporaire if not save_results_intermediate and os.path.exists(repertory_temp): shutil.rmtree(repertory_temp) # Mise à jour du Log ending_event = "mergeMicroclasses() : BD to samples ending : " timeLine(path_time_log,ending_event) return output_merged
def main(gui=False): # Définition des arguments possibles pour l'appel en ligne de commande parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, prog="ClassAmelioration",description="\ Info : Correction macro class classification. \n\ Objectif : Améliorer la qualité des classes du réultat de la classification. \n\ Example : python ClassAmelioration.py -i /mnt/hgfs/Data_Image_Saturn/CUB_zone_test_NE_1/Classification/CUB_zone_test_NE_stack_rf_merged_filtered_chanPIR_rad2_IC2_NDVI_MNH_clean.tif \n\ -o /mnt/hgfs/Data_Image_Saturn/CUB_zone_test_NE_1/Classification/CUB_zone_test_NE_stack_rf_merged_filtered_chanPIR_rad2_IC2_NDVI_MNH_clean2.tif \n\ -classAdd 11100:/mnt/hgfs/Data_Image_Saturn/CUB_zone_test_NE_1/Neocanaux/CUB_zone_test_NE_MNH.tif,2.5,999.0:/mnt/hgfs/Data_Image_Saturn/CUB_zone_test_NE_1/Neocanaux/CUB_zone_test_NE_NDVI.tif,-0.1,0.3 \n\ 12200:/mnt/hgfs/Data_Image_Saturn/CUB_zone_test_NE_1/Neocanaux/CUB_zone_test_NE_NDVI.tif,-2.0,-0.15 \n\ 20000:/mnt/hgfs/Data_Image_Saturn/CUB_zone_test_NE_1/Neocanaux/CUB_zone_test_NE_NDVI.tif,0.4,2.0 \n\ -log /mnt/hgfs/Data_Image_Saturn/CUB_zone_test_NE_1/CUB_zone_test_NE.log") # Paramètres généraux parser.add_argument('-i','--image_input',default="",help="Image classification input to add data", type=str, required=True) parser.add_argument('-o','--image_output',default="",help="Image classification output result additional external file data", type=str, required=True) parser.add_argument('-classAdd','--class_add_data_dico',default="",nargs="+",help="Dictionary of class containt file dat to add and their treatments, (format : classeLabel:[image,threshold_min_image1,threshold_max_image1][..]), ex. 11100:../CUB_zone_test_NE_MNH.tif,2.5,999.0:../CUB_zone_test_NE_NDVI.tif,-0.1,0.3 12200:../CUB_zone_test_NE_NDVI.tif,-2.0,-0.15 20000:../CUB_zone_test_NE_NDVI.tif,0.4,2.0", type=str, required=True) parser.add_argument('-log','--path_time_log',default="",help="Name of log", type=str, required=False) parser.add_argument('-sav','--save_results_inter',action='store_true',default=False,help="Save or delete intermediate result after the process. By default, False", required=False) parser.add_argument('-now','--overwrite',action='store_false',default=True,help="Overwrite files with same names. By default, True",required=False) parser.add_argument('-debug','--debug',default=3,help="Option : Value of level debug trace, default : 3 ",type=int, required=False) args = displayIHM(gui, parser) # RECUPERATION DES ARGUMENTS # Récupération de l'image d'entrée if args.image_input != None: image_input = args.image_input if not os.path.isfile(image_input): raise NameError (cyan + "ClassAmelioration : " + bold + red + "File %s not existe!" %(image_input) + endC) # Récupération de l'image de sortie if args.image_output != None: image_output = args.image_output # creation du dictionaire table macro class contenant les fichiers data à ajouter ainsi que leurs traitements if args.class_add_data_dico != None: class_add_data_dico = extractDico(args.class_add_data_dico) # Récupération du nom du fichier log if args.path_time_log!= None: path_time_log = args.path_time_log if args.save_results_inter != None: save_results_intermediate = args.save_results_inter if args.overwrite != None: overwrite = args.overwrite # Récupération de l'option niveau de debug if args.debug!= None: global debug debug = args.debug if debug >= 3: print(bold + green + "Variables dans le parser" + endC) print(cyan + "ClassAmelioration : " + endC + "image_input : " + str(image_input) + endC) print(cyan + "ClassAmelioration : " + endC + "image_output : " + str(image_output) + endC) print(cyan + "ClassAmelioration : " + endC + "class_add_data_dico : " + str(class_add_data_dico) + endC) print(cyan + "ClassAmelioration : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "ClassAmelioration : " + endC + "save_results_inter : " + str(save_results_intermediate) + endC) print(cyan + "ClassAmelioration : " + endC + "overwrite: " + str(overwrite) + endC) print(cyan + "ClassAmelioration : " + endC + "debug: " + str(debug) + endC) # EXECUTION DE LA FONCTION # Si le dossier de sortie n'existe pas, on le crée repertory_output = os.path.dirname(image_output) if not os.path.isdir(repertory_output): os.makedirs(repertory_output) # execution de la fonction pour une image addCorrectionClass(image_input, image_output, class_add_data_dico, path_time_log, save_results_intermediate, overwrite)
def main(gui=False): # Définition des arguments possibles pour l'appel en ligne de commande parser = argparse.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter, prog="PostTraitementsRasterSuperposition", description="\ Title : Post treat with exogene data in raster format. \n\ Objectif : Permet d'enrichir le resultat de la classification avec une superposition d'element provenant d autres rasters. \n\ Structure de ptrd - post_treatment_raster_dico : label_de_macroclass/indice/texture:seuilmin,seuilmax,dilation_en_pixels,interieur_ou_exterieur_du_masque,classe_à_remplacer/all,classe_de_remplacement \n\ Example : python PostTraitementsRaster.py -i /mnt/hgfs/PartageVM2/D2_Par_Zone/Paysage_01/Corr_3/Resultats/Paysage_01_merged_filtred.tif \n\ -o /mnt/hgfs/PartageVM2/D2_Par_Zone/Paysage_01/Corr_3/Resultats/Paysage_01_merged_filtred_pt.tif \n\ -v /mnt/Data/gilles.fouvet/RA/Rhone/Global/Preparation/Landscapes_Boundaries/Paysage_01.shp \n\ -ptrd pt1:/mnt/hgfs/PartageVM2/D2_Par_Zone/Paysage_01/Neocanaux/Paysage_01_20110508T113726_NDVI.tif,-2,-0.2,0,in,all,11000 \n\ pt2:/mnt/hgfs/PartageVM2/D2_Par_Zone/Paysage_01//mnt/hgfs/PartageVM2/D2_Par_Zone/Paysage_01/Corr_1/Macro/Paysage_01_11000_artificialises_mask_cleaned.tif,0.5,1.5,1,in,all,11000 \n\ pt3:/mnt/hgfs/PartageVM2/D2_Par_Zone/Paysage_01/Corr_1/Macro/Paysage_01_11100_infrastructure_mask_cleaned.tif,0.5,1.5,0,in,all,11100 \n\ pt4:/mnt/hgfs/PartageVM2/D2_Par_Zone/Paysage_01/Corr_1/Macro/Paysage_01_12200_eau_mask_cleaned.tif,0.5,1.5,1,in,all,12200 \n\ -log /mnt/hgfs/PartageVM2/D2_Par_Zone/Paysage_01/APTV_05/fichierTestLog.txt -sav" ) # Paramètres généraux parser.add_argument( '-i', '--image_input', help="Input : classified image on which we will add data", type=str, required=True) parser.add_argument('-o', '--image_output', help="Output : post traited classif", type=str, required=True) parser.add_argument( '-v', '--vector_input', default="", help= "Vector input contain the vector to cut image output. If empty no cutting", type=str, required=False) parser.add_argument('-ptrd', '--post_treatment_raster_dico', default="", nargs="+", help="Dictionnaire for post traitements rasters", type=str, required=True) parser.add_argument( "-ndv", '--no_data_value', default=0, help="Option pixel value for raster file to no data, default : 0 ", type=int, required=False) parser.add_argument( '-raf', '--format_raster', default="GTiff", help="Option : Format output image, by default : GTiff (GTiff, HFA...)", type=str, required=False) parser.add_argument('-vef', '--format_vector', default="ESRI Shapefile", help="Format of the output file.", type=str, required=False) parser.add_argument( '-rae', '--extension_raster', default=".tif", help="Option : Extension file for image raster. By default : '.tif'", type=str, required=False) parser.add_argument('-log', '--path_time_log', default="", help="Name of log", type=str, required=False) parser.add_argument( '-sav', '--save_results_inter', action='store_true', default=False, help= "Save or delete intermediate result after the process. By default, False", required=False) parser.add_argument( '-now', '--overwrite', action='store_false', default=True, help="Overwrite files with same names. By default, True", required=False) parser.add_argument( '-debug', '--debug', default=3, help="Option : Value of level debug trace, default : 3 ", type=int, required=False) args = displayIHM(gui, parser) # RECUPERATION DES ARGUMENTS # Récupération de l'image d'entrée if args.image_input != None: image_input = args.image_input if not os.path.isfile(image_input): raise NameError(cyan + "PostTraitementsRaster : " + bold + red + "File %s not existe!" % (image_input) + endC) # Récupération de l'image de sortie if args.image_output != None: image_output = args.image_output # Récupération du fichier vecteur de découpe enable_cutting_ask = False if args.vector_input != None: vector_input = args.vector_input if vector_input != "": enable_cutting_ask = True if not os.path.isfile(vector_input): raise NameError(cyan + "PostTraitementsRaster : " + bold + red + "File %s not existe!" % (vector_input) + endC) # Creation du dictionaire contenant les valeurs des traitements pour chaque image de correction if args.post_treatment_raster_dico != None: post_treatment_raster_dico = extractDico( args.post_treatment_raster_dico) # Paramettre des no data if args.no_data_value != None: no_data_value = args.no_data_value # Paramètre format des images de sortie if args.format_raster != None: format_raster = args.format_raster # Récupération du format des vecteurs de sortie if args.format_vector != None: format_vector = args.format_vector # Paramètre de l'extension des images rasters if args.extension_raster != None: extension_raster = args.extension_raster # Récupération du nom du fichier log if args.path_time_log != None: path_time_log = args.path_time_log if args.save_results_inter != None: save_results_intermediate = args.save_results_inter if args.overwrite != None: overwrite = args.overwrite # Récupération de l'option niveau de debug if args.debug != None: global debug debug = args.debug if debug >= 3: print(bold + green + "Variables dans le parser" + endC) print(cyan + "PostTraitementsRaster : " + endC + "image_input : " + str(image_input) + endC) print(cyan + "PostTraitementsRaster : " + endC + "image_output : " + str(image_output) + endC) print(cyan + "PostTraitementsRaster : " + endC + "vector_input : " + str(vector_input) + endC) print(cyan + "PostTraitementsRaster : " + endC + "post_treatment_raster_dico : " + str(post_treatment_raster_dico) + endC) print(cyan + "PostTraitementsRaster : " + endC + "no_data_value : " + str(no_data_value) + endC) print(cyan + "PostTraitementsRaster : " + endC + "format_raster : " + str(format_raster) + endC) print(cyan + "PostTraitementsRaster : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "PostTraitementsRaster : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "PostTraitementsRaster : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "PostTraitementsRaster : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC) print(cyan + "PostTraitementsRaster : " + endC + "overwrite: " + str(overwrite) + endC) print(cyan + "PostTraitementsRaster : " + endC + "debug: " + str(debug) + endC) # EXECUTION DE LA FONCTION # Si le dossier de sortie n'existe pas, on le crée repertory_output = os.path.dirname(image_output) if not os.path.isdir(repertory_output): os.makedirs(repertory_output) # Execution de la fonction pour une image if post_treatment_raster_dico != {}: postTraitementsRaster(image_input, image_output, vector_input, enable_cutting_ask, post_treatment_raster_dico, no_data_value, path_time_log, format_raster, format_vector, extension_raster, save_results_intermediate, overwrite)
def main(gui=False): # Définition des arguments possibles pour l'appel en ligne de commande parser = argparse.ArgumentParser( formatter_class=argparse.RawTextHelpFormatter, prog="BuiltDifference", description="\ Info : Create macro samples. \n\ Objectif : Realiser identifier des differences entre la BT topo partie Batie et un MNS. \n\ Example : python BuiltDifference.py -i /mnt/hgfs/Data_Image_Saturn/Test_Methode_CUB_zone_test_NE_1/CUB_zone_test_NE_1.tif \n\ -imns /mnt/hgfs/Data_Image_Saturn/Test_Methode_CUB_zone_test_NE_1/MNS_CUB_zone_test_NE_1.tif \n\ -imnt /mnt/hgfs/Data_Image_Saturn/Test_Methode_CUB_zone_test_NE_1/MNT_CUB_zone_test_NE_1.tif \n\ -o /mnt/hgfs/Data_Image_Saturn/Test_Methode_CUB_zone_test_NE_1/Result/CUB_zone_test_NE_1_diff.tif \n\ -v /mnt/hgfs/Data_Image_Saturn/Test_Methode_CUB_zone_test_NE_1/Result/CUB_zone_test_NE_1_diff_mns.shp \n\ -ibdl /mnt/hgfs/Data_Image_Saturn/BD/BATI_INDIFFERENCIE_033.shp \n\ /mnt/hgfs/Data_Image_Saturn/BD/BATI_INDUSTRIEL_033.shp \n\ -dep 033 \n\ -tndvi 0.3 -tdiff 8.0 -fdif0 5 -fdif1 10 \n\ -log /mnt/hgfs/Data_Image_Saturn/Test_Methode_CUB_zone_test_NE_1/fichierTestLog.txt" ) # Paramètres généraux parser.add_argument('-i', '--image_input', default="", help="Image input to treat", type=str, required=True) parser.add_argument('-imns', '--image_mns', default="", help="Image input of MNS", type=str, required=True) parser.add_argument('-imnt', '--image_mnt', default="", help="Image input of MNT", type=str, required=True) parser.add_argument('-o', '--image_output', help="Image output result difference", type=str, required=False) parser.add_argument('-v', '--vector_output', help="Vector output result difference", type=str, required=False) parser.add_argument( '-ibdl', '--bd_vector_input_list', default="", nargs="+", help="List containt bd vector input concatened to create vector sample", type=str, required=True) parser.add_argument('-field', '--fileld_bd_raster', default="HAUTEUR", help="Filled value used to rasterize shape BD", type=str, required=False) parser.add_argument( '-zone', '--zone_buff_dico', nargs="+", default=["Bati:BATI_INDIFFERENCIE,0:BATI_INDUSTRIEL,0"], help= "Dictionary of zone containt bd and buffer, (format : zone:[BD,sizeBuffer][..]), ex. Bati:BATI_INDIFFERENCIE,0:BATI_INDUSTRIEL,0", type=str, required=False) parser.add_argument( '-dep', '--departments_list', default="", nargs="+", help= "List sources departements selected (add a 0 before dep begining by 0), ex. 001 33.", type=str, required=True) parser.add_argument( '-simp', '--simple_param_vector', default=2.0, help="Parameter of polygons simplification. By default : 2.0", type=float, required=False) parser.add_argument('-tndvi', '--threshold_ndvi', default=0.25, help="Parameter of threshold ndvi. By default : 0.25", type=float, required=False) parser.add_argument( '-tdiff', '--threshold_difference', default=6.0, help="Parameter of threshold difference mns. By default : 6.0", type=float, required=False) parser.add_argument( '-fdif0', '--filter_difference_0', default=5, help= "Parameter of filter clean 0 value, result difference. By default : 5", type=int, required=False) parser.add_argument( '-fdif1', '--filter_difference_1', default=15, help= "Parameter of filter clean 1 value, result difference. By default : 15", type=int, required=False) parser.add_argument( '-chao', '--channel_order', nargs="+", default=['Red', 'Green', 'Blue', 'NIR'], help= "Type of multispectral image : rapideye or spot6 or pleiade. By default : [Red,Green,Blue,NIR]", type=str, required=False) parser.add_argument('-vef', '--format_vector', default="ESRI Shapefile", help="Format of the output file.", type=str, required=False) parser.add_argument( '-rae', '--extension_raster', default=".tif", help="Option : Extension file for image raster. By default : '.tif'", type=str, required=False) parser.add_argument( '-vee', '--extension_vector', default=".shp", help="Option : Extension file for vector. By default : '.shp'", type=str, required=False) parser.add_argument('-log', '--path_time_log', default="", help="Name of log", type=str, required=False) parser.add_argument( '-sav', '--save_results_inter', action='store_true', default=False, help= "Save or delete intermediate result after the process. By default, False", required=False) parser.add_argument( '-now', '--overwrite', action='store_false', default=True, help="Overwrite files with same names. By default, True", required=False) parser.add_argument( '-debug', '--debug', default=3, help="Option : Value of level debug trace, default : 3 ", type=int, required=False) args = displayIHM(gui, parser) # RECUPERATION DES ARGUMENTS # Récupération des arguments du parser images d'entrées if args.image_input != None: image_input = args.image_input if not os.path.isfile(image_input): raise NameError(cyan + "BuiltDifference : " + bold + red + "File %s not existe!" % (image_input) + endC) if args.image_mns != None: image_mns = args.image_mns if not os.path.isfile(image_mns): raise NameError(cyan + "BuiltDifference : " + bold + red + "File %s not existe!" % (image_mns) + endC) if args.image_mnt != None: image_mnt = args.image_mnt if not os.path.isfile(image_mnt): raise NameError(cyan + "BuiltDifference : " + bold + red + "File %s not existe!" % (image_mnt) + endC) # Récupération des vecteurs de bd exogenes if args.bd_vector_input_list != None: bd_vector_input_list = args.bd_vector_input_list # Récupération des arguments du parser images de sorties if args.image_output != None: image_output = args.image_output if args.vector_output != None: vector_output = args.vector_output # Récupération des info données if args.departments_list != None: departments_list = args.departments_list if args.fileld_bd_raster != None: fileld_bd_raster = args.fileld_bd_raster # creation du dictionaire table macro class contenant la BD et le buffer if args.zone_buff_dico != None: zone_buffer_dico = extractDico(args.zone_buff_dico) # Parametres de filtrage if args.simple_param_vector != None: simplifie_param = args.simple_param_vector if args.threshold_ndvi != None: threshold_ndvi = args.threshold_ndvi if args.threshold_difference != None: threshold_difference = args.threshold_difference if args.filter_difference_0 != None: filter_difference_0 = args.filter_difference_0 if args.filter_difference_1 != None: filter_difference_1 = args.filter_difference_1 # Ordre des canaux de l'image ortho if args.channel_order != None: channel_order = args.channel_order # Récupération du format du fichier de sortie if args.format_vector != None: format_vector = args.format_vector # Paramètre de l'extension des images rasters if args.extension_raster != None: extension_raster = args.extension_raster # Récupération de l'extension des fichiers vecteurs if args.extension_vector != None: extension_vector = args.extension_vector # Récupération du nom du fichier log if args.path_time_log != None: path_time_log = args.path_time_log # Ecrasement des fichiers if args.save_results_inter != None: save_results_intermediate = args.save_results_inter if args.overwrite != None: overwrite = args.overwrite # Récupération de l'option niveau de debug if args.debug != None: global debug debug = args.debug if debug >= 3: print(bold + green + "Variables dans le parser" + endC) print(cyan + "BuiltDifference : " + endC + "image_input : " + str(image_input) + endC) print(cyan + "BuiltDifference : " + endC + "image_mns : " + str(image_mns) + endC) print(cyan + "BuiltDifference : " + endC + "image_mnt : " + str(image_mnt) + endC) print(cyan + "BuiltDifference : " + endC + "image_output : " + str(image_output) + endC) print(cyan + "BuiltDifference : " + endC + "bd_vector_input_list : " + str(bd_vector_input_list) + endC) print(cyan + "BuiltDifference : " + endC + "vector_output : " + str(vector_output) + endC) print(cyan + "BuiltDifference : " + endC + "zone_buffer_dico : " + str(zone_buffer_dico) + endC) print(cyan + "BuiltDifference : " + endC + "departments_list : " + str(departments_list) + endC) print(cyan + "BuiltDifference : " + endC + "fileld_bd_raster : " + str(fileld_bd_raster) + endC) print(cyan + "BuiltDifference : " + endC + "simple_param_vector : " + str(simplifie_param) + endC) print(cyan + "BuiltDifference : " + endC + "threshold_ndvi : " + str(threshold_ndvi) + endC) print(cyan + "BuiltDifference : " + endC + "threshold_difference : " + str(threshold_difference) + endC) print(cyan + "BuiltDifference : " + endC + "filter_difference_0 : " + str(filter_difference_0) + endC) print(cyan + "BuiltDifference : " + endC + "filter_difference_1 : " + str(filter_difference_1) + endC) print(cyan + "BuiltDifference : " + endC + "channel_order : " + str(channel_order) + endC) print(cyan + "BuiltDifference : " + endC + "format_vector : " + str(format_vector) + endC) print(cyan + "BuiltDifference : " + endC + "extension_raster : " + str(extension_raster) + endC) print(cyan + "BuiltDifference : " + endC + "extension_vector : " + str(extension_vector) + endC) print(cyan + "BuiltDifference : " + endC + "path_time_log : " + str(path_time_log) + endC) print(cyan + "BuiltDifference : " + endC + "save_results_inter : " + str(save_results_intermediate) + endC) print(cyan + "BuiltDifference : " + endC + "overwrite: " + str(overwrite) + endC) print(cyan + "BuiltDifference : " + endC + "debug: " + str(debug) + endC) # EXECUTION DE LA FONCTION # Si les dossiers de sortie n'existent pas, on les crées repertory_output = os.path.dirname(image_output) if not os.path.isdir(repertory_output): os.makedirs(repertory_output) repertory_output = os.path.dirname(vector_output) if not os.path.isdir(repertory_output): os.makedirs(repertory_output) # execution de la fonction pour une image createDifference(image_input, image_mns, image_mnt, bd_vector_input_list, zone_buffer_dico, departments_list, image_output, vector_output, fileld_bd_raster, simplifie_param, threshold_ndvi, threshold_difference, filter_difference_0, filter_difference_1, path_time_log, format_vector, extension_raster, extension_vector, save_results_intermediate, channel_order, overwrite) # ajouter les valeurs des hauteurs en champs suplementaire au shape statisticsVectorRaster(image_output, vector_output, "", 1, False, False, True, [], [], {}, path_time_log, True, format_vector, save_results_intermediate, overwrite)