distance() public method

Calculate the surface distance between this LatLng object and the one passed in as a parameter.
public distance ( $to ) : the
return the surface distance
Beispiel #1
0
 /**
  * @expectedException \PHPUnit_Framework_Error_Warning
  */
 public function testDistanceDifferentEllipsoids()
 {
     $work = new LatLng(51.54105, -0.12319, RefEll::Airy1830());
     $charingCross = new LatLng(51.507977, -0.124588, RefEll::WGS84());
     $expected = 3678.49665;
     self::assertEquals($expected, $work->distance($charingCross));
 }
    <p>
      <pre>$lld1 = new LatLng(40.718119, -73.995667); // New York
echo "New York Lat/Long: " . $lld1->toString() . "&lt;br /&gt;";
$lld2 = new LatLng(51.499981, -0.125313);  // London
$d = $lld1->distance($lld2);
echo "Surface Distance between New York and London: " . $d . "km";</pre>

      <?php 
$lld1 = new LatLng(40.718119, -73.995667);
// New York
echo "New York Lat/Long: " . $lld1->toString() . "<br />";
$lld2 = new LatLng(51.499981, -0.125313);
// London
echo "London Lat/Long: " . $lld2->toString() . "<br />";
$d = $lld1->distance($lld2);
echo "Surface Distance between New York and London: " . $d . "km";
?>
    </p>

    <h2>Convert OS Grid Reference to Latitude/Longitude</h2>

    <p>
      Note that the OSGB-Latitude/Longitude conversions use the OSGB36 datum by default. The
      majority of applications use the WGS84 datum, for which the appropriate conversions
      need to be added. See the examples below to see the difference between the two data.
    </p>

    <p>
      Using OSGB36 (convert an OSGB grid reference to a latitude and longitude using the OSGB36 datum):
Beispiel #3
0
 public function get_outlets_new($types, $lat, $lng, $zoom)
 {
     //if(!isset($types) && !isset($latitude) && !isset($longitude) && !isset($zoom)) return FALSE;
     if ($types == 'all') {
         $types = "1,2,3,4,5,6,7,8,9,11,12,13,14,16,17,18,19,20,21,22,23,24,25,26,27,29,30,31,32,33,34,37,38,40,41,42,43,44,45,46,49,51,52,53,56,57,80,81,82,83,84,130,145,146,148,149,150,151,152";
     }
     // Script start time - so we can see how long it takes at various stages
     $time_start = microtime(true);
     $output = '';
     $cachetime = 0;
     foreach (glob("/home/recycle/public_html/tmp/*.outlets.json") as $filename) {
         $cachetime = explode('.', basename($filename));
         $cachetime = $cachetime[0];
         //$output .= "Found cached json file with timestamp: $cachetime.\n";
     }
     if (time() - $cachetime < 86400) {
         //$output .= "Found cached json file less than a day old, timestamp: $cachetime. Loading this instead of regenerating outlets array!\n\n";
         $outlets_json = file_get_contents("/home/recycle/public_html/tmp/{$cachetime}.outlets.json");
         $outlets = json_decode($outlets_json, 1);
         //$output .= print_r($outlets,1);
     } else {
         //$output .= "No up to date outlets cache could be found, regenerating outlets array!\n\n";
         // Load ALL OUTLETS and ALL OUTLET RECYCLE TYPES into PHP ARRAYS
         $sql = "SELECT outlets.outlet_id, outlets.latitude, outlets.longitude FROM outlets";
         $query = $this->db->query($sql);
         $outlets_table = $query->result_array();
         $sql = "SELECT * FROM outlets_recycle_types";
         $query = $this->db->query($sql);
         $outlets_recycle_types_table = $query->result_array();
         // Clone outlets array to add more refined data to
         $outlets = array();
         // Loop through all outlets rows to create more useful multidimensional associative array
         foreach ($outlets_table as $outlet_row) {
             $outlets[$outlet_row['outlet_id']] = array('lat' => $outlet_row['latitude'], 'lng' => $outlet_row['longitude'], 'types' => array());
         }
         // Loop through all recycle types rows to create more useful multidimensional associative array inside outlets
         foreach ($outlets_recycle_types_table as $outlets_recycle_types_table_row) {
             $outlets[$outlets_recycle_types_table_row['outlet_id']]['types'][] = $outlets_recycle_types_table_row['recycle_type'];
         }
         foreach (glob("/home/recycle/public_html/tmp/*.outlets.json") as $filename) {
             unlink($filename);
         }
         file_put_contents("/home/recycle/public_html/tmp/" . time() . ".outlets.json", json_encode($outlets));
         //$output .= print_r($outlets,1);
     }
     // Explode array of specified recycle types
     $typesarray = explode(',', $types);
     //$output .= "Types to check for:\n\n".print_r($typesarray,1);
     // Get a fuzzy max distance from center of viewport to points to eliminate points which are off the screen
     switch ($zoom) {
         case 19:
             $maxDistance = 0.13;
             break;
         case 18:
             $maxDistance = 0.2;
             break;
         case 17:
             $maxDistance = 0.4;
             break;
         case 16:
             $maxDistance = 0.9;
             break;
         case 15:
             $maxDistance = 1.8;
             break;
         case 14:
             $maxDistance = 3.5;
             break;
         case 13:
             $maxDistance = 8;
             break;
         case 12:
             $maxDistance = 15;
             break;
         case 11:
             $maxDistance = 27;
             break;
         case 10:
             $maxDistance = 60;
             break;
         case 9:
             $maxDistance = 120;
             break;
         case 8:
             $maxDistance = 250;
             break;
         case 7:
             $maxDistance = 500;
             break;
         case 6:
             $maxDistance = 1000;
             break;
         case 5:
             $maxDistance = 2000;
             break;
         case 4:
             $maxDistance = 4000;
             break;
         case 3:
             $maxDistance = 10000;
             break;
         default:
             $maxDistance = 10000;
     }
     // Get a fuzzy max distance from center of viewport to points to eliminate points which are off the screen
     switch ($zoom) {
         case 19:
             $clusterRadius = 0;
             break;
         case 18:
             $clusterRadius = 0;
             break;
         case 17:
             $clusterRadius = 0.006;
             break;
         case 16:
             $clusterRadius = 0.012;
             break;
         case 15:
             $clusterRadius = 0.024;
             break;
         case 14:
             $clusterRadius = 0.05;
             break;
         case 13:
             $clusterRadius = 0.1;
             break;
         case 12:
             $clusterRadius = 0.2;
             break;
         case 11:
             $clusterRadius = 0.4;
             break;
         case 10:
             $clusterRadius = 0.8;
             break;
         case 9:
             $clusterRadius = 1.6;
             break;
         case 8:
             $clusterRadius = 3.2;
             break;
         case 7:
             $clusterRadius = 6.5;
             break;
         case 6:
             $clusterRadius = 13;
             break;
         case 5:
             $clusterRadius = 26;
             break;
         case 4:
             $clusterRadius = 52;
             break;
         case 3:
             $clusterRadius = 104;
             break;
         default:
             $clusterRadius = 200;
     }
     // Create array to store clusters of outlets in
     $clusters = array();
     // And array to store outlets which aren't in a cluster
     $singleOutlets = array();
     foreach ($outlets as $id => $outlet) {
         //$output .= "Comparing types:\n\n".print_r($typesarray,1);
         //$output .= "With outlet types:\n\n".print_r($outlet['types'],1);
         $foundtypes = 0;
         foreach ($typesarray as $type) {
             if (in_array($type, $outlet['types'])) {
                 $foundtypes++;
             }
         }
         // We only want to process outlets which have at least one of the requested recycle types
         if ($foundtypes > 0) {
             // This outlet supports at least one of the recycle types we want, give it a new parameter to tell us what percentage of the ones we want it supports
             $outlet['typesratio'] = $foundtypes / count($typesarray);
             // Get the distance from this outlet to the centre of the viewport
             $lld1 = new LatLng($lat, $lng);
             // LatLng of viewport center
             $lld2 = new LatLng($outlet['lat'], $outlet['lng']);
             // LatLng of outlet
             $distance = $lld1->distance($lld2);
             // in km
             // Skip this outlet, it's off the screen
             if ($distance > $maxDistance) {
                 //$output .= "$distance > $maxDistance so skipping outlet\n";
                 continue;
             }
             // Create first cluster
             if (empty($clusters)) {
                 $clusters[] = array('id' => $id, 'lat' => $outlet['lat'], 'lng' => $outlet['lng'], 'count' => 1);
                 // Add this outlet to the singleOutlets array, even though we're making a cluster from it, because we don't know if this cluster will get any other outlets yet
                 $singleOutlets[$id] = $outlet;
             } else {
                 // Loop through all clusters which exist to see if we should add this one to a cluster
                 $outletAddedToCluster = false;
                 foreach ($clusters as $clusterKey => $cluster) {
                     // Calculate distance between this outlet and the centre of whichever cluster we are testing
                     $lld1 = new LatLng($cluster['lat'], $cluster['lng']);
                     // LatLng of cluster center
                     $lld2 = new LatLng($outlet['lat'], $outlet['lng']);
                     // LatLng of outlet
                     $clusterOutletDistance = $lld1->distance($lld2);
                     // in km
                     // This outlet is within the radius of a cluster, add it to the cluster
                     if ($clusterOutletDistance < $clusterRadius * 6) {
                         // Increment the count variable of the cluster to show we've added an outlet to it
                         $clusters[$clusterKey]['count']++;
                         // Remove the outlet (which originally created the cluster we are now adding to) from the singleOutlets array
                         // This prevents cluster-starter outlets from appearing as singleOutlets
                         // But doesn't stop clusters with no other outlets from appearing as clusters, this needs to be done outside the outlet loop
                         unset($singleOutlets[$clusters[$clusterKey]['id']]);
                         // This allows us to create a new cluster outside of this loop if we don't find a cluster to add this outlet to
                         $outletAddedToCluster = true;
                         // We've added this outlet to a cluster so there's no need to keep looking for a cluster to add it to
                         break;
                     }
                 }
                 // We didn't find a cluster to add the marker to, let's create a new cluster
                 if ($outletAddedToCluster === false) {
                     $clusters[] = array('id' => $id, 'lat' => $outlet['lat'], 'lng' => $outlet['lng'], 'count' => 1);
                     // Add this outlet to the singleOutlets array, even though we're making a cluster from it, because we don't know if this cluster will get any other outlets yet
                     $singleOutlets[$id] = $outlet;
                 }
             }
             //$output .= "Found outlet with $foundtypes types! ID: $id\n";
         } else {
             //$output .= "Intersect isn't the same as typesarray!\n Intersect:\n".print_r($intersect,1)." ID: $id\n\n";
         }
     }
     // So we've just finished looping through all the outlets
     // We now have two arrays, clusters and singleOutlets. singleOutlets should be fine but clusters still contains clusters which have no other outlets
     // So let's loop through and delete them
     foreach ($clusters as $key => $cluster) {
         if ($cluster['count'] == 1) {
             unset($clusters[$key]);
         }
     }
     //$output .= "\ntotal outlets after filters: ".count($outlets_filtered);
     //$output .= "\ntotal clusters: ".count($clusters);
     //$output .= "\n\n clusters: ".print_r($clusters,1);
     //$output .= "OR-filtered outlets:\n\n".print_r($outlets_filtered_or,1);
     //$output .= "AND-filtered outlets:\n\n".print_r($outlets_filtered_and,1);
     //$output .= "\n\nFiltered outlets:\n\n".print_r($outlets_filtered,1);
     //$output .= "\n\nAll outlets:\n\n".print_r($outlets,1);
     //"<pre>".print_r($outlets,1)."</pre> <br />
     //$output .= "\n\nTook ". (microtime(true)-$time_start) . " seconds, i think";
     //return "<pre>".$output."</pre>";
     return array('clusters' => $clusters, 'singleOutlets' => $singleOutlets);
 }