0

I have a CSV/Excel file which consists of many rows of the format

n x1 y1 x2 y2.

where n is the name of the line, and x1 y1 is the coordinates of the start of the line and x2 y2 the coordinates of the end of the line.

I'd like to import this into a QGIS project where each pair of points is connected and the line is associated with its name. If I edit the file in the form

n x1 y1 1 1
n x2 y2 2 1
n x1 y1 1 2
n x2 y2 2 2
etc...

I can import them as points then join them as lines, but I'm not sure the names would stick. I have used the QuickWKT importer to get the lines, but they don't have a name/label which is important. There are 139 lines so I'd rather not add labels manually.

Is there an easy way to do this?

I'm using QGIS 3.28 Firenze.

1
  • Either convert your data to WKT before importing to QGIS, or use join attributes table to make sure the names are added properly.
    – Erik
    Commented Aug 14 at 14:07

1 Answer 1

1

In QGIS you can make use of virtual layers. Remember that they are computed on the fly, so you may want to export the result.

Add your csv to QGIS (drag and drop), then go to Layer/ Add layer/ add-edit virtual layer and enter the following query with the proper layer name. You may also want to specify the coordinate system.

select *, make_line(make_point(x1,y1),make_point(x2,y2)) as geometry
from my_csv_file
1
  • Perfect! Thank you so much, that is a great help. Commented Aug 15 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.