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)
48 if (!$sUserError) $sUserError = $sError;
49 error_log('ERROR: '.$sError);
50 var_dump($sUserError);
56 function getProcessorCount()
58 $sCPU = file_get_contents('/proc/cpuinfo');
59 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
60 return count($aMatches[0]);
64 function getTotalMemoryMB()
66 $sCPU = file_get_contents('/proc/meminfo');
67 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
68 return (int)($aMatches[1]/1024);
72 function getCacheMemoryMB()
74 $sCPU = file_get_contents('/proc/meminfo');
75 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
76 return (int)($aMatches[1]/1024);
79 function getDatabaseDate(&$oDB)
81 // Find the newest node in the DB
82 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
83 // Lookup the timestamp that node was created
84 $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
85 $sLastNodeXML = file_get_contents($sLastNodeURL);
87 if ($sLastNodeXML === false) {
91 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);
93 return $aLastNodeDate[1];
97 function byImportance($a, $b)
99 if ($a['importance'] != $b['importance'])
100 return ($a['importance'] > $b['importance']?-1:1);
102 return $a['foundorder'] <=> $b['foundorder'];
106 function javascript_renderData($xVal, $iOptions = 0)
108 $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
109 if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
110 // Unset, we call javascript_renderData again during exception handling
111 unset($_GET['json_callback']);
112 throw new Exception('Invalid json_callback value', 400);
115 $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
116 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
117 $iOptions |= JSON_PRETTY_PRINT;
120 $jsonout = json_encode($xVal, $iOptions);
123 header('Content-Type: application/javascript; charset=UTF-8');
124 echo $_GET['json_callback'].'('.$jsonout.')';
126 header('Content-Type: application/json; charset=UTF-8');
131 function addQuotes($s)
136 function parseLatLon($sQuery)
142 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)) {
144 * degrees decimal minutes
145 * N 40 26.767, W 79 58.933
146 * N 40°26.767′, W 79°58.933′
149 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
150 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
151 } 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)) {
153 * degrees decimal minutes
154 * 40 26.767 N, 79 58.933 W
155 * 40° 26.767′ N 79° 58.933′ W
158 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
159 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
160 } 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)) {
162 * degrees decimal seconds
163 * N 40 26 46 W 79 58 56
164 * N 40° 26′ 46″, W 79° 58′ 56″
167 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
168 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
169 } 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)) {
171 * degrees decimal seconds
172 * 40 26 46 N 79 58 56 W
173 * 40° 26′ 46″ N, 79° 58′ 56″ W
174 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
177 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
178 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
179 } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
182 * N 40.446° W 79.982°
185 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
186 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
187 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
190 * 40.446° N 79.982° W
193 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
194 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
195 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
203 $fQueryLat = $aData[2];
204 $fQueryLon = $aData[3];
209 return array($sFound, $fQueryLat, $fQueryLon);
212 function closestHouseNumber($aRow)
214 $fHouse = $aRow['startnumber']
215 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
217 switch ($aRow['interpolationtype']) {
219 $iHn = (int)($fHouse/2) * 2 + 1;
222 $iHn = (int)(round($fHouse/2)) * 2;
225 $iHn = (int)(round($fHouse));
229 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
232 if (!function_exists('array_key_last')) {
233 function array_key_last(array $array)
235 if (!empty($array)) return key(array_slice($array, -1, 1, true));