It is currently Fri Apr 19, 2019 11:27 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 27 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Has anyone done the 'Haversine' formula?
PostPosted: Fri Feb 08, 2019 2:02 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 6:14 pm
Posts: 1671
Location: Germany (Saxony, Deutscheinsiedel)
Thanks for all this wonderful codes, which I'm successfully using to calculate the distance between two geographic points! :mrgreen:

But can you show me the other way around, meaning I have one point (latitude,longitude) and a specific distance (in km) around this point. How can I calculate the most-northern/southern latitude and most-western/eastern longitude matching this distance from the given point?

(I've seen a lot formulas on internet, but don't get it... latitude seems easy, but longitude... :?)

Thanks in advance! :D

_________________
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)


Top
 Profile  
Reply with quote  
 Post subject: Re: Has anyone done the 'Haversine' formula?
PostPosted: Fri Feb 08, 2019 3:44 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 4:34 pm
Posts: 884
Location: Canada
Code:
Define.d lat1,lon1,Distance,Bearing,Radius=6372.795477598
Macro DistFromPoint (lat1,lon1,Distance,Bearing)
   lat2.d=Degree(ASin(Sin(Radian(lat1))*Cos(Distance/Radius)+Cos(Radian(lat1))*Sin(Distance/Radius)*Cos(Radian(Bearing)) ))
   lon2.d=lon1+Degree(ATan2(Cos(Distance/Radius)-Sin(Radian(lat1))*Sin(Radian(lat2)),Sin(Radian(Bearing))*Sin(Distance/Radius)*Cos(Radian(lat1)) ))
EndMacro

DistFromPoint(49,-94,20,0) ;<-- Most northern point 20km from lat/lon
Debug lat2
Debug lon2

DistFromPoint(49,-94,20,180) ;<-- Most southern point 20km from lat/lon
DistFromPoint(49,-94,20,90) ;<-- Most eastern point 20km from lat/lon
DistFromPoint(49,-94,20,270) ;<-- Most western point 20km from lat/lon

_________________
Image Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Has anyone done the 'Haversine' formula?
PostPosted: Fri Feb 08, 2019 5:27 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 6:14 pm
Posts: 1671
Location: Germany (Saxony, Deutscheinsiedel)
Thank you very much, Paul! :D

I tried to encapsulate the complete function and variables a bit more, and extended the examples.
Hopefully all is done the right way and the results are true ;-)
(e.g I'm not sure, why the latitude changes a bit in the 3rd example..., or if the +/- of the resulting lat/long are always handled correctly...)

My own test inside my project will take a bit...

[Update: 9th Feb. 2019]
Code:
Define.d Distance, Bearing

Structure geopoint
  Latitude.d
  Longitude.d
EndStructure
Define pos.geopoint

#MostNorthern = 0
#MostSouthern = 180
#MostEastern  = 90
#MostWestern  = 270

Procedure DistanceFromPoint(*pos.geopoint, Distance, Bearing)
  ; This function calculates the new geographic point in the 'Distance' (of xxx km) from the
  ; given current geographic point '@pos'.
  ; 'Bearing' determines the direction, where the new position should be calculated:
  ;   #MostNorthern and #MostSouthern will give a new latitude value
  ;   #MostEastern and #MostWestern will give a new longitude value
  Protected Radius.d = 6372.795477598  ;  a fixed value
  Protected OldLatitude.d = *pos\Latitude, OldLongitude.d = *pos\Longitude
  Protected NewLatitude.d, NewLongitude.d
 
  ; Now we use the given values (old Lat/Long position, Distance and Bearing/Direction) for calculating the new position:
  NewLatitude  = Degree(ASin(Sin(Radian(OldLatitude))*Cos(Distance/Radius)+Cos(Radian(OldLatitude))*Sin(Distance/Radius)*Cos(Radian(Bearing)) ))
  NewLongitude = OldLongitude + Degree(ATan2(Cos(Distance/Radius)-Sin(Radian(OldLatitude))*Sin(Radian(NewLatitude)),Sin(Radian(Bearing))*Sin(Distance/Radius)*Cos(Radian(OldLatitude))))
 
  ; Return the new position by saving it in the structured variable (which can be accessed in the main program):
  *pos\Latitude  = NewLatitude
  *pos\Longitude = NewLongitude
EndProcedure

; 1st example - going to the North:
pos\Latitude = 49
pos\Longitude = -94
Distance = 200
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
DistanceFromPoint(@pos, Distance, #MostNorthern) ;<-- Most northern point 200 km from lat/lon
Debug "..new latitude/longitude " + Distance + " km into North: " + pos\Latitude + " / " + pos\Longitude
Debug "----------------"

; 2nd example - going to the South:
pos\Latitude = 49
pos\Longitude = -94
Distance = 20
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
DistanceFromPoint(@pos, Distance, #MostSouthern) ;<-- Most southern point 20 km from lat/lon
Debug "..new latitude/longitude " + Distance + " km into South: " + pos\Latitude + " / " + pos\Longitude
Debug "----------------"

; 3rd extended example - going from Northpole (latitude = +90) to Southpole (latitude = -90) and calculate the new longitude for going 200 km to the East:
Debug "Here a more extended example: starting point moves from North to South (latitude decreases 10 degress everytime), but with constant longitude (20):"
For a = 85 To -85 Step -10
  pos\Latitude = a
  pos\Longitude = 20
  Distance = 200
  Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
  DistanceFromPoint(@pos, Distance, #MostEastern) ;<-- Most eastern point 200 km from lat/lon
  Debug "..new latitude/longitude " + Distance + " km into East: " + pos\Latitude + " / " + pos\Longitude
Next
Debug "----------------"

; 4th example - going to the West:
pos\Latitude = 49
pos\Longitude = -94
Distance = 20
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
DistanceFromPoint(@pos, Distance, #MostWestern) ;<-- Most western point 20 km from lat/lon
Debug "..new latitude/longitude " + Distance + " km into West: " + pos\Latitude + " / " + pos\Longitude

_________________
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)


Top
 Profile  
Reply with quote  
 Post subject: Re: Has anyone done the 'Haversine' formula?
PostPosted: Fri Feb 08, 2019 6:01 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 810
Andre wrote:
... latitude seems easy, but longitude...

If you take a closer look, you will find that calculating the latitude is probably the bigger problem.

For example, if you want to drive 2000 km to the west, you have to follow the great circle to really reach the farthest point to the west. To follow the great circle means that on the northern hemisphere you have to go north first and then south again after half of your distance. In the southern hemisphere the opposite is true, at the equator it doesn't matter.

At GPS starting time I wrote a simple navigation program (shit, I'm old). I always calculated the globe to be in the middle of the circle. So all calculations were much easier. I haven't thought it over for your case right now, but maybe this can help.

_________________
sorry for my bad english


Top
 Profile  
Reply with quote  
 Post subject: Re: Has anyone done the 'Haversine' formula?
PostPosted: Fri Feb 08, 2019 8:09 pm 
Offline
Addict
Addict
User avatar

Joined: Thu Jun 07, 2007 3:25 pm
Posts: 3510
Location: Berlin, Germany
A riddle:
You are somewhere on earth. You go 5 km south, then 5 km west and then 5 km north. Then you have arrived back at the place where you started.
You see a bear. What color is it? :D

_________________
Please excuse my flawed English. My native language is PureBasic.
Search
RSBasic's backups


Top
 Profile  
Reply with quote  
 Post subject: Re: Has anyone done the 'Haversine' formula?
PostPosted: Fri Feb 08, 2019 8:42 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 810
Little John wrote:
You see a bear. What color is it? :D

I have never seen a polar bear so far north :lol:

_________________
sorry for my bad english


Top
 Profile  
Reply with quote  
 Post subject: Re: Has anyone done the 'Haversine' formula?
PostPosted: Fri Feb 08, 2019 9:08 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 4:34 pm
Posts: 884
Location: Canada
Josh wrote:
Little John wrote:
You see a bear. What color is it? :D

I have never seen a polar bear so far north :lol:


What about Santa ? :P

_________________
Image Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Has anyone done the 'Haversine' formula?
PostPosted: Sat Feb 09, 2019 7:52 am 
Offline
Addict
Addict
User avatar

Joined: Thu Feb 09, 2006 11:27 pm
Posts: 2402
Tried to do a quick check, don't think, the results are okay (maybe I am mixing bears and penguins here)...

Here's the output...
Old latitude/longitude: 49 / -94
New latitude/longitude 20 km into West: 48.9996754155 / -94.4177635481
30.568590932620445

...and here's the code:
Code:
 :
pos\Latitude = 49
pos\Longitude = -94
new.geopoint=pos
Distance = 20
DistanceFromPoint(@new, Distance, #MostWestern) ;<-- Most western point 20 km from lat/lon
Debug "Old latitude/longitude: " + pos\Latitude + " / " + pos\Longitude
Debug "New latitude/longitude " + Distance + " km into West: " + new\Latitude + " / " + new\Longitude
Debug distVincenty2(pos\Latitude,pos\Longitude,new\Latitude,new\Longitude)


Top
 Profile  
Reply with quote  
 Post subject: Re: Has anyone done the 'Haversine' formula?
PostPosted: Sat Feb 09, 2019 8:21 am 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 4:34 pm
Posts: 884
Location: Canada
When Andre converted my snippet, he missed something...

Code:
  NewLongitude = OldLongitude + Degree(ATan2(Cos(Distance/Radius)-Sin(Radian(OldLatitude))*Sin(Radian(NewLatitude)),
  Sin(Radian(Bearing))*Sin(Distance/Radius)*Cos(Radian(lat1))))
                                                        ^^^

He renamed all the 'lat1' to 'OldLongitude' except this one ;)

_________________
Image Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Has anyone done the 'Haversine' formula?
PostPosted: Sat Feb 09, 2019 2:04 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 6:14 pm
Posts: 1671
Location: Germany (Saxony, Deutscheinsiedel)
Thanks Paul, for pointing this out :D

I've updated my code snippet above, including a slightly modified 3rd example.

But I still think too, that moving to the West (4th example) shouldn't change the latitude, when it doesn't for moving to the East (2nd example)!?

_________________
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)


Top
 Profile  
Reply with quote  
 Post subject: Re: Has anyone done the 'Haversine' formula?
PostPosted: Sat Feb 09, 2019 4:55 pm 
Offline
PureBasic Expert
PureBasic Expert
User avatar

Joined: Fri Apr 25, 2003 4:34 pm
Posts: 884
Location: Canada
This is a good read...
https://www.movable-type.co.uk/scripts/latlong.html

Explains why final bearing differs from the initial heading by varying degrees according to distance and latitude.


(calculations would probably be much easier if everyone would just accept the 'flat earth' theory :lol: )

_________________
Image Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Has anyone done the 'Haversine' formula?
PostPosted: Sun Feb 10, 2019 10:50 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 6:14 pm
Posts: 1671
Location: Germany (Saxony, Deutscheinsiedel)
Paul wrote:
This is a good read...
https://www.movable-type.co.uk/scripts/latlong.html

...

(calculations would probably be much easier if everyone would just accept the 'flat earth' theory :lol: )

Thanks for the link, Paul. :D

But like other links too, this math section is too high for me.... :mrgreen:
So I'm someone for the 'flat earth'.... :wink:

I will post again, when I can report more about testing the code in my project (will take a while).

_________________
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 27 posts ]  Go to page Previous  1, 2

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye