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)";
36 * Check that the coordinates are valid WSG84 coordinates.
38 public function isValid()
40 return ($this->fLat <= 90.1
41 && $this->fLat >= -90.1
42 && $this->fLon <= 180.1
43 && $this->fLon >= -180.1);
47 * Extract a coordinate point from a query string.
49 * If a coordinate is found an array of a new NearPoint and the
50 * remaining query is returned or false otherwise.
52 public static function extractFromQuery($sQuery)
54 // Do we have anything that looks like a lat/lon pair?
55 // returns array(lat,lon,query_with_lat_lon_removed)
61 if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*?\\b/', $sQuery, $aData)) {
63 * degrees decimal minutes
64 * N 40 26.767, W 79 58.933
65 * N 40°26.767′, W 79°58.933′
68 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
69 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
70 } elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\b/', $sQuery, $aData)) {
72 * degrees decimal minutes
73 * 40 26.767 N, 79 58.933 W
74 * 40° 26.767′ N 79° 58.933′ W
77 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
78 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
79 } elseif (preg_match('/\\b([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\b/', $sQuery, $aData)) {
81 * degrees decimal seconds
82 * N 40 26 46 W 79 58 56
83 * N 40° 26′ 46″, W 79° 58′ 56″
86 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
87 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
88 } elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([EW])\\b/', $sQuery, $aData)) {
90 * degrees decimal seconds
91 * 40 26 46 N 79 58 56 W
92 * 40° 26′ 46″ N, 79° 58′ 56″ W
95 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
96 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
97 } elseif (preg_match('/\\b([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\b/', $sQuery, $aData)) {
100 * N 40.446° W 79.982°
103 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
104 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
105 } elseif (preg_match('/\\b([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\b/', $sQuery, $aData)) {
108 * 40.446° N 79.982° W
111 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
112 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
113 } elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData)) {
120 $fQueryLat = $aData[2];
121 $fQueryLon = $aData[3];
126 $oPt = new NearPoint($fQueryLat, $fQueryLon);
128 if (!$oPt->isValid()) return False;
130 $sQuery = trim(str_replace($sFound, ' ', $sQuery));
132 return array('pt' => $oPt, 'query' => $sQuery);