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']);
13 function getSetting($sConfName, $sDefault = null)
15 $sValue = $_SERVER['NOMINATIM_'.$sConfName];
17 if ($sDefault !== null && !$sValue) {
24 function getSettingBool($sConfName)
26 $sVal = strtolower(getSetting($sConfName));
28 return strcmp($sVal, 'yes') == 0
29 || strcmp($sVal, 'true') == 0
30 || strcmp($sVal, '1') == 0;
33 function getSettingConfig($sConfName, $sSystemConfig)
35 $sValue = $_SERVER['NOMINATIM_'.$sConfName];
38 return CONST_DataDir.'/settings/'.$sSystemConfig;
44 function fail($sError, $sUserError = false)
46 if (!$sUserError) $sUserError = $sError;
47 error_log('ERROR: '.$sError);
48 var_dump($sUserError)."\n";
53 function getProcessorCount()
55 $sCPU = file_get_contents('/proc/cpuinfo');
56 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
57 return count($aMatches[0]);
61 function getTotalMemoryMB()
63 $sCPU = file_get_contents('/proc/meminfo');
64 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
65 return (int)($aMatches[1]/1024);
69 function getCacheMemoryMB()
71 $sCPU = file_get_contents('/proc/meminfo');
72 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
73 return (int)($aMatches[1]/1024);
76 function getDatabaseDate(&$oDB)
78 // Find the newest node in the DB
79 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
80 // Lookup the timestamp that node was created
81 $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
82 $sLastNodeXML = file_get_contents($sLastNodeURL);
84 if ($sLastNodeXML === false) {
88 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);
90 return $aLastNodeDate[1];
94 function byImportance($a, $b)
96 if ($a['importance'] != $b['importance'])
97 return ($a['importance'] > $b['importance']?-1:1);
99 return $a['foundorder'] <=> $b['foundorder'];
103 function javascript_renderData($xVal, $iOptions = 0)
105 $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
106 if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
107 // Unset, we call javascript_renderData again during exception handling
108 unset($_GET['json_callback']);
109 throw new Exception('Invalid json_callback value', 400);
112 $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
113 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
114 $iOptions |= JSON_PRETTY_PRINT;
117 $jsonout = json_encode($xVal, $iOptions);
120 header('Content-Type: application/javascript; charset=UTF-8');
121 echo $_GET['json_callback'].'('.$jsonout.')';
123 header('Content-Type: application/json; charset=UTF-8');
128 function addQuotes($s)
133 function fwriteConstDef($rFile, $sConstName, $value)
137 if (is_bool($value)) {
138 $sEscapedValue = $value ? 'true' : 'false';
139 } elseif (is_numeric($value)) {
140 $sEscapedValue = strval($value);
142 $sEscapedValue = 'false';
144 $sEscapedValue = addQuotes(str_replace("'", "\\'", (string)$value));
147 fwrite($rFile, "@define('CONST_$sConstName', $sEscapedValue);\n");
151 function parseLatLon($sQuery)
157 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)) {
159 * degrees decimal minutes
160 * N 40 26.767, W 79 58.933
161 * N 40°26.767′, W 79°58.933′
164 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
165 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
166 } 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)) {
168 * degrees decimal minutes
169 * 40 26.767 N, 79 58.933 W
170 * 40° 26.767′ N 79° 58.933′ W
173 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
174 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
175 } 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)) {
177 * degrees decimal seconds
178 * N 40 26 46 W 79 58 56
179 * N 40° 26′ 46″, W 79° 58′ 56″
182 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
183 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
184 } 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)) {
186 * degrees decimal seconds
187 * 40 26 46 N 79 58 56 W
188 * 40° 26′ 46″ N, 79° 58′ 56″ W
189 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
192 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
193 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
194 } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
197 * N 40.446° W 79.982°
200 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
201 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
202 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
205 * 40.446° N 79.982° W
208 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
209 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
210 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
218 $fQueryLat = $aData[2];
219 $fQueryLon = $aData[3];
224 return array($sFound, $fQueryLat, $fQueryLon);
227 function createPointsAroundCenter($fLon, $fLat, $fRadius)
229 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
230 $fStepSize = (2*pi())/$iSteps;
231 $aPolyPoints = array();
232 for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
233 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
238 function closestHouseNumber($aRow)
240 $fHouse = $aRow['startnumber']
241 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
243 switch ($aRow['interpolationtype']) {
245 $iHn = (int)($fHouse/2) * 2 + 1;
248 $iHn = (int)(round($fHouse/2)) * 2;
251 $iHn = (int)(round($fHouse));
255 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
258 function getSearchRankLabel($iRank)
260 if (!isset($iRank)) return 'unknown';
261 if ($iRank < 2) return 'continent';
262 if ($iRank < 4) return 'sea';
263 if ($iRank < 8) return 'country';
264 if ($iRank < 12) return 'state';
265 if ($iRank < 16) return 'county';
266 if ($iRank == 16) return 'city';
267 if ($iRank == 17) return 'town / island';
268 if ($iRank == 18) return 'village / hamlet';
269 if ($iRank == 20) return 'suburb';
270 if ($iRank == 21) return 'postcode area';
271 if ($iRank == 22) return 'croft / farm / locality / islet';
272 if ($iRank == 23) return 'postcode area';
273 if ($iRank == 25) return 'postcode point';
274 if ($iRank == 26) return 'street / major landmark';
275 if ($iRank == 27) return 'minory street / path';
276 if ($iRank == 28) return 'house / building';
277 return 'other: ' . $iRank;