6

So I have a .csv of points that I've cleaned up using pandas, and appended to a list with each item being Type: QgsPoint, with it's correct X and Y coordinate. I'm trying to find the correct operation that can handle this now that it is in a QgsPoint type.

import pandas as pd
from qgis.core import *
import iface

# Read in .cas with Transects
df = pd.read_csv("csv link here")
# Delete top row with RTC data
dato = df.iloc[1:]
# Rename column headers
dato.columns = ['Transect', 'Point Number', 'Subclass', 'Northing', 'Easting', 'Altitude', 'Notes']
print(dato.head())
# Create empty list to store point data
PointList = []

# Create point for each row based on Northing and Easting data
for index, row in dato.iterrows():
    termino = QgsPoint(float(row['Easting']), float(row['Northing']))

    PointList.append(termino)

print(PointList)

Here is where I have gotten to. I've tried a few forms of iface.addVectorLayer (which appears to be outdated because iface does not have addVectorLayer apparently), QgsGeometry.fromPolyLine/fromMultipoint both have no worked due to lack of documentation about what each argument is for. I've tried the following:

line = QgsGeometry.fromPolyline(PointList)
QgsGeometry.fromMultiPointXY(PointList)
feat = QgsFeature()
feat.setGeometry(QgsGeometry.fromPoint(PointList[]))

The last attempt says QgsGeometry had no module named fromPoint.

I'm mostly just looking for someone to point me towards a module I should use to get a list of QgsPoints to a shapefile, so I can eventually run Points to Path. All of it will eventually be transferred to Jupyter Notebooks so data can be input and these polygons we need can be spit back out.

3
  • 1
    Where do you run this script, in "qgis python editor" or in "pycharm"? If in pycharm, you cannot import iface in it. iface is created at starting of QGIS and refers QGIS Interface. If in QGIS, then you don't have to import ifaceand qgis.core, because, they are automatically imported at starting QGIS. – user156679 Jun 17 '20 at 14:54
  • 1
    Try with line = QgsGeometry.fromPolyline(PointList) and feat.setGeometry(line) – Andreas Müller Jun 17 '20 at 15:01
  • @Kübra Yes within PyCharm- which makes sense why I can't import it. It needs to be uploaded to a High Performance Computer Cluster therefore I needs to be written externally and ran via Jupyter Notebook – Barnard87 Jun 17 '20 at 17:50
7

Solution using a sample data to run in QGIS Python Editor:

import pandas as pd

# Creating sample data
dato = pd.DataFrame(data = [[0, 0], [2, 4], [10, 10]])

# Rename column headers
dato.columns = ['Easting', 'Northing']

# using python list comprehension    
point_list = [ QgsPoint(r['Easting'], r['Northing']) for i, r in dato.iterrows() ]

# creating line from point_list
polyline = QgsGeometry.fromPolyline(point_list)

print(polyline)
# <QgsGeometry: LineString (0 0, 2 4, 10 10)> 

feat = QgsFeature()
feat.setGeometry(polyline)
1
  • Okay that's an amazing start. I got all my data into a polyLine. My next step is to get each into a shape file of its own, based on if it shares the same Transect number which is in the same row. How would you go about incorporating that element? Essentially I'll need some form of if statement to ask if transect == transect, I'm just not sure how to keep all that attribute data with this new LineString file. – Barnard87 Jun 17 '20 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.