]> git.openstreetmap.org Git - nominatim.git/blob - lib/lib.php
replace database settings with dotenv variant
[nominatim.git] / lib / lib.php
1 <?php
2
3 require('Symfony/Component/Dotenv/autoload.php');
4
5 function loadSettings($sProjectDir)
6 {
7     if (!$sProjectDir) {
8         $sProjectDir = getcwd();
9     }
10
11     @define('CONST_InstallDir', $sProjectDir);
12
13     $dotenv = new \Symfony\Component\Dotenv\Dotenv();
14
15     if (file_exists($sProjectDir.'/.env')) {
16         $dotenv->load($sProjectDir.'/.env');
17     }
18     $dotenv->load(CONST_DataDir.'/settings/env.defaults');
19 }
20
21 function getSetting($sConfName)
22 {
23     return $_ENV['NOMINATIM_'.$sConfName];
24 }
25
26 function fail($sError, $sUserError = false)
27 {
28     if (!$sUserError) $sUserError = $sError;
29     error_log('ERROR: '.$sError);
30     var_dump($sUserError)."\n";
31     exit(-1);
32 }
33
34
35 function getProcessorCount()
36 {
37     $sCPU = file_get_contents('/proc/cpuinfo');
38     preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
39     return count($aMatches[0]);
40 }
41
42
43 function getTotalMemoryMB()
44 {
45     $sCPU = file_get_contents('/proc/meminfo');
46     preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
47     return (int)($aMatches[1]/1024);
48 }
49
50
51 function getCacheMemoryMB()
52 {
53     $sCPU = file_get_contents('/proc/meminfo');
54     preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
55     return (int)($aMatches[1]/1024);
56 }
57
58 function getDatabaseDate(&$oDB)
59 {
60     // Find the newest node in the DB
61     $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
62     // Lookup the timestamp that node was created
63     $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
64     $sLastNodeXML = file_get_contents($sLastNodeURL);
65
66     if ($sLastNodeXML === false) {
67         return false;
68     }
69
70     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);
71
72     return $aLastNodeDate[1];
73 }
74
75
76 function byImportance($a, $b)
77 {
78     if ($a['importance'] != $b['importance'])
79         return ($a['importance'] > $b['importance']?-1:1);
80
81     return $a['foundorder'] <=> $b['foundorder'];
82 }
83
84
85 function javascript_renderData($xVal, $iOptions = 0)
86 {
87     $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
88     if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
89         // Unset, we call javascript_renderData again during exception handling
90         unset($_GET['json_callback']);
91         throw new Exception('Invalid json_callback value', 400);
92     }
93
94     $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
95     if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
96         $iOptions |= JSON_PRETTY_PRINT;
97     }
98
99     $jsonout = json_encode($xVal, $iOptions);
100
101     if ($sCallback) {
102         header('Content-Type: application/javascript; charset=UTF-8');
103         echo $_GET['json_callback'].'('.$jsonout.')';
104     } else {
105         header('Content-Type: application/json; charset=UTF-8');
106         echo $jsonout;
107     }
108 }
109
110 function addQuotes($s)
111 {
112     return "'".$s."'";
113 }
114
115 function parseLatLon($sQuery)
116 {
117     $sFound    = null;
118     $fQueryLat = null;
119     $fQueryLon = null;
120
121     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)) {
122         /*               1          2                    3                     4          5             6
123          * degrees decimal minutes
124          * N 40 26.767, W 79 58.933
125          * N 40°26.767′, W 79°58.933′
126          */
127         $sFound    = $aData[0];
128         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
129         $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
130     } 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)) {
131         /*                     1             2                          3           4             5                       6
132          * degrees decimal minutes
133          * 40 26.767 N, 79 58.933 W
134          * 40° 26.767′ N 79° 58.933′ W
135          */
136         $sFound    = $aData[0];
137         $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
138         $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
139     } 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)) {
140         /*                     1          2             3               4                  5          6             7               8
141          * degrees decimal seconds
142          * N 40 26 46 W 79 58 56
143          * N 40° 26′ 46″, W 79° 58′ 56″
144          */
145         $sFound    = $aData[0];
146         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
147         $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
148     } 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)) {
149         /*                     1             2               3                     4           5             6               7                     8
150          * degrees decimal seconds
151          * 40 26 46 N 79 58 56 W
152          * 40° 26′ 46″ N, 79° 58′ 56″ W
153          * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
154          */
155         $sFound    = $aData[0];
156         $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
157         $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
158     } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
159         /*                     1          2                                3          4
160          * degrees decimal
161          * N 40.446° W 79.982°
162          */
163         $sFound    = $aData[0];
164         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
165         $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
166     } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
167         /*                     1                            2           3                            4
168          * degrees decimal
169          * 40.446° N 79.982° W
170          */
171         $sFound    = $aData[0];
172         $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
173         $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
174     } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
175         /*                 1                   2                              3                        4
176          * degrees decimal
177          * 12.34, 56.78
178          * 12.34 56.78
179          * [12.456,-78.90]
180          */
181         $sFound    = $aData[0];
182         $fQueryLat = $aData[2];
183         $fQueryLon = $aData[3];
184     } else {
185         return false;
186     }
187
188     return array($sFound, $fQueryLat, $fQueryLon);
189 }
190
191 function createPointsAroundCenter($fLon, $fLat, $fRadius)
192 {
193     $iSteps = max(8, min(100, ($fRadius * 40000)^2));
194     $fStepSize = (2*pi())/$iSteps;
195     $aPolyPoints = array();
196     for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
197         $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
198     }
199     return $aPolyPoints;
200 }
201
202 function closestHouseNumber($aRow)
203 {
204     $fHouse = $aRow['startnumber']
205                 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
206
207     switch ($aRow['interpolationtype']) {
208         case 'odd':
209             $iHn = (int)($fHouse/2) * 2 + 1;
210             break;
211         case 'even':
212             $iHn = (int)(round($fHouse/2)) * 2;
213             break;
214         default:
215             $iHn = (int)(round($fHouse));
216             break;
217     }
218
219     return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
220 }
221
222 function getSearchRankLabel($iRank)
223 {
224     if (!isset($iRank)) return 'unknown';
225     if ($iRank < 2) return 'continent';
226     if ($iRank < 4) return 'sea';
227     if ($iRank < 8) return 'country';
228     if ($iRank < 12) return 'state';
229     if ($iRank < 16) return 'county';
230     if ($iRank == 16) return 'city';
231     if ($iRank == 17) return 'town / island';
232     if ($iRank == 18) return 'village / hamlet';
233     if ($iRank == 20) return 'suburb';
234     if ($iRank == 21) return 'postcode area';
235     if ($iRank == 22) return 'croft / farm / locality / islet';
236     if ($iRank == 23) return 'postcode area';
237     if ($iRank == 25) return 'postcode point';
238     if ($iRank == 26) return 'street / major landmark';
239     if ($iRank == 27) return 'minory street / path';
240     if ($iRank == 28) return 'house / building';
241     return 'other: ' . $iRank;
242 }