Beispiel #1
0
def export_tfw(path):

    operators_path = join(path, PROJECT_GROUP[2])
    operators_content = get_elements_name(operators_path, True, None)

    for item in operators_content:
        tif_path = join(operators_path, item, 'TIF')
        for infile in glob.glob(join(tif_path, '*.tif')):
            src = gdal.Open(infile)
            xform = src.GetGeoTransform()

            edit1 = xform[0] + xform[1] / 2
            edit2 = xform[3] + xform[5] / 2

            tfw = open(splitext(infile)[0] + '.tfw', 'wt')
            tfw.write("%0.8f\n" % xform[1])
            tfw.write("%0.8f\n" % xform[2])
            tfw.write("%0.8f\n" % xform[4])
            tfw.write("%0.8f\n" % xform[5])
            tfw.write("%0.8f\n" % edit1)
            tfw.write("%0.8f\n" % edit2)
            tfw.close()
        QgsMessageLog.logMessage(
            "Le(s) fichier(s) tfw sont bien enregistré(s) dans {}".format(
                tif_path), 'UnderMap', Qgis.Info)
Beispiel #2
0
def overwrite_layers_merged(project_path):

    root = QgsProject.instance().layerTreeRoot()
    group = root.findGroup(PROJECT_GROUP[2])
    operators_path = join(project_path, PROJECT_GROUP[2])
    operators_content = get_elements_name(operators_path, True, None)
    layers = get_layers_from_folder('SHP_')
    if layers is not None:
        for i_op, item in enumerate(operators_content):
            # load vectors
            shp_path = join(operators_path, item, 'SHP')
            for shp_file in glob.glob(join(shp_path, '*.shp')):
                layer_name = basename(shp_file).replace(".shp", "")
                if '_' not in layer_name:
                    if group is not None:
                        for child in group.children():
                            QgsProject.instance().removeMapLayer(child.layerId())
                    root.removeChildNode(group)
                    if layers[i_op].isValid():
                        QgsVectorFileWriter.deleteShapeFile(shp_file)
                        export_layer_as(layers[i_op], layer_name, "ESRI Shapefile", dirname(shp_file))
                    else:
                        delete_unused_folder(project_path)
        load_unloaded_data(project_path)
    else:
        return
Beispiel #3
0
def initialise_pdf(from_operators):
    """ Initialiser PDF

    :param from_operators: Chemin des données brutes
    :return:
    """
    create_group()
    for i_op, item in enumerate(get_elements_name(from_operators, True, None)):
        operator_dir = join(from_operators, item)
        create_operator(item, operator_dir, i_op)
Beispiel #4
0
def merge_features_connected_layers(project_path):

    operators_path = join(project_path, PROJECT_GROUP[2])
    operators_content = get_elements_name(operators_path, True, None)

    for i_op, item in enumerate(operators_content):
        # load vectors
        shp_path = join(operators_path, item, 'SHP')
        for shp_file in glob.glob(join(shp_path, '*.shp')):
            layer_name = basename(shp_file).replace(".shp", "")
            layer = QgsVectorLayer(shp_file, layer_name, "ogr")
            if layer.isValid() and layer.geometryType() == 1:
                merge_features_connected(layer, shp_file)
Beispiel #5
0
def load_unloaded_data(project_path):

    operators_path = join(project_path, PROJECT_GROUP[2])
    try:
        operators_content = get_elements_name(operators_path, True, None)
    except FileNotFoundError:
        return
    qgis_groups = get_group()
    for i_op, item in enumerate(operators_content):
        # load vectors
        shp_path = join(operators_path, item, 'SHP')
        for shp_file in glob.glob(join(shp_path, '*.shp')):

            layer_name = basename(shp_file).replace(".shp", "")
            layer = QgsVectorLayer(shp_file, layer_name, "ogr")
            if layer.geometryType() == 1:
                try:
                    if layer_name not in get_layers_in_group(PROJECT_GROUP[2]):
                        add_layer_in_group(
                            layer, qgis_groups.findGroup(PROJECT_GROUP[2]),
                            i_op, 'line_style.qml')
                except TypeError:
                    return
            else:
                try:
                    if layer_name not in get_layers_in_group('BUF'):
                        add_layer_in_group(layer, qgis_groups.findGroup('BUF'),
                                           i_op, 'buffer_style.qml')
                except TypeError:
                    return

        # load raster
        tif_path = join(operators_path, item, 'TIF')
        for i_tif, tif_file in enumerate(glob.glob(join(tif_path, '*.tif'))):
            raster_name = basename(tif_file).replace(".tif", "")
            raster = QgsRasterLayer(tif_file, raster_name, 'gdal')
            if raster_name not in get_layers_in_group(item):
                add_layer_in_group(raster, qgis_groups.findGroup(item), i_tif,
                                   None)
Beispiel #6
0
def create_operator(name, pdf, index):
    """ Creation d'un opeérateur

    :param name: Le nom de l'opérateur
    :type name: str

    :param pdf: Tous Le(s) fichier(s) pas seulement pdf associé(s) à un opérateur
    :type pdf: str

    :param index: la position de la couche dans qgis groupe
    """
    root = join(get_project_path(), PROJECT_GROUP[2])
    operator_dir = join(root, name)
    qgis_groups = get_group()
    tif_group = qgis_groups.findGroup(PROJECT_GROUP[3])
    if tif_group is not None:
        if qgis_groups.findGroup(name) is None:
            tif_group.addGroup(name)
    if not exists(root):
        os.makedirs(root)

    operators_content = get_elements_name(root, True, None)
    if not exists(join(root, name)):
        for item_operator_sub_dir in OPERATOR_SUB_DIR:
            os.makedirs(join(operator_dir, item_operator_sub_dir))
            if item_operator_sub_dir == 'SHP':
                layer = create_layer(join(operator_dir, item_operator_sub_dir), name)
                if index is not None:
                    add_layer_in_group(layer, qgis_groups.findGroup(PROJECT_GROUP[2]), index, 'line_style.qml')
                else:
                    add_layer_in_group(layer, qgis_groups.findGroup(PROJECT_GROUP[2]),
                                       len(operators_content), 'line_style.qml')
            elif item_operator_sub_dir == 'PDF':
                for item_sous_pdf in PDF_SUB_DIR:
                    sub_pdf = join(operator_dir, item_operator_sub_dir, item_sous_pdf)
                    os.makedirs(sub_pdf)
                copy_file(pdf, join(operator_dir, item_operator_sub_dir), "")
    else:
        pass
Beispiel #7
0
def manage_buffer(path):

    qgis_groups = get_group()
    operators_path = join(path, PROJECT_GROUP[2])
    operators_content = get_elements_name(operators_path, True, None)
    alg = QgsApplication.processingRegistry().algorithmById(
        "model:Génerer les buffers")

    for index, item in enumerate(operators_content):
        params = {
            'reseau':
            '{}/{}/SHP/{}.shp'.format(operators_path, item, item),
            'qgis:deletecolumn_4:sortie':
            '{}/{}/SHP/{}_BUF.shp'.format(operators_path, item, item)
        }
        result = processing.run(alg, params)
        buf = qgis_groups.findGroup('BUF')
        layer_name = basename(result['qgis:deletecolumn_4:sortie']).replace(
            ".shp", "")
        buf_layer = QgsVectorLayer(result['qgis:deletecolumn_4:sortie'],
                                   layer_name, "ogr")
        if buf is not None:
            add_layer_in_group(buf_layer, buf, index, 'buffer_style.qml')
Beispiel #8
0
def get_layers_from_folder(folder):
    """ Convert les fichies shp en QgsVectorLayer

    :param folder: dossier où se trouve le fichier à convertir
    :type folder: str

    :return: liste de QgsVectorLayer
    :rtype: list
    """

    layers = []
    project_path = get_project_path()
    operators_path = join(project_path, PROJECT_GROUP[2])
    operators_content = get_elements_name(operators_path, True, None)
    for i_op, item in enumerate(operators_content):
        shp_path = join(operators_path, item, folder)
        if exists(shp_path):
            for shp_file in glob.glob(join(shp_path, '*.shp')):
                layer_name = basename(shp_file).replace(".shp", "")
                layer = QgsVectorLayer(shp_file, layer_name, "ogr")
                layers.append(layer)
        else:
            return None
    return layers
def export_report_file(workbook, path):

    operators_path = join(path, PROJECT_GROUP[2])
    operators_content = get_elements_name(operators_path, True, None)

    cell_header = workbook.add_format({
        'bold': 1,
        'border': 1,
        'align': 'center',
        'valign': 'vcenter',
        'fg_color': '#bebebe'})
    cell_rsx_format = workbook.add_format({
        'bold': 1,
        'border': 1,
        'align': 'center',
        'valign': 'vcenter',
    })
    cell_header.set_text_wrap()
    cell_rsx_format.set_text_wrap()


    position = get_number_element_rsx_layers(operators_path, operators_content)

    for i_worksheet, item_worksheet in  enumerate(WORKSHEETS):
        worksheet = workbook.add_worksheet(item_worksheet)
        rsx_layers = [item for item in get_layers_from_folder('SHP') if '_BUF' not in item.name()]
        for i_op, layer in enumerate(rsx_layers):
            item = layer.name()
            field = layer.dataProvider().fieldNameIndex('Reseau')
            values = sorted(layer.uniqueValues(field))

            if i_worksheet == 0:
                create_content(worksheet, 'Synthèse par exploitant', cell_header,
                                          workbook, 8 + i_op + position[i_op], 'E', values, layer)
                if len(values) > 1:
                    worksheet.merge_range('A{}:A{}'.format(8 + i_op + position[i_op] + 1,
                                                           8 + i_op + position[i_op] + len(values)), item,
                                                           cell_rsx_format)
                    worksheet.merge_range('B{}:B{}'.format(8 + i_op + position[i_op] + 1,
                                                       8 + i_op + position[i_op] + len(values)), '',
                                                           cell_rsx_format)
                    worksheet.merge_range('C{}:C{}'.format(8 + i_op + position[i_op] + 1,
                                                       8 + i_op + position[i_op] + len(values)), count_elements_in_to_treat(item)[0],
                                                        cell_rsx_format)
                else:
                    worksheet.write(8 + i_op + position[i_op], 0, item, cell_rsx_format)
                    worksheet.write(8 + i_op + position[i_op], 1, '', cell_rsx_format)
                    worksheet.write(8 + i_op + position[i_op], 2, count_elements_in_to_treat(item)[0], cell_rsx_format)

            if i_worksheet == 1:
                try:
                    worksheet_rsx = workbook.add_worksheet(item)
                except:
                     worksheet_rsx = workbook.add_worksheet(item[0:30])
                last_row = create_content(worksheet_rsx, item, cell_header,
                                          workbook, 8, 'F', values, layer)
                for i_count_file, item_count_file in enumerate(count_elements_in_to_treat(item)[:2]):
                    worksheet_rsx.write(8, i_count_file + 1, item_count_file)
                worksheet_rsx.write_formula(8, 3,
                            '=${}%d+${}%d'.format('B', 'C')%(8 + 1, 8 + 1)
                )
                georeference_report(path, item, last_row + 2, worksheet_rsx, cell_header, workbook)

        if i_worksheet == 1:
            create_content(worksheet, 'Synthèse par réseau', cell_header,
                           workbook, 8, 'B', rsx_color, None)

    workbook.close()
def georeference_report(path, operator_name, row, worksheet, header_format, workbook):
    """

    :param path:
    :param operator_name:
    :param row:
    :param worksheet:
    :param header_format:
    :param workbook:
    :return:
    """
    import numpy as np
    worksheet.merge_range('A{}:A{}'.format(row, row + 1), 'Page', header_format)
    worksheet.merge_range('B{}:B{}'.format(row, row + 1), 'Nombre de points', header_format)
    worksheet.merge_range('C{}:D{}'.format(row, row + 1), 'Methode de calage', header_format)
    worksheet.merge_range('E{0}:F{0}'.format(row), 'Ecart (m)', header_format)
    worksheet.merge_range('G{0}:H{0}'.format(row), 'Ecart (pix)', header_format)
    worksheet.merge_range('I{}:K{}'.format(row, row + 1), 'Remarques', header_format)
    worksheet.merge_range('M{}:O{}'.format(row, row + 1), 'Alertes', header_format)
    ecart_cell = 0

    while ecart_cell < 3:
        for i, item in enumerate(ECART):
            worksheet.write(row, 4 + i + ecart_cell, item, header_format)
        ecart_cell += 2

    operator_path = join(path, PROJECT_GROUP[2], operator_name)
    tif_path = join(operator_path, PROJECT_GROUP[3])
    tif_el = get_elements_name(tif_path, False, '.tif')
    points = get_elements_name(tif_path, False, '.points')
    pdf_root = join(operator_path, OPERATOR_SUB_DIR[0])
    files_path_to_treat = join(pdf_root, PDF_SUB_DIR[0])
    try:
        files_to_treat = get_elements_name(files_path_to_treat, False, '')
    except FileNotFoundError:
        rename(join(pdf_root, 'A-TRAITER'), files_path_to_treat)
        files_to_treat = get_elements_name(files_path_to_treat, False, '')
        if exists(join(pdf_root, 'UTILE')):
            rename(join(pdf_root, 'UTILE'), join(pdf_root, PDF_SUB_DIR[1]))
        if exists(join(pdf_root, 'IGNORE')):
            rmdir(join(pdf_root, 'IGNORE'))

    files_in_tif_path = [item for item in files_to_treat if item.rsplit(".", 1)[0]+'_georef.tif' in tif_el]
    files_not_in_tif = [item for item in files_to_treat if 'xml' not in item and
                        item.rsplit(".", 1)[0]+'_georef.tif' not in tif_el]
    last_row_alerte = len(files_not_in_tif)
    for i_file_treated, item_file_treated in enumerate(files_in_tif_path):
        gcp_file_name = item_file_treated.rsplit(".", 1)[0]+'_georef.tif.points'
        if gcp_file_name in points:
            gcp_file = join(tif_path, gcp_file_name)
            list_of_residual = residual_list(gcp_file)
            if len(residual_list(gcp_file)) < 6:
                worksheet.write(row + 1 + i_file_treated, 0, item_file_treated,
                                customize_cell_format(None, None, "red", workbook))
                worksheet.write(row + 1 + last_row_alerte, 12, "GEOREF : NOMBRE DE POINTS INSUFFISANT",
                                customize_cell_format(None, None, "red", workbook))
                last_row_alerte +=1
            else:
                worksheet.write(row + 1 + i_file_treated, 0, item_file_treated)

            if len(list_of_residual) > 0:
                worksheet.write(row + 1 + i_file_treated, 1, len(residual_list(gcp_file)))
                worksheet.write(row + 1 + i_file_treated, 4, round(
                    sum(list_of_residual)/len(list_of_residual), 2),
                    customize_cell_mean(np.mean(list_of_residual), operator_name, workbook)
                                )
                if max(list_of_residual) > 1:
                    worksheet.write(row + 1 + i_file_treated, 5, round(max(list_of_residual), 2),
                                    customize_cell_format(None, None, "red", workbook))
                else:
                    worksheet.write(row + 1 + i_file_treated, 5, round(max(list_of_residual), 2))
                worksheet.merge_range('C{0}:D{0}'.format(row + 2 + len(files_in_tif_path)),
                                      'Moyenne',
                                      header_format
                                      )
                for i_cell_mean, cell_mean in enumerate(['E','F']):
                    worksheet.write_formula(row + 1+ len(files_in_tif_path),
                                            4+ i_cell_mean,
                                            '=AVERAGE({0}{1}:{0}{2})'.format(cell_mean,
                                                                           row + 2,
                                                                           row+ 1+ len(files_in_tif_path)),
                                            customize_cell_mean(np.mean(list_of_residual), operator_name, workbook)
                                            )
            else:
                return
        else:
           return
    if len(files_not_in_tif) > 0:
        worksheet.write(row + 1 , 12, "CERTAINS PDF N’ONT PAS ETE GEOREFERENCES ")
        for i_pdf_not_treated, item_pdf_not_treated in enumerate(files_not_in_tif):
            worksheet.write(row + 1 + i_pdf_not_treated, 13, item_pdf_not_treated)