How To Create Line From Points in QGIS with PyQGIS and Pandas - Tutorial
/Python scripting allows us to enhance the data management and spatial analysis in QGIS. Most times, spatial data comes from a variety of data providers in a sort of formats and data types that we have to be adapted to the GIS standard. Big data and continuos monitoring create large datasets that have to indexed, sorted and manipulated in a effective way.
QGIS has a Python console and the capability to install external Python packages that run on QGIS session. This time we have installed Pandas in QGIS to handle the data from a sensing device for air quality monitoring made on a Raspberry Pi 3 and then we have defined the path as a succession of points with PyQGIS scripting.
You can read the whole description on the sensing device on this post:
https://www.hackster.io/james-puderer/distributed-air-quality-monitoring-using-taxis-69647e
Tutorial
Code
This is the final Python code:
import os import pandas as pd os.chdir('C:\Users\Saul\Documents\Ih_CreateLineFromPointswithPyQGIS') lista = os.listdir(os.getcwd()) print(lista) datos = pd.read_csv(lista[1],parse_dates=True,index_col=1) datos.sort_index(inplace=True) print(datos.head()) PointList = [] for index, row in datos.iterrows(): termino = QgsPoint(float(row['longitude']),float(row['latitude'])) PointList.append(termino) print(PointList) linea = iface.addVectorLayer("LineString?crs=epsg:4326&field=id:integer&index=yes","Linea","memory") linea.startEditing() feature = QgsFeature() feature.setGeometry(QgsGeometry.fromPolyline(PointList)) feature.setAttributes([1]) linea.addFeature(feature,True) linea.commitChanges() iface.zoomToActiveLayer()
Input Data
You can download the input data on this link.