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_BinDir') or define('CONST_BinDir', $_SERVER['NOMINATIM_BINDIR']);
11 defined('CONST_Default_ModulePath') or define('CONST_Default_ModulePath', $_SERVER['NOMINATIM_DATABASE_MODULE_SRC_PATH']);
14 function getSetting($sConfName, $sDefault = null)
16 $sValue = $_SERVER['NOMINATIM_'.$sConfName];
18 if ($sDefault !== null && !$sValue) {
25 function getSettingBool($sConfName)
27 $sVal = strtolower(getSetting($sConfName));
29 return strcmp($sVal, 'yes') == 0
30 || strcmp($sVal, 'true') == 0
31 || strcmp($sVal, '1') == 0;
34 function getSettingConfig($sConfName, $sSystemConfig)
36 $sValue = $_SERVER['NOMINATIM_'.$sConfName];
39 return CONST_DataDir.'/settings/'.$sSystemConfig;
45 function fail($sError, $sUserError = false)
47 if (!$sUserError) $sUserError = $sError;
48 error_log('ERROR: '.$sError);
49 var_dump($sUserError)."\n";
54 function getProcessorCount()
56 $sCPU = file_get_contents('/proc/cpuinfo');
57 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
58 return count($aMatches[0]);
62 function getTotalMemoryMB()
64 $sCPU = file_get_contents('/proc/meminfo');
65 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
66 return (int)($aMatches[1]/1024);
70 function getCacheMemoryMB()
72 $sCPU = file_get_contents('/proc/meminfo');
73 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
74 return (int)($aMatches[1]/1024);
77 function getDatabaseDate(&$oDB)
79 // Find the newest node in the DB
80 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
81 // Lookup the timestamp that node was created
82 $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
83 $sLastNodeXML = file_get_contents($sLastNodeURL);
85 if ($sLastNodeXML === false) {
89 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);
91 return $aLastNodeDate[1];
95 function byImportance($a, $b)
97 if ($a['importance'] != $b['importance'])
98 return ($a['importance'] > $b['importance']?-1:1);
100 return $a['foundorder'] <=> $b['foundorder'];
104 function javascript_renderData($xVal, $iOptions = 0)
106 $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
107 if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
108 // Unset, we call javascript_renderData again during exception handling
109 unset($_GET['json_callback']);
110 throw new Exception('Invalid json_callback value', 400);
113 $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
114 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
115 $iOptions |= JSON_PRETTY_PRINT;
118 $jsonout = json_encode($xVal, $iOptions);
121 header('Content-Type: application/javascript; charset=UTF-8');
122 echo $_GET['json_callback'].'('.$jsonout.')';
124 header('Content-Type: application/json; charset=UTF-8');
129 function addQuotes($s)
134 function fwriteConstDef($rFile, $sConstName, $value)
138 if (is_bool($value)) {
139 $sEscapedValue = $value ? 'true' : 'false';
140 } elseif (is_numeric($value)) {
141 $sEscapedValue = strval($value);
143 $sEscapedValue = 'false';
145 $sEscapedValue = addQuotes(str_replace("'", "\\'", (string)$value));
148 fwrite($rFile, "@define('CONST_$sConstName', $sEscapedValue);\n");
152 function parseLatLon($sQuery)
158 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)) {
160 * degrees decimal minutes
161 * N 40 26.767, W 79 58.933
162 * N 40°26.767′, W 79°58.933′
165 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
166 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
167 } 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)) {
169 * degrees decimal minutes
170 * 40 26.767 N, 79 58.933 W
171 * 40° 26.767′ N 79° 58.933′ W
174 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
175 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
176 } 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)) {
178 * degrees decimal seconds
179 * N 40 26 46 W 79 58 56
180 * N 40° 26′ 46″, W 79° 58′ 56″
183 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
184 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
185 } 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)) {
187 * degrees decimal seconds
188 * 40 26 46 N 79 58 56 W
189 * 40° 26′ 46″ N, 79° 58′ 56″ W
190 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
193 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
194 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
195 } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
198 * N 40.446° W 79.982°
201 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
202 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
203 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
206 * 40.446° N 79.982° W
209 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
210 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
211 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
219 $fQueryLat = $aData[2];
220 $fQueryLon = $aData[3];
225 return array($sFound, $fQueryLat, $fQueryLon);
228 function createPointsAroundCenter($fLon, $fLat, $fRadius)
230 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
231 $fStepSize = (2*pi())/$iSteps;
232 $aPolyPoints = array();
233 for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
234 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
239 function closestHouseNumber($aRow)
241 $fHouse = $aRow['startnumber']
242 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
244 switch ($aRow['interpolationtype']) {
246 $iHn = (int)($fHouse/2) * 2 + 1;
249 $iHn = (int)(round($fHouse/2)) * 2;
252 $iHn = (int)(round($fHouse));
256 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
259 function getSearchRankLabel($iRank)
261 if (!isset($iRank)) return 'unknown';
262 if ($iRank < 2) return 'continent';
263 if ($iRank < 4) return 'sea';
264 if ($iRank < 8) return 'country';
265 if ($iRank < 12) return 'state';
266 if ($iRank < 16) return 'county';
267 if ($iRank == 16) return 'city';
268 if ($iRank == 17) return 'town / island';
269 if ($iRank == 18) return 'village / hamlet';
270 if ($iRank == 20) return 'suburb';
271 if ($iRank == 21) return 'postcode area';
272 if ($iRank == 22) return 'croft / farm / locality / islet';
273 if ($iRank == 23) return 'postcode area';
274 if ($iRank == 25) return 'postcode point';
275 if ($iRank == 26) return 'street / major landmark';
276 if ($iRank == 27) return 'minory street / path';
277 if ($iRank == 28) return 'house / building';
278 return 'other: ' . $iRank;