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 fwriteConstDef($rFile, $sConstName, $value)
141 if (is_bool($value)) {
142 $sEscapedValue = $value ? 'true' : 'false';
143 } elseif (is_numeric($value)) {
144 $sEscapedValue = strval($value);
146 $sEscapedValue = 'false';
148 $sEscapedValue = addQuotes(str_replace("'", "\\'", (string)$value));
151 fwrite($rFile, "@define('CONST_$sConstName', $sEscapedValue);\n");
155 function parseLatLon($sQuery)
161 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)) {
163 * degrees decimal minutes
164 * N 40 26.767, W 79 58.933
165 * N 40°26.767′, W 79°58.933′
168 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
169 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
170 } 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)) {
172 * degrees decimal minutes
173 * 40 26.767 N, 79 58.933 W
174 * 40° 26.767′ N 79° 58.933′ W
177 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
178 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
179 } 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)) {
181 * degrees decimal seconds
182 * N 40 26 46 W 79 58 56
183 * N 40° 26′ 46″, W 79° 58′ 56″
186 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
187 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
188 } 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)) {
190 * degrees decimal seconds
191 * 40 26 46 N 79 58 56 W
192 * 40° 26′ 46″ N, 79° 58′ 56″ W
193 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
196 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
197 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
198 } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
201 * N 40.446° W 79.982°
204 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
205 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
206 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
209 * 40.446° N 79.982° W
212 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
213 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
214 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
222 $fQueryLat = $aData[2];
223 $fQueryLon = $aData[3];
228 return array($sFound, $fQueryLat, $fQueryLon);
231 function createPointsAroundCenter($fLon, $fLat, $fRadius)
233 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
234 $fStepSize = (2*pi())/$iSteps;
235 $aPolyPoints = array();
236 for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
237 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
242 function closestHouseNumber($aRow)
244 $fHouse = $aRow['startnumber']
245 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
247 switch ($aRow['interpolationtype']) {
249 $iHn = (int)($fHouse/2) * 2 + 1;
252 $iHn = (int)(round($fHouse/2)) * 2;
255 $iHn = (int)(round($fHouse));
259 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
262 function getSearchRankLabel($iRank)
264 if (!isset($iRank)) return 'unknown';
265 if ($iRank < 2) return 'continent';
266 if ($iRank < 4) return 'sea';
267 if ($iRank < 8) return 'country';
268 if ($iRank < 12) return 'state';
269 if ($iRank < 16) return 'county';
270 if ($iRank == 16) return 'city';
271 if ($iRank == 17) return 'town / island';
272 if ($iRank == 18) return 'village / hamlet';
273 if ($iRank == 20) return 'suburb';
274 if ($iRank == 21) return 'postcode area';
275 if ($iRank == 22) return 'croft / farm / locality / islet';
276 if ($iRank == 23) return 'postcode area';
277 if ($iRank == 25) return 'postcode point';
278 if ($iRank == 26) return 'street / major landmark';
279 if ($iRank == 27) return 'minory street / path';
280 if ($iRank == 28) return 'house / building';
281 return 'other: ' . $iRank;