diff options
Diffstat (limited to 'ishtar_common/views_item.py')
-rw-r--r-- | ishtar_common/views_item.py | 725 |
1 files changed, 8 insertions, 717 deletions
diff --git a/ishtar_common/views_item.py b/ishtar_common/views_item.py index 5b7c136fd..b4ef9fce6 100644 --- a/ishtar_common/views_item.py +++ b/ishtar_common/views_item.py @@ -30,7 +30,6 @@ import requests # nosec: no user input used import subprocess # nosec from tempfile import NamedTemporaryFile -from osgeo import ogr, osr import shutil from zipfile import ZipFile @@ -92,6 +91,11 @@ from ishtar_common.utils import ( ) from .menus import Menu +from .qfield_functions import (gpkg_creation_sites, + gpkg_creation, + modification_style_sites, + modification_style) + logger = logging.getLogger(__name__) ENCODING = settings.ENCODING or "utf-8" @@ -3249,8 +3253,9 @@ def get_item( continue col_names.append(str(field.verbose_name)) # 2) Gestion of the project to use - if str(model._meta) == 'archaeological_operations.archaeologicalsite': - sites, finds, cr, list_ope, list_crea = gpkg_creation_sites(root, table_cols, col_names, datas) + # Verification of the origin of the query to decide the specific functions and project to use + if str(model._meta) in ['archaeological_operations.archaeologicalsite', 'archaeological_operations.operation']: + sites, finds, cr, list_ope, list_crea = gpkg_creation_sites(model, root, table_cols, col_names, datas) # Modification of the source to access the desired project depending on source of the data source = 'specific' else: @@ -3296,720 +3301,6 @@ def get_item( return func -def gpkg_creation(model, root, table_cols, col_names, datas): - """ - :param model: Table from the query - :param root: Path to the folder to create the geopackage - :param table_cols: List of the columns used in the query - :param col_names: Name of the columns in the new geopackage - :param datas: Data from the query - :function: Creation of the Finds and Context_Records geopackages when the query come from one of these two tables - :return finds: Geopackage for the Finds - :return cr: Geopackage for the Context_Records - :return list_ope: List of the different operations linked to the Finds and Context_Records - :return list_cr: List of the labels/names of the Context_Records used - """ - # Preparation of important values and parameters for the geopackages - finds = '' - cr = '' - list_ope = [] - list_crea = [] - driver = ogr.GetDriverByName('GPKG') - srs = osr.SpatialReference() - srs.ImportFromEPSG(4326) - # I. Case where the extraction come from Finds - if str(model._meta) == 'archaeological_finds.find': - # 1) Creation of the Finds geopackage - finds = os.path.join(root, 'export', 'Finds.gpkg') - # Verification to delete it if already existing - if os.path.exists(finds): - os.remove(finds) - # 2) Creation of the finds layer and its attributes - datasource = driver.CreateDataSource(finds) - layer = datasource.CreateLayer('Finds', srs, ogr.wkbPoint25D) - layer = attributes_creation_finds_query(layer, col_names, table_cols) - # 4a) Populating the finds layer with the datas - list_cr = populating_layer_finds_query(layer,table_cols,col_names,datas) - datasource = None - # 3) Creation of the Context Records file - cr = os.path.join(root, 'export', 'Context_records.gpkg') - # Verification to delete it if already existing - if os.path.exists(cr): - os.remove(cr) - datasource = driver.CreateDataSource(cr) - # 4) Creation of the Context_Records layer and a list of default attributes - layer = datasource.CreateLayer('Context_records', srs, ogr.wkbMultiPolygon) - list_crea = ['Unité_Enregistrement', 'Opération', 'INSEE_Commune', 'Type', 'Interprétation', - 'Description', 'Localisation', 'Media', 'Periode', 'Type_Activité', 'WKT', 'Infos_Parcelle'] - layer = attributes_creation_cr_default(layer, list_crea) - # 5) Populating the Context_Records layer with datas from the Context_Records of the extracted finds - list_ope = populating_layer_cr_default(layer, list_crea, list_cr) - datasource = None - # 6) Preparation of a list of the attributes names for the style modifications - list_crea = ['cr', list_crea] - # II. Case where the extraction come from Context_Recods - elif str(model._meta) == 'archaeological_context_records.contextrecord': - # 1) Creation of the Context Records geopackage - cr = os.path.join(root, 'export', 'Context_records.gpkg') - # Verification to delete it if already existing - if os.path.exists(cr): - os.remove(cr) - datasource = driver.CreateDataSource(cr) - # 2) Creation of the Context_Records layer and its attributes - layer = datasource.CreateLayer('Context_records', srs, ogr.wkbMultiPolygon) - layer = attributes_creation_cr_query(layer, col_names, table_cols) - # 3) Populating the Finds layer with the datas - list_ope, list_cr = populating_layer_cr_query(layer, table_cols, col_names, datas) - datasource = None - # 4) Creation of the Finds geopackage - finds = os.path.join(root, 'export', 'Finds.gpkg') - # Verification to delete it if already existing - if os.path.exists(finds): - os.remove(finds) - datasource = driver.CreateDataSource(finds) - layer = datasource.CreateLayer('Finds', srs, ogr.wkbPoint25D) - list_crea = ['Identifiant', 'UE', 'Date', 'X', 'Y', 'Z', 'Matériaux', 'Description', 'Media', 'WKT_point', 'Infos_Parcelle'] - attributes_creation_finds_default(layer, list_crea) - # 5) Populating the finds layer with the datas - populating_layer_finds_default(layer, list_crea, list_cr) - # 6) Preparation of a list of the attributes names for the style modifications - list_crea = ['finds', list_crea] - return finds, cr, list_ope, list_crea - - -def gpkg_creation_sites(root, table_cols, col_names, datas): - """ - :param root: Path to the folder to create the geopackage - :param table_cols: List of the columns used in the query - :param col_names: Name of the columns in the new geopackage - :param datas: Data from the query - :function: Specific version for the creation of the needed geopackages when the query come from the - Archaeological_Sites table - :return sites: Geopackage for the Sites - :return finds: Geopackage for the Finds - :return cr: Geopackage for the Context_Records - :return list_ope: List of the different operations linked to the Finds and Context_Records - :return list_cr: List of the labels/names of the Context_Records used - """ - # Preparation of important values and parameters for the geopackages - finds = '' - cr = '' - list_ope = [] - list_crea = [] - driver = ogr.GetDriverByName('GPKG') - srs = osr.SpatialReference() - srs.ImportFromEPSG(4326) - # 1) Creation of the sites layer - sites = os.path.join(root, 'export', 'Sites.gpkg') - if os.path.exists(sites): - os.remove(sites) - datasource = driver.CreateDataSource(sites) - layer = datasource.CreateLayer('Sites', srs, ogr.wkbPoint) - # Creation of the attributes - layer = attributes_creation_sites_query(layer, col_names, table_cols) - # Creation of the entities - list_ope, list_cr = populating_layer_sites_query(layer, table_cols, col_names, datas) - # 2) Creation of the Context_Records layer - cr = os.path.join(root, 'export', 'Context_records.gpkg') - # Verification to delete it if already existing - if os.path.exists(cr): - os.remove(cr) - datasource = driver.CreateDataSource(cr) - layer = datasource.CreateLayer('Context_records', srs, ogr.wkbMultiPolygon) - # Creation of the attributes - list_a = ['Unité_Enregistrement', 'Opération', 'INSEE_Commune', 'Type', 'Interprétation', 'Description', - 'Localisation', 'Media', 'Periode', 'Type_Activité', 'WKT', 'Infos_Parcelle'] - layer = attributes_creation_cr_default(layer, list_a) - # # Creation of the entities - populating_layer_cr_default(layer, list_a, list_cr) - datasource = None - # 3) Creation of the finds layer - finds = os.path.join(root, 'export', 'Finds.gpkg') - # Verification to delete it if already existing - if os.path.exists(finds): - os.remove(finds) - datasource = driver.CreateDataSource(finds) - layer = datasource.CreateLayer('Finds', srs, ogr.wkbPoint25D) - # Creation of the attributes - list_b = ['Identifiant', 'UE', 'Date', 'X', 'Y', 'Z', 'Matériaux', 'Description', 'Media', 'WKT_point', 'Infos_Parcelle'] - layer = attributes_creation_finds_default(layer, list_b) - # Creation of the entities - populating_layer_finds_default(layer, list_b, list_cr) - # Recuperation of all created attributes - list_crea = list_a + list_b - return sites, finds, cr, list_ope, list_crea - - -def attributes_creation_finds_query(layer, col_names, table_cols): - """ - :param layer: Finds layer from the linked geopackage - :param col_names: Name of the columns in the new layer - :param table_cols: List of the columns used in the query - :function: Creation of the attributes of the Finds layer with the information from the exporter - :return layer: Finds layer with attributes - """ - # print(table_cols) # debugtest - # print(col_names) # debugtest - # print(datas) # debugtest - # Looping on all the attributes - for idx in range(0, len(col_names)): - # Prevent missing values (case in some .gpkg) - if table_cols[idx] != '': - # print(table_cols[idx]) # debugtest - # print(col_names[idx]) # debugtest - # Gestion of specific formats of attributes - if any(elem in table_cols[idx] for elem in ['index', 'order', 'quantity', 'taq', 'tpq', 'year']): - layer.CreateField(ogr.FieldDefn(col_names[idx], ogr.OFTInteger64)) - elif any(elem in table_cols[idx] for elem in - ['_x', '_y', '_z', 'circumference', 'cost', 'depth', 'diameter', 'height', 'length', 'number', - 'surface', 'side', 'thickness', 'value', 'volume', 'weight', 'width']): - layer.CreateField(ogr.FieldDefn(col_names[idx], ogr.OFTReal)) - elif '_date' in table_cols[idx]: - layer.CreateField(ogr.FieldDefn(col_names[idx], ogr.OFTDate)) - elif '_datetime' in table_cols[idx]: - layer.CreateField(ogr.FieldDefn(col_names[idx], ogr.OFTDateTime)) - elif any(elem in table_cols[idx] for elem in ['large_area_prescription', 'is_complete', 'executed']): - layer.CreateField(ogr.FieldDefn(col_names[idx], ogr.OFTBinary)) - else: - layer.CreateField(ogr.FieldDefn(col_names[idx], ogr.OFTString)) - return layer - - -def populating_layer_finds_query(layer, table_cols, col_names, datas): - """ - :param layer: Finds layer from the linked geopackage with attributes - :param table_cols: List of the columns used in the query - :param col_names: Name of the columns in the new layer - :param datas: Data from the query - :function: Population of the Finds layer using all the data from the query - :return list_cr: List of all the Context_Records linked to the Finds from the query - """ - max = len(col_names) - list_cr = [] - # Looping on all the datas extracted to create features - for data in datas: - # Creation of a new feature - feature = ogr.Feature(layer.GetLayerDefn()) - # Preparations for the geometry - point = '' - # Looping on the attributes to add them to the feature - for idx in range(0, max): - if col_names[idx] != '': - # print(col_names[idx]) # debugtest - # print(data[idx + 1]) # debugtest - # 4) Completion of the attributes - if any(elem == table_cols[idx] for elem in ['_date', '_datetime']): - # Preparations for specific values for the date and date_time - try: - # First version if it has all the data necessary for an ogr.OFTDateTime - # +1 because the first value in the attributes is '' - feature.SetField(col_names[idx], data[idx + 1]) - except: - # Second version if some values are missing - # +1 because the first value in the attributes is '' - feature.SetField(col_names[idx], data[idx + 1].year, data[idx + 1].month, - data[idx + 1].day, 0, 0, 0) - elif 'context_record__label' in table_cols[idx] and data[idx + 1] not in list_cr: - list_cr.append(data[idx + 1]) - feature.SetField(col_names[idx], str(data[idx + 1])) - else: - # +1 because the first value in the attributes is '' - feature.SetField(col_names[idx], str(data[idx + 1])) - # Gestion of the geometry - id_label = table_cols.index('label') - BaseFind = apps.get_model('archaeological_finds', 'BaseFind') - name = data[id_label + 1] - object, __ = BaseFind.objects.get_or_create( - label=name, - ) - try: - point = ogr.Geometry(ogr.wkbPoint25D) - point.AddPoint(float(object.x), float(object.y), float(object.z)) - except: - try: - point = ogr.Geometry(ogr.wkbPoint25D) - point.AddPoint(float(object.x), float(object.y), float(0.0)) - except: - try: - point = ogr.CreateGeometryFromWkt(str(object.main_geodata.point_3d).split(';')[-1]) - except: - point = '' - print(point) - if point != '': - feature.SetGeometry(point) - # Addition of the new feature - layer.CreateFeature(feature) - feature = None - return list_cr - - -def attributes_creation_finds_default(layer, list_crea): - """ - :param layer: Finds layer from the linked geopackage - :param list_crea: Name of the columns by default - :function: Population of the Finds layer using default attributes - :return layer: Finds layer with attributes - """ - # Gestion of specific types of attributes for the default values - for attribute in list_crea: - if attribute == 'Date': - layer.CreateField(ogr.FieldDefn(attribute, ogr.OFTDate)) - elif attribute in ['X', 'Y', 'Z']: - layer.CreateField(ogr.FieldDefn(attribute, ogr.OFTReal)) - else: - layer.CreateField(ogr.FieldDefn(attribute, ogr.OFTString)) - return layer - - -def populating_layer_finds_default(layer, list_crea, list_cr): - """ - :param layer: Finds layer from the linked geopackage with attributes - :param list_crea: Name of the columns by default - :param list_cr: List of all the Context_Records linked to the Finds from the query - :function: Population of the Finds layer using all the data from a specific query - :return layer: Populated Finds layer - """ - ContextRecord = apps.get_model('archaeological_context_records', 'ContextRecord') - BaseFind = apps.get_model('archaeological_finds', 'BaseFind') - for name in list_cr: - cr, __ = ContextRecord.objects.get_or_create(label=name) - finds = list(BaseFind.objects.filter(context_record=cr.id)) - for find in finds: - if str(find.context_record).split(' | ')[-1] in list_cr: - list_attributes = [] - try:list_attributes.append(find.label) - except:list_attributes.append('') - try:list_attributes.append(str(find.context_record).split(' | ')[-1]) - except:list_attributes.append('') - try:list_attributes.append(find.discovery_date) - except:list_attributes.append('') - try:list_attributes.append(find.x) - except:list_attributes.append('') - try:list_attributes.append(find.y) - except:list_attributes.append('') - try:list_attributes.append(find.z) - except:list_attributes.append('') - try:list_attributes.append(find.material_types) - except:list_attributes.append('') - try:list_attributes.append(find.description) - except:list_attributes.append('') - try:list_attributes.append(find.document.image) - except:list_attributes.append('') - try:list_attributes.append(find.main_geodata.point_3d) - except:list_attributes.append('') - try:list_attributes.append(find.parcel.external_id) - except:list_attributes.append('') - # Creation of a new feature - feature = ogr.Feature(layer.GetLayerDefn()) - for idx in range(0, len(list_crea)): - if idx == 2 : - # Gestion of the dates - try: - # First version if it has all the data necessary for an ogr.OFTDateTime - feature.SetField(list_crea[idx], list_attributes[idx]) - except: - # Second version if some values are missing - feature.SetField(list_crea[idx], int(list_attributes[idx].year), - int(list_attributes[idx].month), int(list_attributes[idx].day), 0, 0, 0.0, 0) - elif idx in [3,4,5]: - # Gestion of the coordinates - try: - feature.SetField(list_crea[idx], float(list_attributes[idx])) - except: - pass - else: - feature.SetField(list_crea[idx], str(list_attributes[idx])) - try: - point = ogr.Geometry(ogr.wkbPoint25D) - point.AddPoint(float(find.x), float(find.y), float(find.z)) - except: - try: - point = ogr.Geometry(ogr.wkbPoint25D) - point.AddPoint(float(find.x), float(find.y), float(0.0)) - except: - try: - point = ogr.CreateGeometryFromWkt(str(find.main_geodata.point_3d).split(';')[-1]) - except: - point = '' - if point != '': - feature.SetGeometry(point) - layer.CreateFeature(feature) - feature = None - return layer - - -def attributes_creation_cr_default(layer, list_crea): - """ - :param layer: Context_Records layer from the linked geopackage - :param list_crea: Name of the columns by default - :function: Population of the Context_Records layer using default attributes - :return layer: Populated Context_Records layer - """ - for idx in range(0, len(list_crea)): - layer.CreateField(ogr.FieldDefn(list_crea[idx], ogr.OFTString)) - return layer - - -def populating_layer_cr_default(layer, list_crea, list_cr): - """ - :param layer: Context_Records layer from the linked geopackage with attributes - :param list_crea: Name of the columns by default - :param list_cr: List of all the Context_Records linked to the Finds from the query - :function: Population of the Finds layer using all the data from a specific query - :return list_ope: List of all the Operations linked to the Context_Records from the query - """ - list_ope = [] - # Query in the DataBase to get information on the Context_Records of the Finds exported - ContextRecord = apps.get_model('archaeological_context_records', 'ContextRecord') - for name in list_cr: - cr, __ = ContextRecord.objects.get_or_create( - label=name - ) - list_attributes = [] - try:list_attributes.append(cr.label) - except:list_attributes.append('') - try:list_attributes.append(str(cr.operation.code_patriarche)) - except:list_attributes.append('') - try:list_attributes.append(cr.town.numero_insee) - except:list_attributes.append('') - try:list_attributes.append(str(cr.unit)) - except:list_attributes.append('') - try:list_attributes.append(cr.interpretation) - except:list_attributes.append('') - try:list_attributes.append(cr.description) - except:list_attributes.append('') - try:list_attributes.append(cr.location) - except:list_attributes.append('') - try:list_attributes.append(cr.documents.image) - except:list_attributes.append('') - try:list_attributes.append(cr.datings.period) - except:list_attributes.append('') - try:list_attributes.append(str(cr.activity)) - except:list_attributes.append('') - try:list_attributes.append(str(cr.main_geodata.multi_polygon)) - except:list_attributes.append('') - try:list_attributes.append(cr.parcel.external_id) - except:list_attributes.append('') - # Creation of a new feature - feature = ogr.Feature(layer.GetLayerDefn( - )) - for idx in range(0, len(list_crea)): - try: - feature.SetField(list_crea[idx], list_attributes[idx]) - except: - pass - # Completion of the list of Operations linked to the exported Context_Records - if cr.operation.code_patriarche not in list_ope: - list_ope.append(cr.operation.code_patriarche) - # Gestion of the geometry - try: - geom = ogr.CreateGeometryFromWkt(str(cr.main_geodata.multi_polygon).split(';')[-1]) - feature.SetGeometry(geom) - except: - pass - layer.CreateFeature(feature) - feature = None - return list_ope - - -def attributes_creation_cr_query(layer, col_names, table_cols): - """ - :param layer: Context_Records layer from the linked geopackage - :param col_names: Name of the columns in the new layer - :param table_cols: List of the columns used in the query - :function: Creation of the attributes of the Context_Records layer with the data from the exporter - :return layer: Layer with attributes - """ - for idx in range(0, len(col_names)): - if table_cols[idx] != '': - # print(table_cols[idx]) # debugtest - # print(col_names[idx]) # debugtest - layer.CreateField(ogr.FieldDefn(col_names[idx], ogr.OFTString)) - return layer - - -def populating_layer_cr_query(layer, table_cols, col_names, datas): - """ - :param layer: Context_Records layer from the linked geopackage with attributes - :param table_cols: List of the columns used in the query - :param col_names: Name of the columns in the new layer - :param datas: Data from the query - :function: Population of the Context_Records layer using all the data from the query - :return list_ope: List of all the Operations linked to the Context_Records from the query - """ - #print(table_cols) #debugtest - #print(col_names) #debugtest - #print(datas) #debugtest - list_ope = [] - list_cr = [] - geom = '' - max = len(col_names) - # Looping on all the datas extracted to create features - for data in datas: - # Creation of a new feature - feature = ogr.Feature(layer.GetLayerDefn()) - for idx in range(0, max): - if col_names[idx] != '': - if 'operation__code_patriarche' in table_cols[idx] and data[idx + 1] not in list_ope: - list_ope.append(data[idx + 1]) - feature.SetField(col_names[idx], str(data[idx + 1])) - else: - feature.SetField(col_names[idx], str(data[idx + 1])) - id_label = table_cols.index(['label']) - name = datas[0][id_label + 1] - list_cr.append(name) - ContextRecord = apps.get_model('archaeological_context_records', 'ContextRecord') - cr, __ = ContextRecord.objects.get_or_create( - label=name - ) - try: - geom = ogr.CreateGeometryFromWkt(str(cr.geodata.multi_polygon).split(';')[-1]) - except: - try: - geom = ogr.CreateGeometryFromWkt(str(cr.main_geodata.multi_polygon).split(';')[-1]) - except: - pass - if geom != '': - feature.SetGeometry(geom) - layer.CreateFeature(feature) - feature = None - return list_ope, list_cr - - -def attributes_creation_sites_query(layer, col_names, table_cols): - """ - :param layer: Sites layer from the linked geopackage - :param col_names: Name of the columns in the new layer - :param table_cols: List of the columns used in the query - :function: Creation of the attributes of the Sites layer with the data from the exporter - :return layer: Layer with attributes - """ - for idx in range(0, len(col_names)): - if table_cols[idx] != '': - # Gestion of the attribute's type - if table_cols[idx] in ['geodata__x', 'geodata__y']: - layer.CreateField(ogr.FieldDefn(col_names[idx], ogr.OFTReal)) - else: - layer.CreateField(ogr.FieldDefn(col_names[idx], ogr.OFTString)) - return layer - - -def populating_layer_sites_query(layer, table_cols, col_names, datas): - """ - :param layer: Sites layer from the linked geopackage with attributes - :param table_cols: List of the columns used in the query - :param col_names: Name of the columns in the new layer - :param datas: Data from the query - :function: Population of the Sites layer using all the data from the query - :return list_cr: List of all the Context_Records linked to the Sites from the query - """ - max = len(col_names) - list_ope = [] - # Looping on all the datas extracted to create features - for data in datas: - # Creation of a new feature - feature = ogr.Feature(layer.GetLayerDefn()) - # Looping on the attributes to add them to the feature - for idx in range(0, max): - if col_names[idx] != '': - # print(table_cols[idx]) # debugtest - # print(data[idx + 1]) # debugtest - if table_cols[idx] == ['operations__code_patriarche'] and data[idx + 1] not in list_ope: - list_ope.append(data[idx + 1]) - feature.SetField(col_names[idx], str(data[idx + 1])) - elif table_cols[idx] in [['geodata__x'], ['geodata__y'], ['geodata__point_2d']]: - feature.SetField(col_names[idx], str(data[idx + 1]).split(' & ')[-1]) - else: - # +1 because the first value in the attributes is '' - feature.SetField(col_names[idx], str(data[idx + 1])) - ArchaeologicalSite = apps.get_model("archaeological_operations", "ArchaeologicalSite") - try: - id_label = table_cols.index(['name']) - label = data[id_label + 1] - object = ArchaeologicalSite.objects.filter( - name=label, - ) - except: - id_label = table_cols.index(['reference']) - label = data[id_label + 1] - object = ArchaeologicalSite.objects.filter( - reference=label, - ) - # Preparations for the geometry - print(object[0],object[0].main_geodata.x,object[0].main_geodata.y,object[0].main_geodata.point_2d) - try: - point = ogr.Geometry(ogr.wkbPoint) - point.AddPoint(float(object[0].main_geodata.x), float(object[0].main_geodata.y)) - except: - try: - point = ogr.CreateGeometryFromWkt(str(object[0].main_geodata.point_2d).split(';')[-1]) - except: - point = '' - if point != '': - feature.SetGeometry(point) - # Addition of the new feature - layer.CreateFeature(feature) - feature = None - ContextRecord = apps.get_model('archaeological_context_records', 'ContextRecord') - # Completion of the list of Context_Records linked to the extracted Sites - list_cr = [] - for elem in list_ope: - if elem != '': - search = ContextRecord.objects.all() - for cr in search: - if elem in str(cr) and cr.label not in list_cr: - list_cr.append(cr.label) - return list_ope, list_cr - - -def modification_style(qgs_path, table_cols, col_names, list_ope, list_crea): - """ - :param qgs_path: Path to the QGIS project, containing the layers style - :param table_cols: List of the columns used in the query to spot specific ones - :param col_names: Name of the columns in the new layer to add their name to the style of the layer - :param list_ope: List of the Operations linked to the entities from the query, to add them as a list - :param list_crea: List of created attributes for the Finds or Context_Records layers - :function: Modification of the QGIS project style to assure the autocompletion/automations for some attributes - :return text: Modified QGIS project - """ - # Lists of default names in the style, attribut names of the datas and new default names - list_ref = ['finds_date', 'finds_time', 'finds_x', 'finds_y', 'finds_z', 'finds_cr', 'finds_parcel', - 'cr_operation', 'cr_insee', 'cr_section', 'cr_parcel', 'cr_full_parcel', 'cr_wkt'] - list_search = ['_date', '_datetime', 'base_finds__x', 'base_finds__y', 'base_finds__z', 'context_record__label', - 'parcel__external_id', 'operation__code_patriarche', 'town__numero_insee', 'parcel__section', - 'parcel__parcel_number', 'parcel__external_id', 'geodata__multi_polygon'] - # Opening of the style - text = open(qgs_path, encoding='utf-8').read() - # Adding the different Operations linked of the Contexts Records and/or Finds exported to a list of possible values - if len(list_ope) > 0: - new_text = "" - for ope in list_ope: - choice = '<Option type="Map">\n <Option name="{}" value="{}" type="QString"/>\n </Option>\n'.format(ope, ope) - new_text += choice - old_text = '<Option type="Map">\n <Option value="Default_value" name="Default_value" type="QString"/>\n </Option>\n' - text = text.replace(old_text, new_text) - else: - text = text.replace("Test_choice", "Null") - # Specifics modifications if the datas don't come from Finds - if list_crea[0] == 'finds': - for ref in list_ref: - id_ref = list_ref.index(ref) - new = '' - for col in table_cols: - if col != '' and list_search[id_ref] in col[0]: - id_new = table_cols.index(col) - new = col_names[id_new] - text = text.replace(ref, new) - # List of corresponding default names in the style linked to the default names used for the Finds - list_corr = ['finds_id', 'finds_cr', 'finds_date', 'finds_x', 'finds_y', 'finds_z', 'find_matériaux', - 'cr_description', 'finds_media', 'finds_wkt_modif', 'finds_parcel'] - # Gestion of the link between the Finds and Context Records layers - id_label = table_cols.index(['label']) - new = col_names[id_label] - text = text.replace("cr_name", new) - if ['documents__image'] in table_cols: - id_media = table_cols.index(['documents__image']) - # Gestion of the link between the Finds and Context Records layers - new = col_names[id_media] - text = text.replace("cr_media", new) - # Replacement of the values from the default names used for the Finds - n = 0 - for elem in list_crea[1]: - old = list_corr[n] - text = text.replace(old, elem) - n += 1 - # Specifics modifications if the datas don't come from Context_Records - elif list_crea[0] == 'cr': - for ref in list_ref: - id_ref = list_ref.index(ref) - new = '' - for col in table_cols: - if col != '' and list_search[id_ref] in col: - id_new = table_cols.index(col) - new = col_names[id_new] - text = text.replace(ref, new) - # List of corresponding default names in the style linked to the default names used for the Finds - list_corr = ['cr_name', 'cr_operation', 'cr_insee', 'cr_type', 'cr_occupation', 'cr_description', - 'cr_localisation', 'cr_media', 'cr_periode', 'cr_activity', 'cr_wkt', 'cr_full_parcel'] - # Test in case the all names of attributes are in lists - try: - id_label = table_cols.index(['label']) - except: - id_label = table_cols.index('label') - # Gestion of the link between the Finds and Context Records layers - new = col_names[id_label] - text = text.replace('finds_id', new) - if 'documents__image' in table_cols: - try: - id_media = table_cols.index(['documents__image']) - except: - id_media = table_cols.index('documents__image') - # Gestion of the link between the Finds and Context Records layers - new = col_names[id_media] - text = text.replace("finds_media", new) - # Specific case to assure the good registration of the z coordinate - if 'geodata__point_3d' in table_cols: - id_new = table_cols.index('geodata__point_3d') - if any('__z' in elem for elem in table_cols): - ref = "finds_wkt_modif" - new = col_names[id_new] - else: - ref = "finds_wkt_simple" - new = col_names[id_new] - text = text.replace(ref, new) - # Replacement of the values from the default names used for the Context Records - n = 0 - for elem in list_crea[1]: - old = list_corr[n] - text = text.replace(old, elem) - n += 1 - else: - pass - return text - - -def modification_style_sites(qgs_path, table_cols, col_names, list_ope, list_crea): - """ - :param qgs_path: Path to the QGIS project, containing the layers style - :param table_cols: List of the columns used in the query to spot specific ones - :param col_names: Name of the columns in the new layer to add their name to the style of the layer - :param list_ope: List of the Operations linked to the entities from the query, to add them as a list - :param list_crea: List of created attributes for the Finds and Context_Records layers - :function: Modification of the QGIS project style to assure the autocompletion/automations for some attributes - :return text: Modified QGIS project - """ - list_ref = ['sites_operation', 'sites_parcel', 'sites_insee', 'sites_x', 'sites_y', 'sites_wkt'] - list_search = ['operations__code_patriarche', 'parcel__external_id', 'towns__numero_insee', 'geodata__x', - 'geodata__y', 'geodata__point_2d'] - # Opening of the style - text = open(qgs_path, encoding='utf-8').read() - # Adding the different Operations linked of the Contexts Records and/or Finds exported to a list of possible values - if len(list_ope) > 0: - new_text = "" - for ope in list_ope: - choice = '<Option type="Map">\n <Option name="{}" value="{}" type="QString"/>\n </Option>\n'.format(ope, ope) - new_text += choice - old_text = '<Option type="Map">\n <Option value="Default_value" name="Default_value" type="QString"/>\n </Option>\n' - text = text.replace(old_text, new_text) - else: - text = text.replace("Test_choice", 'None') - for ref in list_ref: - id_ref = list_ref.index(ref) - new = '' - for col in table_cols: - if col != '' and list_search[id_ref] in col: - id_new = table_cols.index(col) - new = col_names[id_new] - text = text.replace(ref, new) - list_ref = ['cr_name', 'cr_operation', 'cr_insee', 'cr_type', 'cr_occupation', 'cr_description', 'cr_localisation', - 'cr_media', 'cr_periode', 'cr_activity', 'cr_wkt', 'cr_full_parcel', 'finds_id', 'finds_cr', - 'finds_date', 'finds_x', 'finds_y', 'finds_z', 'find_matériaux', 'cr_description', 'finds_media', - 'finds_wkt_modif', 'finds_parcel'] - for id in range(0, len(list_crea)): - text = text.replace(list_ref[id], list_crea[id]) - return text - - def adapt_distant_search(params, src, model): if "search_vector" in params and params["search_vector"]: search_vector = params["search_vector"][0] |