4

I want to create a LineString using points/coordinates in QGIS 3.26 using Python/PyQGIS.

Here is my code :

points = []
x = QgsPointXY(783648,1397243)
points.append(x)
y = QgsPointXY(788883,1396853)
points.append(y)
layer = iface.activeLayer()
fields = layer.dataProvider().fields()
feature = QgsFeature()
feature.setGeometry(QgsGeometry.fromPolylineXY(points))
feature.setFields(fields)
feature.setAttribute(0,12)
layer.addFeature(feature)
QgsProject.instance().addMapLayer(layer)

I am facing no error but I can't see any LineString in QGIS from the given points.

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Nov 14, 2022 at 10:11
7
  • 2
    Try layer.dataProvider().addFeatures([feature]). And remove the last line. If your layer is in the layers panel it is already added to the project. Replace it with layer.triggerRepaint(). Commented Nov 14, 2022 at 10:28
  • Thank you this works Commented Nov 14, 2022 at 10:34
  • Is this a LineString? Commented Nov 14, 2022 at 10:34
  • 1
    The error you may get is AttributeError: 'NoneType' object has no attribute 'dataProvider' Commented Nov 14, 2022 at 13:39
  • Yes its a linestring Commented Nov 16, 2022 at 11:02

1 Answer 1

4

The fundamental reason why your code is not working is that you are trying to add a feature to your layer without making it editable. If you want to immediately commit the changes to the layer's underlying data store, you should work with layer.dataProvider() instead. Also, you should remove the last line from your code snippet, since the active layer is already added to the project, and replace it with a few lines to reflect the changes to the layer.

points = []
x = QgsPointXY(783648,1397243)
points.append(x)
y = QgsPointXY(788883,1396853)
points.append(y)
layer = iface.activeLayer()
if layer.dataProvider().capabilities() & QgsVectorDataProvider.AddFeatures:
 feature = QgsFeature(layer.fields())
 feature.setGeometry(QgsGeometry.fromPolylineXY(points))
 feature.setAttributes([0, 12])
 layer.dataProvider().addFeatures([feature])
 layer.updateExtents()
 layer.triggerRepaint()
 iface.mapCanvas().refresh()
 # Optional...
 iface.zoomToActiveLayer()

On the other hand, if you want the opportunity to undo/ redo then save the edits via the GUI, you can just call layer.startEditing() before adding the feature to the layer:

points = []
x = QgsPointXY(783648,1397243)
points.append(x)
y = QgsPointXY(788883,1396853)
points.append(y)
layer = iface.activeLayer()
if layer.type() == QgsMapLayerType.VectorLayer:
 feature = QgsFeature(layer.fields())
 feature.setGeometry(QgsGeometry.fromPolylineXY(points))
 feature.setAttributes([0, 12])
 if not layer.isEditable():
 layer.startEditing()
 
 layer.addFeature(feature)
 layer.updateExtents()
 layer.triggerRepaint()
 iface.mapCanvas().refresh()
 # Optional...
 iface.zoomToActiveLayer()

I strongly recommend reading the PyQGIS Cookbook section on Modifying Vector Layers.

answered Nov 15, 2022 at 8:51
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.