Sep
15th

PHP and MySQL: Calculating Distance

Thanks for stopping by my personal blog on Marketing Technology! Over 50,000 visitors a month find my content worth returning for, so don't forget to subscribe to the Marketing Technology Blog RSS feed or to the Marketing Technology Email to have new content sent directly to your inbox. You may also find my other business blog helpful, Social Media Domination.

This month I’ve been programming quite a bit in PHP and MySQL with respect to GIS. Snooping around the net, I actually had a hard time finding some of the Geographic calculations to find the distance between two locations so I wanted to share them here.

Flight Map EuropeIf you remember ‘the old days’ of calculating a distance between two points, it was simply the hypotenuse of a triangle (A² + B² = C²).

That’s an interesting start but it doesn’t apply with Geography since the distance between lines of latitude and longitude are not an equal distance apart. As you get closer to the equator, lines of latitude get further apart. If you use some kind of simple triangulation equation, it may measure distance accurately in one location and terribly wrong in the other, because of the curvature of the Earth.

That brings up the Haversine formula, which uses trigonometry to allow for the curvature of the earth. When you’re finding the distance between 2 places on earth (as the crow flies), a straight line is really an arc. This is applicable in air flight - have you ever looked at the actual map of flights and noticed they are arched? That’s because it’s shorter to fly in an arch between two points sometimes than directly to the location.

Anyways, here’s the PHP formula for calculating the distance between two points (along with Mile vs. Kilometer conversion) rounded to two decimal places:

function getDistanceBetweenPointsNew($latitude1, $longitude1, $latitude2, $longitude2, $unit = 'Mi') {
$theta = $longitude1 - $longitude2;
$distance = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) +
(cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) *
cos(deg2rad($theta)));
$distance = acos($distance);
$distance = rad2deg($distance);
$distance = $distance * 60 * 1.1515;
switch($unit) {
case ‘Mi’: break;
case ‘Km’ : $distance = $distance * 1.609344;
}return (round($distance,2));
}

It’s also possible to use MySQL to do a calculation to find all records within a specific distance. In this example, I’m going to query MyTable to find all the records that are less than or equal to variable $distance (in Miles) to my location at $latitude and $longitude:

$qry = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515) as distance FROM `MyTable` WHERE distance <= ".$distance."

For Kilometers:

$qry = "SELECT *,(((acos(sin((".$latitude."*pi()/180)) * sin((`Latitude`*pi()/180))+cos((".$latitude."*pi()/180)) * cos((`Latitude`*pi()/180)) * cos(((".$longitude."- `Longitude`)*pi()/180))))*180/pi())*60*1.1515*1.609344) as distance FROM `MyTable` WHERE distance <= ".$distance."

I utilized similar computations in the Wild Birds Unlimited website.

RSS feed | Trackback URI

20 Comments »

Comment by no imageKerry (SezWho)
2007-10-10 15:47:56

Thank you very much for sharing. This was an easy copy and paste job and works great. You’ve saved me alot of time.
FYI for anyone porting to C:
double deg2rad(double deg) { return deg*(3.14159265358979323846/180.0); }
Rate this:
1.6
 
Comment by no imagePeter M (SezWho)
2007-11-20 17:34:54

Very nice piece of posting - worked very nice - I only had to change the name of the table holding the lat-long. It works pretty fast to.. I have a reasonably small number of lat-longs (< 400) but I think this would scale nicely. Nice site too - i have just added it to my del.icio.us account and will check back regularly.
Rate this:
1.6
Comment by no imageDouglas Karr (SezWho)
2007-11-20 19:48:06

Thanks very much Peter and Kerry! If you like working on GIS projects, I’d recommend:

  1. Taking a look at my GIS category
  2. And definitely check out Mapping News by Mapperz
Rate this:
3.1 (1 person)
 
 
2008-01-03 21:07:54

[...] swath of topics. I might comment on form validation one day, a book that bored me the next, how to calculate distance with PHP and MySQL and a WordPress tweak some other [...]
 
Comment by no imageirmanator (SezWho)
2008-02-04 05:33:38

Thank you very much… :D
Rate this:
1.6
 
Comment by no imagerails friend (SezWho)
2008-04-16 06:33:16

I searched the whole day for distance calculations and found the harversine algorithm, thanks to you for giving the example on how to put it in an sql statement. Thanks and greets, Daniel
Rate this:
1.6
Comment by no imageDouglas Karr (SezWho)
2008-04-16 07:48:55

Glad to help out, rails friend!

Now I’m in search of an ‘in Polygon’ PHP function that will take an array of sequenced latitude and longitude coordinates and figure out if another point is within or outside of the polygon.

Rate this:
2.9
 
 
 
Comment by no imagedavid chan (SezWho)
2008-04-16 08:31:15

i think your SQL needs a having statement.
instead of WHERE distance <= $distance you might need to
use HAVING distance <= $distance

otherwise thanks for saving me a bunch of time and energy.

Rate this:
1.6
Comment by no imageDouglas Karr (SezWho)
2008-04-16 08:40:01

Hi David,

If you’re doing any type of GROUP BY statement, you will need HAVING. I am not doing that in the example above.

Doug

Rate this:
2.9
 
 
Comment by no imageJohn Burch (SezWho)
2008-04-19 14:47:17

As of MySQL 5.x, you can’t use an alias on a WHERE clause see http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html

Use HAVING instead of WHERE in the above querys

Rate this:
1.6
 
Comment by no imageSalman (SezWho)
2008-04-22 07:06:27

Thank you very much. You have done great job That’s the thing what i actually want. Thanks alot.
Rate this:
1.6
 
Comment by no imageGrant (SezWho)
2008-05-01 15:29:27

Thanks so much for sharing this code. It saved me a lot of development time. Also, thanks to your readers for pointing out that a HAVING statement is necessary for MySQL 5.x. Very helpful.
Rate this:
1.6
Comment by no imageDouglas Karr (SezWho)
2008-05-01 16:05:19

I’m blessed to have readers much smarter than I am!

:)

Rate this:
2.9
 
 
Comment by no imageHarry Hobson (SezWho)
2008-05-07 04:12:14

The above formula is saving me a lot of time. Thank you very much.
I also have to switch between the NMEA format and Degrees. I found a formula at this URL at the bottom of the page. http://www.errorforum.com/knowledge-base/16273-converting-nmea-sentence-latitude-longitude-decimal-degrees.html

Does anyone know how to verify this?

Thank you!
Harry

Rate this:
3.0
 
Comment by no imageHarry Hobson (SezWho)
2008-05-07 06:16:53

Hello,

Another question. Is there a formula for NMEA strings like the one below ?

1342.7500,N,10052.2287,E

$GPRMC,032731.000,A,1342.7500,N,10052.2287,E,0.40,106.01,101106,,*0B

Thanks,
Harry

Rate this:
3.0
 
Comment by no imageJohn (SezWho)
2008-05-26 22:40:42

I also found that WHERE did not work for me. Changed it to HAVING and everything works perfect. At first i didnt read the comments and rewrote it using a nested select. Both will work just fine.
Rate this:
1.6
 
Trackback by Daily Relevance
2008-05-26 22:51:52

Calculating distance using lat/long PHP…

After some doing a bit of research on this topic on the web, I was able to find exactly what I was looking for.  At first I looked over how to do this mathmaticly but then tried to find out if anyone else had found a solution using MySql that I could …

 
Comment by no imageGoZilla (SezWho)
2008-07-11 04:51:29

Thank you very mouch for the script written in mysql, just had to make few minor adjustments (HAVING) :)
Gret job
Rate this:
2.5
 
2008-07-24 14:25:11

[...] This month I’ve been programming quite a bit in PHP and MySQL with respect to GIS. Snooping around the net, I actually had a hard time finding some of [...]
 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.

My Comment Policy: I moderate comments. Please be patient:

  • Spam will happily be destroyed.
  • Use your real name, not some keywords. Otherwise it will be destroyed.
  • Mean comments aren't necessary. If I don't post them I will reply personally to let you know why.
  • Lewd comments will be edited, I don't want my readers leaving because of offensive content.
Great debate, criticism and colorful commentary is always appreciated and approved!