I have a line made of 17959 nodes. I want to delete several range of nodes from that line with a Python command.

For example I want to delete all nodes between node 5530 to node 5861 and connect node 5530 to node 5861. Then I want to delete all nodes between node 14094 to node 14398 and connect node 14094 to node 14398.

Here is the raw code I am using now

layer = iface.addVectorLayer("input.shp", "", "ogr")
QgsVectorLayerEditUtils::deleteVertex(QgsFeatureId "input.shp", int vertex)

I am using QGIS 2.18.15

1 Answer 1


While it isn't deleting, per se, this will create a new feature with your desired geometry. I am not a fan of modifying original geometry as it can lead to data loss.

layer = iface.activeLayer()

features = layer.getFeatures()

for feat in features:
    line = feat.geometry().asMultiPolyline()

# debug
print (line)

# example uses only one feature
line = line[0]

section_1 = line[0:2]       # here you can specify your breaks
section_2 = [line[4]]       # if you have a single point, be sure to enclose it in [] so the list concatenation later works
section_3 = line[6:]        # my sample line only had 7 vertices

# concatenate your sections into a new list
new_coords = section_1 + section_2 + section_3

# make QgsPoints from the QgsPointXYs
new_coords = [QgsPoint(x) for x in new_coords]

# create a feature from your new coords list
new_line = QgsFeature()

# if you want to add it to your existing layer

# if you want to add it to a new layer
new_layer = iface.addVectorLayer("LineString?crs=epsg:4326&field=id:integer&index=yes", "new layer", "memory")

enter image description here

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.