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.

Thanks

share|improve this question
    
I suspect you are going to need to use some Python code for this. –  Nathan W Jul 28 '12 at 12:46
    
Do they have to be on the polyline? –  lynxlynxlynx Jul 28 '12 at 13:04
    
I need them to be either on the line or immediately adjacent to it... –  Cec.g Jul 28 '12 at 15:57
    
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
    
I have some Python code that will work just cleaning it up and I'll post it. –  Nathan W Jul 29 '12 at 8:36

2 Answers 2

up vote 11 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 } )
            fet.setGeometry(point)
            feats.append(fet)

        pr.addFeatures(feats)
        vl.updateExtents()  

    QgsMapLayerRegistry.instance().addMapLayer(vl)

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 
locate.createRandomPoints(10)

Python Console

The result should look something like this

Results

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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