1

I would like to add a line to QGIS and export all table values to it. The coordinate data is in the coords column and the format is in .CSV.

Hier is a picture of the table

enter image description here

and here ist the code in .txt

,coords,value1,value2 0,"[[7.89, 48.40], [7.89, 48.]]",-2.24,-0.08 1,"[[7.89, 48.40], [7.89, 48.40], [7.88, 48.40], [7.87, 48.]]",1.23,-0.06

  • what have you tried ? – Taras Nov 07 '22 at 11:32
  • I did the solution for a similar question that is here https://gis.stackexchange.com/questions/443343/creating-polyline-from-csv-data-using-pyqgis. Although you see the lines in QGIS, the problem is that the Table data are not exported (they are empty). Also the columns explained in the same example are not named. – Paulo Matias Montecinos Medel Nov 07 '22 at 11:43

1 Answers1

0

The first thing I did was just to import the columns that have the coordinates in string form. Then I did the procedure according to the answer in Creating polyline from CSV data using PyQGIS to generate a line in the map.

To add the missing columns to the line, I imported the original csv file as a data frame and imported its columns by python code iteration. These two links helped me to understand https://opensourceoptions.com/blog/pyqgis-adding-and-deleting-vector-layer-fields/ and https://opensourceoptions.com/blog/pyqgis-adding-and-deleting-vector-layer-fields/ the full code is below (sorry for the different languages):

position_line =r'//Users/paulo/Documents/QGis/primer_projecto_Qgis/2022.11.17/position_line.csv' #direccion del archivo
position_line_data ='file:///Users/paulo/Documents/QGis/primer_projecto_Qgis/2022.11.17/position_line_data.csv'

#Create empty temp line layer vl = QgsVectorLayer("LineString?crs=epsg:4326&index=yes", "line_geodata", "memory") provider = vl.dataProvider()

#lee csv de datos con pandas df = pd.read_csv(position_line_data,delimiter=',')

#Genera los headers column_headers = list(df.columns.values)

#agrega headers a vl for column_headers in df.columns: provider.addAttributes([QgsField(column_headers, QVariant.String)]) vl.updateFields()

with open(position_line, newline='') as f: reader=csv.reader(f) for row in reader: #row is a list of strings: ['[[7.89471262378373', ' 48.405478200932436]', ' [7.892694850981679', ' 48.40550448264249]', ' [7.887506712484601', ' 48.40574772233518]', ' [7.878533768291611', ' 48.4055148846117]]'] coordinates = eval(','.join(row)) #Join the string with a comma delimiter and turn into a list with eval line = QgsGeometry.fromPolyline(QgsLineString(coordinates)) #Create a line geometry f = QgsFeature() #Create a new feature f.setGeometry(line) #Set the geometry provider.addFeature(f) #Add it

QgsProject.instance().addMapLayer(vl) #agrega linea al mapa

#inicia codigo para actualizar valores de tabla layer vl (line_geodata) caps = vl.dataProvider().capabilities() fid =1 # da id del row al que vamos a modificar el valor shape=df.shape #entrega la dimension de dataframe df.reset_index(drop=True) # borra el index para la iteracion

for row in df.itertuples(index=False): #se tiene que sacar el index para que no aparezca en la iteración for i in range(shape[1]): #shape[1] da numero de columnas if caps & QgsVectorDataProvider.ChangeAttributeValues: attrs = { i : row[i] } vl.dataProvider().changeAttributeValues({ fid : attrs }) fid= fid+1