3

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.

Taras
  • 32,823
  • 4
  • 66
  • 137
  • 3
    From a ease of handling perspective you would be better with connection table |id|from|to|. At the very least all field names need to be unique such as ||Connection ID|From|To1|To2|To3|To4| Are all "to?" always non null? One feature per 'connection id' or just one feature per 'from' 'to?'? – Gault Drakkor Apr 04 '23 at 18:53
  • Okay thanks. I hope I understand what you mean: No, in certain cases "to" is empty. Can I ask what you mean by "feature"? – Granola2345 Apr 05 '23 at 08:15
  • Does the answer in this question help: https://gis.stackexchange.com/questions/141078/generating-line-segments-between-all-points-using-qgis ? – e-shirt Apr 05 '23 at 09:22
  • A tldr for what a qgis feature is, might be one row of a layer. The linked stack exchange has a decent answer. – Gault Drakkor Apr 05 '23 at 17:50
  • Points to path is available in QGIS 3.28 Firenze, its not a plugin: Menu Processing > Toolbox > Points to path: https://docs.qgis.org/3.28/en/docs/user_manual/processing_algs/qgis/vectorcreation.html#qgispointstopath You must make sure you load the CSV in a way to create points – Babel Apr 09 '23 at 15:16
  • By the way: sharing your csv files and showing a screenshot with how you want the result to look like would help a lot to understand what to intend to achieve. – Babel Apr 09 '23 at 15:20

1 Answers1

4

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.

Taras
  • 32,823
  • 4
  • 66
  • 137
Mayo
  • 3,902
  • 3
  • 22