2

In QGIS I have a street layer (lines) and a house layer (points) with a start point (red rectangle):

enter image description here

I want to cluster the points alongside the line layer in groups of 20 or 10 points (I prefer 20). The result should look like that:

enter image description here

Each coloured line represents a different cluster. The result was made manually and took a long time, is there any possibility to cluster the points automatically? I only prefer groups of 20 and 10 adresses per line, in some cases I have to use a bit less or more.

3
  • For such questions it would always be good to provide a sample dataset for testing as it takes a while to create a project with layers.
    – Babel
    Nov 29, 2022 at 9:58
  • @Babel ok, how or where can I provice layers in a question?
    – fabian96
    Nov 29, 2022 at 10:05
  • You can't upload here. Upload to a cloud and post the link in your question - edit the initial post.
    – Babel
    Nov 29, 2022 at 10:06

1 Answer 1

2
  1. Project the points (houses) to the line (streets). Use the following expression or see here for details: closest_point (overlay_nearest ('street',$geometry)[0],$geometry). Name the resulting point layer as addresses.

    Red points created based on your blue points: enter image description here

  2. Create the shortest path from your start point to each address along the street layer with network tools. Run Menu Processing > Toolbox > Shortest path (point to layer).

    enter image description here

  3. In the resulting line layer (a line for each address/house), calculate how many addresses/houses are on this line. Use this expression to create a new attribute field called no_of_houses: array_length(overlay_nearest ('addresses',$id,limit:=-1,max_distance:=0.01))

  4. Delete features with a value for no_of_houses smaller than 20. Keep the ones with no_of_houses = 20.

  5. Lines with value larger than 20 have to be split up. Select features with no_of_houses=20 and create the end point of these lines with Geoemtry by expression: end_point ($geometry). Name the resulting layer end_points.

  6. Use these end points to split the remaining line layers with no_of_houses < 20 and keep only that part of the line after the end points. Use this expression with Geoemtry by Expression:

    line_substring (
     $geometry,
     line_locate_point (
         $geometry,
         overlay_nearest('addresses',$geometry)[0]),
     length($geometry)
     )
    
  7. Repeat step 3 to 7 until no more no_of_houses values are larger than 20.

  8. Don't forget to delete remaining superfluous lines, resulting in step 6. Use e.g. Delete duplicate geometries.

3
  • thank you for your reply! Sadly I get after the third step every time the total number of addresses and not the number of addresses on a specific line. And before that I don't get a line for each address/house. I am not sure about the first step, I think I made some misstakes there maybe.
    – fabian96
    Nov 29, 2022 at 12:10
  • here you can find the concrete Layer examples: ln5.sync.com/dl/970d2d420/zp8webf5-hghnis7b-dewfmeug-vdx7saav
    – fabian96
    Nov 29, 2022 at 13:47
  • Based on your dataset: be sure to run the expression from step 1 on the layer Adressen test and use the name of your street layer, thus with this expression closest_point (overlay_nearest ('street test',$geometry)[0],$geometry). In Geometry by expression, set Output Geometry Type to point. See here for details: gis.stackexchange.com/a/392619/88814 Try this first step and if it doesn't work, describe in detail where you're stuck. Result after step 1 should look like this (red points from you initial blue points): i.stack.imgur.com/btKkT.png
    – Babel
    Nov 29, 2022 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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