LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-10-2007, 04:38 PM   #1
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Rep: Reputation: 16
Retrieving data from MySQL


Hi all,
I've been racking my brains over this for a few hours, with no joy.

Basically, I have a MySQL database that stores a series of latitude/longitude coordinates in two fields (lat and long). I need to be able to search the database and retrieve records that are within a given distance of a lat/long pair that is specified at search using PHP.

I have managed to retrieve everything that is within a set distance east/west and north/south, the only problem is I can only figure out how to get data in a square (ie lat + distance, long + distance), which means that the corners are about 41% too far, but I cant think of any way to get only the data that is within the circle.

I hope that I have managed to explain it well enough, and any help would be really appreciated,
Chris
 
Old 08-10-2007, 06:34 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Since you have restricted your return set to pints that are bounded by a square, I guess that you need to add one more condition to your existing conditions restricting the set of points to lie within the circle that is bounded by the square.

Use the formula a*a + b*b <= r*r, where a is the latitudinal difference, b is the longitudinal difference and r is the given distance.
 
1 members found this post helpful.
Old 08-11-2007, 05:26 AM   #3
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Original Poster
Rep: Reputation: 16
Excellent, thanks graemef. Is it possible to use this in a MySQL SELECT statement, or can it only be done on the returned set?
 
Old 08-11-2007, 11:09 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You should be able to squeeze it into the Select statement, just as you had with the other parts of the Where clause.
 
Old 08-12-2007, 10:24 AM   #5
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Original Poster
Rep: Reputation: 16
Cool, I'll try that. Thanks again.
 
Old 08-12-2007, 12:06 PM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
I would compute the r*r once and feed it into the query rather than recomputing it for every member of the set that is being examined (I rather doubt the SELECT statement will optimize to handle this).

Also, be aware that the accuracy of this approach will vary considerably with latitude because your lat-long "square" isn't a square, it is a trapezoid. The longitudinal distances between whole degree measurements vary from about 69.5 miles (114.4 KM) at the equator to zero at either pole, while the latitude distance is constant at about 69.5 miles. At a latitude of 45 degrees, the longitudinal distance will be about 49.1 miles (80.9 KM)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieving encrypted data fof3 Linux - Newbie 2 09-12-2006 08:56 AM
retrieving lost data mohtasham1983 Linux - General 3 08-31-2005 01:04 PM
Retrieving data from devices is not supported??? Teckno Linux - Newbie 2 05-25-2004 04:49 PM
Retrieving data from devices not supported ickselglic Linux - Hardware 1 03-12-2004 11:16 AM
Retrieving Data Bheki Linux - Newbie 1 05-21-2002 07:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration