Re: Calculate the distance between 2 points [message #171327 is a reply to message #171279] |
Thu, 30 December 2010 12:15 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 30/12/10 00:43, Sarah wrote:
> I know this
>
> $distanceValue =
> rad2deg(acos(sin(deg2rad($LATITUDINE1))*sin(deg2rad($LATITUDINE2))
> +cos(deg2rad($LATITUDINE1))*cos(deg2rad($LATITUDINE2))*cos(deg2rad($LONGITU DINE1-
> $LONGITUDINE2))))*69.09;
>
> or something like this.... but I don't want to have all records of DB
> and than analize them one by one .... I want a query the return only
> nearest values
Then you have to do the calculation as part of the query.
What you can do is pre-calculate some latitude and longitude limits, for
example 1 degree of latitude always equates to the same distance north
to south.
Polar circumference is 40,008 km, half that is 20,004 km, divide by 180
degrees, and you get 111.13333 km per degree of latitude. So, to make it
easy, for every 100 km of distance that you want to limit to, you only
need to check +- 1 degree of latitude of your given point (a more
precise figure is 0.9 degrees).
Therefore, if you're checking for places within 300km, you can
immediately dismiss anywhere that's more than 3 (or 2.7) degrees of
latitude away from your specified location.
Longitude is trickier, because the distance per degree of longitude
varies from 111.3222222 miles at the equator to 0 miles at the pole.
However, you can try and calculate a "box", outside which you don't need
to calculate the actual great circle distance because it will always be
greater than the range you are specifying. You will need to be careful
if your box embraces either a pole or the equator that you don't dismiss
valid points.
What you could then do is copy all the points inside your "might fit"
box into a temporary table, and then run a query on the temporary table
to determine their actual distances. This might be less computationally
intensive than calculating the great circle range for every point.
Rgds
Denis McMahon
|
|
|