6 * A geographic point with a search radius.
16 public function __construct($lat, $lon, $radius = 0.1)
18 $this->fLat = (float)$lat;
19 $this->fLon = (float)$lon;
20 $this->fRadius = (float)$radius;
21 $this->sSQL = 'ST_SetSRID(ST_Point('.$this->fLon.','.$this->fLat.'),4326)';
24 public function lat() { return $this->fLat; }
26 public function lon() { return $this->fLon; }
28 public function radius() { return $this->fRadius; }
30 public function distanceSQL($sObj)
32 return 'ST_Distance('.$this->sSQL.", $sObj)";
35 public function withinSQL($sObj)
38 'ST_DWithin(%S, ST_SetSRID(ST_Point(%F,%F),4326), %F)',
47 * Check that the coordinates are valid WSG84 coordinates.
49 public function isValid()
51 return ($this->fLat <= 90.1
52 && $this->fLat >= -90.1
53 && $this->fLon <= 180.1
54 && $this->fLon >= -180.1);
58 * Extract a coordinate point from a query string.
60 * If a coordinate is found an array of a new NearPoint and the
61 * remaining query is returned or false otherwise.
63 public static function extractFromQuery($sQuery)
65 // Do we have anything that looks like a lat/lon pair?
66 // returns array(lat,lon,query_with_lat_lon_removed)
72 if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*?\\b/', $sQuery, $aData)) {
74 * degrees decimal minutes
75 * N 40 26.767, W 79 58.933
76 * N 40°26.767′, W 79°58.933′
79 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
80 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
81 } elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\b/', $sQuery, $aData)) {
83 * degrees decimal minutes
84 * 40 26.767 N, 79 58.933 W
85 * 40° 26.767′ N 79° 58.933′ W
88 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
89 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
90 } elseif (preg_match('/\\b([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\b/', $sQuery, $aData)) {
92 * degrees decimal seconds
93 * N 40 26 46 W 79 58 56
94 * N 40° 26′ 46″, W 79° 58′ 56″
97 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
98 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
99 } elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([EW])\\b/', $sQuery, $aData)) {
101 * degrees decimal seconds
102 * 40 26 46 N 79 58 56 W
103 * 40° 26′ 46″ N, 79° 58′ 56″ W
106 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
107 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
108 } elseif (preg_match('/\\b([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\b/', $sQuery, $aData)) {
111 * N 40.446° W 79.982°
114 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
115 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
116 } elseif (preg_match('/\\b([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\b/', $sQuery, $aData)) {
119 * 40.446° N 79.982° W
122 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
123 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
124 } elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData)) {
131 $fQueryLat = $aData[2];
132 $fQueryLon = $aData[3];
137 $oPt = new NearPoint($fQueryLat, $fQueryLon);
139 if (!$oPt->isValid()) return False;
141 $sQuery = trim(str_replace($sFound, ' ', $sQuery));
143 return array('pt' => $oPt, 'query' => $sQuery);