1

Problem

I have tried to import edges to QGIS as indicated here:

Importing Edge List into QGIS?

However, I got an error when I try to perform this command

prov.addFeatures(feats)

This is the error:

Traceback (most recent call last):
  File "/Applications/QGIS-LTR.app/Contents/MacOS/../Resources/python/code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "/Applications/QGIS-LTR.app/Contents/MacOS/../Resources/python/codeop.py", line 178, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "/Applications/QGIS-LTR.app/Contents/MacOS/../Resources/python/codeop.py", line 106, in _maybe_compile
    raise err1
  File "/Applications/QGIS-LTR.app/Contents/MacOS/../Resources/python/codeop.py", line 93, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "/Applications/QGIS-LTR.app/Contents/MacOS/../Resources/python/codeop.py", line 143, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 8
    prov.addFeatures(feats)
    ^
SyntaxError: invalid syntax

How to solve it ?

_______________________________________________________________

Here below the original code from Importing Edge List into QGIS?

Replace test.csv with the correct filename and paste the following code into the Python console. Note, that in your csv Lat=y comes before Lon=x.

# specify your csv-file
csvFile = "G:/test.csv"

# create an empty memory layer for polylines
layer = QgsVectorLayer('LineString?crs=EPSG:4326', 'Connected', 'memory')
prov = layer.dataProvider()

# add layer to the map
QgsMapLayerRegistry.instance().addMapLayer(layer)

# open the csv-file for reading and skip the header row
lineStrings = open(csvFile, "rU")
next(lineStrings)

# start editing
layer.startEditing()

# loop over the lines, split them into 4 coordinates, build points from pairs of
# them, and connect the pair of points 
feats = []
for line in lineStrings:
    lineStringAsList = line.split(",")
    from_node = QgsPoint(float(lineStringAsList[1]),float(lineStringAsList[0]))
    to_node = QgsPoint(float(lineStringAsList[3]),float(lineStringAsList[2]))
    feat = QgsFeature()
    feat.setGeometry(QgsGeometry.fromPolyline([from_node, to_node]))
    feats.append(feat)

# finally add all created features and save edits
prov.addFeatures(feats)
layer.updateExtents()
layer.commitChanges()

Please check your latitude values, they are out of range [-90, 90]. _______________________________________________________________

2
  • 3
    Please include relevant code in your question, since links are subject to change.
    – Erik
    Jul 6, 2022 at 13:47
  • 1
    It looks like this script is for QGIS2
    – Mayo
    Jul 6, 2022 at 13:59

1 Answer 1

2

The python script that you provide is for QGIS 2, so this will be the code for QGIS 3.

# specify your csv-file
csvFile = "G:/test.csv"

# create an empty memory layer for polylines
layer = QgsVectorLayer('LineString?crs=EPSG:4326', 'Connected', 'memory')
# prov = layer.dataProvider() now is not necessary to use a QgsDataProvider object to handle layers

# add layer to the map
# QgsMapLayerRegistry.instance().addMapLayer(layer)
QgsProject.instance().addMapLayer(layer) # this is the way to add layer to the QGIS project

# open the csv-file for reading and skip the header row
lineStrings = open(csvFile, "rU")
next(lineStrings)

# start editing
layer.startEditing()

# loop over the lines, split them into 4 coordinates, build points from pairs of
# them, and connect the pair of points 
feats = []
for line in lineStrings:
    lineStringAsList = line.split(",")
    from_node = QgsPoint(float(lineStringAsList[1]),float(lineStringAsList[0]))
    to_node = QgsPoint(float(lineStringAsList[3]),float(lineStringAsList[2]))
    feat = QgsFeature()
    feat.setGeometry(QgsGeometry.fromPolyline([from_node, to_node]))
    feats.append(feat)

# finally add all created features and save edits
# prov.addFeatures(feats)
layer.addFeatures(feats) # add features to the layer object, now you don't need to add layers to a QgsDataProvider object
layer.updateExtents()
layer.commitChanges()
5
  • thanks a lot @Mayo! The code runs well but I do not see anything in the main window of QGIS....
    – Ommo
    Jul 6, 2022 at 15:04
  • 1
    It worked fine for me, but remember that the first column must be the y coordinate of the first point and the second column is the x coordinate of the first point, then the third column must be the y coordinate of the second point and the fourth column is the x coordinate of the second point
    – Mayo
    Jul 6, 2022 at 15:17
  • it works!!!! :-) many many thanks!!!
    – Ommo
    Jul 6, 2022 at 15:34
  • 2
    Please don't just dump a lot of code without a comment. What did you change and why? Jul 6, 2022 at 18:44
  • 1
    Sorry, I commented every line that is from QGIS 2 and I put below QGIS 3 code, I will edit my question to add more details.
    – Mayo
    Jul 6, 2022 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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