I'm new to PyQGIS and I'm having some dificulties. I'm trying to implement / utilize "Add Feature" (polyline) functionality in my python plugin. I know I can do this by defining starting and ending points.

PyQGIS, construct a polyline:

line_start = QgsPoint(50,50)
line_end = QgsPoint(100,150)
line = QgsGeometry.fromPolyline([line_start,line_end])

This is what I'm trying to avoid. I'd like user to digitize / create polyline by clicking on map canvas to preview geometry. How can I do that?

  • Do you want to enable an edit session over a polyline layer you already have or do you want to allow the user to digitize a temporal polyline (e.g, like the one you can see while measuring distances) on map canvas? Sep 19 '16 at 12:37
  • @GermánCarrillo . Yes, I want to digitize / edit on the existing polyline layer on map canvas (not the temporal polyline).
    – Cy T
    Sep 20 '16 at 1:02

You can start an edit session on your line layer and activate the Add Feature tool by including this code in your plugin:

layer = self.iface.activeLayer() # See the note below...

NOTE: the way you access the line layer might differ in your case. You could, for instance, get the layer by name (if it's already loaded into QGIS) or even load it by yourself, providing a URI, name, and data provider.

If you want to save the edits made to the line layer also from your plugin, you can call:


On the other hand, if you want to revert any edit made on the layer since you called startEditing(), you can call:


Hope it helps!


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.