3 require('Symfony/Component/Dotenv/autoload.php');
5 function loadSettings($sProjectDir)
7 @define('CONST_InstallDir', $sProjectDir);
9 $dotenv = new \Symfony\Component\Dotenv\Dotenv();
10 $dotenv->load(CONST_DataDir.'/settings/env.defaults');
12 if (file_exists($sProjectDir.'/.env')) {
13 $dotenv->load($sProjectDir.'/.env');
17 function getSetting($sConfName, $sDefault = null)
19 $sValue = $_SERVER['NOMINATIM_'.$sConfName];
21 if ($sDefault !== null && !$sValue) {
28 function getSettingBool($sConfName)
30 $sVal = strtolower(getSetting($sConfName));
32 return strcmp($sVal, 'yes') == 0
33 || strcmp($sVal, 'true') == 0
34 || strcmp($sVal, '1') == 0;
37 function getSettingConfig($sConfName, $sSystemConfig)
39 $sValue = $_ENV['NOMINATIM_'.$sConfName];
42 return CONST_DataDir.'/settings/'.$sSystemConfig;
48 function fail($sError, $sUserError = false)
50 if (!$sUserError) $sUserError = $sError;
51 error_log('ERROR: '.$sError);
52 var_dump($sUserError)."\n";
57 function getProcessorCount()
59 $sCPU = file_get_contents('/proc/cpuinfo');
60 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
61 return count($aMatches[0]);
65 function getTotalMemoryMB()
67 $sCPU = file_get_contents('/proc/meminfo');
68 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
69 return (int)($aMatches[1]/1024);
73 function getCacheMemoryMB()
75 $sCPU = file_get_contents('/proc/meminfo');
76 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
77 return (int)($aMatches[1]/1024);
80 function getDatabaseDate(&$oDB)
82 // Find the newest node in the DB
83 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
84 // Lookup the timestamp that node was created
85 $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
86 $sLastNodeXML = file_get_contents($sLastNodeURL);
88 if ($sLastNodeXML === false) {
92 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);
94 return $aLastNodeDate[1];
98 function byImportance($a, $b)
100 if ($a['importance'] != $b['importance'])
101 return ($a['importance'] > $b['importance']?-1:1);
103 return $a['foundorder'] <=> $b['foundorder'];
107 function javascript_renderData($xVal, $iOptions = 0)
109 $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
110 if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
111 // Unset, we call javascript_renderData again during exception handling
112 unset($_GET['json_callback']);
113 throw new Exception('Invalid json_callback value', 400);
116 $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
117 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
118 $iOptions |= JSON_PRETTY_PRINT;
121 $jsonout = json_encode($xVal, $iOptions);
124 header('Content-Type: application/javascript; charset=UTF-8');
125 echo $_GET['json_callback'].'('.$jsonout.')';
127 header('Content-Type: application/json; charset=UTF-8');
132 function addQuotes($s)
137 function parseLatLon($sQuery)
143 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)) {
145 * degrees decimal minutes
146 * N 40 26.767, W 79 58.933
147 * N 40°26.767′, W 79°58.933′
150 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
151 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
152 } 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)) {
154 * degrees decimal minutes
155 * 40 26.767 N, 79 58.933 W
156 * 40° 26.767′ N 79° 58.933′ W
159 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
160 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
161 } 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)) {
163 * degrees decimal seconds
164 * N 40 26 46 W 79 58 56
165 * N 40° 26′ 46″, W 79° 58′ 56″
168 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
169 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
170 } 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)) {
172 * degrees decimal seconds
173 * 40 26 46 N 79 58 56 W
174 * 40° 26′ 46″ N, 79° 58′ 56″ W
175 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
178 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
179 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
180 } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
183 * N 40.446° W 79.982°
186 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
187 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
188 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
191 * 40.446° N 79.982° W
194 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
195 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
196 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
204 $fQueryLat = $aData[2];
205 $fQueryLon = $aData[3];
210 return array($sFound, $fQueryLat, $fQueryLon);
213 function createPointsAroundCenter($fLon, $fLat, $fRadius)
215 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
216 $fStepSize = (2*pi())/$iSteps;
217 $aPolyPoints = array();
218 for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
219 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
224 function closestHouseNumber($aRow)
226 $fHouse = $aRow['startnumber']
227 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
229 switch ($aRow['interpolationtype']) {
231 $iHn = (int)($fHouse/2) * 2 + 1;
234 $iHn = (int)(round($fHouse/2)) * 2;
237 $iHn = (int)(round($fHouse));
241 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
244 function getSearchRankLabel($iRank)
246 if (!isset($iRank)) return 'unknown';
247 if ($iRank < 2) return 'continent';
248 if ($iRank < 4) return 'sea';
249 if ($iRank < 8) return 'country';
250 if ($iRank < 12) return 'state';
251 if ($iRank < 16) return 'county';
252 if ($iRank == 16) return 'city';
253 if ($iRank == 17) return 'town / island';
254 if ($iRank == 18) return 'village / hamlet';
255 if ($iRank == 20) return 'suburb';
256 if ($iRank == 21) return 'postcode area';
257 if ($iRank == 22) return 'croft / farm / locality / islet';
258 if ($iRank == 23) return 'postcode area';
259 if ($iRank == 25) return 'postcode point';
260 if ($iRank == 26) return 'street / major landmark';
261 if ($iRank == 27) return 'minory street / path';
262 if ($iRank == 28) return 'house / building';
263 return 'other: ' . $iRank;