5

I have a CSV file that contains attributes for a start node and end node of a pipe in a single record. This is data collected in the field, so the inspector captures the lat/long at each end of the pipe, and then I load the CSV into QGIS to use other attribute data. I would like to find a way to automate/repeat creating a line layer that draws a feature from each start node attribute to the end node attribute for each record.

Here is an example of the data captured in the field. I would like to automate creating a single line from "Location of inlet" to "Location of outlet" for each record in the CSV.

Example of start/end node attributes

1
  • 4
    Providing this sample data not as a screenshot, but as actual data would help using it for testing.
    – Babel
    Aug 18, 2021 at 15:57

5 Answers 5

2
  1. Load the CSV-file twice as a point-layer - see: Importing a delimited text file:
  • The first time, set the coordinates for inlet (lat/lon 1) as coordinate values,

  • the second time those for outlet (lat/lon 2).

    You get two point-layers, one for inlet, one for outlet. Be sure to define the Geometry-CRS as EPSG:4326 (cf. screenshot).

  1. On the inlet layer, now create a line using "Geometry generator" or "Geometry by expression" (see here for details). Use this expression, it will create the lines:
make_line (
    $geometry,
    geometry(
        get_feature_by_id('outlet', $id)
        )
    )

Loading the outlet-layer at the bottom left, creating the line geometry using Geometry generator at the upper right:

enter image description here

1

The "PointsToPaths" plugin (note the plural "Paths") will output a separate line feature for each sequential pair of input points.

Additionally, the tool will add two new fields to the output attribute table that contain the beginning and ending point values, which in your example would be "Location of inlet" and "Location of outlet". Make sure that you check on the Line per vertex option.

enter image description here

1

If you can open the CSV in a spreadsheet editor, you can add a column (ie a new field) at the end which builds the following text string before you import the CSV:

LINESTRING(X1 Y1, X2 Y2)

In Excel, that would look something similar to

=concat( "LINESTRING(",X1," ",Y1,", ",X2," ",Y2,")" )

where you would substitute the X1, X2, Y1, Y2 fields with the cell reference for the coordinates.

This defines your line by the two endpoints in the WKT (well known text) format for a line.

When you create your layer from the delimited text file, under "Geometry Definition" select the WKT option, select your LINESTRING field with the Geometry Type set to "Line".

Now you have your line layer from your CSV and you never had to load the layer twice, put the coordinates on separate lines, or use Points to Path (I've done those as well!). The great thing about QGIS is that there are multiple ways to arrive at the same results, no way is any more right than any other. Play around, try different techniques and enjoy the journey on your path to GIS enlightenment.

0

This is not straight-forward, but I don't know any alternative in QGIS:

You can use the Points to Path Vector creation tool (search in the Processing Toolbox). To use it, you need to restructure your data (most convenient if you're familiar with Python or R): Each point needs to be in a seperate row, not one row for both start and end points. Create an attribute that is 0 for start points and 1 for end points (for parameter Order field). Furthermore, start and end point need to have one similar attribute/id (for parameter Group field) - then you're ready to use the tool.

0

You could edit your csv file to create a new field containing the WKT for a linestring. This could be accomplished using a spreadsheet program, a text editor, or any other method. Then load the CSV file into Qgis and declare that new field to be the geometry for that record.

Admittedly a bit hacky, but it's a fallback when you have data you can't coerce otherwise.

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.