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_DataDir') or define('CONST_DataDir', $_SERVER['NOMINATIM_DATADIR']);
10 defined('CONST_SqlDir') or define('CONST_SqlDir', $_SERVER['NOMINATIM_SQLDIR']);
11 defined('CONST_ConfigDir') or define('CONST_ConfigDir', $_SERVER['NOMINATIM_CONFIGDIR']);
12 defined('CONST_Default_ModulePath') or define('CONST_Default_ModulePath', $_SERVER['NOMINATIM_DATABASE_MODULE_SRC_PATH']);
15 function getSetting($sConfName, $sDefault = null)
17 $sValue = $_SERVER['NOMINATIM_'.$sConfName];
19 if ($sDefault !== null && !$sValue) {
26 function getSettingBool($sConfName)
28 $sVal = strtolower(getSetting($sConfName));
30 return strcmp($sVal, 'yes') == 0
31 || strcmp($sVal, 'true') == 0
32 || strcmp($sVal, '1') == 0;
35 function getSettingConfig($sConfName, $sSystemConfig)
37 $sValue = $_SERVER['NOMINATIM_'.$sConfName];
40 return CONST_ConfigDir.'/'.$sSystemConfig;
46 function fail($sError, $sUserError = false)
49 $sUserError = $sError;
51 error_log('ERROR: '.$sError);
52 var_dump($sUserError);
58 function getProcessorCount()
60 $sCPU = file_get_contents('/proc/cpuinfo');
61 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
62 return count($aMatches[0]);
66 function getTotalMemoryMB()
68 $sCPU = file_get_contents('/proc/meminfo');
69 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
70 return (int)($aMatches[1]/1024);
74 function getCacheMemoryMB()
76 $sCPU = file_get_contents('/proc/meminfo');
77 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
78 return (int)($aMatches[1]/1024);
81 function getDatabaseDate(&$oDB)
83 // Find the newest node in the DB
84 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
85 // Lookup the timestamp that node was created
86 $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
87 $sLastNodeXML = file_get_contents($sLastNodeURL);
89 if ($sLastNodeXML === false) {
93 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);
95 return $aLastNodeDate[1];
99 function byImportance($a, $b)
101 if ($a['importance'] != $b['importance']) {
102 return ($a['importance'] > $b['importance']?-1:1);
105 return $a['foundorder'] <=> $b['foundorder'];
109 function javascript_renderData($xVal, $iOptions = 0)
111 $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
112 if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
113 // Unset, we call javascript_renderData again during exception handling
114 unset($_GET['json_callback']);
115 throw new Exception('Invalid json_callback value', 400);
118 $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
119 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
120 $iOptions |= JSON_PRETTY_PRINT;
123 $jsonout = json_encode($xVal, $iOptions);
126 header('Content-Type: application/javascript; charset=UTF-8');
127 echo $_GET['json_callback'].'('.$jsonout.')';
129 header('Content-Type: application/json; charset=UTF-8');
134 function addQuotes($s)
139 function parseLatLon($sQuery)
145 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)) {
147 * degrees decimal minutes
148 * N 40 26.767, W 79 58.933
149 * N 40°26.767′, W 79°58.933′
152 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
153 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
154 } 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)) {
156 * degrees decimal minutes
157 * 40 26.767 N, 79 58.933 W
158 * 40° 26.767′ N 79° 58.933′ W
161 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
162 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
163 } 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)) {
165 * degrees decimal seconds
166 * N 40 26 46 W 79 58 56
167 * N 40° 26′ 46″, W 79° 58′ 56″
170 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
171 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
172 } 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)) {
174 * degrees decimal seconds
175 * 40 26 46 N 79 58 56 W
176 * 40° 26′ 46″ N, 79° 58′ 56″ W
177 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
180 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
181 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
182 } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
185 * N 40.446° W 79.982°
188 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
189 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
190 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
193 * 40.446° N 79.982° W
196 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
197 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
198 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
206 $fQueryLat = $aData[2];
207 $fQueryLon = $aData[3];
212 return array($sFound, $fQueryLat, $fQueryLon);
215 function closestHouseNumber($aRow)
217 $fHouse = $aRow['startnumber']
218 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
220 switch ($aRow['interpolationtype']) {
222 $iHn = (int)($fHouse/2) * 2 + 1;
225 $iHn = (int)(round($fHouse/2)) * 2;
228 $iHn = (int)(round($fHouse));
232 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
235 if (!function_exists('array_key_last')) {
236 function array_key_last(array $array)
238 if (!empty($array)) return key(array_slice($array, -1, 1, true));