Getting started with geographical coordinates
Graham Samuel
livfoss at mac.com
Tue Apr 7 06:57:03 EDT 2020
Thanks for everyone who has helped with this so far. I have now looked at and tested LC scripts for three different functions for finding the distance - I left one well-known one out (Vincenty) as it’s very complex (it even includes iteration) and I think not justified by what I’m trying to do. As I’m concerned with relatively small distances (up to a few kilometres), I can include a Pythagoras calculation (officially called "Equirectangular approximation”), which doesn’t take account of the curvature of the Earth. My tests have involved real coordinates of places I’ve visited, so can be verified using local maps etc.
What I have found is that the different methods are all good for my purposes, but for me the very simple Pythagoras calculation which involves only two maths functions, gives results which are consistently less than one percent different from the far more complex Haversine and even Vincenty calculations. Incidentally I get differences in Haversine between my implementation (based on Ralph’s, thanks) and Chris Veness’s (movable-type) Javascript version - I’m pretty sure this is due to differences in precision in the underlying maths functions, but I don’t plan to look any further into that!
Just FYI, here’s the Pythagoras method. The inputs are assumed to be numerical GPS-style coordinates, which is what one gets from interrogating a GPS-aware device in LC. The result is in kilometres.
constant R=6371 -- the radius of the earth in kilometres
constant k1 = 0.01745329 -- that's pi/180
function distancePythag lat1,lon1,lat2,lon2
-- Calculate Distance between to points using Equirectangular approximation
-- Assuming a (local) flat surface, using Pythagoras. Won't work when Earth's curvature is significant
local x,y
put k1*lat1 into lat1
put k1*lat2 into lat2
put k1*lon1 into lon1
put k1*lon2 into lon2
put (lon2-lon1) * cos((lat1+lat2)/2) into x
put (lat2 - lat1) into y
return sqrt(x*x+y*y)*R
end distancePythag
Graham
> On 5 Apr 2020, at 22:03, Graham Samuel via use-livecode <use-livecode at lists.runrev.com> wrote:
>
> I’ve been trying these formulae out, and I’ve been using the info on https://www.movable-type.co.uk/scripts/latlong.html - for my purposes (distances from say 100 metres to up to a few kilometers) I think this works as well as haversine, but maybe not for every kind of measurement. The function gives a result in kilometres starting from coordinates in degrees. Seems to work, but I need to do a bit more work on the (real) ground to convince myself.
>
> function distanceslc lat1,lon1,lat2,lon2
>
> constant k1 = 0.017453 -- that's pi/180
>
> -- This implements the spherical law of cosines, from movable-type.co.uk. Confirmed with his javascript version
>
> return acos(sin(lat1*k1) * sin(lat2*k1) + cos(lat1*k1)*cos(lat2*k1)*cos(lon2*k1-lon1*k1))*6371
>
> end distanceslc
>
>
> Graham
>
>> On 4 Apr 2020, at 16:47, Ralph DiMola via use-livecode <use-livecode at lists.runrev.com> wrote:
>>
>> A friend of mine turned me on to this 10 years ago. This is the Haversine
>> formula. It assumes that the earth is sphere and is not very accurate for
>> very small distances. I have not tried to use the Vincenty's formula that
>> does better. For general purposes the HF should be sufficient.
>>
>> Just give me the credit for LC implementation of a friends of mines routine.
>> Although I understand how it works the real credit goes to Don Josef de
>> Mendoza y Rios in 1796.
>>
>> The girls and boys doing math in that period really set the stage. This
>> brought me back to my CGI days when I was patting myself on the back when I
>> self learned(with a friends help after reading the Kreyszig) how to move
>> points in space and calculate lighting. I then self reflected and realized
>> that I was just putting together the pieces of math that these folks created
>> out of thin air. I still felt like I accomplished something but very much
>> smaller the scheme of things.
>>
>> Ralph DiMola
>> IT Director
>> Evergreen Information Services
>> rdimola at evergreeninfo.net
>>
>> -----Original Message-----
>> From: use-livecode [mailto:use-livecode-bounces at lists.runrev.com] On Behalf
>> Of Bob Sneidar via use-livecode
>> Sent: Friday, April 03, 2020 5:31 PM
>> To: How to use LiveCode
>> Cc: Bob Sneidar
>> Subject: Re:
>>
>> Ralph, this is brilliant. I remember trying to do something similar years
>> ago, and giving up because I didn't know how to do the math. I suck at math,
>> or rather I am too lazy and impatient to work the problem.
>>
>> Bob S
>>
>>> On Apr 3, 2020, at 14:27 , Ralph DiMola via use-livecode
>> <use-livecode at lists.runrev.com> wrote:
>>>
>>> Graham,
>>>
>>> This my distance calculation for what it's worth.
>>>
>>> Function distance lat1, lon1, lat2, lon2, unit
>>> -- Calculate Distance between to points
>>> --
>>> --lat1, lon1, lat2, lon2 are in deg.fractionalDegrees
>>> -- Unit
>>> -- if empty then miles
>>> -- K = kilometers
>>> -- N = nautical miles
>>> local theta
>>> local dist
>>>
>>> Put lon1 - lon2 into theta
>>> put Sin(deg2rad(lat1)) * Sin(deg2rad(lat2)) + Cos(deg2rad(lat1)) *
>> Cos(deg2rad(lat2)) * Cos(deg2rad(theta)) into dist
>>>
>>> put Acos(dist) into dist
>>> put rad2deg(dist) into dist
>>> put dist * 60 * 1.1515 into dist
>>>
>>> switch unit
>>> case "K"
>>> put dist * 1.609344 into dist
>>> case "N"
>>> put dist * 0.8684 into dist
>>> end switch
>>>
>>> Return dist
>>>
>>> End distance
>>>
>>>
>>> Function rad2deg rad
>>> Return rad / PI * 180.0
>>> end rad2deg
>>>
>>>
>>> Ralph DiMola
>>> IT Director
>>> Evergreen Information Services
>>> rdimola at evergreeninfo.net
>>
>>
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription
>> preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
>>
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list