I want to add create nodes\points at an interval on a line shapefile let say at every 100 meters and save those points as shapefile.

I came across a plugin for Qgis called Qchainage, this exactly performs the task i want to do, but I am not sure how to adapt this plugin into my python script.

  • In PyQGIS you can use interpolate method from QgsGeometry to do that. Please, see my answer. – xunilk Jul 14 '17 at 21:48

In PyQGIS you can use interpolate method from QgsGeometry to do that. Generated points can be stored as a memory layer. Complete example code is:

layer = iface.activeLayer()

feat = layer.getFeatures().next()

geom = feat.geometry()

length = geom.length()

distance = 100

points = []

iter = distance

while iter <= length:

    pt = feat.geometry().interpolate(iter).exportToWkt()


    iter += distance

epsg = layer.crs().authid()

uri = "Point?crs=" + epsg + "&field=id:integer""&index=yes"

mem_layer = QgsVectorLayer(uri,

prov = mem_layer.dataProvider()

feats = [ QgsFeature() for i in range(len(points)) ]

for i, feat in enumerate(feats):



I tried it out with line shapefile of next image; where it is also observed memory point layer produced.

enter image description here

Editing Note:

A shorter code could be generated if you have installed in your system fiona and shapely python modules.

import fiona
from shapely.geometry import shape, mapping

line = fiona.open('pyqgis_data/new_line.shp')

crs = line.crs

line = line.next()

geom = shape(line['geometry'])

# length of the LineString
length = geom.length

# creation of the resulting shapefile
schema = {'geometry': 'Point','properties': {'id': 'int'}}

with fiona.open('pyqgis_data/new_shape.shp', 'w', 'ESRI Shapefile', schema, crs=crs) as output:
    # create points every 100 meters along the line
    for i, distance in enumerate(range(0, int(length), 100)):
         point = geom.interpolate(distance)   
         output.write({'geometry':mapping(point),'properties': {'id':i}}) 

It produces same result layer (but it's saved in disk as shapefile; not as memory layer).

  • xunilk, is there a way to save the points in a numpy array? – FJ_Abbasi Jul 24 '17 at 12:23
  • Yes, it's possible but in what way? At first code, you have points list that it can be easily changed in a numpy array (import numpy as np; np.array(points)). However, it'll be an array of WKT points. Before that, you need to specify your desired format in another points list. – xunilk Jul 24 '17 at 13:18
  • The first code seems to be working within Qgis environment only right? The second one works for me but I want to save points as [ [x1 y1] [x2 y2] [x3 y3] ] – FJ_Abbasi Jul 26 '17 at 8:42
  • Is it possible to split the fiona + shapely suggestion as a separate answer. This is more generic than the PyQGIS suggestion, – David Jan 15 '19 at 3:47

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.