3 require('Symfony/Component/Dotenv/autoload.php');
5 function loadSettings($sProjectDir)
8 $sProjectDir = getcwd();
11 @define('CONST_InstallDir', $sProjectDir);
13 $dotenv = new \Symfony\Component\Dotenv\Dotenv();
15 if (file_exists($sProjectDir.'/.env')) {
16 $dotenv->load($sProjectDir.'/.env');
18 $dotenv->load(CONST_DataDir.'/settings/env.defaults');
21 function getSetting($sConfName, $sDefault = null)
23 $sValue = $_ENV['NOMINATIM_'.$sConfName];
25 if ($sDefault !== null && !$sValue) {
32 function getSettingBool($sConfName)
34 $sVal = strtolower(getSetting($sConfName));
36 return strcmp($sVal, 'yes') == 0
37 || strcmp($sVal, 'true') == 0
38 || strcmp($sVal, '1') == 0;
41 function getSettingConfig($sConfName, $sSystemConfig)
43 $sValue = $_ENV['NOMINATIM_'.$sConfName];
46 return CONST_DataDir.'/settings/'.$sSystemConfig;
52 function fail($sError, $sUserError = false)
54 if (!$sUserError) $sUserError = $sError;
55 error_log('ERROR: '.$sError);
56 var_dump($sUserError)."\n";
61 function getProcessorCount()
63 $sCPU = file_get_contents('/proc/cpuinfo');
64 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
65 return count($aMatches[0]);
69 function getTotalMemoryMB()
71 $sCPU = file_get_contents('/proc/meminfo');
72 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
73 return (int)($aMatches[1]/1024);
77 function getCacheMemoryMB()
79 $sCPU = file_get_contents('/proc/meminfo');
80 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
81 return (int)($aMatches[1]/1024);
84 function getDatabaseDate(&$oDB)
86 // Find the newest node in the DB
87 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
88 // Lookup the timestamp that node was created
89 $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
90 $sLastNodeXML = file_get_contents($sLastNodeURL);
92 if ($sLastNodeXML === false) {
96 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);
98 return $aLastNodeDate[1];
102 function byImportance($a, $b)
104 if ($a['importance'] != $b['importance'])
105 return ($a['importance'] > $b['importance']?-1:1);
107 return $a['foundorder'] <=> $b['foundorder'];
111 function javascript_renderData($xVal, $iOptions = 0)
113 $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
114 if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
115 // Unset, we call javascript_renderData again during exception handling
116 unset($_GET['json_callback']);
117 throw new Exception('Invalid json_callback value', 400);
120 $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
121 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
122 $iOptions |= JSON_PRETTY_PRINT;
125 $jsonout = json_encode($xVal, $iOptions);
128 header('Content-Type: application/javascript; charset=UTF-8');
129 echo $_GET['json_callback'].'('.$jsonout.')';
131 header('Content-Type: application/json; charset=UTF-8');
136 function addQuotes($s)
141 function parseLatLon($sQuery)
147 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)) {
149 * degrees decimal minutes
150 * N 40 26.767, W 79 58.933
151 * N 40°26.767′, W 79°58.933′
154 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
155 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
156 } 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)) {
158 * degrees decimal minutes
159 * 40 26.767 N, 79 58.933 W
160 * 40° 26.767′ N 79° 58.933′ W
163 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
164 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
165 } 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)) {
167 * degrees decimal seconds
168 * N 40 26 46 W 79 58 56
169 * N 40° 26′ 46″, W 79° 58′ 56″
172 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
173 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
174 } 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)) {
176 * degrees decimal seconds
177 * 40 26 46 N 79 58 56 W
178 * 40° 26′ 46″ N, 79° 58′ 56″ W
179 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
182 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
183 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
184 } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
187 * N 40.446° W 79.982°
190 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
191 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
192 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
195 * 40.446° N 79.982° W
198 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
199 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
200 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
208 $fQueryLat = $aData[2];
209 $fQueryLon = $aData[3];
214 return array($sFound, $fQueryLat, $fQueryLon);
217 function createPointsAroundCenter($fLon, $fLat, $fRadius)
219 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
220 $fStepSize = (2*pi())/$iSteps;
221 $aPolyPoints = array();
222 for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
223 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
228 function closestHouseNumber($aRow)
230 $fHouse = $aRow['startnumber']
231 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
233 switch ($aRow['interpolationtype']) {
235 $iHn = (int)($fHouse/2) * 2 + 1;
238 $iHn = (int)(round($fHouse/2)) * 2;
241 $iHn = (int)(round($fHouse));
245 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
248 function getSearchRankLabel($iRank)
250 if (!isset($iRank)) return 'unknown';
251 if ($iRank < 2) return 'continent';
252 if ($iRank < 4) return 'sea';
253 if ($iRank < 8) return 'country';
254 if ($iRank < 12) return 'state';
255 if ($iRank < 16) return 'county';
256 if ($iRank == 16) return 'city';
257 if ($iRank == 17) return 'town / island';
258 if ($iRank == 18) return 'village / hamlet';
259 if ($iRank == 20) return 'suburb';
260 if ($iRank == 21) return 'postcode area';
261 if ($iRank == 22) return 'croft / farm / locality / islet';
262 if ($iRank == 23) return 'postcode area';
263 if ($iRank == 25) return 'postcode point';
264 if ($iRank == 26) return 'street / major landmark';
265 if ($iRank == 27) return 'minory street / path';
266 if ($iRank == 28) return 'house / building';
267 return 'other: ' . $iRank;