def polygonMerToTDC(input_im_ndvi_dico, output_dir, input_sea_points, fct_bin_mask_vect, simplif, input_cut_vector, buf_pos, buf_neg, no_data_value, path_time_log, epsg=2154, format_vector="ESRI Shapefile", extension_raster=".tif", extension_vector=".shp", save_results_intermediate=True, overwrite=True):

    # Mise à jour du Log
    starting_event = "PolygonMerToTDC() : Select PolygonMerToTDC starting : "
    timeLine(path_time_log,starting_event)

    # Affichage des paramètres
    if debug >= 3:
        print(bold + green + "Variables dans PolygonMerToTDC - Variables générales" + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "input_im_ndvi_dico : " + str(input_im_ndvi_dico) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "output_dir : " + str(output_dir) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "input_sea_points : " + str(input_sea_points) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "fct_bin_mask_vect : " + str(fct_bin_mask_vect) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "simplif : " + str(simplif) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "input_cut_vector : " + str(input_cut_vector) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "buf_pos : " + str(buf_pos) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "buf_neg : " + str(buf_neg) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "no_data_value : " + str(no_data_value) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "path_time_log : " + str(path_time_log) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "epsg : " + str(epsg) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "format_vector : " + str(format_vector) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "extension_raster : " + str(extension_raster) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "extension_vector : " + str(extension_vector) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC)
        print(cyan + "polygonMerToTDC() : " + endC + "overwrite : " + str(overwrite) + endC)

    # Constantes
    REP_TEMP_POLY_MER = "Temp_PolygonMerToTDC_"
    CODAGE_8B = "uint8"

    # 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)

    # Pour toutes les images à traiter
    for r in range(len(input_im_ndvi_dico.split())):
        # L'image à traiter
        input_image = input_im_ndvi_dico.split()[r].split(":")[0]
        image_name = os.path.splitext(os.path.basename(input_image))[0]

        # Création du répertoire temporaire de calcul
        repertory_temp = output_dir + os.sep + REP_TEMP_POLY_MER + image_name
        if not os.path.exists(repertory_temp):
            os.makedirs(repertory_temp)

        if not os.path.exists(input_image):
            print(cyan + "polygonMerToTDC() : " + bold + red + "L'image en entrée : " + input_image + " n'existe pas. Vérifiez le chemin !" + endC, file=sys.stderr)
            sys.exit(1)

        src_input_im = gdal.Open(input_image)
        if src_input_im is None:
            print(cyan + "polygonMerToTDC() : " + bold + red + "Impossible d'ouvrir l'image raster : " + input_image + endC, file=sys.stderr)
            sys.exit(1)
        try :
            srcband = src_input_im.GetRasterBand(2)
        except RuntimeError as err:
            print(cyan + "polygonMerToTDC() : " + bold + red + "Pas de bande 2 trouvée sur : " + input_image + endC, file=sys.stderr)
            e = "OS error: {0}".format(err)
            print(e, file=sys.stderr)
            sys.exit(1)

        # Traiter le cas où pas de NDVI derrière
        if ":" not in input_im_ndvi_dico.split()[r]:
            print(cyan + "polygonMerToTDC() : " + red + bold + "Aucun masque binaire vectorisé spécifié ! Nécessité d'au moins un par image." + endC, file=sys.stderr)
            sys.exit(1)

        # Parcours de toutes les images NDVI correspondant à chaque image
        for ndvi_mask_vect in input_im_ndvi_dico.split()[r].split(":")[1].split(","):
            if debug > 2 :
                print(cyan + "polygonMerToTDC() : " + endC + "Traitement de : " + ndvi_mask_vect)

            # Initialisation des noms des fichiers de sortie
            binary_mask_zeros = repertory_temp + os.sep +  "b_mask_zeros_" + os.path.splitext(os.path.basename(input_image))[0] + extension_raster
            binary_mask_zeros_vector = "b_mask_zeros_vect_" + os.path.splitext(os.path.basename(input_image))[0]
            path_binary_mask_zeros_vector = repertory_temp + os.sep +  binary_mask_zeros_vector + extension_vector
            true_values_buffneg = repertory_temp + os.sep + "true_values_buffneg_" + os.path.splitext(os.path.basename(input_image))[0] + extension_vector
            decoup_buffneg = repertory_temp + os.sep + "decoupe_buffneg_" + os.path.splitext(os.path.basename(input_cut_vector))[0] + extension_vector

            if fct_bin_mask_vect :
                threshold = os.path.splitext(os.path.basename(ndvi_mask_vect))[0].split("_")[-2] + "_" + os.path.splitext(os.path.basename(ndvi_mask_vect))[0].split("_")[-1]
                poly_mer_shp = repertory_temp + os.sep + "poly_mer_" + os.path.splitext(os.path.basename(input_image))[0] + "_" + str(threshold) + extension_vector
                poly_mer_shp_dilat = repertory_temp + os.sep + "poly_mer_dilat_" + os.path.splitext(os.path.basename(input_image))[0] + "_" + str(threshold) + extension_vector
                poly_mer_shp_ferm = repertory_temp + os.sep + "poly_mer_ferm_" + os.path.splitext(os.path.basename(input_image))[0] + "_" + str(threshold) + extension_vector
                polyline_mer = repertory_temp + os.sep + "polyline_mer_" + os.path.splitext(os.path.basename(input_image))[0] + "_" + str(threshold) + extension_vector
                polyline_tdc = repertory_temp + os.sep + "tdc_" + os.path.splitext(os.path.basename(input_image))[0] + "_" + str(threshold) + extension_vector
                polyline_tdc_simplif = repertory_temp + os.sep + "tdcs_" + os.path.splitext(os.path.basename(input_image))[0] + "_" + str(threshold) + extension_vector
                polyline_tdc_simplif_decoup = output_dir + os.sep + "tdcsd_" + os.path.splitext(os.path.basename(input_image))[0] + "_" + str(threshold) + extension_vector
            else :
                poly_mer_shp = repertory_temp + os.sep + "poly_mer_" + os.path.splitext(os.path.basename(ndvi_mask_vect))[0] + extension_vector
                poly_mer_shp_dilat = repertory_temp + os.sep + "poly_mer_dilat_" + os.path.splitext(os.path.basename(input_image))[0] + extension_vector
                poly_mer_shp_ferm = repertory_temp + os.sep + "poly_mer_ferm_" + os.path.splitext(os.path.basename(input_image))[0] + extension_vector
                polyline_mer = repertory_temp + os.sep + "polyline_mer_" + os.path.splitext(os.path.basename(ndvi_mask_vect))[0] + extension_vector
                polyline_tdc = repertory_temp + os.sep + "tdc_" + os.path.splitext(os.path.basename(ndvi_mask_vect))[0] + extension_vector
                polyline_tdc_simplif = repertory_temp + os.sep + "tdcs_" + os.path.splitext(os.path.basename(ndvi_mask_vect))[0] + extension_vector
                polyline_tdc_simplif_decoup = output_dir + os.sep + "tdcsd_" + os.path.splitext(os.path.basename(ndvi_mask_vect))[0] + extension_vector

            # Création shp poly_mer_shp contenant uniquement les polygones mer
            if os.path.exists(poly_mer_shp):
                removeVectorFile(poly_mer_shp)

            # Sélection des polygones qui sont de la mer, dans la liste poly_mer
            withinPolygons(input_sea_points, ndvi_mask_vect, poly_mer_shp, overwrite, format_vector)

            # Fermeture (dilatation - érosion) sur les polygones mer obtenus pour supprimer les petits trous dans les polygones (bateaux, ...) et rassembler les polygones proches
            bufferVector(poly_mer_shp, poly_mer_shp_dilat, buf_pos, "", 1.0, 10, format_vector)
            bufferVector(poly_mer_shp_dilat, poly_mer_shp_ferm, buf_neg, "", 1.0, 10, format_vector)

            # Création masque binaire pour séparer les no data des vraies valeurs
            no_data_ima = getNodataValueImage(input_image)
            if no_data_ima == None :
                no_data_ima = no_data_value
            createBinaryMaskMultiBand(input_image, binary_mask_zeros, no_data_ima, CODAGE_8B)

            # Vectorisation du masque binaire true data/false data -> polygone avec uniquement les vraies valeurs
            if os.path.exists(path_binary_mask_zeros_vector):
                removeVectorFile(path_binary_mask_zeros_vector)
            polygonizeRaster(binary_mask_zeros, path_binary_mask_zeros_vector, binary_mask_zeros_vector)

            # Buffer négatif sur ce polygone
            bufferVector(path_binary_mask_zeros_vector, true_values_buffneg, -2, "", 1.0, 10, format_vector)

            # Transformation des polygones de la couche poly_mer_shp_ferm en polyligne
            convertePolygon2Polylines(poly_mer_shp_ferm, polyline_mer, overwrite, format_vector)

            # Découpe du TDC polyline_mer avec le polygone négatif
            cutVectorAll(true_values_buffneg, polyline_mer, polyline_tdc, overwrite, format_vector)

            # Simplification du TDC
            simplifyVector(polyline_tdc, polyline_tdc_simplif, simplif, format_vector)

            # Buffer négatif autour de input_cut_vector
            bufferVector(input_cut_vector, decoup_buffneg, -1, "", 1.0, 10, format_vector)

            # Découpe du TDC polyline_mer avec le buffer négatif du polygone mer
            cutVectorAll(decoup_buffneg, polyline_tdc_simplif, polyline_tdc_simplif_decoup, overwrite, format_vector)

            tdc_final = polyline_tdc_simplif_decoup

        # 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 = "PolygonMerToTDC() : Select PolygonMerToTDC ending: "
    timeLine(path_time_log,ending_event)

    return tdc_final
def occupationIndicator(input_grid,
                        output_grid,
                        class_label_dico_out,
                        input_vector_classif,
                        field_classif_name,
                        input_soil_occupation,
                        input_height_model,
                        class_build_list,
                        class_road_list,
                        class_baresoil_list,
                        class_water_list,
                        class_vegetation_list,
                        class_high_vegetation_list,
                        class_low_vegetation_list,
                        epsg=2154,
                        no_data_value=0,
                        format_raster='GTiff',
                        format_vector='ESRI Shapefile',
                        extension_raster='.tif',
                        extension_vector='.shp',
                        path_time_log='',
                        save_results_intermediate=False,
                        overwrite=True):

    if debug >= 3:
        print(
            '\n' + bold + green +
            "Calcul d'indicateurs du taux de classes OCS - Variables dans la fonction :"
            + endC)
        print(cyan + "    occupationIndicator() : " + endC + "input_grid : " +
              str(input_grid) + endC)
        print(cyan + "    occupationIndicator() : " + endC + "output_grid : " +
              str(output_grid) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "class_label_dico_out : " + str(class_label_dico_out) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "input_vector_classif : " + str(input_vector_classif) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "field_classif_name : " + str(field_classif_name) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "input_soil_occupation : " + str(input_soil_occupation) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "input_height_model : " + str(input_height_model) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "class_build_list : " + str(class_build_list) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "class_road_list : " + str(class_road_list) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "class_baresoil_list : " + str(class_baresoil_list) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "class_water_list : " + str(class_water_list) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "class_vegetation_list : " + str(class_vegetation_list) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "class_high_vegetation_list : " +
              str(class_high_vegetation_list) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "class_low_vegetation_list : " + str(class_low_vegetation_list) +
              endC)
        print(cyan + "    occupationIndicator() : " + endC + "epsg : " +
              str(epsg) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "no_data_value : " + str(no_data_value) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "format_raster : " + str(format_raster) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "format_vector : " + str(format_vector) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "extension_raster : " + str(extension_raster) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "extension_vector : " + str(extension_vector) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "path_time_log : " + str(path_time_log) + endC)
        print(cyan + "    occupationIndicator() : " + endC +
              "save_results_intermediate : " + str(save_results_intermediate) +
              endC)
        print(cyan + "    occupationIndicator() : " + endC + "overwrite : " +
              str(overwrite) + endC + '\n')

    # Définition des constantes
    CODAGE_8BITS = 'uint8'
    CODAGE_FLOAT = 'float'
    NODATA_FIELD = 'nodata'

    PREFIX_S = 'S_'
    SUFFIX_TEMP = '_temp'
    SUFFIX_RASTER = '_raster'
    SUFFIX_HEIGHT = '_height'
    SUFFIX_VEGETATION = '_vegetation'

    VEG_MEAN_FIELD = 'veg_h_mean'
    VEG_MAX_FIELD = 'veg_h_max'
    VEG_RATE_FIELD = 'veg_h_rate'
    MAJ_OCS_FIELD = 'class_OCS'

    BUILT_FIELD, BUILT_LABEL = 'built', 1
    MINERAL_FIELD, MINERAL_LABEL = 'mineral', 2
    BARESOIL_FIELD, BARESOIL_LABEL = 'baresoil', 3
    WATER_FIELD, WATER_LABEL = 'water', 4
    VEGETATION_FIELD, VEGETATION_LABEL = 'veget', 5
    HIGH_VEGETATION_FIELD, HIGH_VEGETATION_LABEL = 'high_veg', 6
    LOW_VEGETATION_FIELD, LOW_VEGETATION_LABEL = 'low_veg', 7

    # Mise à jour du log
    starting_event = "occupationIndicator() : Début du traitement : "
    timeLine(path_time_log, starting_event)

    print(cyan + "occupationIndicator() : " + bold + green +
          "DEBUT DES TRAITEMENTS" + endC + '\n')

    # Définition des variables 'basename'
    output_grid_basename = os.path.basename(os.path.splitext(output_grid)[0])
    output_grid_dirname = os.path.dirname(output_grid)
    soil_occupation_basename = os.path.basename(
        os.path.splitext(input_soil_occupation)[0])

    # Définition des variables temp
    temp_directory = output_grid_dirname + os.sep + output_grid_basename
    temp_grid = temp_directory + os.sep + output_grid_basename + SUFFIX_TEMP + extension_vector
    temp_soil_occupation = temp_directory + os.sep + soil_occupation_basename + SUFFIX_TEMP + SUFFIX_RASTER + extension_raster
    temp_height_vegetation = temp_directory + os.sep + output_grid_basename + SUFFIX_HEIGHT + SUFFIX_VEGETATION + extension_raster

    # Nettoyage des traitements précédents
    if overwrite:
        if debug >= 3:
            print(cyan + "occupationIndicator() : " + endC +
                  "Nettoyage des traitements précédents." + endC + '\n')
        removeFile(output_grid)
        cleanTempData(temp_directory)
    else:
        if os.path.exists(output_grid):
            raise NameError(
                cyan + "occupationIndicator() : " + bold + yellow +
                "Le fichier de sortie existe déjà et ne sera pas regénéré." +
                endC + '\n')
        pass

    #############
    # Etape 0/3 # Préparation des traitements
    #############

    print(cyan + "occupationIndicator() : " + bold + green +
          "ETAPE 0/3 - Début de la préparation des traitements." + endC + '\n')

    # Rasterisation de l'information de classification (OCS) si au format vecteur en entrée
    if input_vector_classif != "":
        if debug >= 3:
            print(cyan + "occupationIndicator() : " + endC + bold +
                  "Rasterisation de l'OCS vecteur." + endC + '\n')
        reference_image = input_soil_occupation
        soil_occupation_vector_basename = os.path.basename(
            os.path.splitext(input_vector_classif)[0])
        input_soil_occupation = temp_directory + os.sep + soil_occupation_vector_basename + SUFFIX_RASTER + extension_raster
        command = "otbcli_Rasterization -in %s -out %s %s -im %s -background 0 -mode attribute -mode.attribute.field %s" % (
            input_vector_classif, input_soil_occupation, CODAGE_8BITS,
            reference_image, field_classif_name)
        if debug >= 3:
            print(command)
        exit_code = os.system(command)
        if exit_code != 0:
            raise NameError(
                cyan + "occupationIndicator() : " + bold + red +
                "Erreur lors de la rasterisation de l'OCS vecteur." + endC)

    # Analyse de la couche OCS raster
    class_other_list = identifyPixelValues(input_soil_occupation)
    no_data_ocs = getNodataValueImage(input_soil_occupation, 1)
    if no_data_ocs != None:
        no_data_value = no_data_ocs

    # Affectation de nouveaux codes de classification
    divide_vegetation_classes = False
    if class_high_vegetation_list != [] and class_low_vegetation_list != []:
        divide_vegetation_classes = True

    col_to_delete_list = [
        "minority", PREFIX_S + NODATA_FIELD, PREFIX_S + BUILT_FIELD,
        PREFIX_S + MINERAL_FIELD, PREFIX_S + BARESOIL_FIELD,
        PREFIX_S + WATER_FIELD
    ]
    class_label_dico = {
        int(no_data_value): NODATA_FIELD,
        int(BUILT_LABEL): BUILT_FIELD,
        int(MINERAL_LABEL): MINERAL_FIELD,
        int(BARESOIL_LABEL): BARESOIL_FIELD,
        int(WATER_LABEL): WATER_FIELD
    }
    if not divide_vegetation_classes:
        class_label_dico[int(VEGETATION_LABEL)] = VEGETATION_FIELD
        col_to_delete_list.append(PREFIX_S + VEGETATION_FIELD)
    else:
        class_label_dico[int(HIGH_VEGETATION_LABEL)] = HIGH_VEGETATION_FIELD
        class_label_dico[int(LOW_VEGETATION_LABEL)] = LOW_VEGETATION_FIELD
        col_to_delete_list.append(PREFIX_S + HIGH_VEGETATION_FIELD)
        col_to_delete_list.append(PREFIX_S + LOW_VEGETATION_FIELD)

    # Gestion de la réaffectation des classes
    if debug >= 3:
        print(cyan + "occupationIndicator() : " + endC + bold +
              "Reaffectation du raster OCS." + endC + '\n')

    reaff_class_list = []
    macro_reaff_class_list = []

    for label in class_build_list:
        if label in class_other_list:
            class_other_list.remove(label)
        reaff_class_list.append(label)
        macro_reaff_class_list.append(BUILT_LABEL)

    for label in class_road_list:
        if label in class_other_list:
            class_other_list.remove(label)
        reaff_class_list.append(label)
        macro_reaff_class_list.append(MINERAL_LABEL)

    for label in class_baresoil_list:
        if label in class_other_list:
            class_other_list.remove(label)
        reaff_class_list.append(label)
        macro_reaff_class_list.append(BARESOIL_LABEL)

    for label in class_water_list:
        if label in class_other_list:
            class_other_list.remove(label)
        reaff_class_list.append(label)
        macro_reaff_class_list.append(WATER_LABEL)

    if not divide_vegetation_classes:
        for label in class_vegetation_list:
            if label in class_other_list:
                class_other_list.remove(label)
            reaff_class_list.append(label)
            macro_reaff_class_list.append(VEGETATION_LABEL)
    else:
        for label in class_high_vegetation_list:
            if label in class_other_list:
                class_other_list.remove(label)
            reaff_class_list.append(label)
            macro_reaff_class_list.append(HIGH_VEGETATION_LABEL)
        for label in class_low_vegetation_list:
            if label in class_other_list:
                class_other_list.remove(label)
            reaff_class_list.append(label)
            macro_reaff_class_list.append(LOW_VEGETATION_LABEL)

    # Reste des valeurs de pixel nom utilisé
    for label in class_other_list:
        reaff_class_list.append(label)
        macro_reaff_class_list.append(no_data_value)

    reallocateClassRaster(input_soil_occupation, temp_soil_occupation,
                          reaff_class_list, macro_reaff_class_list,
                          CODAGE_8BITS)

    print(cyan + "occupationIndicator() : " + bold + green +
          "ETAPE 0/3 - Fin de la préparation des traitements." + endC + '\n')

    #############
    # Etape 1/3 # Calcul des indicateurs de taux de classes OCS
    #############

    print(
        cyan + "occupationIndicator() : " + bold + green +
        "ETAPE 1/3 - Début du calcul des indicateurs de taux de classes OCS." +
        endC + '\n')

    if debug >= 3:
        print(cyan + "occupationIndicator() : " + endC + bold +
              "Calcul des indicateurs de taux de classes OCS." + endC + '\n')

    statisticsVectorRaster(temp_soil_occupation, input_grid, temp_grid, 1,
                           True, True, False, col_to_delete_list, [],
                           class_label_dico, path_time_log, True,
                           format_vector, save_results_intermediate, overwrite)

    # Fusion des classes végétation dans le cas où haute et basse sont séparées (pour utilisation du taux de végétation dans le logigramme)
    if divide_vegetation_classes:
        temp_grid_v2 = os.path.splitext(
            temp_grid)[0] + "_v2" + extension_vector
        sql_statement = "SELECT *, (%s + %s) AS %s FROM %s" % (
            HIGH_VEGETATION_FIELD, LOW_VEGETATION_FIELD, VEGETATION_FIELD,
            os.path.splitext(os.path.basename(temp_grid))[0])
        os.system("ogr2ogr -sql '%s' -dialect SQLITE %s %s" %
                  (sql_statement, temp_grid_v2, temp_grid))
        removeVectorFile(temp_grid, format_vector=format_vector)
        copyVectorFile(temp_grid_v2, temp_grid, format_vector=format_vector)

    print(cyan + "occupationIndicator() : " + bold + green +
          "ETAPE 1/3 - Fin du calcul des indicateurs de taux de classes OCS." +
          endC + '\n')

    #############
    # Etape 2/3 # Calcul de l'indicateur de "hauteur de végétation"
    #############

    print(
        cyan + "occupationIndicator() : " + bold + green +
        "ETAPE 2/3 - Début du calcul de l'indicateur de \"hauteur de végétation\"."
        + endC + '\n')

    computeVegetationHeight(
        temp_grid, output_grid, temp_soil_occupation, input_height_model,
        temp_height_vegetation, divide_vegetation_classes, VEGETATION_LABEL,
        HIGH_VEGETATION_LABEL, LOW_VEGETATION_LABEL, HIGH_VEGETATION_FIELD,
        LOW_VEGETATION_FIELD, VEG_MEAN_FIELD, VEG_MAX_FIELD, VEG_RATE_FIELD,
        CODAGE_FLOAT, SUFFIX_TEMP, no_data_value, format_vector, path_time_log,
        save_results_intermediate, overwrite)

    print(
        cyan + "occupationIndicator() : " + bold + green +
        "ETAPE 2/3 - Fin du calcul de l'indicateur de \"hauteur de végétation\"."
        + endC + '\n')

    #############
    # Etape 3/3 # Calcul de l'indicateur de classe majoritaire
    #############

    print(
        cyan + "occupationIndicator() : " + bold + green +
        "ETAPE 3/3 - Début du calcul de l'indicateur de classe majoritaire." +
        endC + '\n')

    if input_height_model != "":
        computeMajorityClass(output_grid, temp_directory, NODATA_FIELD,
                             BUILT_FIELD, MINERAL_FIELD, BARESOIL_FIELD,
                             WATER_FIELD, VEGETATION_FIELD,
                             HIGH_VEGETATION_FIELD, LOW_VEGETATION_FIELD,
                             MAJ_OCS_FIELD, VEG_MEAN_FIELD,
                             class_label_dico_out, format_vector,
                             extension_vector, overwrite)
    else:
        print(
            cyan + "occupationIndicator() : " + bold + yellow +
            "Pas de calcul de l'indicateur de classe majoritaire demandé (pas de MNH en entrée)."
            + endC + '\n')

    print(cyan + "occupationIndicator() : " + bold + green +
          "ETAPE 3/3 - Fin du calcul de l'indicateur de classe majoritaire." +
          endC + '\n')

    ####################################################################

    # Suppression des fichiers temporaires
    if not save_results_intermediate:
        if debug >= 3:
            print(cyan + "occupationIndicator() : " + endC +
                  "Suppression des fichiers temporaires." + endC + '\n')
        deleteDir(temp_directory)

    print(cyan + "occupationIndicator() : " + bold + green +
          "FIN DES TRAITEMENTS" + endC + '\n')

    # Mise à jour du log
    ending_event = "occupationIndicator() : Fin du traitement : "
    timeLine(path_time_log, ending_event)

    return 0
Ejemplo n.º 3
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
Ejemplo n.º 4
0
def addDataBaseExo(image_input,
                   image_classif_add_output,
                   class_file_dico,
                   class_buffer_dico,
                   class_sql_dico,
                   path_time_log,
                   format_vector='ESRI Shapefile',
                   extension_raster=".tif",
                   extension_vector=".shp",
                   save_results_intermediate=False,
                   overwrite=True,
                   simplifie_param=10.0):

    # Mise à jour du Log
    starting_event = "addDataBaseExo() : Add data base exogene to classification starting : "
    timeLine(path_time_log, starting_event)

    # Print
    if debug >= 3:
        print(bold + green + "Variables dans la fonction" + endC)
        print(cyan + "addDataBaseExo() : " + endC + "image_input : " +
              str(image_input) + endC)
        print(cyan + "addDataBaseExo() : " + endC +
              "image_classif_add_output : " + str(image_classif_add_output) +
              endC)
        print(cyan + "addDataBaseExo() : " + endC + "class_file_dico : " +
              str(class_file_dico) + endC)
        print(cyan + "addDataBaseExo() : " + endC + "class_buffer_dico : " +
              str(class_buffer_dico) + endC)
        print(cyan + "addDataBaseExo() : " + endC + "class_sql_dico : " +
              str(class_sql_dico) + endC)
        print(cyan + "addDataBaseExo() : " + endC + "path_time_log : " +
              str(path_time_log) + endC)
        print(cyan + "addDataBaseExo() : " + endC + "format_vector : " +
              str(format_vector) + endC)
        print(cyan + "addDataBaseExo() : " + endC + "extension_raster : " +
              str(extension_raster) + endC)
        print(cyan + "addDataBaseExo() : " + endC + "extension_vector : " +
              str(extension_vector) + endC)
        print(cyan + "addDataBaseExo() : " + endC +
              "save_results_intermediate : " + str(save_results_intermediate) +
              endC)
        print(cyan + "addDataBaseExo() : " + endC + "overwrite : " +
              str(overwrite) + endC)

    # Constantes
    FOLDER_MASK_TEMP = 'Mask_'
    FOLDER_FILTERING_TEMP = 'Filt_'
    FOLDER_CUTTING_TEMP = 'Cut_'
    FOLDER_BUFF_TEMP = 'Buff_'

    SUFFIX_MASK_CRUDE = '_mcrude'
    SUFFIX_MASK = '_mask'
    SUFFIX_FUSION = '_info'
    SUFFIX_VECTOR_FILTER = "_filt"
    SUFFIX_VECTOR_CUT = '_decoup'
    SUFFIX_VECTOR_BUFF = '_buff'

    CODAGE = "uint16"

    # ETAPE 1 : NETTOYER LES DONNEES EXISTANTES
    if debug >= 2:
        print(cyan + "addDataBaseExo() : " + bold + green +
              "NETTOYAGE ESPACE DE TRAVAIL..." + endC)

    # Nom de base de l'image
    image_name = os.path.splitext(os.path.basename(image_input))[0]

    # Nettoyage d'anciennes données résultat

    # Si le fichier résultat existent deja et que overwrite n'est pas activé
    check = os.path.isfile(image_classif_add_output)
    if check and not overwrite:
        print(bold + yellow + "addDataBaseExo() : " + endC +
              image_classif_add_output +
              " has already added bd exo and will not be added again." + endC)
    else:
        if check:
            try:
                removeFile(image_classif_add_output
                           )  # Tentative de suppression du fichier
            except Exception:
                pass  # Si le fichier ne peut pas être supprimé, on suppose qu'il n'existe pas et on passe à la suite

        # Définition des répertoires temporaires
        repertory_output = os.path.dirname(image_classif_add_output)
        repertory_mask_temp = repertory_output + os.sep + FOLDER_MASK_TEMP + image_name
        repertory_samples_filtering_temp = repertory_output + os.sep + FOLDER_FILTERING_TEMP + image_name
        repertory_samples_cutting_temp = repertory_output + os.sep + FOLDER_CUTTING_TEMP + image_name
        repertory_samples_buff_temp = repertory_output + os.sep + FOLDER_BUFF_TEMP + image_name

        if debug >= 4:
            print(repertory_mask_temp)
            print(repertory_samples_filtering_temp)
            print(repertory_samples_cutting_temp)
            print(repertory_samples_buff_temp)

        # Creer les répertoires temporaire si ils n'existent pas
        if not os.path.isdir(repertory_output):
            os.makedirs(repertory_output)
        if not os.path.isdir(repertory_mask_temp):
            os.makedirs(repertory_mask_temp)
        if not os.path.isdir(repertory_samples_filtering_temp):
            os.makedirs(repertory_samples_filtering_temp)
        if not os.path.isdir(repertory_samples_cutting_temp):
            os.makedirs(repertory_samples_cutting_temp)
        if not os.path.isdir(repertory_samples_buff_temp):
            os.makedirs(repertory_samples_buff_temp)

        # Nettoyer les répertoires temporaire si ils ne sont pas vide
        cleanTempData(repertory_mask_temp)
        cleanTempData(repertory_samples_filtering_temp)
        cleanTempData(repertory_samples_cutting_temp)
        cleanTempData(repertory_samples_buff_temp)

        if debug >= 2:
            print(cyan + "addDataBaseExo() : " + bold + green +
                  "... FIN NETTOYAGE" + endC)

        # ETAPE 2 : CREER UN SHAPE DE DECOUPE

        if debug >= 2:
            print(cyan + "addDataBaseExo() : " + bold + green +
                  "SHAPE DE DECOUPE..." + endC)

        # 2.1 : Création des masques délimitant l'emprise de la zone par image

        vector_mask = repertory_mask_temp + os.sep + image_name + SUFFIX_MASK_CRUDE + extension_vector
        cols, rows, num_band = getGeometryImage(image_input)
        no_data_value = getNodataValueImage(image_input, num_band)
        if no_data_value == None:
            no_data_value = 0
        createVectorMask(image_input, vector_mask, no_data_value,
                         format_vector)

        # 2.2 : Simplification du masque global

        vector_simple_mask_cut = repertory_mask_temp + os.sep + image_name + SUFFIX_MASK + extension_vector
        simplifyVector(vector_mask, vector_simple_mask_cut, simplifie_param,
                       format_vector)

        if debug >= 2:
            print(cyan + "addDataBaseExo() : " + bold + green +
                  "...FIN SHAPE DE DECOUPEE" + endC)

        # ETAPE 3 : DECOUPER BUFFERISER LES VECTEURS ET FUSIONNER

        if debug >= 2:
            print(cyan + "addDataBaseExo() : " + bold + green +
                  "MISE EN PLACE DES TAMPONS..." + endC)

        image_combined_list = []
        # Parcours du dictionnaire associant les macroclasses aux noms de fichiers
        for macroclass_label in class_file_dico:
            vector_fusion_list = []
            for index_info in range(len(class_file_dico[macroclass_label])):
                input_vector = class_file_dico[macroclass_label][index_info]
                vector_name = os.path.splitext(
                    os.path.basename(input_vector))[0]
                output_vector_filtered = repertory_samples_filtering_temp + os.sep + vector_name + SUFFIX_VECTOR_FILTER + extension_vector
                output_vector_cut = repertory_samples_cutting_temp + os.sep + vector_name + SUFFIX_VECTOR_CUT + extension_vector
                output_vector_buff = repertory_samples_buff_temp + os.sep + vector_name + SUFFIX_VECTOR_BUFF + extension_vector
                sql_expression = class_sql_dico[macroclass_label][index_info]
                buffer_str = class_buffer_dico[macroclass_label][index_info]
                buff = 0.0
                col_name_buf = ""
                try:
                    buff = float(buffer_str)
                except:
                    col_name_buf = buffer_str
                    print(
                        cyan + "addDataBaseExo() : " + bold + green +
                        "Pas de valeur buffer mais un nom de colonne pour les valeur à bufferiser : "
                        + endC + col_name_buf)

                if os.path.isfile(input_vector):
                    if debug >= 3:
                        print(cyan + "addDataBaseExo() : " + endC +
                              "input_vector : " + str(input_vector) + endC)
                        print(cyan + "addDataBaseExo() : " + endC +
                              "output_vector_filtered : " +
                              str(output_vector_filtered) + endC)
                        print(cyan + "addDataBaseExo() : " + endC +
                              "output_vector_cut : " + str(output_vector_cut) +
                              endC)
                        print(cyan + "addDataBaseExo() : " + endC +
                              "output_vector_buff : " +
                              str(output_vector_buff) + endC)
                        print(cyan + "addDataBaseExo() : " + endC + "buff : " +
                              str(buff) + endC)
                        print(cyan + "addDataBaseExo() : " + endC + "sql : " +
                              str(sql_expression) + endC)

                    # 3.0 : Recuperer les vecteurs d'entrée et filtree selon la requete sql par ogr2ogr
                    if sql_expression != "":
                        names_attribut_list = getAttributeNameList(
                            input_vector, format_vector)
                        column = "'"
                        for name_attribut in names_attribut_list:
                            column += name_attribut + ", "
                        column = column[0:len(column) - 2]
                        column += "'"
                        ret = filterSelectDataVector(input_vector,
                                                     output_vector_filtered,
                                                     column, sql_expression,
                                                     format_vector)
                        if not ret:
                            print(
                                cyan + "addDataBaseExo() : " + bold + yellow +
                                "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte"
                                % (sql_expression) + endC)
                            output_vector_filtered = input_vector
                    else:
                        print(cyan + "addDataBaseExo() : " + bold + green +
                              "Pas de filtrage sur le fichier du nom : " +
                              endC + output_vector_filtered)
                        output_vector_filtered = input_vector

                    # 3.1 : Découper le vecteur selon l'empise de l'image d'entrée
                    cutoutVectors(vector_simple_mask_cut,
                                  [output_vector_filtered],
                                  [output_vector_cut], format_vector)

                    # 3.2 : Bufferiser lesvecteurs découpé avec la valeur défini dans le dico ou trouver dans la base du vecteur lui même si le nom de la colonne est passée dans le dico
                    if os.path.isfile(output_vector_cut) and (
                        (buff != 0) or (col_name_buf != "")):
                        bufferVector(output_vector_cut, output_vector_buff,
                                     buff, col_name_buf, 1.0, 10,
                                     format_vector)
                    else:
                        print(cyan + "addDataBaseExo() : " + bold + green +
                              "Pas de buffer sur le fichier du nom : " + endC +
                              output_vector_cut)
                        output_vector_buff = output_vector_cut

                    # 3.3 : Si un shape résulat existe l'ajouté à la liste de fusion
                    if os.path.isfile(output_vector_buff):
                        vector_fusion_list.append(output_vector_buff)
                        if debug >= 3:
                            print("file for fusion : " + output_vector_buff)
                    else:
                        print(bold + yellow +
                              "pas de fichiers avec ce nom : " + endC +
                              output_vector_buff)

                else:
                    print(cyan + "addDataBaseExo() : " + bold + yellow +
                          "Pas de fichier du nom : " + endC + input_vector)

            # 3.4 : Fusionner les shapes transformés d'une même classe, rasterization et labelisations des vecteurs
            # Si une liste de fichier shape existe
            if not vector_fusion_list:
                print(bold + yellow + "Pas de fusion sans donnee a fusionnee" +
                      endC)
            else:
                # Rasterization et BandMath des fichiers shapes
                raster_list = []
                for vector in vector_fusion_list:
                    if debug >= 3:
                        print(cyan + "addDataBaseExo() : " + endC +
                              "Rasterization : " + vector + " label : " +
                              macroclass_label)
                    raster_output = os.path.splitext(
                        vector)[0] + extension_raster

                    # Rasterisation
                    rasterizeBinaryVector(vector, image_input, raster_output,
                                          macroclass_label, CODAGE)
                    raster_list.append(raster_output)

                if debug >= 3:
                    print(cyan + "addDataBaseExo() : " + endC +
                          "nombre d'images a combiner : " +
                          str(len(raster_list)))

                # Liste les images raster combined and sample
                image_combined = repertory_output + os.sep + image_name + '_' + str(
                    macroclass_label) + SUFFIX_FUSION + extension_raster
                image_combined_list.append(image_combined)

                # Fusion des images raster en une seule
                mergeListRaster(raster_list, image_combined, CODAGE)

        if debug >= 2:
            print(cyan + "addDataBaseExo() : " + bold + green +
                  "FIN DE L AFFECTATION DES TAMPONS" + endC)

        # ETAPE 4 : ASSEMBLAGE DE L'IMAGE CLASSEE ET DES BD EXOS
        if debug >= 2:
            print(cyan + "addDataBaseExo() : " + bold + green +
                  "ASSEMBLAGE..." + endC)

        # Ajout de l'image de classification a la liste des image bd conbinées
        image_combined_list.append(image_input)
        # Fusion les images avec la classification
        mergeListRaster(image_combined_list, image_classif_add_output, CODAGE)
        if debug >= 2:
            print(cyan + "addDataBaseExo() : " + bold + green + "FIN" + endC)

    # ETAPE 5 : SUPPRESIONS FICHIERS INTERMEDIAIRES INUTILES

    # Suppression des données intermédiaires
    if not save_results_intermediate:

        image_combined_list.remove(image_input)
        for to_delete in image_combined_list:
            removeFile(to_delete)

        # Suppression des repertoires temporaires
        deleteDir(repertory_mask_temp)
        deleteDir(repertory_samples_filtering_temp)
        deleteDir(repertory_samples_cutting_temp)
        deleteDir(repertory_samples_buff_temp)

    # Mise à jour du Log
    ending_event = "addDataBaseExo() : Add data base exogene to classification ending : "
    timeLine(path_time_log, ending_event)

    return
def createEmprise(input_dir,
                  output_file,
                  is_not_assembled,
                  is_all_polygons_used,
                  is_not_date,
                  is_optimize_emprise,
                  is_optimize_emprise_nodata,
                  no_data_value,
                  size_erode,
                  path_time_log,
                  separ_name="_",
                  pos_date=1,
                  nb_char_date=8,
                  separ_date="",
                  epsg=2154,
                  format_vector='ESRI Shapefile',
                  extension_raster=".tif",
                  extension_vector=".shp",
                  save_results_intermediate=False,
                  overwrite=True):

    # Affichage des paramètres
    if debug >= 3:
        print(bold + green +
              "Variables dans le createEmprise - Variables générales" + endC)
        print(cyan + "createEmprise() : " + endC + "input_dir : " +
              str(input_dir))
        print(cyan + "createEmprise() : " + endC + "output_file : " +
              str(output_file))
        print(cyan + "createEmprise() : " + endC + "is_not_assembled : " +
              str(is_not_assembled))
        print(cyan + "createEmprise() : " + endC + "is_all_polygons_used : " +
              str(is_all_polygons_used))
        print(cyan + "createEmprise() : " + endC + "is_not_date : " +
              str(is_not_date))
        print(cyan + "createEmprise() : " + endC + "is_optimize_emprise : " +
              str(is_optimize_emprise))
        print(cyan + "createEmprise() : " + endC +
              "is_optimize_emprise_nodata : " +
              str(is_optimize_emprise_nodata))
        print(cyan + "createEmprise() : " + endC + "no_data_value : " +
              str(no_data_value))
        print(cyan + "createEmprise() : " + endC + "size_erode : " +
              str(size_erode))
        print(cyan + "createEmprise() : " + endC + "path_time_log : " +
              str(path_time_log))
        print(cyan + "createEmprise() : " + endC + "separ_name : " +
              str(separ_name))
        print(cyan + "createEmprise() : " + endC + "pos_date : " +
              str(pos_date))
        print(cyan + "createEmprise() : " + endC + "nb_char_date : " +
              str(nb_char_date))
        print(cyan + "createEmprise() : " + endC + "separ_date : " +
              str(separ_date))
        print(cyan + "createEmprise() : " + endC + "epsg : " + str(epsg))
        print(cyan + "createEmprise() : " + endC + "format_vector : " +
              str(format_vector))
        print(cyan + "createEmprise() : " + endC + "extension_raster : " +
              str(extension_raster) + endC)
        print(cyan + "createEmprise() : " + endC + "extension_vector : " +
              str(extension_vector) + endC)
        print(cyan + "createEmprise() : " + endC +
              "save_results_intermediate : " + str(save_results_intermediate))
        print(cyan + "createEmprise() : " + endC + "overwrite : " +
              str(overwrite))

# Constantes
    EXT_LIST_HDF5 = ['h5', 'H5', 'he5', 'HE5', 'hdf5', 'HDF5']
    EXT_LIST = EXT_LIST_HDF5 + [
        'tif', 'TIF', 'tiff', 'TIFF', 'ecw', 'ECW', 'jp2', 'JP2', 'dim', 'DIM',
        'asc', 'ASC'
    ]
    SUFFIX_DETAILLEE = "_detail"
    SUFFIX_MASK_ZERO = "_mask_zeros"
    SUFFIX_TMP = "_tmp"

    CODAGE_8B = "uint8"
    ATTR_NAME_ID = "Id"
    ATTR_NAME_NOMIMAGE = "NomImage"
    ATTR_NAME_DATEACQUI = "DateAcqui"
    ATTR_NAME_HEUREACQUI = "HeureAcqui"
    ATTR_NAME_REFDOSSIER = "RefDossier"

    # Variables
    points_list = []
    name_image_list = []
    name_rep_list = []
    ref_dossier_list = []
    date_list = []
    heure_list = []
    optimize_emprise_nodata_shape_list = []
    polygons_attr_coord_dico = {}
    pos_date = pos_date - 1

    repertory_output = os.path.dirname(output_file)
    file_name = os.path.splitext(os.path.basename(output_file))[0]
    extension = os.path.splitext(output_file)[1]
    file_vector_detail = repertory_output + os.sep + file_name + SUFFIX_DETAILLEE + extension

    # Si un fichier de sortie avec le même nom existe déjà, et si l'option ecrasement est à false, alors passe au masque suivant
    check = os.path.isfile(output_file)
    if check and not overwrite:
        print(
            bold + yellow + "createEmprise() : " + endC +
            "Le fichier vecteur d'emprise %s existe déjà : pas d'actualisation"
            % (output_file) + 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
                removeVectorFile(output_file)
                removeVectorFile(file_vector_detail)
            except Exception:
                pass  # Si le fichier ne peut pas être supprimé, on suppose qu'il n'existe pas et on passe à la suite

        # Récuperer tous les sous répertoires
        sub_rep_list = getSubRepRecursifList(input_dir)
        sub_rep_list.append(input_dir)

        # Parcours de chaque dossier image du dossier en entrée
        for repertory in sub_rep_list:
            if os.path.isdir(repertory):

                if debug >= 2:
                    print(cyan + "createEmprises() : " + endC + bold + green +
                          "Traitement de : " + endC + repertory)

                # Récupération des images du dossier en entrée
                imagettes_jp2_tif_ecw_list = []
                imagettes_list = os.listdir(repertory)

                for elt1 in imagettes_list:
                    path_image = repertory + os.sep + elt1
                    if (os.path.isfile(path_image)) and (len(
                            elt1.rsplit('.', 1)) == 2) and (elt1.rsplit(
                                '.', 1)[1] in EXT_LIST):
                        if elt1.rsplit('.', 1)[1] in EXT_LIST_HDF5:
                            elt1_new = os.path.splitext(
                                elt1)[0] + extension_raster
                            path_image_new = repertory + os.sep + elt1_new
                            h5ToGtiff(path_image, path_image_new)
                            imagettes_jp2_tif_ecw_list.append(elt1_new)
                        else:
                            imagettes_jp2_tif_ecw_list.append(elt1)

                # Pour le cas ou le repertoire contient des fichiers images
                if not imagettes_jp2_tif_ecw_list == []:

                    # Cas ou chaque emprise d'image est un polygone
                    if is_not_assembled or is_optimize_emprise or is_optimize_emprise_nodata:

                        for imagette in imagettes_jp2_tif_ecw_list:
                            # Récupération des emprises de l'image
                            path_image = repertory + os.sep + imagette
                            path_info_acquisition = repertory
                            xmin, xmax, ymin, ymax = getEmpriseImage(
                                path_image)
                            coord_list = [
                                xmin, ymax, xmax, ymax, xmax, ymin, xmin, ymin,
                                xmin, ymax
                            ]

                            # Saisie des données
                            points_list.append(coord_list)

                            # Récupération du nom de l'image pour la création des champs
                            input_image_name = os.path.splitext(
                                os.path.basename(path_image))[0]
                            name_image_list.append(input_image_name)

                            # Cas optimisation de l'emprise en elevant les nodata
                            if is_optimize_emprise_nodata:

                                path_info_acquisition = path_image
                                optimize_emprise_nodata_shape = repertory_output + os.sep + input_image_name + extension_vector
                                optimize_emprise_tmp1_shape = repertory_output + os.sep + input_image_name + SUFFIX_TMP + str(
                                    1) + extension_vector
                                optimize_emprise_tmp2_shape = repertory_output + os.sep + input_image_name + SUFFIX_TMP + str(
                                    2) + extension_vector
                                optimize_emprise_tmp3_shape = repertory_output + os.sep + input_image_name + SUFFIX_TMP + str(
                                    3) + extension_vector
                                optimize_emprise_tmp4_shape = repertory_output + os.sep + input_image_name + SUFFIX_TMP + str(
                                    4) + extension_vector
                                binary_mask_zeros_raster = repertory_output + os.sep + input_image_name + SUFFIX_MASK_ZERO + extension_raster
                                optimize_emprise_nodata_shape_list.append(
                                    optimize_emprise_nodata_shape)

                                # Création masque binaire pour séparer les no data des vraies valeurs
                                no_data_value_img = getNodataValueImage(
                                    path_image)
                                if no_data_value_img == None:
                                    no_data_value_img = no_data_value
                                createBinaryMaskMultiBand(
                                    path_image, binary_mask_zeros_raster,
                                    no_data_value_img, CODAGE_8B)

                                # Vectorisation du masque binaire true data/false data -> polygone avec uniquement les vraies valeurs
                                if os.path.exists(
                                        optimize_emprise_nodata_shape):
                                    removeVectorFile(
                                        optimize_emprise_nodata_shape)

                                polygonizeRaster(binary_mask_zeros_raster,
                                                 optimize_emprise_tmp1_shape,
                                                 input_image_name,
                                                 ATTR_NAME_ID, format_vector)

                                # Nettoyage des polygones parasites pour ne garder que le polygone pricipale si l'option "all" n'est pas demandée
                                if not is_all_polygons_used:
                                    geometry_list = getGeomPolygons(
                                        optimize_emprise_tmp1_shape, None,
                                        None, format_vector)
                                    geometry_orded_dico = {}
                                    geometry_orded_list = []
                                    for geometry in geometry_list:
                                        area = geometry.GetArea()
                                        geometry_orded_dico[area] = geometry
                                        geometry_orded_list.append(area)
                                    geometry_orded_list.sort()
                                    if len(geometry_orded_list) > 0:
                                        max_area = geometry_orded_list[
                                            len(geometry_orded_list) - 1]
                                        geom_max = geometry_orded_dico[
                                            max_area]
                                        attribute_dico = {
                                            ATTR_NAME_ID: ogr.OFTInteger
                                        }
                                        polygons_attr_geom_dico = {}
                                        polygons_attr_geom_dico[str(1)] = [
                                            geom_max, {
                                                ATTR_NAME_ID: str(1)
                                            }
                                        ]
                                        createPolygonsFromGeometryList(
                                            attribute_dico,
                                            polygons_attr_geom_dico,
                                            optimize_emprise_tmp2_shape, epsg,
                                            format_vector)
                                    else:
                                        print(
                                            cyan + "createEmprise() : " +
                                            bold + yellow +
                                            " Attention!!! Fichier non traite (ne contient pas de polygone): "
                                            + optimize_emprise_tmp1_shape +
                                            endC)
                                        optimize_emprise_tmp2_shape = optimize_emprise_tmp1_shape
                                else:
                                    optimize_emprise_tmp2_shape = optimize_emprise_tmp1_shape

                                # Nettoyage des polygones simplification et supression des trous
                                cleanRingVector(optimize_emprise_tmp2_shape,
                                                optimize_emprise_tmp3_shape,
                                                format_vector)
                                simplifyVector(optimize_emprise_tmp3_shape,
                                               optimize_emprise_tmp4_shape, 2,
                                               format_vector)
                                if size_erode != 0.0:
                                    bufferVector(
                                        optimize_emprise_tmp4_shape,
                                        optimize_emprise_nodata_shape,
                                        size_erode * -1, "", 1.0, 10,
                                        format_vector)
                                else:
                                    copyVectorFile(
                                        optimize_emprise_tmp4_shape,
                                        optimize_emprise_nodata_shape,
                                        format_vector)

                                # Nettoyage des fichier intermediaires
                                if not save_results_intermediate:
                                    removeFile(binary_mask_zeros_raster)
                                    removeVectorFile(
                                        optimize_emprise_tmp1_shape)
                                    removeVectorFile(
                                        optimize_emprise_tmp2_shape)
                                    removeVectorFile(
                                        optimize_emprise_tmp3_shape)
                                    removeVectorFile(
                                        optimize_emprise_tmp4_shape)

                            # Recuperation de la date et l'heure d'acquisition
                            # Gestion de l'emprise optimisé nodata on utilise le nom de l'image pour la date d'acquisition sion c'est le nom du repertoire
                            getDataToFiels(
                                path_info_acquisition, is_not_date,
                                is_optimize_emprise
                                or is_optimize_emprise_nodata, separ_name,
                                pos_date, nb_char_date, separ_date,
                                points_list, ref_dossier_list, name_rep_list,
                                date_list, heure_list)

                    # Cas ou l'on prend l'emprise globale des images un seul plolygone correspondant a l'emprise globale
                    else:

                        # Récupération des emprises des images du dossier
                        liste_x_l = []
                        liste_y_b = []
                        liste_x_r = []
                        liste_y_t = []

                        for imagette in imagettes_jp2_tif_ecw_list:
                            path_image = repertory + os.sep + imagette
                            xmin, xmax, ymin, ymax = getEmpriseImage(
                                path_image)

                            liste_x_l.append(xmin)
                            liste_x_r.append(xmax)
                            liste_y_b.append(ymin)
                            liste_y_t.append(ymax)

                        # Récupération des min et max de la liste des imagettes
                        # Coin haut gauche
                        xmin_l_t = str(min(liste_x_l))

                        # Coin bas gauche
                        ymin_l_b = str(min(liste_y_b))
                        xmin_l_b = xmin_l_t

                        # Coin bas doite
                        xmax_r_b = str(max(liste_x_r))

                        # Coin haut droite
                        ymax_r_t = str(max(liste_y_t))
                        xmax_r_t = xmax_r_b
                        ymax_r_b = ymin_l_b
                        ymin_l_t = ymax_r_t

                        coord_list = [
                            xmin_l_t, ymin_l_t, xmin_l_b, ymin_l_b, xmax_r_b,
                            ymax_r_b, xmax_r_t, ymax_r_t, xmin_l_t, ymin_l_t
                        ]
                        points_list.append(coord_list)

                        # Récupération du nom du répertoire pour création des champs
                        getDataToFiels(repertory, is_not_date,
                                       is_optimize_emprise, separ_name,
                                       pos_date, nb_char_date, separ_date,
                                       points_list, ref_dossier_list,
                                       name_rep_list, date_list, heure_list)

        #  Préparation des attribute_dico et polygons_attr_coord_dico
        if is_not_assembled:
            attribute_dico = {
                ATTR_NAME_ID: ogr.OFTInteger,
                ATTR_NAME_NOMIMAGE: ogr.OFTString,
                ATTR_NAME_DATEACQUI: ogr.OFTDate,
                ATTR_NAME_HEUREACQUI: ogr.OFTString
            }

            for i in range(len(points_list)):
                polygons_attr_coord_dico[str(i)] = [
                    points_list[i], {
                        ATTR_NAME_ID: i + 1,
                        ATTR_NAME_NOMIMAGE: name_image_list[i],
                        ATTR_NAME_DATEACQUI: date_list[i],
                        ATTR_NAME_HEUREACQUI: heure_list[i]
                    }
                ]

        else:
            attribute_dico = {
                ATTR_NAME_NOMIMAGE: ogr.OFTString,
                ATTR_NAME_REFDOSSIER: ogr.OFTString,
                ATTR_NAME_DATEACQUI: ogr.OFTDate,
                ATTR_NAME_HEUREACQUI: ogr.OFTString
            }

            for i in range(len(points_list)):
                polygons_attr_coord_dico[str(i)] = [
                    points_list[i], {
                        ATTR_NAME_NOMIMAGE: name_rep_list[i],
                        ATTR_NAME_REFDOSSIER: ref_dossier_list[i],
                        ATTR_NAME_DATEACQUI: date_list[i],
                        ATTR_NAME_HEUREACQUI: heure_list[i]
                    }
                ]

        # Cas optimisation de l'emprise en elevant les nodata
        colum = ""
        if is_optimize_emprise_nodata:

            if is_not_assembled:
                file_vector = output_file
            else:
                file_vector = file_vector_detail

            # Fusion des polygones d'emprises images optimisées sans nodata
            polygons_attr_geom_dico = {}
            i = 0
            for shape_file in optimize_emprise_nodata_shape_list:
                geom_list = getGeomPolygons(shape_file, ATTR_NAME_ID, 1,
                                            format_vector)
                if not is_all_polygons_used:
                    if geom_list is not None and len(geom_list) > 0:
                        geom = geom_list[0]
                        polygons_attr_geom_dico[str(i)] = [
                            geom, polygons_attr_coord_dico[str(i)][1]
                        ]
                else:
                    j = 1
                    for geom in geom_list:
                        polygons_attr_geom_dico[str(i + 1000000 * j)] = [
                            geom, polygons_attr_coord_dico[str(i)][1]
                        ]
                        j += 1
                i += 1

            createPolygonsFromGeometryList(attribute_dico,
                                           polygons_attr_geom_dico,
                                           file_vector, epsg, format_vector)

            # Suppression des fichiers intermediaires
            if not save_results_intermediate:
                for vector_to_del in optimize_emprise_nodata_shape_list:
                    removeVectorFile(vector_to_del)

        else:
            # Utilisation de createPolygonsFromCoordList()
            if is_optimize_emprise:
                file_vector = file_vector_detail
            else:
                file_vector = output_file

            # Creation des polygones a partir de la liste des coordonnées des emprises
            createPolygonsFromCoordList(attribute_dico,
                                        polygons_attr_coord_dico, file_vector,
                                        epsg, format_vector)

        # Cas fusion des polygones pour avoir une emprise constituée d'un seul polygone
        if not is_not_assembled:
            if is_optimize_emprise or is_optimize_emprise_nodata or is_all_polygons_used:
                column_name = ""
                if is_all_polygons_used:
                    column_name = ATTR_NAME_DATEACQUI
                elif is_optimize_emprise or is_optimize_emprise_nodata:
                    column_name = ATTR_NAME_NOMIMAGE

                # Fusion des polygones
                if is_all_polygons_used and is_not_date:
                    fusionNeighbourPolygonsBySameValue(file_vector,
                                                       output_file,
                                                       column_name,
                                                       format_vector)
                    #dissolveVector(file_vector, output_file, column_name, format_vector)
                else:
                    if not geometries2multigeometries(file_vector, output_file,
                                                      column_name,
                                                      format_vector):
                        copyVectorFile(file_vector, output_file, format_vector)

                # Suppression des fichiers intermediaires
                if not save_results_intermediate:
                    removeVectorFile(file_vector_detail)

    return
def sobelToOuvrages(input_im_seuils_dico, output_dir, input_cut_vector, no_data_value, path_time_log, format_raster='GTiff', format_vector="ESRI Shapefile", extension_raster=".tif", extension_vector=".shp", save_results_intermediate=True, overwrite=True):

    # Constantes
    REPERTORY_TEMP = "temp_sobel"
    CODAGE_8B = "uint8"
    ID = "id"

    # Mise à jour du Log
    starting_event = "sobelToOuvrages() : Select Sobel to ouvrages starting : "
    timeLine(path_time_log,starting_event)

    # Création du répertoire de sortie s'il n'existe pas déjà
    if not os.path.exists(output_dir + os.sep + REPERTORY_TEMP):
        os.makedirs(output_dir + os.sep + REPERTORY_TEMP)

    # Affichage des paramètres
    if debug >= 3:
        print(bold + green + "Variables dans SobelToOuvrages - Variables générales" + endC)
        print(cyan + "sobelToOuvrages() : " + endC + "input_im_seuils_dico : " + str(input_im_seuils_dico) + endC)
        print(cyan + "sobelToOuvrages() : " + endC + "output_dir : " + str(output_dir) + endC)
        print(cyan + "sobelToOuvrages() : " + endC + "input_cut_vector : " + str(input_cut_vector) + endC)
        print(cyan + "sobelToOuvrages() : " + endC + "path_time_log : " + str(path_time_log) + endC)
        print(cyan + "sobelToOuvrages() : " + endC + "format_raster : " + str(format_raster) + endC)
        print(cyan + "sobelToOuvrages() : " + endC + "format_vector : " + str(format_vector) + endC)
        print(cyan + "sobelToOuvrages() : " + endC + "extension_raster : " + str(extension_raster) + endC)
        print(cyan + "sobelToOuvrages() : " + endC + "extension_vector : " + str(extension_vector) + endC)
        print(cyan + "sobelToOuvrages() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC)
        print(cyan + "sobelToOuvrages() : " + endC + "overwrite : " + str(overwrite) + endC)

    sobel_ouvrages_shp_list = []

    for elt in input_im_seuils_dico.split():
        raw_image = elt.split(":")[0]
        sobel_image = elt.split(":")[1].split(",")[0]

        for i in range(1,len(elt.split(":")[1].split(","))):
            seuil = elt.split(":")[1].split(",")[i]

            # Initialisation des noms des fichiers en sortie
            image_name = os.path.splitext(os.path.basename(raw_image))[0]
            sobel_binary_mask = output_dir + os.sep + REPERTORY_TEMP + os.sep + "bin_mask_sobel_" + image_name + "_" + str(seuil) + extension_raster
            sobel_binary_mask_vector_name = "bin_mask_vect_sobel_" + image_name + "_" + str(seuil)
            sobel_binary_mask_vector = output_dir + os.sep + REPERTORY_TEMP + os.sep + sobel_binary_mask_vector_name + extension_vector
            sobel_binary_mask_vector_cleaned = output_dir + os.sep + REPERTORY_TEMP + os.sep + "bin_mask_vect_sobel_cleaned_" + image_name + "_" + str(seuil) + extension_vector
            sobel_decoup = output_dir + os.sep + "sobel_decoup_" + image_name + "_" + str(seuil) + extension_vector

            binary_mask_zeros_name = "b_mask_zeros_vect_" + image_name
            binary_mask_zeros_raster = output_dir + os.sep + REPERTORY_TEMP + os.sep + "b_mask_zeros_" + image_name + extension_raster
            binary_mask_zeros_vector = output_dir + os.sep + REPERTORY_TEMP + os.sep + binary_mask_zeros_name + extension_vector
            binary_mask_zeros_vector_simpl = output_dir + os.sep + REPERTORY_TEMP + os.sep + "b_mask_zeros_vect_simpl_" + image_name + extension_vector
            true_values_buffneg = output_dir + os.sep + REPERTORY_TEMP + os.sep + "true_values_buffneg_" + image_name + extension_vector
            ouvrages_decoup_final = output_dir + os.sep + "ouvrages_sobel_" + image_name + "_" + str(seuil) + extension_vector

            # Création du masque binaire
            createBinaryMask(sobel_image, sobel_binary_mask, float(seuil), True)

            # Découpe du masque binaire par le shapefile de découpe en entrée
            cutImageByVector(input_cut_vector, sobel_binary_mask, sobel_decoup, None, None, no_data_value, 0, format_raster, format_vector)

            # Vectorisation du masque binaire Sobel découpé
            polygonizeRaster(sobel_decoup, sobel_binary_mask_vector, sobel_binary_mask_vector_name)

            # Création masque binaire pour séparer les no data des vraies valeurs
            nodata_value = getNodataValueImage(raw_image)
            if no_data_value == None :
                no_data_value = 0
            createBinaryMaskMultiBand(raw_image, binary_mask_zeros_raster, no_data_value, CODAGE_8B)

            # Vectorisation du masque binaire true data/false data -> polygone avec uniquement les vraies valeurs
            if os.path.exists(binary_mask_zeros_vector):
                removeVectorFile(binary_mask_zeros_vector, format_vector)

            # Polygonisation
            polygonizeRaster(binary_mask_zeros_raster, binary_mask_zeros_vector, binary_mask_zeros_name, ID, format_vector)

            # Simplification du masque obtenu
            simplifyVector(binary_mask_zeros_vector, binary_mask_zeros_vector_simpl, 2, format_vector)

            # Buffer négatif sur ce polygone
            bufferVector(binary_mask_zeros_vector_simpl, true_values_buffneg, -2, "", 1.0, 10, format_vector)
            cleanMiniAreaPolygons(sobel_binary_mask_vector, sobel_binary_mask_vector_cleaned, 15, ID, format_vector)

            # Découpe par le buffer négatif autour des true data
            cutVectorAll(true_values_buffneg, sobel_binary_mask_vector_cleaned, ouvrages_decoup_final, overwrite, format_vector)
            sobel_ouvrages_shp_list.append(ouvrages_decoup_final)

        return sobel_ouvrages_shp_list
Ejemplo n.º 7
0
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
            cols, rows, num_band = getGeometryImage(image_input)
            no_data_value = getNodataValueImage(image_input, num_band)
            if no_data_value == None:
                no_data_value = 0
            createVectorMask(image_input, vector_mask, no_data_value,
                             format_vector)

            # 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