Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
PHP GeoCalc (0 Comments)
Admin: Posted Date: April 4, 2010

PHP GeoCalc will calculate the distance in kilometers and the azimuth between two Latitude/Longitude locations. It will also calculate kilometers per degrees latitude/longitude and degrees latitude/longitude per kilometer for a given location.

Geographic Distance and Azimuth Calculations

GeoCalc is a PHP and (MySQL) API that is useful in performing distance calculations between two distinct points on the earth's surface. Used in most GIS (Geographic Information Systems), these are standard algorithms with various levels of accuracy. Simple example of uses for this software would be to find the nearest store location to you, or finding the straight-line distance between two points on a map.

The GeoCalc library is available for download at the bottom of this page.

Methods

The following methods are available in the API. All code samples below are written in PHP.

Basic Methods

01 // The default constructor
02 GeoCalc()
03  
04 // Using the Great Circle formula, calculate the distance in
05 // kilometers between Latitude/Longitude 1 and Latitude/Longitude 2
06 GCDistance($lat1, $lon1, $lat2, $lon2)
07  
08 // Using the Great Circle formula, calculate the azimuth between
09 // Latitude/Longitude 1 and Latitude/Longitude 2
10 GCAzimuth($lat1, $lon1, $lat2, $lon2)
11  
12 // Using the Ellipsoidal Approximation formula, calculate the distance
13 // in kilometers between Latitude/Longitude 1 and Latitude/Longitude 2
14 ApproxDistance($lat1, $lon1, $lat2, $lon2)
15  
16 // Using the Ellipsoidal Distance formula, calculate the distance in
17 // kilometers between Latitude/Longitude 1 and Latitude/Longitude 2.
18 // This formula is the most accurate, as it accounts for the "egg-like"
19 // shape of the earth
20 EllipsoidDistance($lat1, $lon1, $lat2, $lon2)  

Basic Methods

01 // Get the number of Kilometers per degree longitude at the given latitude.
02 getKmPerLonAtLat($dLatitude) 
03  
04 // Get the number of degrees longitude per kilometer at the given latitude.
05 getLonPerKmAtLat($dLatitude)
06  
07 // Get the number of kilometers per degree latitude (average of 111.0
08 // kilometers per degree of latitude)
09 getKmPerLat()
10  
11 // Get the number of degrees latitude per kilometer (average of
12 // 1/111 degrees per kilometer)
13 getLatPerKm()

Helper Function

1

ConvKilometersToMiles($dValue) //   Convert a distance from kilometers to miles (km / 1.609344 = miles).

Examples

Here are some example uses for this class:

01 include_once("GeoCalc.class.php");
02 $oGC = new GeoCalc();
03  
04 // Great Circle Distance
05 $dDist = $oGC->GCDistance(38.9333,-94.3253,38.9314,-94.4876);
06  
07 // Great Circle Azimuth
08 $dDist =  $oGC->GCAzimuth(38.9333,-94.3253,38.9314,-94.4876);
09  
10 // Approximate Ellipsoid Distance
11 $dDist = $oGC->ApproxDistance(38.9333,-94.3253,38.9314,-94.4876);
12  
13 // Accurate Ellipsoid Distance
14 $dDist = $oGC->EllipsoidDistance(38.9333,-94.3253,38.9314,-94.4876);
15  
16 // Convert distance from kilometers to miles
17 $dDistMiles = ConvKilometersToMiles($dDist);
18  
19 // Advanced Calculation:
20   // The following will search for ZIP codes
21   // within a radius (roughly calculated)
22  
23   // Define the center of the search bounds...
24   $dLongitude = -94.44590241;
25   $dLatitude = 38.7996;
26  
27   // Define the maximum search distance
28   $dRadius = 100.00;  // in kilometers
29  
30   // Calculate the boundary distance in degrees longitude / latitude
31   $dAddLat = $oGC->getLatPerKm() * $dRadius;
32   $dAddLon = $oGC->getLonPerKmAtLat($dLatitude) * $dRadius;
33  
34   // Calculate the boundaries
35   $dNorthBounds = $dLatitude + $dAddLat;
36   $dSouthBounds = $dLatitude - $dAddLat;
37   $dWestBounds = $dLongitude - $dAddLon;
38   $dEastBounds = $dLongitude + $dAddLon;
39  
40   print "Center Longitude: $dLongitude\n";
41   print "Center Latitude: $dLatitude\n";
42   print "Radius: $dRadius kilometers\n";
43  
44   print "North Bounds: $dNorthBounds\n";
45   print "South Bounds: $dSouthBounds\n";
46   print "East Bounds: $dEastBounds\n";
 

  print "West Bounds: $dWestBounds\n";

   
49   // Sample SQL query statement based on above boundaries:
50   $strQuery = "SELECT * FROM PostalCodes " .
51               "WHERE Latitude > $dSouthBounds " .
52               "AND Latitude < $dNorthBounds " .
53               "AND Longitude > $dWestBounds " .
54               "AND Longitude < $dEastBounds";

For Additional Performance

Using MySQL User Defined Functions (UDF), it is possible to speed up the execution of your distance calculation queries. The performance increase is achieved by relying on a pre-compiled, native library being linked into a running MySQL server.

 

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: