3

I would like to use a function like SAGA LINE-POLYGON INTERSECTION in QGIS, that splits a polyline into several polylines at every intersection of another layer of polygons. I would like the polylines index to be incremented along the orignal polyline (start_point = Index : 0 ; end_point index = 126)

In others words, I would like to keep the same logic of index incrementation used while digitizing the orignal polyline, defined by start_point and end_point.

I use 2 layers:

  • Polygons layer named : Polygon
  • Polyline layer named : Main Polyline enter image description here My aim is to create one polyline at the intersection of every polygon. That is why I use this function : SAGA LINE-POLYGON INTERSECTION. enter image description here

It creates a new layer Intersection in which there are several polylines as I expect. enter image description here The problem is that the index given at every new polyline created doesn't match with the digitization of the original polyline enter image description here

Regarding the start point and end point of the original Polyline, the index should look like this :

enter image description here

I'm blocked at this step. My final aim is to attribute an incremented index to each polygon along the (original) polyline, following its direction.

Could my method work? if so, how to fix the index issue?

If not, what workflow should I use instead?

2 Answers 2

3

You can achieve this using a virtual layer.

The idea is to compute the intersections between the line and the polygons, to locate the intersection along the original line (expressed as a % along the line) and finally to change this % to an ordered index.

Go the the menu layer / add layer / add-edit virtual layer and enter the following query:

select geometry, ROW_NUMBER() over (order by lpcent) as ID FROM 
(
  select st_intersection(l.geometry, p.geometry) as geometry,  
         ST_Line_Locate_Point(
           l.geometry, 
           st_startpoint(
             st_intersection(l.geometry, p.geometry)
           )
         ) as lpcent
from myPolygon p
 join myLine l
 on st_intersects(l.geometry, p.geometry)
)

enter image description here

edit

If you have multiple lines and want to set an order line by line while not restarting the index, you can select the line ID and order by line ID + percentage along the line:

select geometry, ROW_NUMBER() over (order by id, lpcent) as ID FROM 
(
select st_intersection(l.geometry, p.geometry) as geometry,
       l.id,
       ST_Line_Locate_Point(l.geometry, st_startpoint(st_intersection(l.geometry, p.geometry))) as lpcent
from p
 join l
 on st_intersects(l.geometry, p.geometry)
)

enter image description here

10
  • thanks for you answer, it works perfectly. But is there any way to modify this code to make this work in the case there are several entities (polyline) into myLine? For example : if I stop the 1rst line at index 18, then, then the second line start_point will be index 19 ?
    – zlikotp
    Jul 20 at 13:19
  • @zlikotp see edited answer for a multi-line-single-index solution
    – JGH
    Jul 20 at 13:28
  • I'm not able to reach the same result than you. Could you double check the code please? I renamed my layer : myLine and myPolygon (in order to work with your first code) but with the second, the index doesn't look like your screenshoot with 2x lines
    – zlikotp
    Jul 20 at 13:46
  • it orders the output by line ID, but it won't re-order the original lines, so if the left line has id 1 and the right line has id 2, the output will be very different than shown in my screenshot.
    – JGH
    Jul 20 at 14:13
  • my right line ID is 0, and my left line ID is 1 but it doesn't work as expected. I do not know where is my mistake : the output IDs are mixed randomly between both lines. I had to modify this part to run the second code : from myPolygon p join myLine l
    – zlikotp
    Jul 20 at 14:21
3

I Found a workflow that should work fine if you have a single line going through all your polygons:

  1. clip your line by polygon to keep only the segments of line you need with the "Clip" processing algorithm using line layer as input layer and polygon layer as overlay
  2. use the "Multipart to singleparts" processing algorithm to split the feature in the 'Clipped' layer into multiple features

You end up with a 'Single parts' layer that should have features with $id's sorted by the direction of the original line

ex: enter image description here

6
  • +1, but is it guaranteed to always be ordered in this order?
    – JGH
    Jul 20 at 12:33
  • I am not a 100% certain but its logical considering how I think the clipping algorithm works since it's procedural but thats why I mentioned the fact that it might not work if the line was splitted either in multiple feature or as a multilinestring Jul 20 at 12:45
  • This solution works , thanks ! But I would prefer to use the one of @JGH because I also need to manage the case of several lines and I guess python is the only way to reach this aim.
    – zlikotp
    Jul 20 at 13:23
  • No problem, You can still try it with multiple lines if they are in order it will likely work Jul 20 at 13:26
  • Hello @LouisCottereau unfortunatly, ID 1 is in first, then ID 0, and then ID 2 instead of 0, 1 , 2
    – zlikotp
    Jul 21 at 3:49

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.