I have the following situation:
I have two CSV files: One file lists all nodes and their longitude and latitude in the following manner with the following columns:
ID | Name | Longitude | Latitude |
---|
No importing these points into QGIS is no problem. However, I would now like to import a second CSV to connect these points. That CSV has the following columns
Connection ID | From | To | To | To | To | To |
---|
I give each connection an ID and then under "From"
and "To"
I put the "ID"
that corresponds to the nodes in the first CSV files. Importantly, these connections usually have more than two points (thus the several "To"
columns).
Is there an easy way of creating these connections (lines) between the points in the first CSV? And is it possible to do it with the second CSV files I am using, most importantly in terms of having multiple points for each connection?
I know that certain plugins ("PointstoPaths") can be used for this, but I am using QGIS Firenze and have not been able to find a suitable plugin that works with this version.
1 Answer 1
I created a CSV file, with columns with the same name and then I load it in QGIS. What QGIS does is to add a consecutive suffix separated by an underscore in the repeated fields (e.j. To, To_1, To_2, ...). So every "To" field name has the form To_#
.
Try this, open the "Geometry by expression" tool, set the output geometry type to the line and use this expression. Remember to change the node layer name/id in the expression.
collect_geometries(
array_foreach(
array_filter(
map_akeys(attributes($currentfeature)),
regexp_match(
@element,
'^To_[0-9]+$'
)
AND
NOT attribute(@element) IS NULL
),
make_line(
geometry(
get_feature(
'Point layer name/id', --replace here with your values
'ID',
attribute(@element)
)
),
geometry(
get_feature(
'Point layer name/id', --replace here with your values
'ID',
attribute('From')
)
)
)
)
)
Then use the "Multipart to singleparts" tool to separate each different line, because the geometry generator will generate a multipart geometry in this case.
Explore related questions
See similar questions with these tags.
Points to path
is available in QGIS 3.28 Firenze, its not a plugin: Menu Processing > Toolbox > Points to path: docs.qgis.org/3.28/en/docs/user_manual/processing_algs/qgis/… You must make sure you load the CSV in a way to create points