# How to create points in a specified distance along the line in QGIS？

I want to use QGIS creating points along the line, but in specified distance. I found two GRASS tools in SEXTANTE Toolbox：

• v.to.points
• v.segment

I tried v.to.points, setting the maximum distance between points in map units to 100 and got this：

The second photo showed a close look from the upper one. The result was what I want, but I want to get less points, but even I set the maximum distance to 1000, or 1000000, the result was the same.

I tried to use v.segment, but I had no file containing segment rules.

Underdark gave me some advice, and I tried Densify geometries given an interval. First it gave me a polyline shapefile the same as I input, and then I used extract nodes to try to get those points. But the result was the same as I got using v.to.points, no matter what I set the distance interval.

How can I create points along the line, and in specified distance？

-
Have a look at this, let us know nathanw.net/2012/08/05/… – Willy Jun 11 '13 at 11:02
Thanks for giving me this helpful advice! If I want to use your script, have I to install python? or in the new version QGIS 2.0, does it already have this function built in it? – Heinz Dec 3 '13 at 8:58
There is a plugin for this now in QGIS 2.0, called QChainage. Fetch the plugin and install it. – Willy Dec 8 '13 at 8:53
Note that the "Sextante" toolbox is called "Processing" in QGIS 2.2.0+ and the the Densify geometries given a an interval" is under the ProcessingToolbox/QGISGeoalgorithms/VectorGeometryTools menu. Note that this denifies the geometry and doesn't remove vertices that are closer than the the chosen interval. – Dave X Jul 22 '14 at 20:18

In the Python console:

1) create a memory layer class (complete class (without attributes) at crea_mem_layer.py)

``````class Create_vlayer(object):
'''creation of a virtual layer'''
def __init__(self,nom,type):
self.type=type
self.name = nom
self.layer =  QgsVectorLayer(self.type, self.name , "memory")
self.pr =self.layer.dataProvider()
def create_point(self,geometry):
# add point to the layer
self.seg = QgsFeature()
self.seg.setGeometry(QgsGeometry.fromPoint(geometry))
self.layer.updateExtents()
@property
def display_layer(self):
#end of layer and display layer
``````

2) vector algebra functions (from algèbre_vect_PyQGIS.py)

``````import math
def mag(point):
# magnitude of a vector
return math.sqrt(point.x()**2 + point.y()**2)
def diff(point2, point1):
# substraction betwen two vector
return QgsPoint(point2.x()-point1.x(), point2.y() - point1.y())
def length(point1,point2):
# with PyQGIS: sqrDist
return math.sqrt(point1.sqrDist(point2))
``````

3) direction cosines

``````def dircos(point):
cosa = point.x() / mag(point)
cosb = point.y()/ mag(point)
return cosa,cosb
``````

4) process line or line segments

``````def pairs(list):
# list pairs iteration
for i in range(1, len(list)):
yield list[i-1], list[i]

layer = qgis.utils.iface.activeLayer()
# interval between points
interval = 5 m
# create virtual layer
gen_pt  = Create_vlayer("mid5", "Point")

for elem in layer():
line = elem.geometry()
for seg_start, seg_end in pairs(line.asPolyline()):
line_start = QgsPoint(seg_start)
line_end = QgsPoint(seg_end)
# mid point = vector coordinates [x2-x1,y2-y1]
pointm =diff(line_end, line_start)
# direction cosines of the segment
cosa,cosb = dircos(pointm)
# length of the segment
lg = length(line_end, line_start)
# generate and add points to the virtual layer
for i in range(interval,lg,interval):
gen_pt.create_point(QgsPoint(line_start.x()  + (i * cosa), line_start.y() + (i*cosb)))

# display layer
gen_pt.display_layer
``````

Results

polyline, equidistant point with Shapely or PyQGIS2, with direction cosines

-
From looking at the junction, it looks like it keeps the pre-existing node points on the line, so it can't thin them to a resolution less than the vertices already on the line. With complicated, curvy lines as in the OP, these procedures may not have much of an effect. – Dave X Jul 22 '14 at 20:01
you can perfectly adapt the script. – gene Jul 22 '14 at 20:32

I've written a script that changes the Sextante Densify geometries tool to accept a certain distance. It's called Densify geometries given an interval.

After running Densify, you can extract the points using Extract nodes tool.

You can get it from Github and install instructions are on my blog.

-
Hi, Anita, I have installed SEXTANTE Toolbox of version 1.0.9, and I tried this 'Densify geometries given an interval', but got a polyline output. Should I install the 1.0.8 version? – Heinz Jun 12 '13 at 4:51
i think things are not quiet clear here. With underdarks tool, your just receive a new line layer, but with additional vertices, which are added to your original ones. But thats not what you are looking for, so i think the link in Willy's comment is the way to go (not out-of-the-box, but seems to be well explained) – Bernd V. Jun 12 '13 at 9:56
Sextante offers an "extract nodes" tool for that. – underdark Jun 12 '13 at 17:09
but then you still got your original vertices from the line feature which do not fit into the set distance scheme, right? I don't see a way to remove them easily from the point feature after extracting the nodes. I think he is asking for a chainage function just as described in Nathans blog linked by Willy above. – Bernd V. Jun 12 '13 at 21:41
Yeah, I still got the same result from v.to.points, and the distance interval I set was not matter. – Heinz Jun 13 '13 at 3:40

There is now a plugin called QChainage that does exactly what you want. You specify an interval distance and the plugin creates points along your line at the specified interval.

-
*Note that this won't capture your line nodes, so if you need them you will also need to use the geometry tool - extract nodes and append to your chainage points. – Jason Jun 25 '14 at 3:15
Welcome to GIS SE! I think the information that you have appended here as a Comment would be better incorporated into your actual Answer by using the edit button beneath it. You can do this with a Copy/Paste and the delete your comment by clicking the little cross next to it. – PolyGeo Jun 25 '14 at 3:49

Well not sure if this is in the stable release, but in the sextante toolbox on 1.9 alpha under Geoalgorithims->Vector is the "Convert Lines to Points" option. Works a treat, be great if it added a field for distance along the line as well though.

Interesting it did add the NAME field from my vector layer though.

I tried getting that Python Script by Nathan Woodrow working, but I suck with python. And with code in general it seems. :p

-

If you don't want to horse around with python scripts you can simply install and use the "Profile from line" plugin and ignore / delete the raster value column. The chainage will be whatever you set teh sampling interval to be.

Neil

-
In QGIS 2.2.0, I see a "Profile tool", "qProf" and "VoGIS-ProfilTool", but not a "Profile from line" tool. – Dave X Jul 22 '14 at 20:05