Re: Best way to...
- Posted by ArthurCrump Oct 17, 2010
- 1791 views
Firstly:
At some point the strings should be split up into a name, Easting, Northing. For example:
{"KLAX",-118.408074361323,33.9425346884195}
The best time to do this is before any comparison so that it is only done once on each string.
Secondly:
Convert each location into a cartesian coordinate system.
Using N for the Northing, in degrees, and E for the Easting, also in degrees:
sequence XYZ[location_number] = R * { cos(N)*cos(E), cos(N)*sin(E), sin(N) }
R should be the radius of the Earth. However, it is irrelevant when distances are only being compared, so leave it out.
Thirdly:
There is no need to calculate the surface distance between locations. The straight line between them is sufficent for finding the nearest. To find this chord:
Find the difference between the cartesian coordinates:
XYZdif = XYZ[1] - XYZ[2]
Don't worry about the sign, because the three elements XYZdif are now going to be squared and added together. The chord length is:
chord = sqrt( XYZdif[1]*XYZdif[1]+XYZdif[2]*XYZdif[2]+XYZdif[3]*XYZdif[3] )
Just find the shortest chord length.
The sqrt is not important, you might just as well compare the square of the chord length.