6 * A geographic point with a search radius.
17 public function __construct($lat, $lon, $radius = 0.1)
19 $this->fLat = (float)$lat;
20 $this->fLon = (float)$lon;
21 $this->fRadius = (float)$radius;
22 $this->sSQL = 'ST_SetSRID(ST_Point('.$this->fLon.','.$this->fLat.'),4326)';
35 public function radius()
37 return $this->fRadius;
40 public function distanceSQL($sObj)
42 return 'ST_Distance('.$this->sSQL.", $sObj)";
45 public function withinSQL($sObj)
47 return sprintf('ST_DWithin(%s, %s, %F)', $sObj, $this->sSQL, $this->fRadius);
51 * Check that the coordinates are valid WSG84 coordinates.
53 * @return bool True if the coordinates are correctly bounded.
55 public function isValid()
57 return ($this->fLat <= 90.1
58 && $this->fLat >= -90.1
59 && $this->fLon <= 180.1
60 && $this->fLon >= -180.1);
64 * Extract a coordinate point from a query string.
66 * If a coordinate is found an array of a new NearPoint and the
67 * remaining query is returned or false otherwise.
69 * @param string $sQuery Query to scan.
71 * @return array|false If a coordinate was found, an array with
72 * `pt` as the NearPoint coordinates and `query`
73 * with the remaining query string. False otherwiese.
75 public static function extractFromQuery($sQuery)
77 // Do we have anything that looks like a lat/lon pair?
78 // returns array(lat,lon,query_with_lat_lon_removed)
84 if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*?\\b/', $sQuery, $aData)) {
86 * degrees decimal minutes
87 * N 40 26.767, W 79 58.933
88 * N 40°26.767′, W 79°58.933′
91 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
92 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
93 } elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\b/', $sQuery, $aData)) {
95 * degrees decimal minutes
96 * 40 26.767 N, 79 58.933 W
97 * 40° 26.767′ N 79° 58.933′ W
100 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
101 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
102 } elseif (preg_match('/\\b([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\b/', $sQuery, $aData)) {
104 * degrees decimal seconds
105 * N 40 26 46 W 79 58 56
106 * N 40° 26′ 46″, W 79° 58′ 56″
109 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
110 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
111 } elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([EW])\\b/', $sQuery, $aData)) {
113 * degrees decimal seconds
114 * 40 26 46 N 79 58 56 W
115 * 40° 26′ 46″ N, 79° 58′ 56″ W
118 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
119 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
120 } elseif (preg_match('/\\b([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\b/', $sQuery, $aData)) {
123 * N 40.446° W 79.982°
126 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
127 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
128 } elseif (preg_match('/\\b([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\b/', $sQuery, $aData)) {
131 * 40.446° N 79.982° W
134 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
135 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
136 } elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData)) {
143 $fQueryLat = $aData[2];
144 $fQueryLon = $aData[3];
149 $oPt = new NearPoint($fQueryLat, $fQueryLon);
151 if (!$oPt->isValid()) return false;
153 $sQuery = trim(str_replace($sFound, ' ', $sQuery));
155 return array('pt' => $oPt, 'query' => $sQuery);