3 require_once(CONST_BasePath.'/lib/ClassTypes.php');
5 function fail($sError, $sUserError = false)
7 if (!$sUserError) $sUserError = $sError;
8 error_log('ERROR: '.$sError);
14 function getProcessorCount()
16 $sCPU = file_get_contents('/proc/cpuinfo');
17 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
18 return count($aMatches[0]);
22 function getTotalMemoryMB()
24 $sCPU = file_get_contents('/proc/meminfo');
25 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
26 return (int)($aMatches[1]/1024);
30 function getCacheMemoryMB()
32 $sCPU = file_get_contents('/proc/meminfo');
33 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
34 return (int)($aMatches[1]/1024);
37 function getDatabaseDate(&$oDB)
39 // Find the newest node in the DB
40 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
41 // Lookup the timestamp that node was created
42 $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
43 $sLastNodeXML = file_get_contents($sLastNodeURL);
45 if ($sLastNodeXML === false) {
49 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);
51 return $aLastNodeDate[1];
55 function byImportance($a, $b)
57 if ($a['importance'] != $b['importance'])
58 return ($a['importance'] > $b['importance']?-1:1);
60 return ($a['foundorder'] < $b['foundorder']?-1:1);
64 function javascript_renderData($xVal, $iOptions = 0)
66 $iOptions |= JSON_UNESCAPED_UNICODE;
67 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
68 $iOptions |= JSON_PRETTY_PRINT;
71 $jsonout = json_encode($xVal, $iOptions);
73 if (!isset($_GET['json_callback'])) {
74 header('Content-Type: application/json; charset=UTF-8');
77 if (preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $_GET['json_callback'])) {
78 header('Content-Type: application/javascript; charset=UTF-8');
79 echo $_GET['json_callback'].'('.$jsonout.')';
81 header('HTTP/1.0 400 Bad Request');
86 function addQuotes($s)
91 function parseLatLon($sQuery)
97 if (preg_match('/\\s*([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*\\s*/', $sQuery, $aData)) {
99 * degrees decimal minutes
100 * N 40 26.767, W 79 58.933
101 * N 40°26.767′, W 79°58.933′
104 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
105 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
106 } elseif (preg_match('/\\s*([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\s*/', $sQuery, $aData)) {
108 * degrees decimal minutes
109 * 40 26.767 N, 79 58.933 W
110 * 40° 26.767′ N 79° 58.933′ W
113 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
114 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
115 } elseif (preg_match('/\\s*([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\s*/', $sQuery, $aData)) {
117 * degrees decimal seconds
118 * N 40 26 46 W 79 58 56
119 * N 40° 26′ 46″, W 79° 58′ 56″
122 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
123 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
124 } 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)) {
126 * degrees decimal seconds
127 * 40 26 46 N 79 58 56 W
128 * 40° 26′ 46″ N, 79° 58′ 56″ W
129 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
132 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
133 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
134 } elseif (preg_match('/\\s*([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
137 * N 40.446° W 79.982°
140 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
141 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
142 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\s*/', $sQuery, $aData)) {
145 * 40.446° N 79.982° W
148 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
149 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
150 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
158 $fQueryLat = $aData[2];
159 $fQueryLon = $aData[3];
164 return array($sFound, $fQueryLat, $fQueryLon);
168 function geometryText2Points($geometry_as_text, $fRadius)
171 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
173 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
175 } elseif (preg_match('#LINESTRING\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
177 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
179 } elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
181 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
183 } elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#', $geometry_as_text, $aMatch)) {
185 $aPolyPoints = createPointsAroundCenter($aMatch[1], $aMatch[2], $fRadius);
189 if (isset($aPolyPoints)) {
190 $aResultPoints = array();
191 foreach ($aPolyPoints as $aPoint) {
192 $aResultPoints[] = array($aPoint[1], $aPoint[2]);
194 return $aResultPoints;
200 function createPointsAroundCenter($fLon, $fLat, $fRadius)
202 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
203 $fStepSize = (2*pi())/$iSteps;
204 $aPolyPoints = array();
205 for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
206 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
211 function closestHouseNumber($aRow)
213 $fHouse = $aRow['startnumber']
214 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
216 switch ($aRow['interpolationtype']) {
218 $iHn = (int)($fHouse/2) * 2 + 1;
221 $iHn = (int)(round($fHouse/2)) * 2;
224 $iHn = (int)(round($fHouse));
228 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);