Calculating distance between consecutive rows in QGIS

I have a attribute table with point data (ordered on id) that looks like this. I want to calculate the distance between consecutive rows in meters (EPSG:28992)

fid  car_id   lng       lat
1    57       4.932413  52.372022
2    57       4.932533  52.372323
3    57       4.932553  52.372994
4    57       4.932663  52.372312
5    57       4.932333  52.372582
6    57       4.932213  52.372693
7    26       4.932223  52.372343
8    26       4.932413  52.372372
9    26       4.932103  52.372952
10   26       4.932223  52.372912

Eventually I would like to have something like this with an extra distance column that shows the difference between each pair of points. Any ideas on how to do this in the field calculator of QGIS 3.10?

fid  car_id   lng       lat             distance
1    57       4.932413  52.372022       0
2    57       4.932533  52.372323       47
3    57       4.932553  52.372994       29
4    57       4.932663  52.372312       58
5    57       4.932333  52.372582       46
6    57       4.932213  52.372693       56
7    26       4.932223  52.372343       38
8    26       4.932413  52.372372       60
9    26       4.932103  52.372952       21
10   26       4.932223  52.372912       31
• If there are ten points, that means there are nine consecutive distances. Mar 4 '20 at 23:20
• The distance "as the crow flies" or the distance a car would take on streets? (Considering that you have a car_id column) Mar 5 '20 at 0:59
• The distance as the crow flies is what I need Mar 5 '20 at 7:34

I assume that the point layer is in WGS 84. You can use the following script using QGIS Python Editor. Select the layer and run the script.

from qgis.PyQt.QtCore import QVariant

layer = iface.activeLayer()

# if "Distance" field doesn't exist, add one.
if layer.fields().indexFromName("Distance")==-1:
field = QgsField("Distance", QVariant.Double, prec=3, len=10)
layer.updateFields()

index = layer.fields().indexFromName("Distance")

# create feature list
fs = list(layer.getFeatures())
# sort the list by fid.
fs.sort(key=lambda f: f["fid"])

transform = QgsCoordinateTransform(QgsCoordinateReferenceSystem(4326), # WGS 84
QgsCoordinateReferenceSystem(28992),
QgsProject.instance())
for i in range(len(fs)-1):
geom1 = QgsGeometry(fs[i].geometry())
geom2 = QgsGeometry(fs[i+1].geometry())
geom1.transform(transform)
geom2.transform(transform)
distance = geom1.distance(geom2)
layer.dataProvider().changeAttributeValues({fs[i].id(): {index: distance}})

Result: • Thanks for sharing your code. It does create a distance column but unfortunately without any values. Besides, I would like to to have the distance between two points displayed in the row of the consecutive point. So in your example, the distance from point 1 to 2 (889,075) should be presented in row 2 Mar 5 '20 at 8:44
• What are CRS of the point layer and CRS of the project? It seems point layer is in WGS 84. I think so, because it has lat, lon fields. Though, that doesn't mean the point layer is in WGS 84. If it is in WGS 84 and if you want to get distances in EPSG:28992, points need transformation. If it is in EPSG:28992, no need to transform. Mar 5 '20 at 13:55
• Also, the script doesn't use lat, lon fields to calculate distances. It gets coordinates from point geometry. Even if the layer has lat, lon fields, if the layer is in EPSG:28992, it's possible that Distance has no values, because of transformation. Mar 5 '20 at 14:01