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)
37 return sprintf('ST_DWithin(%s, %s, %F)', $sObj, $this->sSQL, $this->fRadius);
41 * Check that the coordinates are valid WSG84 coordinates.
43 public function isValid()
45 return ($this->fLat <= 90.1
46 && $this->fLat >= -90.1
47 && $this->fLon <= 180.1
48 && $this->fLon >= -180.1);
52 * Extract a coordinate point from a query string.
54 * If a coordinate is found an array of a new NearPoint and the
55 * remaining query is returned or false otherwise.
57 public static function extractFromQuery($sQuery)
59 // Do we have anything that looks like a lat/lon pair?
60 // returns array(lat,lon,query_with_lat_lon_removed)
66 if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*?\\b/', $sQuery, $aData)) {
68 * degrees decimal minutes
69 * N 40 26.767, W 79 58.933
70 * N 40°26.767′, W 79°58.933′
73 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
74 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
75 } elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\b/', $sQuery, $aData)) {
77 * degrees decimal minutes
78 * 40 26.767 N, 79 58.933 W
79 * 40° 26.767′ N 79° 58.933′ W
82 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
83 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
84 } elseif (preg_match('/\\b([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\b/', $sQuery, $aData)) {
86 * degrees decimal seconds
87 * N 40 26 46 W 79 58 56
88 * N 40° 26′ 46″, W 79° 58′ 56″
91 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
92 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
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 seconds
96 * 40 26 46 N 79 58 56 W
97 * 40° 26′ 46″ N, 79° 58′ 56″ W
100 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
101 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
102 } elseif (preg_match('/\\b([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\b/', $sQuery, $aData)) {
105 * N 40.446° W 79.982°
108 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
109 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
110 } elseif (preg_match('/\\b([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\b/', $sQuery, $aData)) {
113 * 40.446° N 79.982° W
116 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
117 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
118 } elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData)) {
125 $fQueryLat = $aData[2];
126 $fQueryLon = $aData[3];
131 $oPt = new NearPoint($fQueryLat, $fQueryLon);
133 if (!$oPt->isValid()) return False;
135 $sQuery = trim(str_replace($sFound, ' ', $sQuery));
137 return array('pt' => $oPt, 'query' => $sQuery);