Author: Eduard Maximovich (edward.vstock@gmail.com)
Esempio n. 1
0
 /**
  * Compute distance (in radians) to a geo point
  *
  * @param GeoPoint $point Other geo point
  * @return number
  */
 public function radiansTo(GeoPoint $point)
 {
     $d2r = M_PI / 180.0;
     $lat1rad = $this->getLatitude() * $d2r;
     $lon1rad = $this->getLongitude() * $d2r;
     $lat2rad = $point->getLatitude() * $d2r;
     $lon2rad = $point->getLongitude() * $d2r;
     $deltaLat = $lat1rad - $lat2rad;
     $deltaLon = $lon1rad - $lon2rad;
     $sinLat = sin($deltaLat / 2);
     $sinLon = sin($deltaLon / 2);
     $a = $sinLat * $sinLat + cos($lat1rad) * cos($lat2rad) * $sinLon * $sinLon;
     $a = min(1.0, $a);
     return 2 * asin(sqrt($a));
 }
 /**
  * Returns a Google Geopoint matching the given address.
  */
 function addressToPoint($address)
 {
     $baseURL = "http://" . GOOGLEMAPS_HOST . "/maps/geo?output=xml" . "&key=" . GOOGLEMAPS_API_KEY;
     $requestURL = $baseURL . "&q=" . urlencode($address);
     $delay = 0;
     $status = "620";
     while (strcmp($status, "620") == 0) {
         $xml = simplexml_load_file($requestURL) or user_error("Can't access {$url}", E_USER_WARNING);
         if (!$xml) {
             return;
         }
         $status = $xml->Response->Status->code;
         if (strcmp($status, "200") == 0) {
             // Successful geocode
             $coordinates = $xml->Response->Placemark->Point->coordinates;
             $coordinatesSplit = split(",", $coordinates);
             // Format: Longitude, Latitude, Altitude
             $lat = $coordinatesSplit[1];
             $lng = $coordinatesSplit[0];
             return GeoPoint::from_x_y($lng, $lat);
         } else {
             if (strcmp($status, "620") == 0) {
                 // sent geocodes too fast
                 $delay += 100000;
             }
         }
     }
 }
 function testSubclassWriteToDatabase()
 {
     /* Test writing to database for GeoPoints on defined on subclass DataObjects */
     $pointObj = new GeoPointTest_ChildObj2();
     $pointObj->Title = "Test item";
     $pointObj->Point = GeoPoint::from_x_y(2, 3);
     $pointObj->write();
     // Test that the geo-data was saved properly
     $this->assertEquals("POINT(2 3)", DB::query("SELECT AsText(Point) FROM GeoPointTest_ChildObj2 WHERE ID = {$pointObj->ID}")->value());
 }
Esempio n. 4
0
 /**
  * @covers GeoPoint::boundingCoordinates
  */
 public function testBoundingCoordinatesWithRadius()
 {
     $expected = array(new GeoPoint(33.456826860664776, -82.01903313493325, GeoPoint::DEGREES), new GeoPoint(33.54677313933523, -81.91116686506675, GeoPoint::DEGREES));
     $this->assertEquals($expected, $this->instance->boundingCoordinates(5, 6370));
 }
Esempio n. 5
0
 /**
  * @param int $precision
  * @param bool $fixedPrecisionLength
  * @return string
  */
 public function getString($precision, $fixedPrecisionLength = false)
 {
     return sprintf("%s:%s - %s:%s", $this->northWest->getString($precision, $fixedPrecisionLength), $this->southWest->getString($precision, $fixedPrecisionLength));
 }
 public function addNearCondition($field, GeoPoint $point, $booleanOperator = 'AND')
 {
     if ($point->hasAllDistances()) {
         $prepared = ['$near' => ['$geometry' => ['type' => 'Point', 'coordinates' => [$point->getLatitude(), $point->getLongitude()]]], '$minDistance' => $point->getMinDistance(), '$maxDistance' => $point->getMaxDistance()];
     } else {
         if ($point->hasMaxDistance()) {
             $prepared = ['$near' => ['$geometry' => ['type' => 'Point', 'coordinates' => [$point->getLatitude(), $point->getLongitude()]]], '$maxDistance' => $point->getMaxDistance()];
         } else {
             if ($point->hasMinDistance()) {
                 $prepared = ['$near' => ['$geometry' => ['type' => 'Point', 'coordinates' => [$point->getLatitude(), $point->getLongitude()]]], '$minDistance' => $point->getMinDistance()];
             } else {
                 $prepared = ['$near' => ['$geometry' => ['type' => 'Point', 'coordinates' => [$point->getLatitude(), $point->getLongitude()]]]];
             }
         }
     }
     $this->addRawCondition($field, $prepared, $booleanOperator);
 }
Esempio n. 7
0
 /**
  * Returns a Google Geopoint matching the given address.
  */
 static function addressToPoint($address)
 {
     $baseURL = YAHOO_GEOCODER_URL;
     $requestURL = $baseURL . "?appid=" . GeoKeys::$yahoo_maps_api . "&output=php&location=" . urlencode($address);
     // Do a curl request to get the actual contents
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $requestURL);
     curl_setopt($ch, CURLOPT_HEADER, 1);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
     curl_setopt($ch, CURLOPT_FAILONERROR, 1);
     $raw_response = curl_exec($ch);
     if (curl_errno($ch)) {
         $result = array_merge(curl_getinfo($ch), array('status' => 'BAD', 'error_code' => curl_errno($ch), 'error_message' => curl_error($ch), 'raw' => $raw_response));
     } else {
         $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
         $result = array();
         $result['status'] = 'OK';
         $result['header'] = substr($raw_response, 0, $header_size);
         $result['body'] = substr($raw_response, $header_size);
         $result['http_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
         $result['last_url'] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
         $result['raw'] = $raw_response;
     }
     /*
      * Close The Curl Connection
      */
     curl_close($ch);
     $rslt = false;
     if ($result['status'] == 'OK') {
         $geodata = unserialize($result['body']);
         echo var_export($geodata, true);
         $georslt = $geodata['ResultSet']['Result'];
         // hack to fix multiple returned results
         if (isset($georslt[0])) {
             $georslt = $georslt[0];
         }
         try {
             if (!isset($georslt['warning'])) {
                 $lat = $georslt['Latitude'];
                 $lng = $georslt['Longitude'];
                 $rslt = GeoPoint::from_x_y($lng, $lat);
             }
         } catch (Exception $e) {
             echo 'Caught exception: ', $e->getMessage(), "\n";
             echo var_export($geodata, true);
             $rslt = false;
         }
     }
     return $rslt;
     //var_export($result);
     //var_export($geodata);
     //return false;
     $delay = 0;
     $status = "620";
     while (strcmp($status, "620") == 0) {
         $xml = simplexml_load_file($requestURL) or user_error("Can't access {$url}", E_USER_WARNING);
         if (!$xml) {
             return;
         }
         $status = $xml->Response->Status->code;
         if (strcmp($status, "200") == 0) {
             // Successful geocode
             $coordinates = $xml->Response->Placemark->Point->coordinates;
             $coordinatesSplit = split(",", $coordinates);
             // Format: Longitude, Latitude, Altitude
             $lat = $coordinatesSplit[1];
             $lng = $coordinatesSplit[0];
             return GeoPoint::from_x_y($lng, $lat);
         } else {
             if (strcmp($status, "620") == 0) {
                 // sent geocodes too fast
                 $delay += 100000;
             }
         }
     }
 }