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');
87 function getAddressDetails(&$oDB, $sLanguagePrefArraySQL, $iPlaceID, $sCountryCode = false, $housenumber = -1, $bRaw = false)
89 $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata($iPlaceID, $housenumber)";
90 if (!$bRaw) $sSQL .= " WHERE isaddress OR type = 'country_code'";
91 $sSQL .= ' order by rank_address desc,isaddress desc';
93 $aAddressLines = chksql($oDB->getAll($sSQL));
94 if ($bRaw) return $aAddressLines;
96 //var_dump($aAddressLines);
99 foreach ($aAddressLines as $aLine) {
101 $aTypeLabel = Nominatim\ClassTypes\getInfo($aLine);
103 if ($aTypeLabel === false) {
104 $aTypeLabel = Nominatim\ClassTypes\getFallbackInfo($aLine);
108 if ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])) {
109 $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
110 $sTypeLabel = str_replace(' ', '_', $sTypeLabel);
111 if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place') {
112 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
114 $aFallback[$sTypeLabel] = $bFallback;
121 function addQuotes($s)
126 function parseLatLon($sQuery)
132 if (preg_match('/\\s*([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*\\s*/', $sQuery, $aData)) {
134 * degrees decimal minutes
135 * N 40 26.767, W 79 58.933
136 * N 40°26.767′, W 79°58.933′
139 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
140 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
141 } elseif (preg_match('/\\s*([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\s*/', $sQuery, $aData)) {
143 * degrees decimal minutes
144 * 40 26.767 N, 79 58.933 W
145 * 40° 26.767′ N 79° 58.933′ W
148 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
149 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
150 } elseif (preg_match('/\\s*([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\s*/', $sQuery, $aData)) {
152 * degrees decimal seconds
153 * N 40 26 46 W 79 58 56
154 * N 40° 26′ 46″, W 79° 58′ 56″
157 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
158 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
159 } 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)) {
161 * degrees decimal seconds
162 * 40 26 46 N 79 58 56 W
163 * 40° 26′ 46″ N, 79° 58′ 56″ W
164 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
167 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
168 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
169 } elseif (preg_match('/\\s*([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
172 * N 40.446° W 79.982°
175 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
176 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
177 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\s*/', $sQuery, $aData)) {
180 * 40.446° N 79.982° W
183 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
184 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
185 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
193 $fQueryLat = $aData[2];
194 $fQueryLon = $aData[3];
199 return array($sFound, $fQueryLat, $fQueryLon);
203 function geometryText2Points($geometry_as_text, $fRadius)
206 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
208 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
210 } elseif (preg_match('#LINESTRING\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
212 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
214 } elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
216 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
218 } elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#', $geometry_as_text, $aMatch)) {
220 $aPolyPoints = createPointsAroundCenter($aMatch[1], $aMatch[2], $fRadius);
224 if (isset($aPolyPoints)) {
225 $aResultPoints = array();
226 foreach ($aPolyPoints as $aPoint) {
227 $aResultPoints[] = array($aPoint[1], $aPoint[2]);
229 return $aResultPoints;
235 function createPointsAroundCenter($fLon, $fLat, $fRadius)
237 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
238 $fStepSize = (2*pi())/$iSteps;
239 $aPolyPoints = array();
240 for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
241 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
246 function closestHouseNumber($aRow)
248 $fHouse = $aRow['startnumber']
249 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
251 switch ($aRow['interpolationtype']) {
253 $iHn = (int)($fHouse/2) * 2 + 1;
256 $iHn = (int)(round($fHouse/2)) * 2;
259 $iHn = (int)(round($fHouse));
263 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);