4

How do I construct a line from this set of data?

row 1: 11.617573 -86.099086 91 4.000001;11.617569999999999 -86.09908999999999 91 4.000001;11.617593 -86.099119 90 4.000001;11.61765 -86.09920699999999 89 4.000001;11.617773 -86.09936499999999 89 6.000002;11.61778 -86.09949 88 6.000002;11.617709999999999 -86.0996

row 2: 10.793031 -83.69040299999999 13.0 3.0; 10.792513 -83.690227 13.0 3.0; 10.79214 -83.69010899999999 13.0 3.0

I have a csv file with a field that includes this type of data, and I need to construct a line from this, the number of xy pairs varies from within this field.

I use QGIS 3.

6
  • If you can import the CSV as points, there are several tools to convert points to lines.
    – csk
    Jun 27 '18 at 17:32
  • No, I can´t because GPS data (coords, altitude and precision) is all grouped and just delimited by space, all in one single field, and there might be two or more GPS points Jun 27 '18 at 18:59
  • Can you show us exactly what is in the CSV? You say this is a field, so you've got some other stuff, then "comma - stuff like row1/row2 - comma"? Which bits of row1 and row2 are the coordinates? The first two of the space-separated numbers between the semicolon-separated quads?
    – Spacedman
    Jun 28 '18 at 18:29
  • I think the best solution is going to be to turn the field into a LINESTRING in a new field and then use the approach of gis.stackexchange.com/questions/34342/…
    – Spacedman
    Jun 28 '18 at 18:31
  • Do you need to retain the elevation and precision, or can they be discarded? Jul 3 '18 at 20:54
7

To get this into a useful format as a CSV is going to take some doing.

The first step is to open the data in a text editor (like Notepad) and replace the semicolons with commas (edit>replace, or ctrl-h). That gives you a group of items (with internal spaces), separated by commas, as below:comma separated values in text file

Note that I am only using points that you have listed as "Row 1" for this example.

Save the result as a CSV file and open it in a spreadsheet program (I'm using Excel). You should get a row of items, with the contents of each cell made up from the values in your data. If each of the items include coordinates (and it looks like they do, in the first two values in each group), you'll need to turn that row into columns, which is pretty easy to do. Create a new sheet in your CSV, select and copy the row of your data. You must choose "copy" since this paste command does not work with "cut" data, which is why we've created a new sheet. Choose the first cell on the new sheet that you wish to save the data in and right-click. In the "Paste Options:" palette that appears, you'll find the "transpose" option (highlighted below), that converts the row of cells into a column of the same length.

Transpose tool highlight

At this point, using the text to columns wizard in the data ribbon will allow you to choose space as a character delimiter to break the cells into columns. I suggest inserting a row and at least adding Lat and Long headers to the two coordinate columns (or X and Y, depends on what you need). You'll need some way to refer to them in QGIS when you import the layer. You should get a result like below: Coordinate Table

Save that file as a CSV (you'll get a warning about saving the current sheet, which is fine, that's what you need), which you can bring to QGIS. Open QGIS and select the "Add Delimited Text Layer" tool. You should browse to the location that you saved the CSV and select it. You'll need to identify the columns that have to X and Y coordinate data under the "Geometry Definition" tab, so select "Long" and "Lat", respectively. CSV Import

Open the Processing Toolbox (under the Processing Menu) and look for the "Points to Path" tool. (It is under the Vector Creation tab, or you can search for it) In the interface for the tool, make sure that your points are selected as the input layer and give the tool an order in which to connect the points (I'm using the "Long" field here, but you might have reasons to choose another field, or add a field that you can use to identify the drawing order before you import the CSV.) You can also group the points to get smaller path segments, or just work from a selection of points. You can save the path, or just create a temporary layer to save later, and hit "Run in Background" to start the process.

Points to path

After it has finished, you'll have a layer that is a path between all of the points in your data, although it is independent from them, so you can style or edit the path as you need. If it is what you want, then right-click on the layer in the layer stack, select "Export>Save Features As.." and save it to your file directory.

Points to path output

2
  • Thank @Josh ! this is very simple and clever. I will use this procedure to write a guide so my colleagues can use the data as they needed to. Thanks again. Jul 21 '18 at 23:03
  • How can I import a line label with the CSV data?
    – bdelliott
    Oct 21 '20 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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