NogginBoink
Diamond Member
So I've got a database of people with their ZIP codes, and another table that lists latitudes and longitudes for each ZIP code in the united states.
I want to find the 10 closest people to me in the database. I enter my ZIP code and resolve to lat/lon.
Target location is:
Latitude: 32.675604
Longitude: -97.023462
The equation for great circle distance is fairly straightforward:
D = acos[sin(La1) sin(La2) + cos(La1) cos(La2) cos(Lo1 - Lo2)]
(D = distance, La = latitude, Lo = longitude)
However, my SQL implementation (MS Access) doesn't have the acos() function, so it has to be derived from other trig functions. Oh yes, and let us not forget that computers use radians for trig functions instead of degrees.
The gawdawful result:
Select TOP 10 Name, Num, City, ZipCodes.State, ((Atn(-((sin(ZipCodes.Latitude*3.1415/180) * sin(0.570296874877829) + cos(ZipCodes.Latitude*3.1415/180) * cos(0.570296874877829) * cos(ZipCodes.Longitude*3.1415/180 - -1.6933788635836)))/ Sqr((-((sin(ZipCodes.Latitude*3.1415/180) * sin(0.570296874877829) + cos(ZipCodes.Latitude*3.1415/180) * cos(0.570296874877829) * cos(ZipCodes.Longitude*3.1415/180 - -1.6933788635836)))* ((sin(ZipCodes.Latitude*3.1415/180) * sin(0.570296874877829) + cos(ZipCodes.Latitude*3.1415/180) * cos(0.570296874877829) * cos(ZipCodes.Longitude*3.1415/180 - -1.6933788635836))))+1))+2*Atn(1))*57.29746) AS Distance2, Int(Int(Distance2) *60 + (Distance2 - Int(Distance2)) * 60) AS Distance from Builders, ZipCodes WHERE Builders.BuilderZip = ZipCodes.ZIP ORDER BY ((Atn(-((sin(ZipCodes.Latitude*3.1415/180) * sin(0.570296874877829) + cos(ZipCodes.Latitude*3.1415/180) * cos(0.570296874877829) * cos(ZipCodes.Longitude*3.1415/180 - -1.6933788635836)))/ Sqr((-((sin(ZipCodes.Latitude*3.1415/180) * sin(0.570296874877829) + cos(ZipCodes.Latitude*3.1415/180) * cos(0.570296874877829) * cos(ZipCodes.Longitude*3.1415/180 - -1.6933788635836)))* ((sin(ZipCodes.Latitude*3.1415/180) * sin(0.570296874877829) + cos(ZipCodes.Latitude*3.1415/180) * cos(0.570296874877829) * cos(ZipCodes.Longitude*3.1415/180 - -1.6933788635836))))+1))+2*Atn(1))*57.29746)
Ick!
But I'm so proud of myself.... it's actually working! 😀
I want to find the 10 closest people to me in the database. I enter my ZIP code and resolve to lat/lon.
Target location is:
Latitude: 32.675604
Longitude: -97.023462
The equation for great circle distance is fairly straightforward:
D = acos[sin(La1) sin(La2) + cos(La1) cos(La2) cos(Lo1 - Lo2)]
(D = distance, La = latitude, Lo = longitude)
However, my SQL implementation (MS Access) doesn't have the acos() function, so it has to be derived from other trig functions. Oh yes, and let us not forget that computers use radians for trig functions instead of degrees.
The gawdawful result:
Select TOP 10 Name, Num, City, ZipCodes.State, ((Atn(-((sin(ZipCodes.Latitude*3.1415/180) * sin(0.570296874877829) + cos(ZipCodes.Latitude*3.1415/180) * cos(0.570296874877829) * cos(ZipCodes.Longitude*3.1415/180 - -1.6933788635836)))/ Sqr((-((sin(ZipCodes.Latitude*3.1415/180) * sin(0.570296874877829) + cos(ZipCodes.Latitude*3.1415/180) * cos(0.570296874877829) * cos(ZipCodes.Longitude*3.1415/180 - -1.6933788635836)))* ((sin(ZipCodes.Latitude*3.1415/180) * sin(0.570296874877829) + cos(ZipCodes.Latitude*3.1415/180) * cos(0.570296874877829) * cos(ZipCodes.Longitude*3.1415/180 - -1.6933788635836))))+1))+2*Atn(1))*57.29746) AS Distance2, Int(Int(Distance2) *60 + (Distance2 - Int(Distance2)) * 60) AS Distance from Builders, ZipCodes WHERE Builders.BuilderZip = ZipCodes.ZIP ORDER BY ((Atn(-((sin(ZipCodes.Latitude*3.1415/180) * sin(0.570296874877829) + cos(ZipCodes.Latitude*3.1415/180) * cos(0.570296874877829) * cos(ZipCodes.Longitude*3.1415/180 - -1.6933788635836)))/ Sqr((-((sin(ZipCodes.Latitude*3.1415/180) * sin(0.570296874877829) + cos(ZipCodes.Latitude*3.1415/180) * cos(0.570296874877829) * cos(ZipCodes.Longitude*3.1415/180 - -1.6933788635836)))* ((sin(ZipCodes.Latitude*3.1415/180) * sin(0.570296874877829) + cos(ZipCodes.Latitude*3.1415/180) * cos(0.570296874877829) * cos(ZipCodes.Longitude*3.1415/180 - -1.6933788635836))))+1))+2*Atn(1))*57.29746)
Ick!
But I'm so proud of myself.... it's actually working! 😀