]> git.openstreetmap.org Git - nominatim.git/blob - lib/lib.php
simplify constructor of SetupFunctions
[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     echo $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']?-1:1);
59 }
60
61
62 function javascript_renderData($xVal, $iOptions = 0)
63 {
64     $iOptions |= JSON_UNESCAPED_UNICODE;
65     if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
66         $iOptions |= JSON_PRETTY_PRINT;
67     }
68
69     $jsonout = json_encode($xVal, $iOptions);
70
71     if (!isset($_GET['json_callback'])) {
72         header('Content-Type: application/json; charset=UTF-8');
73         echo $jsonout;
74     } else {
75         if (preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $_GET['json_callback'])) {
76             header('Content-Type: application/javascript; charset=UTF-8');
77             echo $_GET['json_callback'].'('.$jsonout.')';
78         } else {
79             header('HTTP/1.0 400 Bad Request');
80         }
81     }
82 }
83
84 function addQuotes($s)
85 {
86     return "'".$s."'";
87 }
88
89 function parseLatLon($sQuery)
90 {
91     $sFound    = null;
92     $fQueryLat = null;
93     $fQueryLon = null;
94
95     if (preg_match('/\\s*([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*\\s*/', $sQuery, $aData)) {
96         /*               1         2                   3                    4         5            6
97          * degrees decimal minutes
98          * N 40 26.767, W 79 58.933
99          * N 40°26.767′, W 79°58.933′
100          */
101         $sFound    = $aData[0];
102         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
103         $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
104     } elseif (preg_match('/\\s*([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\s*/', $sQuery, $aData)) {
105         /*                     1            2                         3          4            5                      6
106          * degrees decimal minutes
107          * 40 26.767 N, 79 58.933 W
108          * 40° 26.767′ N 79° 58.933′ W
109          */
110         $sFound    = $aData[0];
111         $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
112         $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
113     } elseif (preg_match('/\\s*([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\s*/', $sQuery, $aData)) {
114         /*                     1        2            3              4                 5        6            7              8
115          * degrees decimal seconds
116          * N 40 26 46 W 79 58 56
117          * N 40° 26′ 46″, W 79° 58′ 56″
118          */
119         $sFound    = $aData[0];
120         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
121         $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
122     } elseif (preg_match('/\\s*([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+[0-9.]*)[″" ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+[0-9.]*)[″" ]+([EW])\\s*/', $sQuery, $aData)) {
123         /*                     1            2              3                    4          5            6              7                     8
124          * degrees decimal seconds
125          * 40 26 46 N 79 58 56 W
126          * 40° 26′ 46″ N, 79° 58′ 56″ W
127          * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
128          */
129         $sFound    = $aData[0];
130         $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
131         $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
132     } elseif (preg_match('/\\s*([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
133         /*                     1        2                               3        4
134          * degrees decimal
135          * N 40.446° W 79.982°
136          */
137         $sFound    = $aData[0];
138         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
139         $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
140     } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\s*/', $sQuery, $aData)) {
141         /*                     1                           2          3                           4
142          * degrees decimal
143          * 40.446° N 79.982° W
144          */
145         $sFound    = $aData[0];
146         $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
147         $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
148     } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
149         /*                 1                   2                             3                        4
150          * degrees decimal
151          * 12.34, 56.78
152          * 12.34 56.78
153          * [12.456,-78.90]
154          */
155         $sFound    = $aData[0];
156         $fQueryLat = $aData[2];
157         $fQueryLon = $aData[3];
158     } else {
159         return false;
160     }
161
162     return array($sFound, $fQueryLat, $fQueryLon);
163 }
164
165
166 function geometryText2Points($geometry_as_text, $fRadius)
167 {
168     $aPolyPoints = null;
169     if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
170         //
171         preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
172         //
173     } elseif (preg_match('#LINESTRING\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
174         //
175         preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
176         //
177     } elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch)) {
178         //
179         preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
180         //
181     } elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#', $geometry_as_text, $aMatch)) {
182         //
183         $aPolyPoints = createPointsAroundCenter($aMatch[1], $aMatch[2], $fRadius);
184         //
185     }
186
187     if (isset($aPolyPoints)) {
188         $aResultPoints = array();
189         foreach ($aPolyPoints as $aPoint) {
190             $aResultPoints[] = array($aPoint[1], $aPoint[2]);
191         }
192         return $aResultPoints;
193     }
194
195     return;
196 }
197
198 function createPointsAroundCenter($fLon, $fLat, $fRadius)
199 {
200     $iSteps = max(8, min(100, ($fRadius * 40000)^2));
201     $fStepSize = (2*pi())/$iSteps;
202     $aPolyPoints = array();
203     for ($f = 0; $f < 2*pi(); $f += $fStepSize) {
204         $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
205     }
206     return $aPolyPoints;
207 }
208
209 function closestHouseNumber($aRow)
210 {
211     $fHouse = $aRow['startnumber']
212                 + ($aRow['endnumber'] - $aRow['startnumber']) * $aRow['fraction'];
213
214     switch ($aRow['interpolationtype']) {
215         case 'odd':
216             $iHn = (int)($fHouse/2) * 2 + 1;
217             break;
218         case 'even':
219             $iHn = (int)(round($fHouse/2)) * 2;
220             break;
221         default:
222             $iHn = (int)(round($fHouse));
223             break;
224     }
225
226     return max(min($aRow['endnumber'], $iHn), $aRow['startnumber']);
227 }