Hi
Wonder if anybody work with the gps distance calculation.
Any suggestions how to implement the Haversine formula?
Thanks.
Haversine formula
-
viktor_au
- Posts: 60
- http://meble-kuchenne.info.pl
- Joined: Wed Jul 12, 2023 7:09 am
- Has thanked: 11 times
- Been thanked: 19 times
-
chipfryer27
- Valued Contributor
- Posts: 1768
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 391 times
- Been thanked: 602 times
Re: Haversine formula
Hi Viktor
Whilst not actually doing it myself I did earmark this site as it gave many different ways to calculate, and also mentions a TRS-80 being used to calculate (my second computer)
https://www.movable-type.co.uk/scripts/latlong.html
Might be of some interest.
I found a lot of sites "scary" with their equations, but it isn't too difficult really and the above site also has some excel formulas.
Regards
Whilst not actually doing it myself I did earmark this site as it gave many different ways to calculate, and also mentions a TRS-80 being used to calculate (my second computer)
https://www.movable-type.co.uk/scripts/latlong.html
Might be of some interest.
I found a lot of sites "scary" with their equations, but it isn't too difficult really and the above site also has some excel formulas.
Regards
-
medelec35
- Matrix Staff
- Posts: 2191
- Joined: Wed Dec 02, 2020 11:07 pm
- Has thanked: 665 times
- Been thanked: 743 times
Re: Haversine formula
Hi Victor.
This is what I found:
Haversine Formula - Simple Explanation
The Haversine formula calculates the distance between two points on Earth using their latitude and longitude coordinates. It accounts for Earth being a sphere (roughly), not flat.
What it does: Given two GPS coordinates (lat1, lon1) and (lat2, lon2), it tells you the straight-line distance across Earth's surface.
The Formula Simplified
The formula works in steps:
Calculate the differences in latitude and longitude
Apply sine functions to account for Earth's curvature
Use Earth's radius (6371 km) to convert to actual distance
Implementation in Flowcode
Here's how to break it down into Flowcode calculation icons:
Step 1: Convert degrees to radians
lat1_rad = lat1 × π / 180
lon1_rad = lon1 × π / 180
lat2_rad = lat2 × π / 180
lon2_rad = lon2 × π / 180
Step 2: Calculate differences
dlat = lat2_rad - lat1_rad
dlon = lon2_rad - lon1_rad
Step 3: Haversine calculation
a = sin(dlat/2)² + cos(lat1_rad) × cos(lat2_rad) × sin(dlon/2)²
c = 2 × atan2(√a, √(1-a))
distance = 6371 × c // Result in kilometers
In Flowcode Calculation Icons:
Use separate calculation icons for each step
Store intermediate results in float variables
Use the built-in sin(), cos(), sqrt() and atan() functions
You can then use the link that chipfryer27 posted for testing the formula works.
If will be best if you use floats for the constants, e.g. instead of 2 use 2.0
This is what I found:
Haversine Formula - Simple Explanation
The Haversine formula calculates the distance between two points on Earth using their latitude and longitude coordinates. It accounts for Earth being a sphere (roughly), not flat.
What it does: Given two GPS coordinates (lat1, lon1) and (lat2, lon2), it tells you the straight-line distance across Earth's surface.
The Formula Simplified
The formula works in steps:
Calculate the differences in latitude and longitude
Apply sine functions to account for Earth's curvature
Use Earth's radius (6371 km) to convert to actual distance
Implementation in Flowcode
Here's how to break it down into Flowcode calculation icons:
Step 1: Convert degrees to radians
lat1_rad = lat1 × π / 180
lon1_rad = lon1 × π / 180
lat2_rad = lat2 × π / 180
lon2_rad = lon2 × π / 180
Step 2: Calculate differences
dlat = lat2_rad - lat1_rad
dlon = lon2_rad - lon1_rad
Step 3: Haversine calculation
a = sin(dlat/2)² + cos(lat1_rad) × cos(lat2_rad) × sin(dlon/2)²
c = 2 × atan2(√a, √(1-a))
distance = 6371 × c // Result in kilometers
In Flowcode Calculation Icons:
Use separate calculation icons for each step
Store intermediate results in float variables
Use the built-in sin(), cos(), sqrt() and atan() functions
You can then use the link that chipfryer27 posted for testing the formula works.
If will be best if you use floats for the constants, e.g. instead of 2 use 2.0
Martin
Re: Haversine formula
Hi again.
Haversine
Did not work for me.
a = sin² (Δlat / 2) + cos(lat1) × cos(lat2) × sin²(Δlong / 2)
Flowcode:
a = (sin (((lat_diff / 2) * sin (lat_diff / 2))) + cos (Lat1) * cos (Lat2) * (sin (lon_diff / 2) * sin (lon_diff / 2)))
c = 2 * atan2 (sqrt (a),sqrt (1 - a))
d = 6371.0 * c
Example1 -Warsaw to Rome-1787 km
Result: 17.72km
LatDeg1 = 52.2296
LonDeg1 = 21.0122
LatDeg2 = 48.8919
LonDeg2 = 12.5113
Other examples:
0.42, instead of 20km
0.03 instead of 2km
Pythagorean theorem
https://smart.dhgate.com/a-simple-guide ... -location/
https://youtu.be/CWUr6Jo6tag
Flowcode
convert to radians, find the difference, etc.
c = sqrt (((lon2 - lon1) * (lon2 - lon1)) + ((lat2 - lat1) * (lat2 - lat1)))
d = 6371.0 * c
Examples
2.05km for 2km (as should be)
24.385km for 20km
1015km, instead of Warsaw to Rome-1787 km
4974km, instead of 3935.75km
Equirectangular method (used by Google maps)
convert to radians, find the difference, etc.
a = sqrt ((lon_diff * cos ((lat1 + lat2) / 2)) * (lon_diff * cos ((lat1 + lat2) / 2)) + (lat2 - lat1) * (lat2 - lat1))
c=a
d=6371.0 * c
Examples
2.05km for 2km (as it should be)
24.37km for 20km
1014km, instead of Warsaw to Rome-1787 km
4971km, instead of 3935.75km
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Looks like the Pythagorean method works OK for a small distances (<=20km) and should be OK for my boat. Note from internet: The Pythagorean formula can be used for GPS coordinates on a small, flat scale, but for accurate distances on Earth, a more complex formula like the Haversine formula is needed to account for the planet's curvature.
Haversine
Did not work for me.
a = sin² (Δlat / 2) + cos(lat1) × cos(lat2) × sin²(Δlong / 2)
Flowcode:
a = (sin (((lat_diff / 2) * sin (lat_diff / 2))) + cos (Lat1) * cos (Lat2) * (sin (lon_diff / 2) * sin (lon_diff / 2)))
c = 2 * atan2 (sqrt (a),sqrt (1 - a))
d = 6371.0 * c
Example1 -Warsaw to Rome-1787 km
Result: 17.72km
LatDeg1 = 52.2296
LonDeg1 = 21.0122
LatDeg2 = 48.8919
LonDeg2 = 12.5113
Other examples:
0.42, instead of 20km
0.03 instead of 2km
Pythagorean theorem
https://smart.dhgate.com/a-simple-guide ... -location/
https://youtu.be/CWUr6Jo6tag
Flowcode
convert to radians, find the difference, etc.
c = sqrt (((lon2 - lon1) * (lon2 - lon1)) + ((lat2 - lat1) * (lat2 - lat1)))
d = 6371.0 * c
Examples
2.05km for 2km (as should be)
24.385km for 20km
1015km, instead of Warsaw to Rome-1787 km
4974km, instead of 3935.75km
Equirectangular method (used by Google maps)
convert to radians, find the difference, etc.
a = sqrt ((lon_diff * cos ((lat1 + lat2) / 2)) * (lon_diff * cos ((lat1 + lat2) / 2)) + (lat2 - lat1) * (lat2 - lat1))
c=a
d=6371.0 * c
Examples
2.05km for 2km (as it should be)
24.37km for 20km
1014km, instead of Warsaw to Rome-1787 km
4971km, instead of 3935.75km
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Looks like the Pythagorean method works OK for a small distances (<=20km) and should be OK for my boat. Note from internet: The Pythagorean formula can be used for GPS coordinates on a small, flat scale, but for accurate distances on Earth, a more complex formula like the Haversine formula is needed to account for the planet's curvature.
-
chipfryer27
- Valued Contributor
- Posts: 1768
- Joined: Thu Dec 03, 2020 10:57 am
- Has thanked: 391 times
- Been thanked: 602 times
Re: Haversine formula
Hi Viktor
Your coordinates are a bit off
My attached chart gets the same result as in that link I sent.
I made a constant of Pi/180 using 3.1415926 for Pi and more or less used Martin's calculation. You could probably compress the calculations a bit (and tidy things up).
Regards
Edit...km)
A bit bored so I entered details of Manhattan and London. Chart was within 20Km of the site in the link (5568Km)
Your coordinates are a bit off
Puts you roughly at Straubing airport in Germany, 705Km from Warsaw.LatDeg2 = 48.8919
LonDeg2 = 12.5113
My attached chart gets the same result as in that link I sent.
I made a constant of Pi/180 using 3.1415926 for Pi and more or less used Martin's calculation. You could probably compress the calculations a bit (and tidy things up).
Regards
Edit...km)
A bit bored so I entered details of Manhattan and London. Chart was within 20Km of the site in the link (5568Km)
- Attachments
-
- Haversine-1.fcfx
- (10.9 KiB) Downloaded 8 times
Re: Haversine formula
Hi Iain.
You are a good man.
I see the difference between the:
-------------------------------------------------------------------------
a = (sin (((lat_diff / 2) * sin (lat_diff / 2))) + cos (Lat1) * cos (Lat2) * (sin (lon_diff / 2) * sin (lon_diff / 2)))
And:
a = ((sin(dlat / 2) * sin(dlat / 2))) + (cos(lat1_rad) * cos(lat2_rad)) * ((sin(dlon / 2) * sin(dlon / 2)))
-----------------------------------------------------
If London= 51.5072° N, 0.1276° W, OR:
London latitude = 51.5072,
If Manhattan latitude =40.7685° N, 73.9822° W
The distance between London and Manhattan should be approximately
5,5785 kilometers. The result is: 5564.8km
Thank you for the correction.
And 2km distance work OK as well.
You are a good man.
I see the difference between the:
-------------------------------------------------------------------------
a = (sin (((lat_diff / 2) * sin (lat_diff / 2))) + cos (Lat1) * cos (Lat2) * (sin (lon_diff / 2) * sin (lon_diff / 2)))
And:
a = ((sin(dlat / 2) * sin(dlat / 2))) + (cos(lat1_rad) * cos(lat2_rad)) * ((sin(dlon / 2) * sin(dlon / 2)))
-----------------------------------------------------
If London= 51.5072° N, 0.1276° W, OR:
London latitude = 51.5072,
If Manhattan latitude =40.7685° N, 73.9822° W
The distance between London and Manhattan should be approximately
5,5785 kilometers. The result is: 5564.8km
Thank you for the correction.
And 2km distance work OK as well.
- Attachments
-
- Haversine_2km_1.fcfx
- (19.04 KiB) Downloaded 6 times