""" This is a script to have logs after geological field or drilling survey. This is a synthetic logs with choice of number of logs like in your csv file. The many logs are here to show lithostratigraphic correlation. Labriki M. fev. 2021 """ from qgis.core import * from PyQt5.QtGui import * from PyQt5.QtCore import * from qgis.PyQt.QtCore import QVariant from qgis.utils import * from qgis.gui import * from os import path from pathlib import Path import processing from random import randrange import time # qid = QInputDialog() canvas = iface.mapCanvas() # input, Filepathin = QInputDialog.getText(qid, "Filepath 'input' for csv:", "Filepath 'input' like this:\n/Users/HP/Documents/loglitho.csv\n" + " - The file to load must be a csv format", QLineEdit.Normal, "/Users/HP/Documents/loglitho.csv") if Filepathin: filepathin = str(input.split()[0]) way = "'" + filepathin + "'" print(way) #is file exist? if os.path.isfile(filepathin): print("Fichier trouvé") else: print("Fichier non trouvé") iface.messageBar().pushMessage("Error","File "+ way + " not found, verifiy your path", level=1,duration=10) self.iface.actionExit().trigger() inputlayer = filepathin #look for fields from and to and hardness input, NameFields = QInputDialog.getText(qid, "from - to and Hardness", "- from_m and to_m\n- relative hardness of the rocks to have erosion component in log in decimal number >=1\n"+ "- code of your lithological discription", QLineEdit.Normal, "from_m,to_m,hardness,code") if NameFields: from_m = str(input.split(",")[0]) to_m = str(input.split(",")[1]) hard = str(input.split(",")[2]) code = str(input.split(",")[3]) if not NameFields: iface.messageBar().pushMessage("Error","Program aborted", level=2,duration=5) self.iface.actionExit().trigger() #select a non earth epsg or equivalent like 3006 input, Non_Earth = QInputDialog.getText(qid, "Non-Earth projection", "It's important to have a non-earth projection or equivalent like:\n" + "scr : SWEREF99TM\n" + "EPSG: 3006\n" + " -Give your equivalent non-earth projection like this:\nUSER: 3006", QLineEdit.Normal, "3006") if Non_Earth: proj = str(input.split(",")[0]) xp_epsg='Polygon?crs=epsg:' + proj #output file input, Filepathout = QInputDialog.getText(qid, "Filepath 'output':", "File path like this:\n/Users/HP/Documents/", QLineEdit.Normal, "/Users/HP/Documents/") if Filepathout: FilePathout = str(input.split()[0]) #File gpkg input, FileName = QInputDialog.getText(qid, "File for your 'output':", "gpkg format like:\nFieldLog.gpkg", QLineEdit.Normal, "FieldLog.gpkg") if FileName: FileName = str(input.split()[0]) crs = int(proj) # set the crs as needed #-------------------- #outputlayer = filepathout # set the filepath for the output shapefile outputlayer = FilePathout + FileName print(outputlayer) #-------------------- spatRef = QgsCoordinateReferenceSystem(crs, QgsCoordinateReferenceSystem.EpsgCrsId) #--------------------- inp_tab = QgsVectorLayer(inputlayer, 'Input_Table', 'ogr') pr_ = inp_tab.dataProvider() inp_tab.updateFields() fields = inp_tab.fields() outLayer = QgsVectorFileWriter(outputlayer, None, fields, QgsWkbTypes.Polygon, spatRef, "gpkg") #--------------------- pt = QgsPointXY() outFeature = QgsFeature() #from_m = 0. #to_m = 0. scale = 0. scale_0 = 0. points=[] scale_list = [] max_list = 0 n = 0 count = 0 #--- for feat in inp_tab.getFeatures(): #--------------------- n = n + 1 count = count + 1 attrs = feat.attributes() from_m = -1*float(attrs[1]) to_m = -1*float(attrs[2]) scale = float(attrs[3]) # scale_list.append(scale) if from_m == 0 and n != 1: max_list = max_list + max(scale_list)+20 #initialize scale_list n = 0 scale_list[:] = [] #Create polygons from each pass points.append(QgsPointXY(scale_0 + max_list,from_m)) # a2 = (scale, from_m) 2 points.append(QgsPointXY(scale + max_list,from_m)) # a3 = (scale, to_m) 3 points.append(QgsPointXY(scale + max_list,to_m)) # a4 = (scale, to_m) 4 points.append(QgsPointXY(scale_0 + max_list,to_m)) #finalize realizations outFeature.setGeometry(QgsGeometry.fromPolygonXY([points])) outFeature.setAttributes(attrs) outLayer.addFeature(outFeature) scale_list.append(scale) #initialize parameters and list scale = 0. points[:] = [] #terminate loop del outLayer #call layer to canvas layer = QgsVectorLayer(outputlayer,FileName,'ogr') QgsProject.instance().addMapLayer(layer) features = layer.getFeatures() #position in list of code_lithology list = layer.fields().names() code = list[5] #--------------------------------------------- #symbology: create a random symbology in the logs #using code field #Create symbology for all categorized entities fni = layer.fields().indexFromName(code) unique_values = layer.uniqueValues(fni) # fill categories categories = [] for unique_value in unique_values: # initialize the default symbol for this geometry type symbol = QgsSymbol.defaultSymbol(layer.geometryType()) # configure a symbol layer layer_style = {} layer_style['color'] = '%d, %d, %d' % (randrange(0, 256), randrange(0, 256), randrange(0, 256)) layer_style['outline'] = '#000000' symbol_layer = QgsSimpleFillSymbolLayer.create(layer_style) # replace default symbol layer with the configured one if symbol_layer is not None: symbol.changeSymbolLayer(0, symbol_layer) # create renderer object category = QgsRendererCategory(unique_value, symbol, str(unique_value)) # entry for the list of category items categories.append(category) # create renderer object renderer = QgsCategorizedSymbolRenderer(code, categories) # assign the created renderer to the layer if renderer is not None: layer.setRenderer(renderer) layer.triggerRepaint() #add a provisoir field for this layer #use it to have abscisss of many logs to cretae scales prov = layer.dataProvider() field_names = [field.name() for field in prov.fields()] #print(field_names) for count, f in enumerate(field_names): print("{}:{}".format(count, f)) #add x_log as double features = layer.getFeatures() prov.addAttributes([QgsField("x_log", QVariant.Double)]) #layer.startEditing() for f in features: id=f.id() attrs = f.attributes() dur = float(attrs[4]) valeur= dur val = valeur + 20 attr_value={count+1:val} prov.changeAttributeValues({id:attr_value}) layer.updateFields() layer.commitChanges() #========= #memory layer with non-earth projection scale = QgsVectorLayer("LineString?crs=EPSG:3006","Scale","memory") pr = scale.dataProvider() pr.addAttributes([QgsField("niveau", QVariant.Int)]) scale.updateFields() line = QgsFeature() field_mem = [field.name() for field in pr.fields()] #activate layer (log layers FieldLog.gpkg) layer = qgis.utils.iface.activeLayer() # get active layer if not already set prov = layer.dataProvider() field_names = [field.name() for field in prov.fields()] print(field_names) features = layer.getFeatures() #list_from = [] list_to = [] list_xlog = [] list_x = [] list_len = [] list_rap = [] abcisse = [0] max_xlog = 0. max_to = 0. n = 0 x = 0. x0 = 0. for f in features: n = n + 1 attrs = f.attributes() from_m = float(attrs[2]) to_m = float(attrs[3]) xlog = float(attrs[-1]) list_xlog.append(xlog) list_to.append(to_m) if from_m == 0 and n > 1: # list_x.append(max(list_xlog)) list_len.append(max(list_to)) x = x + max(list_xlog) abcisse.append(x) # list_xlog[:] = [] list_to[:] = [] #create parameters (list, and variables) rapport = [] list_len.append(to_m) s = sum(list_len) rapport = s/100 #initialize variables i = 0 j = 0 for i in range(len(abcisse)): for j in range(int(list_len[i])): point1 = QgsPoint(int(abcisse[i]),-j) if j % 10 == 0: point2 = QgsPoint((abcisse[i]-(1*rapport)),-j) else: point2 = QgsPoint((abcisse[i]-(0.5*rapport)),-j) line.setGeometry(QgsGeometry.fromPolyline([point1,point2])) line.setAttributes([j]) pr.addFeature(line) scale.updateExtents() j += 1 QgsProject.instance().addMapLayer(scale) #======== #labels for scale for layer in QgsProject.instance().mapLayers().values(): layer_settings = QgsPalLayerSettings() text_format = QgsTextFormat() text_format.setFont(QFont("Arial", 6)) text_format.setSize(6) buffer_settings = QgsTextBufferSettings() buffer_settings.setEnabled(False) buffer_settings.setSize(0.10) buffer_settings.setColor(QColor("black")) text_format.setBuffer(buffer_settings) layer_settings.setFormat(text_format) layer_settings.fieldName = "niveau" layer_settings.placement = 2 layer_settings.enabled = True layer_settings = QgsVectorLayerSimpleLabeling(layer_settings) layer.setLabelsEnabled(True) layer.setLabeling(layer_settings) layer.triggerRepaint() #======== #delete th provisoir layer x_log layers= QgsProject.instance().mapLayersByName('FieldLog.gpkg')[0] # I select the layer object iface.setActiveLayer(layers) prov = layers.dataProvider() list_layer = [] last = [] field_names = [field.name() for field in prov.fields()] for f in field_names: list_layer.append(f) #list_layer.reverse() n_xlog = len(list_layer)-1 prov.deleteAttributes([n_xlog])