Re: Calculate the distance between 2 points [message #171378 is a reply to message #171330] |
Fri, 31 December 2010 02:13 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma:
|
Senior Member |
|
|
Bill Braun wrote:
> On 12/30/2010 7:15 AM, Denis McMahon wrote:
>> 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.
>
> It may be possible to develop a non-linearity table with would produce a
> correction factor for every X degrees of longitude, and store that in
> the database as LONGADJ or the like. It would be as accurate as it is
> detailed. A preliminary calculation would take the declining distance
> per degree of longitude into account, and the result of that calculation
> would be carried forward for the rest of the equation.
>
> Bill B
Of you want to find the closest, and don't care about the exaxct
distance, and exclude polar regions, you can simply find all longitude
offsets less than root 2 time s the smallest longitude offset, and do
the same with latitude, and that will give you a very small set and if
you have enough points, you can dis regard the sphericity and use plane
geometry.
should be a pretty fast 2 pas alogorithm through all the data with very
little maths to do.
|
|
|