3 function fail($sError, $sUserError = false)
5 if (!$sUserError) $sUserError = $sError;
6 error_log('ERROR: '.$sError);
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 $iOptions |= JSON_UNESCAPED_UNICODE;
65 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
66 $iOptions |= JSON_PRETTY_PRINT;
69 $jsonout = json_encode($xVal, $iOptions);
71 if (!isset($_GET['json_callback'])) {
72 header('Content-Type: application/json; charset=UTF-8');
75 if (preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $_GET['json_callback'])) {
76 header('Content-Type: application/javascript; charset=UTF-8');
77 echo $_GET['json_callback'].'('.$jsonout.')';
79 header('HTTP/1.0 400 Bad Request');
84 function addQuotes($s)
89 function parseLatLon($sQuery)
95 if (preg_match('/\\s*([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*\\s*/', $sQuery, $aData)) {
97 * degrees decimal minutes
98 * N 40 26.767, W 79 58.933
99 * N 40°26.767′, W 79°58.933′
102 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
103 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
104 } elseif (preg_match('/\\s*([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\s*/', $sQuery, $aData)) {
106 * degrees decimal minutes
107 * 40 26.767 N, 79 58.933 W
108 * 40° 26.767′ N 79° 58.933′ W
111 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
112 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
113 } elseif (preg_match('/\\s*([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\s*/', $sQuery, $aData)) {
115 * degrees decimal seconds
116 * N 40 26 46 W 79 58 56
117 * N 40° 26′ 46″, W 79° 58′ 56″
120 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
121 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
122 } 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)) {
124 * degrees decimal seconds
125 * 40 26 46 N 79 58 56 W
126 * 40° 26′ 46″ N, 79° 58′ 56″ W
127 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
130 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
131 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
132 } elseif (preg_match('/\\s*([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
135 * N 40.446° W 79.982°
138 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
139 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
140 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\s*/', $sQuery, $aData)) {
143 * 40.446° N 79.982° W
146 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
147 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
148 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
156 $fQueryLat = $aData[2];
157 $fQueryLon = $aData[3];
162 return array($sFound, $fQueryLat, $fQueryLon);
166 function geometryText2Points($geometry_as_text, $fRadius)
169 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
171 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
173 } elseif (preg_match('#LINESTRING\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
175 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
177 } elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
179 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
181 } elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#', $geometry_as_text, $aMatch)) {
183 $aPolyPoints = createPointsAroundCenter($aMatch[1], $aMatch[2], $fRadius);
187 if (isset($aPolyPoints)) {
188 $aResultPoints = array();
189 foreach ($aPolyPoints as $aPoint) {
190 $aResultPoints[] = array($aPoint[1], $aPoint[2]);
192 return $aResultPoints;
198 function createPointsAroundCenter($fLon, $fLat, $fRadius)
200 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
201 $fStepSize = (2*pi())/$iSteps;
202 $aPolyPoints = array();
203 for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
204 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
209 function closestHouseNumber($aRow)
211 $fHouse = $aRow['startnumber']
212 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
214 switch ($aRow['interpolationtype']) {
216 $iHn = (int)($fHouse/2) * 2 + 1;
219 $iHn = (int)(round($fHouse/2)) * 2;
222 $iHn = (int)(round($fHouse));
226 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);