Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to create random points along a polyline shapefile in QGIS. Basically, like the 'random points' tool in 'research tools' but for polylines instead of polygons.

I have tried converting the line file to a polygon shapefile, but it fills in some areas with polygons, whereas other areas remain long line-type polygons.

I am pretty new to QGIS, and am not really familiar with Python code.


share|improve this question
If you're willing to get into R, the spatstat package has tools to create random points on lines. –  Micha Jul 28 '12 at 19:13
Thanks for the code. I was wondering if someone could help me modify it so that it placed points on the line at regular spaced intervals with a randomized start? It would be greatly appreciated. I have no working knowledge of python. –  user18268 May 17 '13 at 18:15

2 Answers 2

up vote 12 down vote accepted

This code will work on the lastest dev build of QGIS.

from qgis.utils import iface
from qgis.core import *
from PyQt4.QtCore import QVariant
import random

def createRandomPoints(count):       
    # Create a new memory layer to store the points.
    vl = QgsVectorLayer("Point", "distance nodes", "memory") 
    pr = vl.dataProvider()  
    pr.addAttributes( [ QgsField("distance", QVariant.Int) ] )
    layer = iface.mapCanvas().currentLayer()

    # For each selected object
    for feature in layer.selectedFeatures():
        geom = feature.geometry()
        length = geom.length()
        feats = []
        # Loop until we reach the needed count of points.
        for i in xrange(0,count):
            # Get the random distance along the line.
            distance = random.uniform(0, length)
            # Work out the location of the point at that distance.
            point = geom.interpolate(distance)

            # Create the new feature.
            fet = QgsFeature()
            fet.setAttributeMap( { 0 : distance } )



I know you said you are not very familiar with Python code but you should be able to run this pretty easy. Copy the above code into a file (mine is called locate.py) and place it in your ~/.qgis/python if you are on Windows 7 that will be in C:\Users\{your user name}\.qgis\python\ or on Windows XP C:\Documents and Settings\{your user name}\.qgis\python\

Once the file is in the python folder open QGIS and select some line objects.
Layer selection

Then open the Python console and run the following code:

import locate.py 

Python Console

The result should look something like this


If you want to run it again just select some more lines and run locate.createRandomPoints(10) in the Python console again.

Note: locate.createRandomPoints(10) the 10 here is the number of points to generate per line

share|improve this answer
Thanks for all your help! I am not sure in what format to save the code though - how do I make it a file with a py extension? Sorry if these are pretty basic questions. –  Cec.g Aug 1 '12 at 11:54
Copy the text into a normal text file and just save it with .py as the extension. –  Nathan W Aug 1 '12 at 12:42
I did try that but it comes up with this error: ImportError: No module named locate.py –  Cec.g Aug 1 '12 at 12:54
This is the file path: C:\Users\Cecily\.qgis\python –  Cec.g Aug 1 '12 at 12:56
did you use import locate no need for the .py in the Python console. –  Nathan W Aug 1 '12 at 13:18

You could buffer the polylines (minimally) and then run the sampling on the resulting polygons. It could work fine by itself if you don't have any other limiting factors, eg. on minimum interpoint spacing, density or somesuch.

For more complicated cases, I would create a much denser random sample and then pick appropriate (whatever that may be) points in a second step. Something similar could be done with the densify tool, but then all the points would be on the polylines.

share|improve this answer

protected by underdark Apr 18 at 16:26

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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