3 function loadSettings($sProjectDir)
5 @define('CONST_InstallDir', $sProjectDir);
6 // Temporary hack to set the direcory via environment instead of
7 // the installed scripts. Neither setting is part of the official
9 defined('CONST_ConfigDir') or define('CONST_ConfigDir', $_SERVER['NOMINATIM_CONFIGDIR']);
12 function getSetting($sConfName, $sDefault = null)
14 $sValue = $_SERVER['NOMINATIM_'.$sConfName];
16 if ($sDefault !== null && !$sValue) {
23 function getSettingBool($sConfName)
25 $sVal = strtolower(getSetting($sConfName));
27 return strcmp($sVal, 'yes') == 0
28 || strcmp($sVal, 'true') == 0
29 || strcmp($sVal, '1') == 0;
32 function fail($sError, $sUserError = false)
35 $sUserError = $sError;
37 error_log('ERROR: '.$sError);
38 var_dump($sUserError);
44 function getProcessorCount()
46 $sCPU = file_get_contents('/proc/cpuinfo');
47 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
48 return count($aMatches[0]);
52 function getTotalMemoryMB()
54 $sCPU = file_get_contents('/proc/meminfo');
55 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
56 return (int)($aMatches[1]/1024);
60 function getCacheMemoryMB()
62 $sCPU = file_get_contents('/proc/meminfo');
63 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
64 return (int)($aMatches[1]/1024);
67 function getDatabaseDate(&$oDB)
69 // Find the newest node in the DB
70 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
71 // Lookup the timestamp that node was created
72 $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
73 $sLastNodeXML = file_get_contents($sLastNodeURL);
75 if ($sLastNodeXML === false) {
79 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);
81 return $aLastNodeDate[1];
85 function byImportance($a, $b)
87 if ($a['importance'] != $b['importance']) {
88 return ($a['importance'] > $b['importance']?-1:1);
91 return $a['foundorder'] <=> $b['foundorder'];
95 function javascript_renderData($xVal, $iOptions = 0)
97 $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
98 if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
99 // Unset, we call javascript_renderData again during exception handling
100 unset($_GET['json_callback']);
101 throw new Exception('Invalid json_callback value', 400);
104 $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
105 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
106 $iOptions |= JSON_PRETTY_PRINT;
109 $jsonout = json_encode($xVal, $iOptions);
112 header('Content-Type: application/javascript; charset=UTF-8');
113 echo $_GET['json_callback'].'('.$jsonout.')';
115 header('Content-Type: application/json; charset=UTF-8');
120 function addQuotes($s)
125 function parseLatLon($sQuery)
131 if (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9.]*)[°\s]+([0-9.]+)?[′\']*[,\s]+([EW])[\s]+([0-9]+)[°\s]+([0-9]+[0-9.]*)[′\']*\\s*/', $sQuery, $aData)) {
133 * degrees decimal minutes
134 * N 40 26.767, W 79 58.933
135 * N 40°26.767′, W 79°58.933′
138 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
139 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
140 } elseif (preg_match('/\\s*([0-9]+)[°\s]+([0-9]+[0-9.]*)?[′\']*[\s]+([NS])[,\s]+([0-9]+)[°\s]+([0-9]+[0-9.]*)?[′\'\s]+([EW])\\s*/', $sQuery, $aData)) {
142 * degrees decimal minutes
143 * 40 26.767 N, 79 58.933 W
144 * 40° 26.767′ N 79° 58.933′ W
147 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
148 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
149 } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+)[°\s]+([0-9]+)[′\'\s]+([0-9]+)[″"]*[,\s]+([EW])[\s]+([0-9]+)[°\s]+([0-9]+)[′\'\s]+([0-9]+)[″"]*\\s*/', $sQuery, $aData)) {
151 * degrees decimal seconds
152 * N 40 26 46 W 79 58 56
153 * N 40° 26′ 46″, W 79° 58′ 56″
156 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
157 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
158 } elseif (preg_match('/\\s*([0-9]+)[°\s]+([0-9]+)[′\'\s]+([0-9]+[0-9.]*)[″"\s]+([NS])[,\s]+([0-9]+)[°\s]+([0-9]+)[′\'\s]+([0-9]+[0-9.]*)[″"\s]+([EW])\\s*/', $sQuery, $aData)) {
160 * degrees decimal seconds
161 * 40 26 46 N 79 58 56 W
162 * 40° 26′ 46″ N, 79° 58′ 56″ W
163 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
166 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
167 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
168 } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
171 * N 40.446° W 79.982°
174 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
175 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
176 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
179 * 40.446° N 79.982° W
182 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
183 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
184 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
192 $fQueryLat = $aData[2];
193 $fQueryLon = $aData[3];
198 return array($sFound, $fQueryLat, $fQueryLon);
201 function closestHouseNumber($aRow)
203 $fHouse = $aRow['startnumber']
204 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
206 switch ($aRow['interpolationtype']) {
208 $iHn = (int)($fHouse/2) * 2 + 1;
211 $iHn = (int)(round($fHouse/2)) * 2;
214 $iHn = (int)(round($fHouse));
218 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
221 if (!function_exists('array_key_last')) {
222 function array_key_last(array $array)
224 if (!empty($array)) return key(array_slice($array, -1, 1, true));