FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Calculate the distance between 2 points
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Calculate the distance between 2 points [message #171275] Wed, 29 December 2010 23:55 Go to next message
Sarah is currently offline  Sarah
Messages: 30
Registered: December 2010
Karma: 0
Member
Hi! I've a table with a list of record that have latitude and
longitude

I've know new point with a position (new lati and new longi)

how can I show all records that have a distance <= of XX Kilometers??

Is it possible?

Thanks
Re: Calculate the distance between 2 points [message #171276 is a reply to message #171275] Thu, 30 December 2010 00:02 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On 29/12/10 23:55, Sarah wrote:
> Hi! I've a table with a list of record that have latitude and
> longitude
>
> I've know new point with a position (new lati and new longi)
>
> how can I show all records that have a distance <= of XX Kilometers??
>
> Is it possible?

You need a great circle calculator, and to calculate the great circle
distances between your new point and each point in the table.

Rgds

Denis McMahon
Re: Calculate the distance between 2 points [message #171278 is a reply to message #171276] Thu, 30 December 2010 00:34 Go to previous messageGo to next message
Sarah is currently offline  Sarah
Messages: 30
Registered: December 2010
Karma: 0
Member
Can you tell me how to calculate this using mySQL query and PHP?

Thanks
Re: Calculate the distance between 2 points [message #171279 is a reply to message #171276] Thu, 30 December 2010 00:43 Go to previous messageGo to next message
Sarah is currently offline  Sarah
Messages: 30
Registered: December 2010
Karma: 0
Member
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
Re: Calculate the distance between 2 points [message #171280 is a reply to message #171278] Thu, 30 December 2010 00:42 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
Sarah wrote:

> Can you tell me how to calculate this using mySQL query and PHP?

Can *you* do your homework?

> Thanks

You're welcome.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Re: Calculate the distance between 2 points [message #171281 is a reply to message #171279] Thu, 30 December 2010 01:06 Go to previous messageGo to next message
RMP is currently offline  RMP
Messages: 6
Registered: December 2010
Karma: 0
Junior Member
Seems like your model needs to eat a lots of cookies or even cache some results :P

This will be a hudge amount of data so you better think about some algorithm to divide and conquer your problem. For example build a subset:

If you are in (Lat/Long) 35° 18' S 149° 7' E and want to find ONE nearest point then first take a look around and get everything that is 35 S 149 E. If there are more than get more detailled. Otherwise spread more. And so on.

This rectangle around your pos might speed up your algorithm, because only a small subset left over to deal with.
Re: Calculate the distance between 2 points [message #171284 is a reply to message #171279] Thu, 30 December 2010 01:23 Go to previous messageGo to next message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
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

So RTFMySQLM.


PointedEars
--
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
-- Richard Cornford, cljs, <cife6q$253$1$8300dec7(at)news(dot)demon(dot)co(dot)uk> (2004)
Re: Calculate the distance between 2 points [message #171289 is a reply to message #171284] Thu, 30 December 2010 01:42 Go to previous messageGo to next message
me is currently offline  me
Messages: 192
Registered: September 2010
Karma: 0
Senior Member
On 12/29/2010 8:23 PM, Thomas 'PointedEars' Lahn wrote:
> 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
>
> So RTFMySQLM.
>
>
> PointedEars

It seems to be the time of year, or possibly the lunar cycle, for
bullies to crawl out from under their rocks. Is this place the meeting
ground of choice for miserable people?

You can express an opinion in decent, respectful language. But crude and
insulting behavior gets no pass from me.

Dial it down, Pointed Ears.

Bill B
Re: Calculate the distance between 2 points [message #171300 is a reply to message #171289] Thu, 30 December 2010 04:53 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 12/29/2010 8:42 PM, Bill Braun wrote:
> On 12/29/2010 8:23 PM, Thomas 'PointedEars' Lahn wrote:
>> 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
>>
>> So RTFMySQLM.
>>
>>
>> PointedEars
>
> It seems to be the time of year, or possibly the lunar cycle, for
> bullies to crawl out from under their rocks. Is this place the meeting
> ground of choice for miserable people?
>
> You can express an opinion in decent, respectful language. But crude and
> insulting behavior gets no pass from me.
>
> Dial it down, Pointed Ears.
>
> Bill B

Bill, ignore "pointed head". That's just how he is. If he had to wait
until he had an intelligent answer, he wouldn't be able to post anything.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Calculate the distance between 2 points [message #171301 is a reply to message #171275] Thu, 30 December 2010 04:58 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 12/29/2010 6:55 PM, Sarah wrote:
> Hi! I've a table with a list of record that have latitude and
> longitude
>
> I've know new point with a position (new lati and new longi)
>
> how can I show all records that have a distance<= of XX Kilometers??
>
> Is it possible?
>
> Thanks

Sarah,

As you've probably found, just scanning the database for all rows
meeting this condition is pretty slow, especially if you have a lot of
rows in your table.

One way I've found to make this faster is to create a square of the size
necessary. For instance, if you want to find everything within 25 km of
a point, create a square in your program with the corners your point +=
25km longitude and latitude. Now you have a preliminary area to work with.

Then you can search the database for anything within that square, and
fine tune the results to be within a circle of 25 km.

Hope this is clear.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Calculate the distance between 2 points [message #171309 is a reply to message #171301] Thu, 30 December 2010 09:23 Go to previous messageGo to next message
RMP is currently offline  RMP
Messages: 6
Registered: December 2010
Karma: 0
Junior Member
As I said ;D
Re: Calculate the distance between 2 points [message #171312 is a reply to message #171275] Thu, 30 December 2010 09:28 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Sarah wrote:
> Hi! I've a table with a list of record that have latitude and
> longitude
>
> I've know new point with a position (new lati and new longi)
>
> how can I show all records that have a distance <= of XX Kilometers??
>
> Is it possible?
>

Of course.

A simple course in spherical trigonometry and computer programming is
all you need.

Or is that in fact, the courses you are taking, and this is your assigment?


> Thanks
Re: Calculate the distance between 2 points [message #171313 is a reply to message #171278] Thu, 30 December 2010 09:28 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Sarah wrote:
> Can you tell me how to calculate this using mySQL query and PHP?
>
> Thanks
Yes.
Re: Calculate the distance between 2 points [message #171314 is a reply to message #171289] Thu, 30 December 2010 09:31 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Bill Braun wrote:
> On 12/29/2010 8:23 PM, Thomas 'PointedEars' Lahn wrote:
>> 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
>>
>> So RTFMySQLM.
>>
>>
>> PointedEars
>
> It seems to be the time of year, or possibly the lunar cycle, for
> bullies to crawl out from under their rocks.


No. its the time of year when thick students who haven't done their
coursework expect someone else to do it for them.


> Is this place the meeting
> ground of choice for miserable people?
>

No, its a place where people who should have listened to lectures, come
for answers they have actually learnt already.


> You can express an opinion in decent, respectful language. But crude and
> insulting behavior gets no pass from me.
>

And students who cheat and crib get no pass from me either.
Re: Calculate the distance between 2 points [message #171316 is a reply to message #171314] Thu, 30 December 2010 09:37 Go to previous messageGo to next message
me is currently offline  me
Messages: 192
Registered: September 2010
Karma: 0
Senior Member
On 12/30/2010 4:31 AM, The Natural Philosopher wrote:
> Bill Braun wrote:
>> On 12/29/2010 8:23 PM, Thomas 'PointedEars' Lahn wrote:
>>> 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
>>>
>>> So RTFMySQLM.
>>>
>>>
>>> PointedEars
>>
>> It seems to be the time of year, or possibly the lunar cycle, for
>> bullies to crawl out from under their rocks.
>
>
> No. its the time of year when thick students who haven't done their
> coursework expect someone else to do it for them.
>
>
>> Is this place the meeting ground of choice for miserable people?
>>
>
> No, its a place where people who should have listened to lectures, come
> for answers they have actually learnt already.
>
>
>> You can express an opinion in decent, respectful language. But crude
>> and insulting behavior gets no pass from me.
>>
>
> And students who cheat and crib get no pass from me either.

Such points can be made, as forcefully as you wish, in a respectful
tone. There is no need or reason to belittle or demean.

Bill B
Re: Calculate the distance between 2 points [message #171318 is a reply to message #171316] Thu, 30 December 2010 09:47 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Bill Braun wrote:
> On 12/30/2010 4:31 AM, The Natural Philosopher wrote:
>> Bill Braun wrote:
>>> On 12/29/2010 8:23 PM, Thomas 'PointedEars' Lahn wrote:
>>>> 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
>>>>
>>>> So RTFMySQLM.
>>>>
>>>>
>>>> PointedEars
>>>
>>> It seems to be the time of year, or possibly the lunar cycle, for
>>> bullies to crawl out from under their rocks.
>>
>>
>> No. its the time of year when thick students who haven't done their
>> coursework expect someone else to do it for them.
>>
>>
>>> Is this place the meeting ground of choice for miserable people?
>>>
>>
>> No, its a place where people who should have listened to lectures, come
>> for answers they have actually learnt already.
>>
>>
>>> You can express an opinion in decent, respectful language. But crude
>>> and insulting behavior gets no pass from me.
>>>
>>
>> And students who cheat and crib get no pass from me either.
>
> Such points can be made, as forcefully as you wish, in a respectful
> tone. There is no need or reason to belittle or demean.
>

There is no reason not to, or to complain about it, either.

RTFM is a well known phrase.

FFS. ;-)

> Bill B
Re: Calculate the distance between 2 points [message #171327 is a reply to message #171279] Thu, 30 December 2010 12:15 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
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
Re: Calculate the distance between 2 points [message #171330 is a reply to message #171327] Thu, 30 December 2010 12:31 Go to previous messageGo to next message
me is currently offline  me
Messages: 192
Registered: September 2010
Karma: 0
Senior Member
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
Re: Calculate the distance between 2 points [message #171331 is a reply to message #171318] Thu, 30 December 2010 13:04 Go to previous messageGo to next message
me is currently offline  me
Messages: 192
Registered: September 2010
Karma: 0
Senior Member
On 12/30/2010 4:47 AM, The Natural Philosopher wrote:
> Bill Braun wrote:
>> On 12/30/2010 4:31 AM, The Natural Philosopher wrote:
>>> Bill Braun wrote:
>>>> On 12/29/2010 8:23 PM, Thomas 'PointedEars' Lahn wrote:
>>>> > 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
>>>> >
>>>> > So RTFMySQLM.
>>>> >
>>>> >
>>>> > PointedEars
>>>>
>>>> It seems to be the time of year, or possibly the lunar cycle, for
>>>> bullies to crawl out from under their rocks.
>>>
>>>
>>> No. its the time of year when thick students who haven't done their
>>> coursework expect someone else to do it for them.
>>>
>>>
>>>> Is this place the meeting ground of choice for miserable people?
>>>>
>>>
>>> No, its a place where people who should have listened to lectures, come
>>> for answers they have actually learnt already.
>>>
>>>
>>>> You can express an opinion in decent, respectful language. But crude
>>>> and insulting behavior gets no pass from me.
>>>>
>>>
>>> And students who cheat and crib get no pass from me either.
>>
>> Such points can be made, as forcefully as you wish, in a respectful
>> tone. There is no need or reason to belittle or demean.
>>
>
> There is no reason not to, or to complain about it, either.

Bullies, thugs, and tyrants count on reasonable people turning their
backs and letting their boorish behavior go unchecked. They are for the
most part cowards, who, when made accountable, tuck tail and run.

There is a reason, and I will complain, drawing attention to it as needed.

Bill B
Re: Calculate the distance between 2 points [message #171334 is a reply to message #171316] Thu, 30 December 2010 13:10 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 12/30/2010 4:37 AM, Bill Braun wrote:
> On 12/30/2010 4:31 AM, The Natural Philosopher wrote:
>> Bill Braun wrote:
>>> On 12/29/2010 8:23 PM, Thomas 'PointedEars' Lahn wrote:
>>>> 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
>>>>
>>>> So RTFMySQLM.
>>>>
>>>>
>>>> PointedEars
>>>
>>> It seems to be the time of year, or possibly the lunar cycle, for
>>> bullies to crawl out from under their rocks.
>>
>>
>> No. its the time of year when thick students who haven't done their
>> coursework expect someone else to do it for them.
>>
>>
>>> Is this place the meeting ground of choice for miserable people?
>>>
>>
>> No, its a place where people who should have listened to lectures, come
>> for answers they have actually learnt already.
>>
>>
>>> You can express an opinion in decent, respectful language. But crude
>>> and insulting behavior gets no pass from me.
>>>
>>
>> And students who cheat and crib get no pass from me either.
>
> Such points can be made, as forcefully as you wish, in a respectful
> tone. There is no need or reason to belittle or demean.
>
> Bill B

No, Bill, it is necessary for TNP to belittle and demean. He needs to
feel superior to everyone else.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Calculate the distance between 2 points [message #171352 is a reply to message #171331] Thu, 30 December 2010 18:32 Go to previous messageGo to next message
Felix Saphir is currently offline  Felix Saphir
Messages: 8
Registered: December 2010
Karma: 0
Junior Member
Bill Braun <me(at)privacy(dot)net> wrote:
> On 12/30/2010 4:47 AM, The Natural Philosopher wrote:
>>
>> There is no reason not to, or to complain about it, either.
>
> Bullies, thugs, and tyrants count on reasonable people turning
> their backs and letting their boorish behavior go unchecked.
> They are for the most part cowards, who, when made accountable,
> tuck tail and run.
>
> There is a reason, and I will complain, drawing attention to it
> as needed.

Could you please trim the messages you're drawing attention to?

Felix
Re: Calculate the distance between 2 points [message #171355 is a reply to message #171352] Thu, 30 December 2010 18:48 Go to previous messageGo to next message
me is currently offline  me
Messages: 192
Registered: September 2010
Karma: 0
Senior Member
On 12/30/2010 1:32 PM, Felix Saphir wrote:
> Bill Braun<me(at)privacy(dot)net> wrote:
>> On 12/30/2010 4:47 AM, The Natural Philosopher wrote:
>>>
>>> There is no reason not to, or to complain about it, either.
>>
>> Bullies, thugs, and tyrants count on reasonable people turning
>> their backs and letting their boorish behavior go unchecked.
>> They are for the most part cowards, who, when made accountable,
>> tuck tail and run.
>>
>> There is a reason, and I will complain, drawing attention to it
>> as needed.
>
> Could you please trim the messages you're drawing attention to?
>
> Felix
>


Of course. I thought I trimmed at the right spot, leaving some context.
Will redouble my efforts, thanks for the nudge.

Bill B
Re: Calculate the distance between 2 points [message #171378 is a reply to message #171330] Fri, 31 December 2010 02:13 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
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.
Re: Calculate the distance between 2 points [message #171380 is a reply to message #171275] Fri, 31 December 2010 02:25 Go to previous message
Martin is currently offline  Martin
Messages: 8
Registered: October 2010
Karma: 0
Junior Member
On Dec 29, 11:55 pm, Sarah <dandi....@gmail.com> wrote:
> Hi! I've a table with a list of record that have latitude and
> longitude
>
> I've know new point with a position (new lati and new longi)
>
> how can I show all records that have a distance <= of XX Kilometers??
>
> Is it possible?
>
> Thanks

There's a demo here that might be of use to you:

http://code.google.com/apis/maps/articles/phpsqlsearch.html

Scroll down the page to 'Finding locations nearby with MySQL'.

Martin.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Checking equal number of <div> and </div>
Next Topic: extract variable from an array
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 10 06:12:51 GMT 2024

Total time taken to generate the page: 0.02853 seconds