Re: Best way to...
- Posted by ArthurCrump Oct 19, 2010
- 1690 views
I should get out of the habit of doing things in too much of a hurry.
In my previous reply, I forgot that sin and cos want radians, not degrees.
Here is a function that takes the list of airports and a current location as degrees East and North. It seemed to get the distance from San Francisco to Los Angeles airport about right. It uses Euphoria version 4.
include std/sequence.e include std/math.e include std/convert.e constant EarthRadius = 3963 -- Approximate miles, or 6378 Kilometres -- Parameters of 'nearest': -- site_list: The list of airports, in the original form, {"KLAX|-118.4etc|33.94etc",...} -- E: The Easting of the current location, ie degrees longitude East. -- N: The Northing of the current location, ie degrees latitude North. public function nearest(sequence site_list, atom E, atom N ) sequence bestsite, this, XYZhere, XYZsite atom bestcp2, ChordSquared integer bestindex N = deg2rad(N) E = deg2rad(E) XYZhere = { cos(N)*cos(E), cos(N)*sin(E), sin(N) } bestcp2 = 9.9 -- The actual range is 0.0 to 2.0 for n = 1 to length(site_list) do -- First convert "name|degrees|degrees" to {"name",radians, radians} this = split(site_list[n],'|') this[2] = deg2rad(to_number(this[2])) this[3] = deg2rad(to_number(this[3])) -- It would be more efficient if site_list entries were already in this form. XYZsite = { cos(this[3])*cos(this[2]), cos(this[3])*sin(this[2]), sin(this[3]) } ChordSquared = sum(power(XYZsite-XYZhere,2)) if ChordSquared < bestcp2 then bestcp2 = ChordSquared bestsite = this end if end for return bestsite & 2*arcsin(sqrt(bestcp2)/2)*EarthRadius & sqrt(bestcp2) end function -- The function returns a sequence of 4 elements for the nearest airport: -- {Name, Easting, Northing, Surface_distance} -- Currently in miles. -- To take account of polar flattening, the Z coordinate should be multiplied by -- 0.99665. The chord lengths and the nearest airport would be correct, but the -- surface distance would be more difficult to work out from the chord length.
Arthur