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