def estimateQualityMns(image_input,
                       vector_cut_input,
                       vector_sample_input_list,
                       vector_sample_points_input,
                       raster_input_dico,
                       vector_output,
                       no_data_value,
                       path_time_log,
                       format_raster='GTiff',
                       epsg=2154,
                       format_vector='ESRI Shapefile',
                       extension_raster=".tif",
                       extension_vector=".shp",
                       save_results_intermediate=False,
                       overwrite=True):

    # Mise à jour du Log
    starting_event = "estimateQualityMns() : Masks creation starting : "
    timeLine(path_time_log, starting_event)

    print(endC)
    print(bold + green + "## START : CREATE HEIGHT POINTS FILE FROM MNS" +
          endC)
    print(endC)

    if debug >= 2:
        print(bold + green +
              "estimateQualityMns() : Variables dans la fonction" + endC)
        print(cyan + "estimateQualityMns() : " + endC + "image_input : " +
              str(image_input) + endC)
        print(cyan + "estimateQualityMns() : " + endC + "vector_cut_input : " +
              str(vector_cut_input) + endC)
        print(cyan + "estimateQualityMns() : " + endC +
              "vector_sample_input_list : " + str(vector_sample_input_list) +
              endC)
        print(cyan + "estimateQualityMns() : " + endC +
              "vector_sample_points_input : " +
              str(vector_sample_points_input) + endC)
        print(cyan + "estimateQualityMns() : " + endC +
              "raster_input_dico : " + str(raster_input_dico) + endC)
        print(cyan + "estimateQualityMns() : " + endC + "vector_output : " +
              str(vector_output) + endC)
        print(cyan + "estimateQualityMns() : " + endC + "no_data_value : " +
              str(no_data_value))
        print(cyan + "estimateQualityMns() : " + endC + "path_time_log : " +
              str(path_time_log) + endC)
        print(cyan + "estimateQualityMns() : " + endC + "epsg  : " +
              str(epsg) + endC)
        print(cyan + "estimateQualityMns() : " + endC + "format_raster : " +
              str(format_raster) + endC)
        print(cyan + "estimateQualityMns() : " + endC + "format_vector : " +
              str(format_vector) + endC)
        print(cyan + "estimateQualityMns() : " + endC + "extension_raster : " +
              str(extension_raster) + endC)
        print(cyan + "estimateQualityMns() : " + endC + "extension_vector : " +
              str(extension_vector) + endC)
        print(cyan + "estimateQualityMns() : " + endC +
              "save_results_intermediate : " + str(save_results_intermediate) +
              endC)
        print(cyan + "estimateQualityMns() : " + endC + "overwrite : " +
              str(overwrite) + endC)

    # Définion des constantes
    EXT_DBF = '.dbf'
    EXT_CSV = '.csv'

    CODAGE = "uint16"

    SUFFIX_STUDY = '_study'
    SUFFIX_CUT = '_cut'
    SUFFIX_TEMP = '_temp'
    SUFFIX_CLEAN = '_clean'
    SUFFIX_SAMPLE = '_sample'

    ATTRIBUTE_ID = "ID"
    ATTRIBUTE_Z_INI = "Z_INI"
    ATTRIBUTE_Z_FIN = "Z_FIN"
    ATTRIBUTE_PREC_ALTI = "PREC_ALTI"
    ATTRIBUTE_Z_REF = "Z_Ref"
    ATTRIBUTE_Z_MNS = "Z_Mns"
    ATTRIBUTE_Z_DELTA = "Z_Delta"

    ERODE_EDGE_POINTS = -1.0

    ERROR_VALUE = -99.0
    ERROR_MIN_VALUE = -9999
    ERROR_MAX_VALUE = 9999

    # ETAPE 0 : PREPARATION DES FICHIERS INTERMEDIAIRES

    # Si le fichier de sortie existe on ecrase
    check = os.path.isfile(vector_output)
    if check and not overwrite:  # Si un fichier de sortie avec le même nom existe déjà, et si l'option ecrasement est à false, alors FIN
        print(cyan + "estimateQualityMns() : " + bold + yellow +
              "Create  file %s already exist : no actualisation" %
              (vector_output) + endC)
        return

    if os.path.isfile(os.path.splitext(vector_output)[0] + EXT_CSV):
        removeFile(os.path.splitext(vector_output)[0] + EXT_CSV)

    repertory_output = os.path.dirname(vector_output)
    base_name = os.path.splitext(os.path.basename(vector_output))[0]

    vector_output_temp = repertory_output + os.sep + base_name + SUFFIX_TEMP + extension_vector
    raster_study = repertory_output + os.sep + base_name + SUFFIX_STUDY + extension_raster
    vector_study = repertory_output + os.sep + base_name + SUFFIX_STUDY + extension_vector
    vector_study_clean = repertory_output + os.sep + base_name + SUFFIX_STUDY + SUFFIX_CLEAN + extension_vector
    image_cut = repertory_output + os.sep + base_name + SUFFIX_CUT + extension_raster
    vector_sample_temp = repertory_output + os.sep + base_name + SUFFIX_SAMPLE + SUFFIX_TEMP + extension_vector
    vector_sample_temp_clean = repertory_output + os.sep + base_name + SUFFIX_SAMPLE + SUFFIX_TEMP + SUFFIX_CLEAN + extension_vector

    # Utilisation des données raster externes
    raster_cut_dico = {}
    for raster_input in raster_input_dico:
        base_name_raster = os.path.splitext(os.path.basename(raster_input))[0]
        raster_cut = repertory_output + os.sep + base_name_raster + SUFFIX_CUT + extension_raster
        raster_cut_dico[raster_input] = raster_cut
        if os.path.exists(raster_cut):
            removeFile(raster_cut)

    # ETAPE 1 : DEFINIR UN SHAPE ZONE D'ETUDE

    if (not vector_cut_input is None) and (vector_cut_input != "") and (
            os.path.isfile(vector_cut_input)):
        cutting_action = True
        vector_study = vector_cut_input

    else:
        cutting_action = False
        createVectorMask(image_input, vector_study)

    # ETAPE 2 : DECOUPAGE DU RASTEUR PAR LE VECTEUR D'ETUDE SI BESOIN ET REECHANTILLONAGE SI BESOIN

    if cutting_action:
        # Identification de la tailles de pixels en x et en y du fichier MNS de reference
        pixel_size_x, pixel_size_y = getPixelWidthXYImage(image_input)

        # Si le fichier de sortie existe deja le supprimer
        if os.path.exists(image_cut):
            removeFile(image_cut)

        # Commande de découpe
        if not cutImageByVector(vector_study, image_input, image_cut,
                                pixel_size_x, pixel_size_y, no_data_value, 0,
                                format_raster, format_vector):
            print(
                cyan + "estimateQualityMns() : " + bold + red +
                "Une erreur c'est produite au cours du decoupage de l'image : "
                + image_input + endC,
                file=sys.stderr)
            raise

        if debug >= 2:
            print(cyan + "estimateQualityMns() : " + bold + green +
                  "DECOUPAGE DU RASTER %s AVEC LE VECTEUR %s" %
                  (image_input, vector_study) + endC)
    else:
        image_cut = image_input

    # Definir l'emprise du fichier MNS de reference

    # Decoupage de chaque raster de la liste des rasters
    for raster_input in raster_input_dico:
        raster_cut = raster_cut_dico[raster_input]
        if not cutImageByVector(vector_study, raster_input, raster_cut,
                                pixel_size_x, pixel_size_y, no_data_value, 0,
                                format_raster, format_vector):
            raise NameError(
                cyan + "estimateQualityMns() : " + bold + red +
                "Une erreur c'est produite au cours du decoupage du raster : "
                + raster_input + endC)

    # Gémotrie de l'image
    pixel_size_x, pixel_size_y = getPixelWidthXYImage(image_cut)
    cols, rows, bands = getGeometryImage(image_cut)
    xmin, xmax, ymin, ymax = getEmpriseImage(image_cut)

    if debug >= 3:
        print("Geometrie Image : ")
        print("  cols = " + str(cols))
        print("  rows = " + str(rows))
        print("  xmin = " + str(xmin))
        print("  xmax = " + str(xmax))
        print("  ymin = " + str(ymin))
        print("  ymax = " + str(ymax))
        print("  pixel_size_x = " + str(pixel_size_x))
        print("  pixel_size_y = " + str(pixel_size_y))
        print("\n")

    # Création du dico coordonnées des points en systeme cartographique
    points_random_value_dico = {}
    # liste coordonnées des points au format matrice image brute
    points_coordonnees_image_list = []

    # Selon que l'on utilise le fichier de points d'echantillons ou que l'on recréé a partir des sommets des vecteurs lignes
    if (vector_sample_points_input is None) or (vector_sample_points_input
                                                == ""):

        # ETAPE 3 : DECOUPAGES DES VECTEURS DE REFERENCE D'ENTREE PAR LE VECTEUR D'ETUDE ET LEUR FUSION ET
        #           LECTURE D'UN VECTEUR DE LIGNES ET SAUVEGARDE DES COORDONNEES POINTS DES EXTREMITEES ET LEUR HAUTEUR

        # Découpage des vecteurs de bd réference avec le vecteur zone d'étude
        vector_sample_input_cut_list = []
        for vector_sample in vector_sample_input_list:
            vector_name = os.path.splitext(os.path.basename(vector_sample))[0]
            vector_sample_cut = repertory_output + os.sep + vector_name + SUFFIX_CUT + extension_vector
            vector_sample_input_cut_list.append(vector_sample_cut)
        cutoutVectors(vector_study, vector_sample_input_list,
                      vector_sample_input_cut_list, format_vector)

        # Fusion des vecteurs de bd réference découpés
        fusionVectors(vector_sample_input_cut_list, vector_sample_temp,
                      format_vector)

        # Preparation des colonnes
        names_column_start_point_list = [
            ATTRIBUTE_ID, ATTRIBUTE_Z_INI, ATTRIBUTE_PREC_ALTI
        ]
        names_column_end_point_list = [
            ATTRIBUTE_ID, ATTRIBUTE_Z_FIN, ATTRIBUTE_PREC_ALTI
        ]
        fields_list = [
            ATTRIBUTE_ID, ATTRIBUTE_PREC_ALTI, ATTRIBUTE_Z_INI, ATTRIBUTE_Z_FIN
        ]

        multigeometries2geometries(vector_sample_temp,
                                   vector_sample_temp_clean, fields_list,
                                   "MULTILINESTRING", format_vector)
        points_coordinates_dico = readVectorFileLinesExtractTeminalsPoints(
            vector_sample_temp_clean, names_column_start_point_list,
            names_column_end_point_list, format_vector)

    else:
        # ETAPE 3_BIS : DECOUPAGE DE VECTEURS D'ECHANTILLONS POINTS PAR LE VECTEUR D'EMPRISE ET
        #               LECTURE DES COORDONNES D'ECHANTILLONS DURECTEMENT DANS LE FICHIER VECTEUR POINTS

        # Liste coordonnées des points au format matrice image brute
        cutVectorAll(vector_study, vector_sample_points_input,
                     vector_sample_temp, format_vector)
        points_coordinates_dico = readVectorFilePoints(vector_sample_temp,
                                                       format_vector)

    # ETAPE 4 : PREPARATION DU VECTEUR DE POINTS

    for index_key in points_coordinates_dico:
        # Recuperer les valeurs des coordonnees
        coord_info_list = points_coordinates_dico[index_key]
        coor_x = coord_info_list[0]
        coor_y = coord_info_list[1]
        attribut_dico = coord_info_list[2]

        # Coordonnées des points au format matrice image
        pos_x = int(round((coor_x - xmin) / abs(pixel_size_x)) - 1)
        pos_y = int(round((ymax - coor_y) / abs(pixel_size_y)) - 1)

        if pos_x < 0:
            pos_x = 0
        if pos_x >= cols:
            pos_x = cols - 1
        if pos_y < 0:
            pos_y = 0
        if pos_y >= rows:
            pos_y = rows - 1

        coordonnees_list = [pos_x, pos_y]
        points_coordonnees_image_list.append(coordonnees_list)

        value_ref = 0.0
        if ATTRIBUTE_Z_INI in attribut_dico.keys():
            value_ref = float(attribut_dico[ATTRIBUTE_Z_INI])
        if ATTRIBUTE_Z_FIN in attribut_dico.keys():
            value_ref = float(attribut_dico[ATTRIBUTE_Z_FIN])

        precision_alti = 0.0
        if ATTRIBUTE_PREC_ALTI in attribut_dico.keys():
            precision_alti = float(attribut_dico[ATTRIBUTE_PREC_ALTI])

        point_attr_dico = {
            ATTRIBUTE_ID: index_key,
            ATTRIBUTE_Z_REF: value_ref,
            ATTRIBUTE_PREC_ALTI: precision_alti,
            ATTRIBUTE_Z_MNS: 0.0,
            ATTRIBUTE_Z_DELTA: 0.0
        }

        for raster_input in raster_input_dico:
            field_name = raster_input_dico[raster_input][0][0]
            point_attr_dico[field_name] = 0.0

        points_random_value_dico[index_key] = [[coor_x, coor_y],
                                               point_attr_dico]

    # ETAPE 5 : LECTURE DES DONNEES DE HAUTEURS ISSU DU MNS et autre raster

    # Lecture dans le fichier raster des valeurs
    values_height_list = getPixelsValueListImage(
        image_cut, points_coordonnees_image_list)
    values_others_dico = {}
    for raster_input in raster_input_dico:
        raster_cut = raster_cut_dico[raster_input]
        values_list = getPixelsValueListImage(raster_cut,
                                              points_coordonnees_image_list)
        values_others_dico[raster_input] = values_list

    for i in range(len(points_random_value_dico)):
        value_mns = values_height_list[i]
        value_ref = points_random_value_dico[i][1][ATTRIBUTE_Z_REF]

        points_random_value_dico[i][1][ATTRIBUTE_Z_MNS] = float(value_mns)
        precision_alti = points_random_value_dico[i][1][ATTRIBUTE_PREC_ALTI]
        points_random_value_dico[i][1][ATTRIBUTE_PREC_ALTI] = float(
            precision_alti)
        value_diff = value_ref - value_mns
        points_random_value_dico[i][1][ATTRIBUTE_Z_DELTA] = float(value_diff)

        for raster_input in raster_input_dico:
            field_name = raster_input_dico[raster_input][0][0]
            value_other = values_others_dico[raster_input][i]
            points_random_value_dico[i][1][field_name] = float(value_other)

    # ETAPE 6 : CREATION D'UN VECTEUR DE POINTS AVEC DONNEE COORDONNES POINT ET HAUTEUR REFERENCE ET MNS

    # Suppression des points contenant des valeurs en erreur et en dehors du filtrage
    points_random_value_dico_clean = {}
    for i in range(len(points_random_value_dico)):
        value_ref = points_random_value_dico[i][1][ATTRIBUTE_Z_REF]
        if value_ref != ERROR_VALUE and value_ref > ERROR_MIN_VALUE and value_ref < ERROR_MAX_VALUE:

            points_is_valid = True
            for raster_input in raster_input_dico:
                if len(raster_input_dico[raster_input]) > 1 and len(
                        raster_input_dico[raster_input][1]) > 1:
                    threshold_min = float(
                        raster_input_dico[raster_input][1][0])
                    threshold_max = float(
                        raster_input_dico[raster_input][1][1])
                    field_name = raster_input_dico[raster_input][0][0]
                    value_raster = float(
                        points_random_value_dico[i][1][field_name])
                    if value_raster < threshold_min or value_raster > threshold_max:
                        points_is_valid = False

            if points_is_valid:
                points_random_value_dico_clean[i] = points_random_value_dico[i]

    # Définir les attibuts du fichier résultat
    attribute_dico = {
        ATTRIBUTE_ID: ogr.OFTInteger,
        ATTRIBUTE_PREC_ALTI: ogr.OFTReal,
        ATTRIBUTE_Z_REF: ogr.OFTReal,
        ATTRIBUTE_Z_MNS: ogr.OFTReal,
        ATTRIBUTE_Z_DELTA: ogr.OFTReal
    }

    for raster_input in raster_input_dico:
        field_name = raster_input_dico[raster_input][0][0]
        attribute_dico[field_name] = ogr.OFTReal

    createPointsFromCoordList(attribute_dico, points_random_value_dico_clean,
                              vector_output_temp, epsg, format_vector)

    # Suppression des points en bord de zone d'étude
    bufferVector(vector_study, vector_study_clean, ERODE_EDGE_POINTS, "", 1.0,
                 10, format_vector)
    cutVectorAll(vector_study_clean, vector_output_temp, vector_output, True,
                 format_vector)

    # ETAPE 7 : TRANSFORMATION DU FICHIER .DBF EN .CSV
    dbf_file = repertory_output + os.sep + base_name + EXT_DBF
    csv_file = repertory_output + os.sep + base_name + EXT_CSV

    if debug >= 2:
        print(cyan + "estimateQualityMns() : " + bold + green +
              "Conversion du fichier DBF %s en fichier CSV %s" %
              (dbf_file, csv_file) + endC)

    convertDbf2Csv(dbf_file, csv_file)

    # ETAPE 8 : SUPPRESIONS FICHIERS INTERMEDIAIRES INUTILES

    # Suppression des données intermédiaires
    if not save_results_intermediate:
        if cutting_action:
            if os.path.isfile(image_cut):
                removeFile(image_cut)
        else:
            if os.path.isfile(vector_study):
                removeVectorFile(vector_study)

        for raster_input in raster_input_dico:
            raster_cut = raster_cut_dico[raster_input]
            if os.path.isfile(raster_cut):
                removeFile(raster_cut)

        if os.path.isfile(vector_output_temp):
            removeVectorFile(vector_output_temp)

        if os.path.isfile(vector_study_clean):
            removeVectorFile(vector_study_clean)

        if os.path.isfile(vector_sample_temp):
            removeVectorFile(vector_sample_temp)

        if os.path.isfile(vector_sample_temp_clean):
            removeVectorFile(vector_sample_temp_clean)

        for vector_file in vector_sample_input_cut_list:
            if os.path.isfile(vector_file):
                removeVectorFile(vector_file)

    print(bold + green + "## END : CREATE HEIGHT POINTS FILE FROM MNSE" + endC)

    # Mise à jour du Log
    ending_event = "estimateQualityMns() : Masks creation ending : "
    timeLine(path_time_log, ending_event)

    return
def vectorsPreparation(emprise_file, classif_input, grid_input, built_input_list, roads_input_list, grid_output, grid_output_cleaned, built_output, roads_output, col_code_ua, col_item_ua, epsg, path_time_log, format_vector='ESRI Shapefile', extension_vector=".shp", save_results_intermediate=False, overwrite=True):

    print(bold + yellow + "Début de la préparation des fichiers vecteurs." + endC + "\n")
    timeLine(path_time_log, "Début de la préparation des fichiers vecteurs : ")

    if debug >= 3 :
        print(bold + green + "vectorsPreparation() : Variables dans la fonction" + endC)
        print(cyan + "vectorsPreparation() : " + endC + "emprise_file : " + str(emprise_file) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "classif_input : " + str(classif_input) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "grid_input : " + str(grid_input) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "built_input_list : " + str(built_input_list) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "roads_input_list : " + str(roads_input_list) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "grid_output : " + str(grid_output) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "grid_output_cleaned : " + str(grid_output_cleaned) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "built_output : " + str(built_output) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "roads_output : " + str(roads_output) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "col_code_ua : " + str(col_code_ua) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "col_item_ua : " + str(col_item_ua) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "epsg : " + str(epsg))
        print(cyan + "vectorsPreparation() : " + endC + "path_time_log : " + str(path_time_log) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "format_vector : " + str(format_vector) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "extension_vector : " + str(extension_vector) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "save_results_intermediate : " + str(save_results_intermediate) + endC)
        print(cyan + "vectorsPreparation() : " + endC + "overwrite : " + str(overwrite) + endC)

    FOLDER_TEMP = 'TEMP'
    SUFFIX_VECTOR_REPROJECT = '_reproject'
    SUFFIX_VECTOR_INTERSECT = '_intersect'
    SUFFIX_VECTOR_MERGE = '_merge'
    SUFFIX_VECTOR_SELECT = '_select'

    if not os.path.exists(grid_output) or not os.path.exists(built_output) or not os.path.exists(roads_output) or overwrite:

        ############################################
        ### Préparation générale des traitements ###
        ############################################

        path_grid_temp = os.path.dirname(grid_output) + os.sep + FOLDER_TEMP
        path_built_temp = os.path.dirname(built_output) + os.sep + FOLDER_TEMP
        path_roads_temp = os.path.dirname(roads_output) + os.sep + FOLDER_TEMP

        if os.path.exists(path_grid_temp):
            shutil.rmtree(path_grid_temp)
        if os.path.exists(path_built_temp):
            shutil.rmtree(path_built_temp)
        if os.path.exists(path_roads_temp):
            shutil.rmtree(path_roads_temp)

        if not os.path.exists(path_grid_temp):
            os.mkdir(path_grid_temp)
        if not os.path.exists(path_built_temp):
            os.mkdir(path_built_temp)
        if not os.path.exists(path_roads_temp):
            os.mkdir(path_roads_temp)

        basename_grid = os.path.splitext(os.path.basename(grid_output))[0]
        basename_built = os.path.splitext(os.path.basename(built_output))[0]
        basename_roads = os.path.splitext(os.path.basename(roads_output))[0]

        # Variables pour ajout colonne ID
        field_name = 'ID' # Attention ! Nom fixé en dur dans les scripts indicateurs, pas dans le script final
        field_type = ogr.OFTInteger

        ##############################################
        ### Traitements sur le vecteur Urban Atlas ###
        ##############################################

        if not os.path.exists(grid_output) or overwrite :

            if os.path.exists(grid_output):
                removeVectorFile(grid_output)
            if os.path.exists(grid_output_cleaned):
                removeVectorFile(grid_output_cleaned)

            # MAJ projection
            grid_reproject = path_grid_temp + os.sep + basename_grid + SUFFIX_VECTOR_REPROJECT + extension_vector
            updateProjection(grid_input, grid_reproject, projection=epsg)

            # Découpage du fichier Urban Atlas d'entrée à l'emprise de la zone d'étude
            grid_output_temp = os.path.splitext(grid_output)[0] + "_temp" + extension_vector
            cutVector(emprise_file, grid_reproject, grid_output_temp, overwrite, format_vector)

            # Suppression des très petits polygones qui introduisent des valeurs NaN
            pixel_size = getPixelSizeImage(classif_input)
            min_size_area = pixel_size * 2
            cleanMiniAreaPolygons(grid_output_temp, grid_output, min_size_area, '', format_vector)
            if not save_results_intermediate:
                if os.path.exists(grid_output_temp):
                    removeVectorFile(grid_output_temp, format_vector)

            # Ajout d'un champ ID
            addNewFieldVector(grid_output, field_name, field_type, 0, None, None, format_vector)
            updateIndexVector(grid_output, field_name, format_vector)

            # Suppression des polygones eau et routes (uniquement pour le calcul des indicateurs)
            column = "'%s, %s, %s'" % (field_name, col_code_ua, col_item_ua)
            expression = "%s NOT IN ('12210', '12220', '12230', '50000')" % (col_code_ua)
            ret = filterSelectDataVector(grid_output, grid_output_cleaned, column, expression, format_vector)
            if not ret :
                raise NameError (cyan + "vectorsPreparation : " + bold + red  + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" %(expression) + endC)

        #########################################
        ### Traitements sur les vecteurs bâti ###
        #########################################
        if not os.path.exists(built_output) or overwrite :

            if os.path.exists(built_output):
                removeVectorFile(built_output)

            # MAJ projection
            built_reproject_list=[]
            for built_input in built_input_list:
                built_reproject = path_built_temp + os.sep + os.path.splitext(os.path.basename(built_input))[0] + SUFFIX_VECTOR_REPROJECT + extension_vector
                updateProjection(built_input, built_reproject, projection=epsg)
                built_reproject_list.append(built_reproject)

            # Sélection des entités bâti dans l'emprise de l'étude
            built_intersect_list = []
            for built_reproject in built_reproject_list:
                built_intersect = path_built_temp + os.sep + os.path.splitext(os.path.basename(built_reproject))[0] + SUFFIX_VECTOR_INTERSECT + extension_vector
                intersectVector(emprise_file, built_reproject, built_intersect, format_vector)
                built_intersect_list.append(built_intersect)

            # Fusion des couches bâti de la BD TOPO
            built_merge = path_built_temp + os.sep + basename_built + SUFFIX_VECTOR_MERGE + extension_vector
            built_select = path_built_temp + os.sep + basename_built + SUFFIX_VECTOR_SELECT + extension_vector
            fusionVectors(built_intersect_list, built_merge)

            # Suppression des polygones où la hauteur du bâtiment est à 0
            column = "HAUTEUR"
            expression = "HAUTEUR > 0"
            ret = filterSelectDataVector(built_merge, built_select, column, expression, format_vector)
            if not ret :
                raise NameError (cyan + "vectorsPreparation : " + bold + red  + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" %(expression) + endC)

            # Découpage des bati d'entrée à l'emprise de la zone d'étude
            cutVector(emprise_file, built_select, built_output, overwrite, format_vector)

            # Ajout d'un champ ID
            addNewFieldVector(built_output, field_name, field_type, 0, None, None, format_vector)
            updateIndexVector(built_output, field_name, format_vector)

        ###########################################
        ### Traitements sur les vecteurs routes ###
        ###########################################

        if not os.path.exists(roads_output) or overwrite :

            if os.path.exists(roads_output):
                removeVectorFile(roads_output)

            # MAJ projection
            roads_reproject_list=[]
            for roads_input in roads_input_list:
                roads_reproject = path_roads_temp + os.sep + os.path.splitext(os.path.basename(roads_input))[0] + SUFFIX_VECTOR_REPROJECT + extension_vector
                updateProjection(roads_input, roads_reproject, projection=epsg)
                roads_reproject_list.append(roads_reproject)

            # Sélection des entités routes dans l'emprise de l'étude
            roads_intersect_list = []
            for roads_reproject in roads_reproject_list:
                roads_intersect = path_roads_temp + os.sep + os.path.splitext(os.path.basename(roads_reproject))[0] + SUFFIX_VECTOR_INTERSECT + extension_vector
                intersectVector(emprise_file, roads_reproject, roads_intersect, format_vector)
                roads_intersect_list.append(roads_intersect)

            # Fusion des couches route de la BD TOPO
            roads_merge = path_roads_temp + os.sep + basename_roads + SUFFIX_VECTOR_MERGE + extension_vector
            roads_select = path_roads_temp + os.sep + basename_roads + SUFFIX_VECTOR_SELECT + extension_vector
            fusionVectors(roads_intersect_list, roads_merge)

            # Sélection des entités suivant la nature de la route dans la couche routes de la BD TOPO
            column = "NATURE"
            expression = "NATURE IN ('Autoroute', 'Bretelle', 'Quasi-autoroute', 'Route  1 chausse', 'Route  2 chausses', 'Route a 1 chaussee', 'Route a 2 chaussees', 'Route à 1 chaussée', 'Route à 2 chaussées')"
            ret = filterSelectDataVector (roads_merge, roads_select, column, expression, format_vector)
            if not ret :
                raise NameError (cyan + "vectorsPreparation : " + bold + red  + "Attention problème lors du filtrage des BD vecteurs l'expression SQL %s est incorrecte" %(expression) + endC)

            # Découpage des routes d'entrée à l'emprise de la zone d'étude
            cutVectorAll(emprise_file, roads_select, roads_output, overwrite, format_vector)

            # Ajout d'un champ ID
            addNewFieldVector(roads_output, field_name, field_type, 0, None, None, format_vector)
            updateIndexVector(roads_output, field_name, format_vector)

        ##########################################
        ### Nettoyage des fichiers temporaires ###
        ##########################################

        if not save_results_intermediate:
            if os.path.exists(path_grid_temp):
                shutil.rmtree(path_grid_temp)
            if os.path.exists(path_built_temp):
                shutil.rmtree(path_built_temp)
            if os.path.exists(path_roads_temp):
                shutil.rmtree(path_roads_temp)

    else:
        print(bold + magenta + "La préparation des fichiers vecteurs a déjà eu lieu.\n" + endC)

    print(bold + yellow + "Fin de la préparation des fichiers vecteurs.\n" + endC)
    timeLine(path_time_log, "Fin de la préparation des fichiers vecteurs : ")

    return
def 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
Exemplo n.º 4
0
def buffersOuvrages(input_tdc_shp,
                    output_dir,
                    buf_pos,
                    buf_neg,
                    input_cut_vector,
                    path_time_log,
                    format_vector="ESRI Shapefile",
                    extension_vector=".shp",
                    save_results_intermediate=True,
                    overwrite=True):

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

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

    # Constantes
    REP_TEMP = "temp_buffersOuvrages"

    # Variables
    repertory_temp = output_dir + os.sep + REP_TEMP
    nom_tdc = os.path.splitext(os.path.basename(input_tdc_shp))[0]
    output_vector = output_dir + os.sep + "OuvragesBuffers" + str(
        buf_pos) + str(buf_neg) + "_" + nom_tdc + extension_vector
    tdc_decoup_vector = repertory_temp + os.sep + "decoup_" + nom_tdc + extension_vector
    temp_buf_vector = repertory_temp + os.sep + "buffer" + str(
        buf_pos) + "_" + nom_tdc + extension_vector

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

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

    # Vérification de l'existance du fichier tdc en entrée
    driver = ogr.GetDriverByName(format_vector)
    data_source_tdc = driver.Open(input_tdc_shp, 0)
    if data_source_tdc is None:
        print(cyan + "buffersOuvrages() : " + bold + red +
              "Could not open file : " + str(input_tdc_shp) + endC,
              file=sys.stderr)
        sys.exit(1)

    # Découpe du TDC par le shapefile en entrée pour la suppression des artéfacts
    cutVectorAll(input_cut_vector, input_tdc_shp, tdc_decoup_vector, overwrite,
                 format_vector)

    # Buffer positif puis négatif sur le tdc pour extraire les ouvrages
    bufferVector(input_tdc_shp, temp_buf_vector, str(buf_pos), "", 1.0, 10,
                 format_vector)
    bufferVector(temp_buf_vector, output_vector, buf_neg, "", 1.0, 10,
                 format_vector)

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

    return output_vector
Exemplo n.º 5
0
def vectorsListToOcs(input_text,
                     output_raster,
                     footprint_vector,
                     reference_raster,
                     codage_raster='uint8',
                     epsg=2154,
                     no_data_value=0,
                     format_raster='GTiff',
                     format_vector='ESRI Shapefile',
                     extension_raster='.tif',
                     extension_vector='.shp',
                     path_time_log='',
                     save_results_intermediate=False,
                     overwrite=True):

    if debug >= 3:
        print(
            '\n' + bold + green +
            "OCS raster à partir d'une liste de vecteurs - Variables dans la fonction :"
            + endC)
        print(cyan + "    vectorsListToOcs() : " + endC + "input_text : " +
              str(input_text) + endC)
        print(cyan + "    vectorsListToOcs() : " + endC + "output_raster : " +
              str(output_raster) + endC)
        print(cyan + "    vectorsListToOcs() : " + endC +
              "footprint_vector : " + str(footprint_vector) + endC)
        print(cyan + "    vectorsListToOcs() : " + endC +
              "reference_raster : " + str(reference_raster) + endC)
        print(cyan + "    vectorsListToOcs() : " + endC + "codage_raster : " +
              str(codage_raster) + endC)
        print(cyan + "    vectorsListToOcs() : " + endC + "epsg : " +
              str(epsg) + endC)
        print(cyan + "    vectorsListToOcs() : " + endC + "no_data_value : " +
              str(no_data_value) + endC)
        print(cyan + "    vectorsListToOcs() : " + endC + "format_raster : " +
              str(format_raster) + endC)
        print(cyan + "    vectorsListToOcs() : " + endC + "format_vector : " +
              str(format_vector) + endC)
        print(cyan + "    vectorsListToOcs() : " + endC +
              "extension_raster : " + str(extension_raster) + endC)
        print(cyan + "    vectorsListToOcs() : " + endC +
              "extension_vector : " + str(extension_vector) + endC)
        print(cyan + "    vectorsListToOcs() : " + endC + "path_time_log : " +
              str(path_time_log) + endC)
        print(cyan + "    vectorsListToOcs() : " + endC +
              "save_results_intermediate : " + str(save_results_intermediate) +
              endC)
        print(cyan + "    vectorsListToOcs() : " + endC + "overwrite : " +
              str(overwrite) + endC + '\n')

    # Définition des constantes
    SUFFIX_TEMP = '_temp'
    SUFFIX_CUT = '_cut'
    SUFFIX_FILTER = '_filter'
    SUFFIX_BUFFER = '_buffer'
    TEXT_SEPARATOR = ':'

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

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

    # Définition des variables 'basename'
    output_raster_basename = os.path.basename(
        os.path.splitext(output_raster)[0])
    output_raster_dirname = os.path.dirname(output_raster)

    # Définition des variables temp
    temp_directory = output_raster_dirname + os.sep + output_raster_basename + SUFFIX_TEMP
    temp_raster = temp_directory + os.sep + output_raster_basename + SUFFIX_TEMP + extension_raster

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

    # Test de l'emprise des fichiers vecteur d'emprise et raster de référence (le raster doit être de même taille ou plus grand que le vecteur)
    xmin_fpt, xmax_fpt, ymin_fpt, ymax_fpt = getEmpriseFile(
        footprint_vector, format_vector=format_vector)
    xmin_ref, xmax_ref, ymin_ref, ymax_ref = getEmpriseImage(reference_raster)
    if round(xmin_fpt, 4) < round(xmin_ref, 4) or round(xmax_fpt, 4) > round(
            xmax_ref, 4) or round(ymin_fpt, 4) < round(ymin_ref, 4) or round(
                ymax_fpt, 4) > round(ymax_ref, 4):
        print(cyan + "vectorsListToOcs() : " + bold + red +
              "xmin_fpt, xmax_fpt, ymin_fpt, ymax_fpt" + endC,
              xmin_fpt,
              xmax_fpt,
              ymin_fpt,
              ymax_fpt,
              file=sys.stderr)
        print(cyan + "vectorsListToOcs() : " + bold + red +
              "xmin_ref, xmax_ref, ymin_ref, ymax_ref" + endC,
              xmin_ref,
              xmax_ref,
              ymin_ref,
              ymax_ref,
              file=sys.stderr)
        raise NameError(
            cyan + "vectorsListToOcs() : " + bold + red +
            "The extend of the footprint vector (%s) is greater than the reference raster (%s)."
            % (footprint_vector, reference_raster) + endC)

    # Récupération des traitements à faire dans le fichier texte d'entrée
    text_list = readTextFileBySeparator(input_text, TEXT_SEPARATOR)

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

    print(cyan + "vectorsListToOcs() : " + bold + green +
          "Début de la génération de l'OCS raster à partir de vecteurs." +
          endC + '\n')

    # Boucle sur les traitements à réaliser
    for text in text_list:
        idx = text_list.index(text) + 1
        class_label = int(text[0])
        vector_file = text[1]
        if debug >= 3:
            print(cyan + "vectorsListToOcs() : " + endC + bold +
                  "Génération %s/%s : " % (idx, len(text_list)) + endC +
                  "traitement du fichier %s (label %s)." %
                  (vector_file, str(class_label)) + '\n')

        # Gestion des noms des fichiers temporaires
        vector_file_basename = os.path.basename(
            os.path.splitext(vector_file)[0])
        vector_file_cut = temp_directory + os.sep + vector_file_basename + SUFFIX_CUT + extension_vector
        vector_file_filter = temp_directory + os.sep + vector_file_basename + SUFFIX_FILTER + extension_vector
        vector_file_buffer = temp_directory + os.sep + vector_file_basename + SUFFIX_BUFFER + extension_vector
        vector_file_raster = temp_directory + os.sep + vector_file_basename + extension_raster

        # Gestion des variables de traitement (tampon et filtrage SQL)
        try:
            buffer_len = float(text[2])
        except ValueError:
            buffer_len = text[2]
        except Exception:
            buffer_len = ''
        try:
            sql_filter = text[3]
        except Exception:
            sql_filter = ''

        # Découpage à l'emprise de la zone d'étude
        if debug >= 3:
            print(cyan + "vectorsListToOcs() : " + endC +
                  "Découpage à l'emprise de la zone d'étude." + '\n')
        cutVectorAll(footprint_vector,
                     vector_file,
                     vector_file_cut,
                     overwrite=overwrite,
                     format_vector=format_vector)

        # Filtrage SQL (facultatif)
        if sql_filter != '':
            if debug >= 3:
                print(cyan + "vectorsListToOcs() : " + endC +
                      "Application du filtrage SQL : %s." % sql_filter + '\n')
            attr_names_list = getAttributeNameList(vector_file_cut,
                                                   format_vector=format_vector)
            column = "'"
            for attr_name in attr_names_list:
                column += attr_name + ", "
            column = column[:-2]
            column += "'"
            filterSelectDataVector(vector_file_cut,
                                   vector_file_filter,
                                   column,
                                   sql_filter,
                                   overwrite=overwrite,
                                   format_vector=format_vector)
        else:
            vector_file_filter = vector_file_cut

        # Application d'un tampon (facultatif)
        if buffer_len != '' and buffer_len != 0:
            if debug >= 3:
                print(cyan + "vectorsListToOcs() : " + endC +
                      "Application d'un buffer : %s." % buffer_len + '\n')
            if type(buffer_len) is float:
                bufferVector(vector_file_filter,
                             vector_file_buffer,
                             buffer_len,
                             col_name_buf='',
                             fact_buf=1.0,
                             quadsecs=10,
                             format_vector=format_vector)
            else:
                bufferVector(vector_file_filter,
                             vector_file_buffer,
                             0,
                             col_name_buf=buffer_len,
                             fact_buf=1.0,
                             quadsecs=10,
                             format_vector=format_vector)
        else:
            vector_file_buffer = vector_file_filter

        # Rastérisation du vecteur préparé
        if debug >= 3:
            print(cyan + "vectorsListToOcs() : " + endC +
                  "Rastérisation du vecteur préparé." + '\n')
        rasterizeBinaryVector(vector_file_buffer,
                              reference_raster,
                              vector_file_raster,
                              label=class_label,
                              codage=codage_raster)

        # Ajout de l'information dans le raster de sortie
        if debug >= 3:
            print(cyan + "vectorsListToOcs() : " + endC +
                  "Ajout de l'information dans le raster de sortie." + '\n')
        if idx == 1:
            shutil.copy(vector_file_raster, output_raster)
        else:
            removeFile(temp_raster)
            shutil.copy(output_raster, temp_raster)
            removeFile(output_raster)
            expression = "im1b1!=%s ? im1b1 : im2b1" % no_data_value
            rasterCalculator([temp_raster, vector_file_raster],
                             output_raster,
                             expression,
                             codage=codage_raster)

    print(cyan + "vectorsListToOcs() : " + bold + green +
          "Fin de la génération de l'OCS raster à partir de vecteurs." + endC +
          '\n')

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

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

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

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

    return 0
Exemplo n.º 6
0
def comparareClassificationToReferenceGrid(image_input,
                                           vector_cut_input,
                                           vector_sample_input,
                                           vector_grid_input,
                                           vector_grid_output,
                                           size_grid,
                                           field_value_verif,
                                           no_data_value,
                                           path_time_log,
                                           epsg=2154,
                                           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 = "comparareClassificationToReferenceGrid() : starting : "
    timeLine(path_time_log, starting_event)

    print(endC)
    print(bold + green +
          "## START : COMPARE QUALITY FROM CLASSIF IMAGE BY GRID" + endC)
    print(endC)

    if debug >= 2:
        print(
            bold + green +
            "comparareClassificationToReferenceGrid() : Variables dans la fonction"
            + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "image_input : " + str(image_input) + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "vector_cut_input : " + str(vector_cut_input) + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "vector_sample_input : " + str(vector_sample_input) + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "vector_grid_input : " + str(vector_grid_input) + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "vector_grid_output : " + str(vector_grid_output) + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "size_grid : " + str(size_grid) + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "field_value_verif : " + str(field_value_verif))
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "no_data_value : " + str(no_data_value))
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "path_time_log : " + str(path_time_log) + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "epsg  : " + str(epsg) + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "format_raster : " + str(format_raster) + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "format_vector : " + str(format_vector) + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "extension_raster : " + str(extension_raster) + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "extension_vector : " + str(extension_vector) + endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "save_results_intermediate : " + str(save_results_intermediate) +
              endC)
        print(cyan + "comparareClassificationToReferenceGrid() : " + endC +
              "overwrite : " + str(overwrite) + endC)

    # ETAPE 0 : PREPARATION DES FICHIERS INTERMEDIAIRES'

    CODAGE = "uint16"
    SUFFIX_STUDY = '_study'
    SUFFIX_TEMP = '_temp'
    SUFFIX_FUSION = '_other_fusion'

    NONE_VALUE_QUANTITY = -1.0
    FIELD_VALUE_OTHER = 65535

    FIELD_NAME_ID = "id"
    FIELD_NAME_RATE_BUILD = "rate_build"
    FIELD_NAME_RATE_OTHER = "rate_other"
    FIELD_NAME_SREF_BUILD = "sref_build"
    FIELD_NAME_SCLA_BUILD = "scla_build"
    FIELD_NAME_SREF_OTHER = "sref_other"
    FIELD_NAME_SCLA_OTHER = "scla_other"
    FIELD_NAME_KAPPA = "kappa"
    FIELD_NAME_ACCURACY = "accuracy"

    pixel_size_x, pixel_size_y = getPixelWidthXYImage(image_input)

    repertory_output = os.path.dirname(vector_grid_output)
    base_name = os.path.splitext(os.path.basename(vector_grid_output))[0]

    vector_study = repertory_output + os.sep + base_name + SUFFIX_STUDY + extension_vector
    vector_grid_temp = repertory_output + os.sep + base_name + SUFFIX_TEMP + extension_vector
    image_raster_other_fusion = repertory_output + os.sep + base_name + SUFFIX_FUSION + extension_raster

    # ETAPE 0 : VERIFICATION

    # Verification de la valeur de la nomemclature à verifier
    if field_value_verif >= FIELD_VALUE_OTHER:
        print(
            cyan + "comparareClassificationToReferenceGrid() : " + bold + red +
            "Attention de valeur de nomenclature à vérifier  : " +
            str(field_value_verif) +
            " doit être inferieur à la valeur de fusion des valeur autre arbitraire de : "
            + str(FIELD_VALUE_OTHER) + endC,
            file=sys.stderr)
        sys.exit(1)  #exit with an error code

    # ETAPE 1 : DEFINIR UN SHAPE ZONE D'ETUDE

    if (not vector_cut_input is None) and (vector_cut_input != "") and (
            os.path.isfile(vector_cut_input)):
        cutting_action = True
        vector_study = vector_cut_input
    else:
        cutting_action = False
        createVectorMask(image_input, vector_study)

    # ETAPE 2 : UNIFORMISATION DE LA ZONE OTHER

    # Réalocation des valeurs de classification pour les valeurs autre que le bati
    change_reaff_value_list = []
    reaff_value_list = identifyPixelValues(image_input)
    if field_value_verif in reaff_value_list:
        reaff_value_list.remove(field_value_verif)
    if no_data_value in reaff_value_list:
        reaff_value_list.remove(no_data_value)
    for elem in reaff_value_list:
        change_reaff_value_list.append(FIELD_VALUE_OTHER)
    reallocateClassRaster(image_input, image_raster_other_fusion,
                          reaff_value_list, change_reaff_value_list)

    # ETAPE 3 : CREATION DE LA GRILLE SUR LA ZONE D'ETUDE

    # Définir les attibuts du fichier
    attribute_dico = {
        FIELD_NAME_ID: ogr.OFTInteger,
        FIELD_NAME_RATE_BUILD: ogr.OFTReal,
        FIELD_NAME_RATE_OTHER: ogr.OFTReal,
        FIELD_NAME_SREF_BUILD: ogr.OFTReal,
        FIELD_NAME_SCLA_BUILD: ogr.OFTReal,
        FIELD_NAME_SREF_OTHER: ogr.OFTReal,
        FIELD_NAME_SCLA_OTHER: ogr.OFTReal,
        FIELD_NAME_KAPPA: ogr.OFTReal,
        FIELD_NAME_ACCURACY: ogr.OFTReal
    }
    nb_polygon = 0

    if (not vector_grid_input is None) and (vector_grid_input != "") and (
            os.path.isfile(vector_grid_input)):
        # Utilisation du fichier grille d'entrée

        # Recopie du fichier grille d'entrée vers le fichier grille de sortie
        copyVectorFile(vector_grid_input, vector_grid_output)

        # Ajout des champs au fichier grille de sortie
        for field_name in attribute_dico:
            addNewFieldVector(vector_grid_output, field_name,
                              attribute_dico[field_name], None, None, None,
                              format_vector)

        # Mettre le champs "id" identifiant du carré de l'élément de la grille
        nb_polygon = updateIndexVector(vector_grid_output, FIELD_NAME_ID,
                                       format_vector)

    else:
        # Si il n'existe pas de fichier grille on en créer un avec la valeur de size_grid

        # Creer le fichier grille
        nb_polygon = createGridVector(vector_study, vector_grid_temp,
                                      size_grid, size_grid, attribute_dico,
                                      overwrite, epsg, format_vector)

        # Découper la grille avec le shape zone d'étude
        cutVectorAll(vector_study, vector_grid_temp, vector_grid_output,
                     format_vector)

    # ETAPE 4 : CALCUL DE L'INDICATEUR DE QUALITE POUR CHAQUE CASE DE LA GRILLE

    if debug >= 2:
        print(bold + "nb_polygon = " + endC + str(nb_polygon) + "\n")

    # Pour chaque polygone existant
    sum_rate_quantity_build = 0
    nb_rate_sum = 0
    size_area_pixel = abs(pixel_size_x * pixel_size_y)

    for id_polygon in range(nb_polygon):
        geom_list = getGeomPolygons(vector_grid_output, FIELD_NAME_ID,
                                    id_polygon, format_vector)
        if geom_list is not None and geom_list != []:  # and (id_polygon == 24 or id_polygon == 30):

            if debug >= 1:
                print(cyan + "comparareClassificationToReferenceGrid() : " +
                      bold + green +
                      "Calcul de la matrice pour le polygon n°: " +
                      str(id_polygon) + endC)

            geom = geom_list[0]
            class_ref_list, class_pro_list, rate_quantity_list, kappa, accuracy, matrix = computeQualityIndiceRateQuantity(
                image_raster_other_fusion, vector_sample_input,
                repertory_output, base_name + str(id_polygon), geom, size_grid,
                pixel_size_x, pixel_size_y, field_value_verif,
                FIELD_VALUE_OTHER, no_data_value, epsg, format_raster,
                format_vector, extension_raster, extension_vector, overwrite,
                save_results_intermediate)

            # Si les calculs indicateurs de qualité sont ok
            if debug >= 2:
                print(matrix)
            if matrix != None and matrix != [] and matrix[0] != []:

                # Récuperer la quantité de bati et calcul de la surface de référence et de la surface de classification (carreau entier ou pas!)
                if len(class_ref_list) == 2 and len(
                        class_pro_list
                ) == 2:  # Cas ou l'on a des pixels de build et other (en ref et en prod)
                    rate_quantity_build = rate_quantity_list[0]
                    rate_quantity_other = rate_quantity_list[1]
                    size_area_ref_build = (matrix[0][0] +
                                           matrix[0][1]) * size_area_pixel
                    size_area_classif_build = (matrix[0][0] +
                                               matrix[1][0]) * size_area_pixel
                    size_area_ref_other = (matrix[1][0] +
                                           matrix[1][1]) * size_area_pixel
                    size_area_classif_other = (matrix[0][1] +
                                               matrix[1][1]) * size_area_pixel
                    sum_rate_quantity_build += rate_quantity_build
                    nb_rate_sum += 1

                else:  # Cas ou l'on a uniquement des pixels de build OU uniquement des pixels de other

                    if class_ref_list[
                            0] == field_value_verif:  # Cas ou l'on a uniquement des pixels references build
                        rate_quantity_build = rate_quantity_list[0]
                        rate_quantity_other = NONE_VALUE_QUANTITY
                        size_area_ref_other = 0

                        if len(
                                class_pro_list
                        ) == 2:  # Cas ou l'on a des pixels de prod build et other
                            size_area_ref_build = (
                                matrix[0][0] + matrix[0][1]) * size_area_pixel
                            size_area_classif_build = matrix[0][
                                0] * size_area_pixel
                            size_area_classif_other = matrix[0][
                                1] * size_area_pixel

                        else:
                            size_area_ref_build = matrix[0][0] * size_area_pixel
                            if class_pro_list[
                                    0] == field_value_verif:  # Cas ou l'on a uniquement des pixels prod build
                                size_area_classif_build = matrix[0][
                                    0] * size_area_pixel
                                size_area_classif_other = 0

                            else:  # Cas ou l'on a uniquement des pixels prod other
                                size_area_classif_build = 0
                                size_area_classif_other = matrix[0][
                                    0] * size_area_pixel

                    else:  # Cas ou l'on a uniquement des pixels references other
                        rate_quantity_build = NONE_VALUE_QUANTITY
                        rate_quantity_other = rate_quantity_list[0]
                        size_area_ref_build = 0

                        if len(
                                class_pro_list
                        ) == 2:  # Cas ou l'on a des pixels de prod build et other
                            size_area_ref_other = (
                                matrix[0][0] + matrix[0][1]) * size_area_pixel
                            size_area_classif_build = matrix[0][
                                0] * size_area_pixel
                            size_area_classif_other = matrix[0][
                                1] * size_area_pixel

                        else:
                            size_area_ref_other = matrix[0][0] * size_area_pixel
                            if class_pro_list[
                                    0] == field_value_verif:  # Cas ou l'on a uniquement des pixels prod build
                                size_area_classif_build = matrix[0][
                                    0] * size_area_pixel
                                size_area_classif_other = 0

                            else:  # Cas ou l'on a uniquement des pixels prod other
                                size_area_classif_build = 0
                                size_area_classif_other = matrix[0][
                                    0] * size_area_pixel

                # Mettre à jour ses éléments du carré de la grille
                setAttributeValues(
                    vector_grid_output, FIELD_NAME_ID, id_polygon, {
                        FIELD_NAME_RATE_BUILD: rate_quantity_build,
                        FIELD_NAME_RATE_OTHER: rate_quantity_other,
                        FIELD_NAME_SREF_BUILD: size_area_ref_build,
                        FIELD_NAME_SCLA_BUILD: size_area_classif_build,
                        FIELD_NAME_SREF_OTHER: size_area_ref_other,
                        FIELD_NAME_SCLA_OTHER: size_area_classif_other,
                        FIELD_NAME_KAPPA: kappa,
                        FIELD_NAME_ACCURACY: accuracy
                    }, format_vector)

    # Calcul de la moyenne
    if nb_rate_sum != 0:
        average_quantity_build = sum_rate_quantity_build / nb_rate_sum
    else:
        average_quantity_build = 0
    if debug >= 2:
        print(bold + "nb_polygon_used = " + endC + str(nb_rate_sum))
        print(bold + "average_quantity_build = " + endC +
              str(average_quantity_build) + "\n")

    # ETAPE 5 : SUPPRESIONS FICHIERS INTERMEDIAIRES INUTILES

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

        if not cutting_action:
            if os.path.isfile(vector_study):
                removeVectorFile(vector_study)

        if os.path.isfile(image_raster_other_fusion):
            removeFile(image_raster_other_fusion)

        if os.path.isfile(vector_grid_temp):
            removeVectorFile(vector_grid_temp)

    print(endC)
    print(bold + green +
          "## END : COMPARE QUALITY FROM CLASSIF IMAGE BY GRID" + endC)
    print(endC)

    # Mise à jour du Log
    ending_event = "comparareClassificationToReferenceGrid() :  ending : "
    timeLine(path_time_log, ending_event)

    return average_quantity_build
Exemplo n.º 7
0
def estimateQualityClassification(image_input,
                                  vector_cut_input,
                                  vector_sample_input,
                                  vector_output,
                                  nb_dot,
                                  no_data_value,
                                  column_name_vector,
                                  column_name_ref,
                                  column_name_class,
                                  path_time_log,
                                  epsg=2154,
                                  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 = "estimateQualityClassification() : Masks creation starting : "
    timeLine(path_time_log, starting_event)

    print(endC)
    print(bold + green +
          "## START : CREATE PRINT POINTS FILE FROM CLASSIF IMAGE" + endC)
    print(endC)

    if debug >= 2:
        print(bold + green +
              "estimateQualityClassification() : Variables dans la fonction" +
              endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "image_input : " + str(image_input) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "vector_cut_input : " + str(vector_cut_input) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "vector_sample_input : " + str(vector_sample_input) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "vector_output : " + str(vector_output) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "nb_dot : " + str(nb_dot) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "no_data_value : " + str(no_data_value) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "column_name_vector : " + str(column_name_vector) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "column_name_ref : " + str(column_name_ref) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "column_name_class : " + str(column_name_class) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "path_time_log : " + str(path_time_log) + endC)
        print(cyan + "estimateQualityClassification() : " + endC + "epsg  : " +
              str(epsg) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "format_raster : " + str(format_raster) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "format_vector : " + str(format_vector) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "extension_raster : " + str(extension_raster) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "extension_vector : " + str(extension_vector) + endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "save_results_intermediate : " + str(save_results_intermediate) +
              endC)
        print(cyan + "estimateQualityClassification() : " + endC +
              "overwrite : " + str(overwrite) + endC)

    # ETAPE 0 : PREPARATION DES FICHIERS INTERMEDIAIRES

    CODAGE = "uint16"

    SUFFIX_STUDY = '_study'
    SUFFIX_CUT = '_cut'
    SUFFIX_TEMP = '_temp'
    SUFFIX_SAMPLE = '_sample'

    repertory_output = os.path.dirname(vector_output)
    base_name = os.path.splitext(os.path.basename(vector_output))[0]

    vector_output_temp = repertory_output + os.sep + base_name + SUFFIX_TEMP + extension_vector
    raster_study = repertory_output + os.sep + base_name + SUFFIX_STUDY + extension_raster
    vector_study = repertory_output + os.sep + base_name + SUFFIX_STUDY + extension_vector
    raster_cut = repertory_output + os.sep + base_name + SUFFIX_CUT + extension_raster
    vector_sample_temp = repertory_output + os.sep + base_name + SUFFIX_SAMPLE + SUFFIX_TEMP + extension_vector

    # Mise à jour des noms de champs
    input_ref_col = ""
    val_ref = 0
    if (column_name_vector != "") and (not column_name_vector is None):
        input_ref_col = column_name_vector
    if (column_name_ref != "") and (not column_name_ref is None):
        val_ref_col = column_name_ref
    if (column_name_class != "") and (not column_name_class is None):
        val_class_col = column_name_class

    # ETAPE 1 : DEFINIR UN SHAPE ZONE D'ETUDE

    if (not vector_cut_input is None) and (vector_cut_input != "") and (
            os.path.isfile(vector_cut_input)):
        cutting_action = True
        vector_study = vector_cut_input

    else:
        cutting_action = False
        createVectorMask(image_input, vector_study)

    # ETAPE 2 : DECOUPAGE DU RASTEUR PAR LE VECTEUR D'EMPRISE SI BESOIN

    if cutting_action:
        # Identification de la tailles de pixels en x et en y
        pixel_size_x, pixel_size_y = getPixelWidthXYImage(image_input)

        # Si le fichier de sortie existe deja le supprimer
        if os.path.exists(raster_cut):
            removeFile(raster_cut)

        # Commande de découpe
        if not cutImageByVector(vector_study, image_input, raster_cut,
                                pixel_size_x, pixel_size_y, no_data_value, 0,
                                format_raster, format_vector):
            raise NameError(
                cyan + "estimateQualityClassification() : " + bold + red +
                "Une erreur c'est produite au cours du decoupage de l'image : "
                + image_input + endC)
        if debug >= 2:
            print(cyan + "estimateQualityClassification() : " + bold + green +
                  "DECOUPAGE DU RASTER %s AVEC LE VECTEUR %s" %
                  (image_input, vector_study) + endC)
    else:
        raster_cut = image_input

    # ETAPE 3 : CREATION DE LISTE POINTS AVEC DONNEE ISSU D'UN FICHIER RASTER

    # Gémotrie de l'image
    cols, rows, bands = getGeometryImage(raster_cut)
    xmin, xmax, ymin, ymax = getEmpriseImage(raster_cut)
    pixel_width, pixel_height = getPixelWidthXYImage(raster_cut)

    if debug >= 2:
        print("cols : " + str(cols))
        print("rows : " + str(rows))
        print("bands : " + str(bands))
        print("xmin : " + str(xmin))
        print("ymin : " + str(ymin))
        print("xmax : " + str(xmax))
        print("ymax : " + str(ymax))
        print("pixel_width : " + str(pixel_width))
        print("pixel_height : " + str(pixel_height))

    # ETAPE 3-1 : CAS CREATION D'UN FICHIER DE POINTS PAR TIRAGE ALEATOIRE DANS LA MATRICE IMAGE
    if (vector_sample_input is None) or (vector_sample_input == ""):
        is_sample_file = False

        # Les dimensions de l'image
        nb_pixels = abs(cols * rows)

        # Tirage aléatoire des points
        drawn_dot_list = []
        while len(drawn_dot_list) < nb_dot:
            val = random.randint(0, nb_pixels)
            if not val in drawn_dot_list:
                drawn_dot_list.append(val)

        # Creation d'un dico index valeur du tirage et attibuts pos_x, pos_y et value pixel
        points_random_value_dico = {}

        points_coordonnees_list = []
        for point in drawn_dot_list:
            pos_y = point // cols
            pos_x = point % cols
            coordonnees_list = [pos_x, pos_y]
            points_coordonnees_list.append(coordonnees_list)

        # Lecture dans le fichier raster des valeurs
        values_list = getPixelsValueListImage(raster_cut,
                                              points_coordonnees_list)
        print(values_list)
        for idx_point in range(len(drawn_dot_list)):
            val_class = values_list[idx_point]
            coordonnees_list = points_coordonnees_list[idx_point]
            pos_x = coordonnees_list[0]
            pos_y = coordonnees_list[1]
            coor_x = xmin + (pos_x * abs(pixel_width))
            coor_y = ymax - (pos_y * abs(pixel_height))
            point_attr_dico = {
                "Ident": idx_point,
                val_ref_col: int(val_ref),
                val_class_col: int(val_class)
            }
            points_random_value_dico[idx_point] = [[coor_x, coor_y],
                                                   point_attr_dico]

            if debug >= 4:
                print("idx_point : " + str(idx_point))
                print("pos_x : " + str(pos_x))
                print("pos_y : " + str(pos_y))
                print("coor_x : " + str(coor_x))
                print("coor_y : " + str(coor_y))
                print("val_class : " + str(val_class))
                print("")

    # ETAPE 3-2 : CAS D'UN FICHIER DE POINTS DEJA EXISTANT MISE A JOUR DE LA DONNEE ISSU Du RASTER
    else:
        # Le fichier de points d'analyses existe
        is_sample_file = True
        cutVectorAll(vector_study, vector_sample_input, vector_sample_temp,
                     format_vector)
        if input_ref_col != "":
            points_coordinates_dico = readVectorFilePoints(
                vector_sample_temp, [input_ref_col], format_vector)
        else:
            points_coordinates_dico = readVectorFilePoints(
                vector_sample_temp, [], format_vector)

        # Création du dico
        points_random_value_dico = {}

        points_coordonnees_list = []
        for index_key in points_coordinates_dico:
            # Recuperer les valeurs des coordonnees
            coord_info_list = points_coordinates_dico[index_key]
            coor_x = coord_info_list[0]
            coor_y = coord_info_list[1]
            pos_x = int(round((coor_x - xmin) / abs(pixel_width)))
            pos_y = int(round((ymax - coor_y) / abs(pixel_height)))
            coordonnees_list = [pos_x, pos_y]
            points_coordonnees_list.append(coordonnees_list)

        # Lecture dans le fichier raster des valeurs
        values_list = getPixelsValueListImage(raster_cut,
                                              points_coordonnees_list)

        for index_key in points_coordinates_dico:
            # Récuperer les valeurs des coordonnees
            coord_info_list = points_coordinates_dico[index_key]
            coor_x = coord_info_list[0]
            coor_y = coord_info_list[1]
            # Récupérer la classe de référence dans le vecteur d'entrée
            if input_ref_col != "":
                label = coord_info_list[2]
                val_ref = label.get(input_ref_col)
            # Récupérer la classe issue du raster d'entrée
            val_class = values_list[index_key]
            # Création du dico contenant identifiant du point, valeur de référence, valeur du raster d'entrée
            point_attr_dico = {
                "Ident": index_key,
                val_ref_col: int(val_ref),
                val_class_col: int(val_class)
            }
            if debug >= 4:
                print("point_attr_dico: " + str(point_attr_dico))
            points_random_value_dico[index_key] = [[coor_x, coor_y],
                                                   point_attr_dico]

    # ETAPE 4 : CREATION ET DECOUPAGE DU FICHIER VECTEUR RESULTAT PAR LE SHAPE D'ETUDE

    # Creer le fichier de points
    if is_sample_file and os.path.exists(vector_sample_temp):

        attribute_dico = {val_class_col: ogr.OFTInteger}
        # Recopie du fichier
        removeVectorFile(vector_output_temp)
        copyVectorFile(vector_sample_temp, vector_output_temp)

        # Ajout des champs au fichier de sortie
        for field_name in attribute_dico:
            addNewFieldVector(vector_output_temp, field_name,
                              attribute_dico[field_name], 0, None, None,
                              format_vector)

        # Préparation des donnees
        field_new_values_list = []
        for index_key in points_random_value_dico:
            point_attr_dico = points_random_value_dico[index_key][1]
            point_attr_dico.pop(val_ref_col, None)
            field_new_values_list.append(point_attr_dico)

        # Ajout des donnees
        setAttributeValuesList(vector_output_temp, field_new_values_list,
                               format_vector)

    else:
        # Définir les attibuts du fichier résultat
        attribute_dico = {
            "Ident": ogr.OFTInteger,
            val_ref_col: ogr.OFTInteger,
            val_class_col: ogr.OFTInteger
        }

        createPointsFromCoordList(attribute_dico, points_random_value_dico,
                                  vector_output_temp, epsg, format_vector)

    # Découpage du fichier de points d'echantillons
    cutVectorAll(vector_study, vector_output_temp, vector_output,
                 format_vector)

    # ETAPE 5 : SUPPRESIONS FICHIERS INTERMEDIAIRES INUTILES

    # Suppression des données intermédiaires
    if not save_results_intermediate:
        if cutting_action:
            removeFile(raster_cut)
        else:
            removeVectorFile(vector_study)
            removeFile(raster_study)
        if is_sample_file:
            removeVectorFile(vector_sample_temp)
        removeVectorFile(vector_output_temp)

    print(endC)
    print(bold + green +
          "## END : CREATE PRINT POINTS FILE FROM CLASSIF IMAGE" + endC)
    print(endC)

    # Mise à jour du Log
    ending_event = "estimateQualityClassification() : Masks creation ending : "
    timeLine(path_time_log, ending_event)

    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