I have a CSV-file with latitudes and longitudes and I should calculate the distance between the points in ascending order (distance from 1 to 2, from 2 to 3, etc.)

I tried with the distance matrix but it calculates each point the distance with the others.

In addition, I am working in EPSG:23033 - ED50 / UTM zone 33N but when I made the distance matrix no measurement is returned. The ruler tool tells me that EPSG:7022 works automatically but I do not want to work with that CRS.

My initial CSV-file with 241rows looks like this:

enter image description here

I would like a new column.

  • 1
    You want a new table as output? Or a new column?
    – BERA
    Mar 25, 2021 at 13:24
  • 2
    Can you please show us how does your csv look like ?
    – Taras
    Mar 25, 2021 at 13:24
  • I would like a new column. my csv consists of 4 columns: ID-Date-Time-Latitude.vessel-Longitude.vessel and I have 241rows
    – lorenzo
    Mar 25, 2021 at 13:35
  • If this is a single path, you may use the ID to convert your points to a line, then split the line by the points and calculate the length of each segment.
    – Erik
    Mar 25, 2021 at 14:26

2 Answers 2


Load you points to the project as described in step 1 of the solution by @Taras.

You can then automatically calculate the length you want without first reprojecting the points - this is done inside the expression with the transform function:

length (
    transform (
        make_line ( 
            geometry ( get_feature_by_id ( @layer, $id )),
            geometry ( get_feature_by_id ( @layer, $id+1 ))

You can use this expression e.g. to set a dynamic label. If you want to create a new field, you must first save the layer in antother format (Geopackage, ShapeFile) as QGIS can't edit CSV files. If you reproject the layer during saving (see step 2 in the answer by @Taras), delete the transform () part of the expression (lines 2, 7 to 9 plus the , on line 6). If you leave the layer in EPSG:4326, use the expression exactly as stated above to get length values measured in EPSG:23033.

When you have the layer as Geopackage, use the expression with field calculator to create the new field.

When id + 1 exceeds the last number of a point feature, the result is a NULL value (no data): that is correct as the last point does not have a next point the measure a length.

Screenshot: as you see, the layer is a csv in EPSG:4326, the project is in EPSG:23033. Each point has a label that is dynamically generated by the expression above (plus a round function to get rid of decimals) that calculates the distance to the next point. The line between the points is also created dynamically with geometry generator, based on the same expression:

enter image description here

  • What happens when id + 1 exceeds the last number of a point feature?!
    – Taras
    Mar 25, 2021 at 20:52
  • It creates a NULL value. Updated the answer accordingly, with screenshot.
    – Babel
    Mar 25, 2021 at 21:01
  • You can define a custom value like 0 for the last point by adding this part at the beginning of the expression: if ( $id = count ( $id) +1, 0 , and an additional ) at the very end.
    – Babel
    Mar 25, 2021 at 21:23
  • thank you very much it works perfectly
    – lorenzo
    Mar 26, 2021 at 9:26

I assume that @Erik will agree with these basic steps:

  1. Import CSV into QGIS, via Layer > Add layer > Add delimited text layer... using the EPSG:4326
  2. Reproject your layer, e.g. use Export > Save features as... with the EPSG:23033 for 'CRS' (because your original layer seems to be in EPSG:4326). To avoid this step, see step 5.
  3. Apply the 'Points to path' to your imported point layer, use "ID" as the 'Order field'.
  4. Run the 'Explode lines' to split lines into linestrings
  5. In the Attribute table calculate linestring's length using the $length. Otherwise try the transform() function, i.e. length(transform($geometry,'EPSG:4326','EPSG:23033')). This step was described by @Babel in his answer.
  6. For getting back values from the original point layer, practise witchcraft with joins. Mind that there will be one-to-many connections because one linestring is a connection between two points.
  • Thanks, all works correct but the the unit of measurement is meters but the distances I get are wrong as if it did not measure in my sr
    – lorenzo
    Mar 25, 2021 at 16:02
  • I need to see your data, can you share a piece of it ?
    – Taras
    Mar 25, 2021 at 16:45

Your Answer

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

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