3 function fail($sError, $sUserError = false)
5 if (!$sUserError) $sUserError = $sError;
6 error_log('ERROR: '.$sError);
7 var_dump($sUserError)."\n";
12 function getProcessorCount()
14 $sCPU = file_get_contents('/proc/cpuinfo');
15 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
16 return count($aMatches[0]);
20 function getTotalMemoryMB()
22 $sCPU = file_get_contents('/proc/meminfo');
23 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
24 return (int)($aMatches[1]/1024);
28 function getCacheMemoryMB()
30 $sCPU = file_get_contents('/proc/meminfo');
31 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
32 return (int)($aMatches[1]/1024);
35 function getDatabaseDate(&$oDB)
37 // Find the newest node in the DB
38 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
39 // Lookup the timestamp that node was created
40 $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
41 $sLastNodeXML = file_get_contents($sLastNodeURL);
43 if ($sLastNodeXML === false) {
47 preg_match('#timestamp="(([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})Z)"#', $sLastNodeXML, $aLastNodeDate);
49 return $aLastNodeDate[1];
53 function byImportance($a, $b)
55 if ($a['importance'] != $b['importance'])
56 return ($a['importance'] > $b['importance']?-1:1);
58 return ($a['foundorder'] < $b['foundorder']?-1:1);
62 function javascript_renderData($xVal, $iOptions = 0)
64 $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
65 if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
66 // Unset, we call javascript_renderData again during exception handling
67 unset($_GET['json_callback']);
68 throw new Exception('Invalid json_callback value', 400);
71 $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
72 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
73 $iOptions |= JSON_PRETTY_PRINT;
76 $jsonout = json_encode($xVal, $iOptions);
79 header('Content-Type: application/javascript; charset=UTF-8');
80 echo $_GET['json_callback'].'('.$jsonout.')';
82 header('Content-Type: application/json; charset=UTF-8');
87 function addQuotes($s)
92 function parseLatLon($sQuery)
98 if (preg_match('/\\s*([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*\\s*/', $sQuery, $aData)) {
100 * degrees decimal minutes
101 * N 40 26.767, W 79 58.933
102 * N 40°26.767′, W 79°58.933′
105 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
106 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
107 } elseif (preg_match('/\\s*([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\s*/', $sQuery, $aData)) {
109 * degrees decimal minutes
110 * 40 26.767 N, 79 58.933 W
111 * 40° 26.767′ N 79° 58.933′ W
114 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
115 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
116 } elseif (preg_match('/\\s*([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\s*/', $sQuery, $aData)) {
118 * degrees decimal seconds
119 * N 40 26 46 W 79 58 56
120 * N 40° 26′ 46″, W 79° 58′ 56″
123 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
124 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
125 } elseif (preg_match('/\\s*([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+[0-9.]*)[″" ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+[0-9.]*)[″" ]+([EW])\\s*/', $sQuery, $aData)) {
127 * degrees decimal seconds
128 * 40 26 46 N 79 58 56 W
129 * 40° 26′ 46″ N, 79° 58′ 56″ W
130 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
133 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
134 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
135 } elseif (preg_match('/\\s*([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
138 * N 40.446° W 79.982°
141 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
142 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
143 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\s*/', $sQuery, $aData)) {
146 * 40.446° N 79.982° W
149 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
150 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
151 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
159 $fQueryLat = $aData[2];
160 $fQueryLon = $aData[3];
165 return array($sFound, $fQueryLat, $fQueryLon);
169 function geometryText2Points($geometry_as_text, $fRadius)
172 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
174 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
176 } elseif (preg_match('#LINESTRING\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
178 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
180 /* } elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
182 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
184 } elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#', $geometry_as_text, $aMatch)) {
186 $aPolyPoints = createPointsAroundCenter($aMatch[1], $aMatch[2], $fRadius);
190 if (isset($aPolyPoints)) {
191 $aResultPoints = array();
192 foreach ($aPolyPoints as $aPoint) {
193 $aResultPoints[] = array($aPoint[1], $aPoint[2]);
195 return $aResultPoints;
201 function createPointsAroundCenter($fLon, $fLat, $fRadius)
203 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
204 $fStepSize = (2*pi())/$iSteps;
205 $aPolyPoints = array();
206 for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
207 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
212 function closestHouseNumber($aRow)
214 $fHouse = $aRow['startnumber']
215 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
217 switch ($aRow['interpolationtype']) {
219 $iHn = (int)($fHouse/2) * 2 + 1;
222 $iHn = (int)(round($fHouse/2)) * 2;
225 $iHn = (int)(round($fHouse));
229 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
232 function getSearchRankLabel($iRank)
234 if (!isset($iRank)) return 'unknown';
235 if ($iRank < 2) return 'continent';
236 if ($iRank < 4) return 'sea';
237 if ($iRank < 8) return 'country';
238 if ($iRank < 12) return 'state';
239 if ($iRank < 16) return 'county';
240 if ($iRank == 16) return 'city';
241 if ($iRank == 17) return 'town / island';
242 if ($iRank == 18) return 'village / hamlet';
243 if ($iRank == 20) return 'suburb';
244 if ($iRank == 21) return 'postcode area';
245 if ($iRank == 22) return 'croft / farm / locality / islet';
246 if ($iRank == 23) return 'postcode area';
247 if ($iRank == 25) return 'postcode point';
248 if ($iRank == 26) return 'street / major landmark';
249 if ($iRank == 27) return 'minory street / path';
250 if ($iRank == 28) return 'house / building';
251 return 'other: ' . $iRank;