3 require('Symfony/Component/Dotenv/autoload.php');
5 function loadSettings($sProjectDir)
7 @define('CONST_InstallDir', $sProjectDir);
8 // Temporary hack to set the direcory via environment instead of
9 // the installed scripts. Neither setting is part of the official
11 defined('CONST_DataDir') or define('CONST_DataDir', $_SERVER['NOMINATIM_DATADIR']);
12 defined('CONST_BinDir') or define('CONST_BinDir', $_SERVER['NOMINATIM_BINDIR']);
14 $dotenv = new \Symfony\Component\Dotenv\Dotenv();
15 $dotenv->load(CONST_DataDir.'/settings/env.defaults');
17 if (file_exists($sProjectDir.'/.env')) {
18 $dotenv->load($sProjectDir.'/.env');
22 function getSetting($sConfName, $sDefault = null)
24 $sValue = $_SERVER['NOMINATIM_'.$sConfName];
26 if ($sDefault !== null && !$sValue) {
33 function getSettingBool($sConfName)
35 $sVal = strtolower(getSetting($sConfName));
37 return strcmp($sVal, 'yes') == 0
38 || strcmp($sVal, 'true') == 0
39 || strcmp($sVal, '1') == 0;
42 function getSettingConfig($sConfName, $sSystemConfig)
44 $sValue = $_ENV['NOMINATIM_'.$sConfName];
47 return CONST_DataDir.'/settings/'.$sSystemConfig;
53 function fail($sError, $sUserError = false)
55 if (!$sUserError) $sUserError = $sError;
56 error_log('ERROR: '.$sError);
57 var_dump($sUserError)."\n";
62 function getProcessorCount()
64 $sCPU = file_get_contents('/proc/cpuinfo');
65 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
66 return count($aMatches[0]);
70 function getTotalMemoryMB()
72 $sCPU = file_get_contents('/proc/meminfo');
73 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
74 return (int)($aMatches[1]/1024);
78 function getCacheMemoryMB()
80 $sCPU = file_get_contents('/proc/meminfo');
81 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
82 return (int)($aMatches[1]/1024);
85 function getDatabaseDate(&$oDB)
87 // Find the newest node in the DB
88 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
89 // Lookup the timestamp that node was created
90 $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
91 $sLastNodeXML = file_get_contents($sLastNodeURL);
93 if ($sLastNodeXML === false) {
97 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);
99 return $aLastNodeDate[1];
103 function byImportance($a, $b)
105 if ($a['importance'] != $b['importance'])
106 return ($a['importance'] > $b['importance']?-1:1);
108 return $a['foundorder'] <=> $b['foundorder'];
112 function javascript_renderData($xVal, $iOptions = 0)
114 $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
115 if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
116 // Unset, we call javascript_renderData again during exception handling
117 unset($_GET['json_callback']);
118 throw new Exception('Invalid json_callback value', 400);
121 $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
122 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
123 $iOptions |= JSON_PRETTY_PRINT;
126 $jsonout = json_encode($xVal, $iOptions);
129 header('Content-Type: application/javascript; charset=UTF-8');
130 echo $_GET['json_callback'].'('.$jsonout.')';
132 header('Content-Type: application/json; charset=UTF-8');
137 function addQuotes($s)
142 function fwriteConstDef($rFile, $sConstName, $value)
146 if (is_bool($value)) {
147 $sEscapedValue = $value ? 'true' : 'false';
148 } elseif (is_numeric($value)) {
149 $sEscapedValue = strval($value);
151 $sEscapedValue = 'false';
153 $sEscapedValue = addQuotes(str_replace("'", "\\'", (string)$value));
156 fwrite($rFile, "@define('CONST_$sConstName', $sEscapedValue);\n");
160 function parseLatLon($sQuery)
166 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)) {
168 * degrees decimal minutes
169 * N 40 26.767, W 79 58.933
170 * N 40°26.767′, W 79°58.933′
173 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
174 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
175 } 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)) {
177 * degrees decimal minutes
178 * 40 26.767 N, 79 58.933 W
179 * 40° 26.767′ N 79° 58.933′ W
182 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
183 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
184 } 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)) {
186 * degrees decimal seconds
187 * N 40 26 46 W 79 58 56
188 * N 40° 26′ 46″, W 79° 58′ 56″
191 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
192 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
193 } 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)) {
195 * degrees decimal seconds
196 * 40 26 46 N 79 58 56 W
197 * 40° 26′ 46″ N, 79° 58′ 56″ W
198 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
201 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
202 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
203 } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
206 * N 40.446° W 79.982°
209 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
210 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
211 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
214 * 40.446° N 79.982° W
217 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
218 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
219 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
227 $fQueryLat = $aData[2];
228 $fQueryLon = $aData[3];
233 return array($sFound, $fQueryLat, $fQueryLon);
236 function createPointsAroundCenter($fLon, $fLat, $fRadius)
238 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
239 $fStepSize = (2*pi())/$iSteps;
240 $aPolyPoints = array();
241 for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
242 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
247 function closestHouseNumber($aRow)
249 $fHouse = $aRow['startnumber']
250 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
252 switch ($aRow['interpolationtype']) {
254 $iHn = (int)($fHouse/2) * 2 + 1;
257 $iHn = (int)(round($fHouse/2)) * 2;
260 $iHn = (int)(round($fHouse));
264 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
267 function getSearchRankLabel($iRank)
269 if (!isset($iRank)) return 'unknown';
270 if ($iRank < 2) return 'continent';
271 if ($iRank < 4) return 'sea';
272 if ($iRank < 8) return 'country';
273 if ($iRank < 12) return 'state';
274 if ($iRank < 16) return 'county';
275 if ($iRank == 16) return 'city';
276 if ($iRank == 17) return 'town / island';
277 if ($iRank == 18) return 'village / hamlet';
278 if ($iRank == 20) return 'suburb';
279 if ($iRank == 21) return 'postcode area';
280 if ($iRank == 22) return 'croft / farm / locality / islet';
281 if ($iRank == 23) return 'postcode area';
282 if ($iRank == 25) return 'postcode point';
283 if ($iRank == 26) return 'street / major landmark';
284 if ($iRank == 27) return 'minory street / path';
285 if ($iRank == 28) return 'house / building';
286 return 'other: ' . $iRank;