TransWikia.com

Creating multiple separate vector lines from list of coordinate pairs in QGIS

Geographic Information Systems Asked on June 21, 2021

I’m a QGIS user with 2 years of experience but I don’t know how to do this task easily. I’m on QGIS 3.10.6 A Coruna.

I know how to import CSV coordinate points and then convert them to a vector using "Points to Path" but I don’t want to make one long vector from all of the points, I would like to create multiple separate vectors from each coordinate pair.

I could import each pair as a separate CSV list of two points, join them ("Points to Path"), and then merge each resulting vector file but I’m hoping for an easier way as I will have many pairs to import and merge into one layer.

My data looks like the snapshot below (coordinate system: EPSG:27700 – OSGB 1936 / British National Grid). I can also easily reformat the CSV if needed.

Is there a more efficient way to do this?

enter image description here


The Linestring / WKT geometry is the most solution in my case due to the application of the dataset:

Each point marks the start and end of sections of interest on a train line. After visualization, I can take a subset of points separated by larger distances to add a 3rd point in the Linestring expression to better match the curve of the track and then re-import the CSV.
I also found a similar explanation to the problem at:
https://www.reddit.com/r/QGIS/comments/98t8r3/generating_lines_from_csv_containing_xy_coords/

3 Answers

When working with CSV's, sometimes is more efficient to preprocess them before ingesting them in QGIS.

In this case, if I understand right, you want one "vector" (line feature) per line of data. So edit the CSV in Excel or OpenOffice or wherever you created it, to force the geometry you want using its WKT representation.

In your instance, the following Excel formula, entered in cell D2 and copied down, should work: ="LINESTRING("&A2&" "&B2&","&C2&" "&D2&")"

Note the distinction where you splice together with " " (between eastings and northings) versus "," (between points in the line). Call the column you have created WKT in cell D1 and resave the CSV.

Now import it in QGIS, but specify it should take the geometry from the WKT column (as WKT rather than (x,y), of course).

Alternative:

You could do something similar a bit more elegantly wholly within QGIS, for instance using the Field Calculator to create a new field with a QGIS expression involving the functions make_line and make_point.

But you'd need to write back the new field and persuade QGIS to change the geometry. So this approach would be more effective if your your data was already a table in GeoPackage or fancier database, where you could write the new field to the provider and then reimport the same data as a new layer. Or if you want merely to add connecting lines for visual display (using a make_line expression in a Geometry Generator renderer layer, for instance) while retaining the original point geometry.

Correct answer by Houska on June 21, 2021

Points to path allows you to sort and group your points to lines. Sorting is line-specific, and lines are defined by grouping. So you could add a column containing 1s, another containing 2s, and a third containing incrementing numbers.

When loading your data into QGIS, you load your starting points as one layer with the first line specific ID, and your end points as the second layer with the second line specific ID. Merge them, then create a new column holding the line specific ID, then run points to path.

Answered by Erik on June 21, 2021

  1. Drag&Drop your CSV-file into QGIS, so you can see it in Layers tab

    input

  2. Use a "Virtual Layer" through Layer > Add Layer > Add/Edit Virtual Layer... with the following query

    SELECT make_line(setsrid(make_point(start_easting, start_northing),27700),
                     setsrid(make_point(end_easting, end_northing),27700)
           ) AS geom
    FROM "test"
    

    and get lines as the output. In my case no attributes were created.

    result

Answered by Taras on June 21, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP