enter image description here

I'm trying to solve a spatial problem in QGIS. I have a set of polygons which I would like to add points along the centre line of each polygon. These points should be placed at 3 metres between each other. I've used different tools such as 'Points along geometry' or a plugin called 'QChainage' but the result is what you see on the picture below.

Could you help me with this?

Either using a tool/plugin in QGIS or a script in python that I can save anywhere.

  • what exactly did u do to get the result u show ? – Snaileater Apr 3 '19 at 16:18
  • I used 'Points along geometry' and distance 3m. But as you see, the points are on the vertices which I don't want. – Senda Apr 3 '19 at 16:21
  • never tested that treatment ... but that means u don't create this "mid-line" by yourself ? u can't or u don't want to ? are all your polygons rectangles ? – Snaileater Apr 3 '19 at 16:28
  • Yeah, I used another plugin called HCMGIS to find the centre line so these lines are multistrings. All the polygons are rectangles with different lengths so maybe I could use the length add to the points. – Senda Apr 3 '19 at 16:30
  • 1
    Points along geometry should do exactly what you want. It shouldn't add points at the vertices, and it shouldn't be placing points more frequently than the specified interval. Something strange is going wrong with that tool. Can you update your question with information about the settings you used in the points along geometry tool? – csk Apr 3 '19 at 22:18

edited after refining the solution

I was able to get a solution by starting from a model I had built for another question. The main issue here seems to be in creating the centerlines. Trying to refrain from using plugins, finding a way to use the graphical modeler was the most accessible. Of course, this assumes the rectangles are 'perfect' and they only have four vertices per polygon. Also, I added a sequential field in the rectangles attributes called Rect_ID to associate each segment to their original polygon. This is the only needed attribute and if you already have someting of the sort, then just change the field name accordingly.

The process I used is:

  • Polygons to lines: turn the rectangles into lines
  • Explode lines: get one feature per rectangle side
  • Field calculator: add the length of each side in the attributes
    • I called the field len, make sure it's float and use expression $length
  • Extract by expression: find the two short sides for each rectangle
    • round("len",3) = minimum(round("len",3), group_by:="Rect_ID")
  • Densify by count: add a vertice in the middle of each segment
    • Vertices to add: 1
  • Extract specific vertices: get each midpoint vertice from the segments
    • Vertex indices: 1 (0 is start and in this case the second vertice is the midpoint)
  • Points to path: using some of the generated attributes, create lines between points for each original rectangle
    • Order field: angle
    • Group field: Rect_ID
  • Field calculator: again, add a field called len containing the segment length (set field length and field precision to 0)
  • Extend lines: extend the centerlines so its length can be divided by 3
    • Start/End distance: (("len" - ("len" % 3) + 3) - "len") /2
  • Points along geometry: create the points along the centerline with the correct distance (here I used 3 meters)
  • Extract by location: Keep only the points inside the original rectangles using the are within predicate

The results look like this:

enter image description here

The model looks like this:

enter image description here

  • Wow! I didn't expect this. This is what I was looking for, a way to automate the task without using plugins. I'll definitely use this workflow. Thank you so much to share this. – Senda Apr 4 '19 at 19:43
  • Try it out but as I said, it doesn't seem to work entirely perfectly. If it's satisfying though, don't hesitate to resolve the question by accepting the answer! – Gabriel Apr 4 '19 at 19:45
  • @Senda I went back to my answer after thinking of a more robust way to do it. Now it should work for any rectangle with no issue and the start and end points will be equidistant from the rectangle edges. – Gabriel Apr 5 '19 at 20:31
  • Thanks for your solution but I'm still not getting the result that I'm looking for. It seems to extract 3 sides of the polygons instead of the two shortest sides. I've tried different things without luck. The thing that I know for sure is that the short side it's less than 2.5m for all the polygons so maybe this can help. See bellow a screenshot.@Gabriel C – Senda Apr 7 '19 at 9:58
  • When you look at the attribute table for the different segments that form the sides of your polygons (after calculating their lengths in a new column), do the values match for opposite sides? Are they matching at least to 3 decimals? Also, what's your project CRS? Make sure you're working in a projected system with planar units and not angular measurements. – Gabriel Apr 7 '19 at 22:23

The points on geometry tool will always put a point at the beginning of each line segment. My guess is that each "line" is actually made up of separate line segments (they could be separate features, or one multi-part feature).

If they're separate features, use the dissolve algorithm to combine them. If they're separate parts of a multi-part feature, use merge lines to combine them. Then try points on geometry again.

If that doesn't work, there may be small gaps between the start and end points of the different segments. Fix this by snapping the line layer to itself with snap geometries to layer. Repeat the appropriate combination method, and run points on geometry again.


This is the result that I get when I put 3 metres distance along the centre lines of each polygon. The lines are all Line (MultiLineString) and they have been created from a plugin called HCMGIS, not sure if this is relevant. Also I tried to change the distance to 5m and 10m and I get the same result as you can see on the image. It looks like that the tool is placing the points on each vertex which I don't want. @csk


Thanks for your solution but I'm still not getting the result that I'm looking for. It seems to extract 3 sides of the polygons instead of the two shortest sides. I've tried different things without luck. The thing that I know for sure is that the short side it's less than 2.5m for all the polygons so maybe this can help.

  • Since you're new to Stack Exchange, make sure to check the tour. Don't add precisions to your question in answers but edit your original question with the details. The site doesn't work like a forum, it's a Q&A and you just posted some Q related stuff in an A post. – Gabriel Apr 7 '19 at 22:25

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.