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 fail($sError, $sUserError = false)
23 if (!$sUserError) $sUserError = $sError;
24 error_log('ERROR: '.$sError);
25 var_dump($sUserError)."\n";
30 function getProcessorCount()
32 $sCPU = file_get_contents('/proc/cpuinfo');
33 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
34 return count($aMatches[0]);
38 function getTotalMemoryMB()
40 $sCPU = file_get_contents('/proc/meminfo');
41 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
42 return (int)($aMatches[1]/1024);
46 function getCacheMemoryMB()
48 $sCPU = file_get_contents('/proc/meminfo');
49 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
50 return (int)($aMatches[1]/1024);
53 function getDatabaseDate(&$oDB)
55 // Find the newest node in the DB
56 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
57 // Lookup the timestamp that node was created
58 $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
59 $sLastNodeXML = file_get_contents($sLastNodeURL);
61 if ($sLastNodeXML === false) {
65 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);
67 return $aLastNodeDate[1];
71 function byImportance($a, $b)
73 if ($a['importance'] != $b['importance'])
74 return ($a['importance'] > $b['importance']?-1:1);
76 return $a['foundorder'] <=> $b['foundorder'];
80 function javascript_renderData($xVal, $iOptions = 0)
82 $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
83 if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
84 // Unset, we call javascript_renderData again during exception handling
85 unset($_GET['json_callback']);
86 throw new Exception('Invalid json_callback value', 400);
89 $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
90 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
91 $iOptions |= JSON_PRETTY_PRINT;
94 $jsonout = json_encode($xVal, $iOptions);
97 header('Content-Type: application/javascript; charset=UTF-8');
98 echo $_GET['json_callback'].'('.$jsonout.')';
100 header('Content-Type: application/json; charset=UTF-8');
105 function addQuotes($s)
110 function parseLatLon($sQuery)
116 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)) {
118 * degrees decimal minutes
119 * N 40 26.767, W 79 58.933
120 * N 40°26.767′, W 79°58.933′
123 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
124 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
125 } 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)) {
127 * degrees decimal minutes
128 * 40 26.767 N, 79 58.933 W
129 * 40° 26.767′ N 79° 58.933′ W
132 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
133 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
134 } 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)) {
136 * degrees decimal seconds
137 * N 40 26 46 W 79 58 56
138 * N 40° 26′ 46″, W 79° 58′ 56″
141 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
142 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
143 } 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)) {
145 * degrees decimal seconds
146 * 40 26 46 N 79 58 56 W
147 * 40° 26′ 46″ N, 79° 58′ 56″ W
148 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
151 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
152 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
153 } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
156 * N 40.446° W 79.982°
159 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
160 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
161 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
164 * 40.446° N 79.982° W
167 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
168 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
169 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
177 $fQueryLat = $aData[2];
178 $fQueryLon = $aData[3];
183 return array($sFound, $fQueryLat, $fQueryLon);
186 function createPointsAroundCenter($fLon, $fLat, $fRadius)
188 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
189 $fStepSize = (2*pi())/$iSteps;
190 $aPolyPoints = array();
191 for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
192 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
197 function closestHouseNumber($aRow)
199 $fHouse = $aRow['startnumber']
200 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
202 switch ($aRow['interpolationtype']) {
204 $iHn = (int)($fHouse/2) * 2 + 1;
207 $iHn = (int)(round($fHouse/2)) * 2;
210 $iHn = (int)(round($fHouse));
214 return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
217 function getSearchRankLabel($iRank)
219 if (!isset($iRank)) return 'unknown';
220 if ($iRank < 2) return 'continent';
221 if ($iRank < 4) return 'sea';
222 if ($iRank < 8) return 'country';
223 if ($iRank < 12) return 'state';
224 if ($iRank < 16) return 'county';
225 if ($iRank == 16) return 'city';
226 if ($iRank == 17) return 'town / island';
227 if ($iRank == 18) return 'village / hamlet';
228 if ($iRank == 20) return 'suburb';
229 if ($iRank == 21) return 'postcode area';
230 if ($iRank == 22) return 'croft / farm / locality / islet';
231 if ($iRank == 23) return 'postcode area';
232 if ($iRank == 25) return 'postcode point';
233 if ($iRank == 26) return 'street / major landmark';
234 if ($iRank == 27) return 'minory street / path';
235 if ($iRank == 28) return 'house / building';
236 return 'other: ' . $iRank;