#Labriki Mohammed, email: mlabriki@gmail.com, mobile: +212661084186 #Titres miniers ou autres #Année 2020-2021 #QGis 3. ou postérieur #********************** """ it's important in this script to conserve an ordre of the csv file, like this disposition indicates: Num_permit - Name_pivot-xp-yp-px-dx-py-dy-..... 0 1 2 3 4 5 6 7 After 'dy' you put what you wante (Titular, Map, date-decision etc...) """ #********************** from qgis.core import * from qgis.core import QgsVectorFileWriter, QgsWkbTypes from PyQt5.QtGui import * from PyQt5.QtCore import * from qgis.PyQt.QtCore import QVariant from qgis.utils import * from qgis.gui import * from qgis.core import QgsMessageLog from os import path from pathlib import Path # qid = QInputDialog() canvas = iface.mapCanvas() #choice PR or AE - LE input, choix = QInputDialog.getText(qid, "PR or LE or AE", "Choice between PR or AE - LE...", QLineEdit.Normal, "PR") if choix : choix = input.split()[0] if choix == "PR": #Enter your crs input, proj = QInputDialog.getText(qid, "Enter epsg", "Enter projection area (epsg): \nLambert nord Maroc=26191\nLambert sud Maroc=26192\n or other...", QLineEdit.Normal, "26191") if proj: proj = int(input.split()[0]) print(proj) #Filepath for your csv input, Filepathin = QInputDialog.getText(qid, "Filepath 'input' for csv:", "Filepath 'input' like this:\n/Users/HP/Documents/input.csv\n" + " - The file to load is a csv format", QLineEdit.Normal, "/Users/HP/Documents/input.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() #print(filepath) inputlayer = filepathin # set the filepath for the input CSV print (inputlayer) #Name of field of csv file input, Namefields = QInputDialog.getText(qid, "Names of the principal fields as you have in csv file:", "Xpivot and Ypivot: fields of pivot coords.\npxpivot: east 'e' or west 'w'\ndxpivot: distance x (pivot-centre permit)" +"\npypivot= north 'n' or south 's'\ndypivot=distance y (pivot-centre permit)\n"+ "Xpivot,Ypivot,pxpivot,dxpivot,pypivot,dypivot\n- RESPECT THIS OR ORDER IN YOUR CSV AND HERE IN SCRIPT\n"+ "- YOU CAN USE THESE FIELDS NAMES DEFAULT IN YOUR CSV\n"+ "Fields names defaults:",QLineEdit.Normal, "xp,yp,px,dx,py,dy") if Namefields: xp = str(input.split(",")[0]) yp = str(input.split(",")[1]) px = str(input.split(",")[2]) dx = str(input.split(",")[3]) py = str(input.split(",")[4]) dy = str(input.split(",")[5]) ##fields for coordiantes points of pivot xpivot = xp # set the name for the field containing the longitude ypivot = yp # set the name for the field containing the latitude px = px # position east or west pivot/permit dx = dx #distance x pivot/centre of permit py = py #position north or south pivot/permit dy = dy #distance y pivot/centre of permit #-------------------- #File path for the output file input, Filepathout = QInputDialog.getText(qid, "Filepath 'output':", "File path like this\n/Users/HP/Documents/ouput.gpkg\n" + " -The output file is a gpkg format", QLineEdit.Normal, "/Users/HP/Documents/output.gpkg") if Filepathout: filepathout = str(input.split()[0]) #-------------------- crs = proj # set the crs as needed #-------------------- outputlayer = filepathout # set the filepath for the output shapefile #-------------------- spatRef = QgsCoordinateReferenceSystem(crs, QgsCoordinateReferenceSystem.EpsgCrsId) #-------------------- inp_tab = QgsVectorLayer(inputlayer, 'Input_Table', 'ogr') fields = inp_tab.fields() outLayer = QgsVectorFileWriter(outputlayer, None, fields, QgsWkbTypes.Polygon, spatRef, "gpkg") outFeature = QgsFeature() pt = QgsPointXY() xp = 0. yp = 0. #--------------------- for feat in inp_tab.getFeatures(): xc = 0. yc = 0. attrs = feat.attributes() #--------------------- ax1=attrs[3] ax2=float(attrs[4]) ay1 = attrs[5] ay2 = float(attrs[6]) print(ax1,ax2,"-",ay1,ay2) pt.setX(float(feat[xpivot])) pt.setY(float(feat[ypivot])) #Center of permit PR xc and yc if ax1 == "w": xc = float(pt[0]) - ax2 else: xc = float(pt[0]) + ax2 print ("xc: ",xc) # if ay1 == "s": yc = float(pt[1]) - ay2 else: yc = float(pt[1]) + ay2 print ("xc: ",xc,"yc :",yc) #Angles of square PR i = 1 points = [] while i <5: if i == 1: xa = xc - 2000 ya = yc + 2000 if i == 2: xa = xc + 2000 ya = yc + 2000 if i == 3: xa = xc + 2000 ya = yc - 2000 if i == 4: xa = xc - 2000 ya = yc - 2000 # if i == 5: # xa = xc - 2000 # ya = yc + 2000 points.append(QgsPointXY(xa,ya)) i+=1 print("points: ",points) outFeature.setAttributes(attrs) outFeature.setGeometry(QgsGeometry.fromPolygonXY([points])) outLayer.addFeature(outFeature) outFeature.setAttributes(attrs) points[:]=[] print(points) del outLayer iface.messageBar().pushMessage("OK!!!","you can add your vector file: "+ filepathout, level=1,duration=10) #------------------------------ #------------------------------ #------------------------------ if choix == "AE" or choix == "LE": #Enter your crs input, proj = QInputDialog.getText(qid, "Enter epsg", "Enter projection area (epsg): \nLambert nord Maroc=26191\nLambert sud Maroc=26192\n or other...", QLineEdit.Normal, "26191") if proj: proj = int(input.split()[0]) print(proj) #Filepath for your csv input, Filepathin = QInputDialog.getText(qid, "Filepath 'input' for csv:", "Filepath 'input' like this:\n/Users/HP/Documents/input.csv\n"+ " -The file 'input' is a csv format", QLineEdit.Normal, "/Users/HP/Documents/input.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() #print(filepath) inputlayer = filepathin # set the filepath for the input CSV print (inputlayer) print("AE, LE") input, Namefields = QInputDialog.getText(qid, "Names of fields 'number angles' and the polygon 'angles coords':", "the coordinates of the angles in the csv\nmust be classified in the clockwise or anti-clockwise direction\n" + "nangles,xangle,yangle: ",QLineEdit.Normal, "xangle,yangle") if Namefields: xangle = str(input.split(",")[0]) yangle = str(input.split(",")[1]) #-------------------------------- #File path for the output file input, Filepathout = QInputDialog.getText(qid, "Filepath 'output':", "Filepath 'output' like this:\n/Users/HP/Documents/output.csv\n" + " -The output file is a gpkg format", QLineEdit.Normal, "/Users/HP/Documents/output.gpkg") if Filepathout: filepathout = str(input.split()[0]) # crs = proj # set the crs as needed # outputlayer = filepathout # set the filepath for the output shapefile # spatRef = QgsCoordinateReferenceSystem(crs, QgsCoordinateReferenceSystem.EpsgCrsId) # inp_tab = QgsVectorLayer(inputlayer, 'Input_Table', 'ogr') fields = inp_tab.fields() outLayer = QgsVectorFileWriter(outputlayer, None, fields, QgsWkbTypes.Polygon, spatRef, "gpkg") pt = QgsPointXY() outFeature = QgsFeature() xangle = 0. yangle = 0. points = [] #--------------------- for feat in inp_tab.getFeatures(): attrs = feat.attributes() xangle = float(attrs[1]) yangle = float(attrs[2]) if xangle != 0 and yangle != 0: points.append(QgsPointXY(xangle,yangle)) attrs[1] = "-" attrs [2] = "-" outFeature.setAttributes(attrs) if xangle == 0 and yangle == 0: outFeature.setGeometry(QgsGeometry.fromPolygonXY([points])) outLayer.addFeature(outFeature) outFeature.setAttributes(attrs) print(points) from_m = 0. to_m = 0. scale = 0. points[:]=[] del outLayer iface.messageBar().pushMessage("OK!!!","you can add your vector file: "+ filepathout, level=1,duration=10) # #---------------------- #------------------------- if choix != "AE" and choix != "LE" and choix != "PR": print("Bad choice") iface.messageBar().pushMessage("BAD CHOICE....CHOICE IS 'PR' or 'AE' or 'LE'", level=1,duration=10)