NYCPHP Meetup

NYPHP.org

[nycphp-talk] Hello / Geo coords

Hans Zaunere hans at nyphp.org
Fri Nov 21 09:56:43 EST 2003



Mark Armendariz wrote:
> here's a function i've ben using for a couple years now.  I can honestly 
> say I have no idea whether or not i wrote it as i'd done a LOT of 
> variations of it at the time.  From what I know it's worked well for 
> around 2 years now (for my client).  I've only tested with US values, 
> but i imagine it should work for other values as well.

This, and Daniel's code, are perfect candidates for a PCOM!  :)

Avoid the holiday rush - Submit Yours Today at http://pcomd.net !

H


>  
> Good luck!!
> 
> function getDistance($fromLatitude, $fromLongitude, $toLatitude, 
> $toLongitude) {
>  
>  /* assume your points, in decimal, are in $fromLongitude,$fromLatitude 
> and $toLongitude,$toLatitude
>  * this can also be useful with the US ZIPcodes at
>  * http://ftp.census.gov/geo/www/gazetteer/places.html
>  */
>  
>  $Pi   = 3.1415926535897932384626;
>  $Radius  = doubleval($Pi/180.0);
>  
>  $fromLongitude  = doubleval($fromLongitude) * $Radius; $fromLatitude = 
> doubleval($fromLatitude) * $Radius;
>  $toLongitude   = doubleval($toLongitude) * $Radius; $toLatitude = 
> doubleval($toLatitude) * $Radius;
>  
>  $theta = $toLongitude - $fromLongitude;
>  $Distance = acos(sin($fromLatitude) * sin($toLatitude) + 
> cos($fromLatitude) * cos($toLatitude) * cos($theta));
>  if ($Distance < 0) {
>   $Distance += $Pi;
>  }
>  
>  $Distance  = $Distance * 6371.2;
>  $Miles   = doubleval($Distance * 0.621);
>  $Distance  = sprintf("%.2f", $Distance);
>  $Miles   = sprintf("%.4f", $Miles);
>  
>  return $Miles;
> }
>  
> 
> ------------------------------------------------------------------------
> *From:* talk-bounces at lists.nyphp.org 
> [mailto:talk-bounces at lists.nyphp.org] *On Behalf Of *Daniel Kushner
> *Sent:* Thursday, November 20, 2003 10:54 PM
> *To:* NYPHP Talk
> *Subject:* RE: [nycphp-talk] Hello / Geo coords
> 
> Spot,
>  
> Here's some code that I have from a long time ago that calculates the 
> distance between airports based on longitude and latitude. You can do 
> the same with Zip codes once you convert a zip code to long/lat.
>  
> Best,
> Daniel Kushner
>  
> $db = new My_DB("SELECT *
>       FROM airports AS a
>          WHERE id IN ('$origin', '$destination')");
>  
>  $db->next_record();
>  $a = deg2rad($db->f('latitude'));
>  $b = deg2rad($db->f('longitude'));
>  $origin_id = $db->f('id');
>  $origin_name = $db->f('name');
>  
>  $db->next_record();
>  $c = deg2rad($db->f('latitude'));
>  $d = deg2rad($db->f('longitude'));
>  $destination_id = $db->f('id');
>  $destination_name = $db->f('name');
>  
>  
>  $r=3963.1; //radius of the earth in miles
>  
>  //calculate the distance between the two points
>  $distance = acos(
>       (cos($a) * cos($b) * cos($c) * cos($d)) +
>       (cos($a) * sin($b) * cos($c) * sin($d)) +
>       (sin($a) * sin($c))
>      ) * $r;
>      
>  if($origin == $destination) {
>   echo "Distance = 0<br>"; 
>  } else {
>   echo "Origin: ($origin_id) $origin_name<br>";
>   echo "Destination: ($destination_id) $destination_name<br>";
>   echo "Distance in miles: ", round($distance,4), "<br>";
>   echo "Distance in kilometers: ", round(($distance*1.609),4), "<br>";
>  }
> 
>     -----Original Message-----
>     *From:* talk-bounces at lists.nyphp.org
>     [mailto:talk-bounces at lists.nyphp.org]*On Behalf Of *Spot
>     *Sent:* Thursday, November 20, 2003 2:56 PM
>     *To:* talk at lists.nyphp.org
>     *Subject:* [nycphp-talk] Hello / Geo coords
> 
>     Hey everyone,
>      
>     Just thought I would introduce myself as new member to the list and
>     pose a question at the same time.
> 
>     My name is Spot(yes, legally). I am with deviantART.com. We are the
>     largest art community in the world with a little under 500,000
>     artists, almost 3 million pieces of art and close to 80 million hits
>     a month. We move about 55mbit/s average. Running on 37 boxes (last I
>     checked) and two 9 terabyte fibre enclosures. All on
>     Apache/PHP/Mysql. Except for Zues talking to the fibre enclosures
>     for raw data (images). All of our code is from the ground up. No
>     external libraries.
> 
>     Currently we are employing somewhat of a locational system where we
>     allow artists to provide their Geo coords (after getting them from
>     Maporama.com) and then, currently using GeoURL.org, we pull in RSS
>     feeds and allow them to see lists of artists close to them.
> 
>     We are interested in bringing as much of this "in-house" as
>     possible. Especially the GeoURL functionality. Does anyone have any
>     information on methods for creating "crow flies" distances between
>     artists based on geo coords? I would be interested in using a third
>     party library if there is one.
>      
>      
>     Thanks!
> 
>     Spot
>     Director of Prints
>     deviantART Inc.
>     www.deviantart.com
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk




More information about the talk mailing list