Note: There is now a QGIS plugin QChainage
. It does all this and more. The code below is out of date with QGIS 2.0 and above.
Here is some Python code that you can stick in a file and use inside QGIS:
QGIS does have a method in it API to do liner referencing however I couldn't get it to work correctly, but I will contact the author of the code and see if I was doing something wrong.
For now you will need the shapely Python library, which you should install anyway because it's handy to have around. It also has great documentation at http://toblerity.github.com/shapely/manual.html
This is the section I am using in the following example http://toblerity.github.com/shapely/manual.html#interoperation.
Most of the following code is QGIS boilerplate code just creating the features, layers, converting from wkb and wkt and back. The core bit is the point = line.interpolate(currentdistance)
which returns a point at a distance along a line. We just wrap this in a loop until we run out of line.
import qgis
from qgis.core import *
from PyQt4.QtCore import QVariant
from shapely.wkb import loads
from shapely.wkt import dumps
vl = None
pr = None
def createPointsAt(distance, geom):
if distance > geom.length():
print "No Way Man!"
return
length = geom.length()
currentdistance = distance
feats = []
while currentdistance < length:
line = loads(geom.asWkb())
point = line.interpolate(currentdistance)
fet = QgsFeature()
fet.setAttributeMap( { 0 : currentdistance } )
qgsgeom = QgsGeometry.fromWkt(dumps(point))
fet.setGeometry(qgsgeom)
feats.append(fet)
currentdistance = currentdistance + distance
pr.addFeatures(feats)
vl.updateExtents()
def pointsAlongLine(distance):
global vl
vl = QgsVectorLayer("Point", "distance nodes", "memory")
global pr
pr = vl.dataProvider()
pr.addAttributes( [ QgsField("distance", QVariant.Int) ] )
layer = qgis.utils.iface.mapCanvas().currentLayer()
for feature in layer.selectedFeatures():
geom = feature.geometry()
createPointsAt(distance, geom)
QgsMapLayerRegistry.instance().addMapLayer(vl)
Copy and paste the above code into file, I called my locate.py, in ~./qgis/python
directory (because it is in the Python path) and just do the this in the Python console inside QGIS.
import locate
locate.pointsAlongLine(30)
That will create a new point layer with points at every 30 meters along the selected lines, like so:
Note: Code is pretty rough and might need some clean up.
EDIT: The lastest QGIS dev build can now do this natively.
Change the while loop in createPointsAt
to:
while currentdistance < length:
point = geom.interpolate(distance)
fet = QgsFeature()
fet.setAttributeMap( { 0 : currentdistance } )
fet.setGeometry(point)
feats.append(fet)
currentdistance = currentdistance + distance
and you can remove the
from shapely.wkb import loads
from shapely.wkt import dumps
One workaround by using PyQGIS is in the next code. I put your original distances in a list but, it was assumed that each point should be placed in positions along the line based in a sum_distances list ([50, 150, 201]).
registry = QgsMapLayerRegistry.instance()
layer = registry.mapLayersByName('my_line2')
distances = [50, 100, 51]
sum = 0
sum_distances = []
for dist in distances:
sum += dist
sum_distances.append(sum)
feat = layer[0].getFeatures().next()
geom_points = [feat.geometry().interpolate(distance).exportToWkt()
for distance in sum_distances]
epsg = layer[0].crs().postgisSrid()
uri = "Point?crs=epsg:" + str(epsg) + "&field=id:integer""&index=yes"
mem_layer = QgsVectorLayer(uri,
'points',
'memory')
prov = mem_layer.dataProvider()
feats = [ QgsFeature() for i in range(len(sum_distances)) ]
for i, feat in enumerate(feats):
feat.setAttributes([i])
feat.setGeometry(QgsGeometry.fromWkt(geom_points[i]))
prov.addFeatures(feats)
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
After running the code at the Python Console of QGIS I got:
It seems to work correctly.
Best Answer
Here's a quick PyQGIS script which should do the trick
Just change the
num_points
field name and output file name to match your data, select the input layer, and run it in the python console.