I have 10 points in QGIS layer as shown in Image 1
.
I am trying to connect points with lines in required sequence as shown in
Image 2
. However, when I tried to connect all points with virtual layer, the connections are not according to my required sequence (yellow lines) as shown inImage 3
. What should I do to get the required connections?The features in attribute table are shown here. Do I need to change the IDs of points to achieve the required connections?
I was thinking about parent layer and child layer for required connections but I am not sure how to achieve that.
The shapfile is available here
Virtual layer query that I have tried (Image 3):
In this query, I am trying to connect points with line by saving the start and end points
of each connecting line.
with
build_segment as (
select
a.GlobalID as start_point,
b.GlobalID as end_point,
makeline(a.geometry, b.geometry) as geometry
from Points_Colac_Demo_advanced a
left join Points_Colac_Demo_advanced b on (a.OBJECTID + 1 = b.OBJECTID))
select
*
from build_segment
where geometry is not null
2 Answers 2
Looking at your files, your feature id and OBJECTID are different, I would first update the OBJECTID column with the feature id (using the calculator $id varible) then create a new column called 'to_id' (or something else you want. then attaribute this new column with the connecting points id (from the updated UNIQUEID column).
You can then simply apply a geometry generator to generate the lines on the fly, see below screenshots:
EDIT: You can also use the "Join by lines" option in the processing toolbox to generate the lines. Process would still be the same as my original answer, after updating the IDs you duplicate the layer and use the tool on the two layers joining the original OBJECTID with the to_id field. See image:
EDIT 2: To use the value of a feature from the browser you would use the get feature options:
make_line(
$geometry ,
geometry(
get_feature( 'Points_Colac_Demo_advanced', 'OBJECTID' , to_id )
)
)
Simply substitute for the filed you want to use "GlobalID" in your case.
Just dropping this here for a Virtual Layer: Connect all points with all other points with lines and putting length in meters into the attribute table.
SELECT
a.fid * b.fid AS id,
make_line(a.geometry, b.geometry) AS line,
st_length(make_line(a.geometry, b.geometry)) AS length
FROM locations a
JOIN locations b ON b.fid > a.fid
Explore related questions
See similar questions with these tags.
a.id = relationTable.FromID
andb.id = relationTable.ToId