3 * SPDX-License-Identifier: GPL-2.0-only
5 * This file is part of Nominatim. (https://nominatim.org)
7 * Copyright (C) 2022 by the Nominatim developer community.
8 * For a full list of authors see the git log.
11 function loadSettings($sProjectDir)
13 @define('CONST_InstallDir', $sProjectDir);
14 // Temporary hack to set the direcory via environment instead of
15 // the installed scripts. Neither setting is part of the official
17 defined('CONST_ConfigDir') or define('CONST_ConfigDir', $_SERVER['NOMINATIM_CONFIGDIR']);
20 function getSetting($sConfName, $sDefault = null)
22 $sValue = $_SERVER['NOMINATIM_'.$sConfName];
24 if ($sDefault !== null && !$sValue) {
31 function getSettingBool($sConfName)
33 $sVal = strtolower(getSetting($sConfName));
35 return strcmp($sVal, 'yes') == 0
36 || strcmp($sVal, 'true') == 0
37 || strcmp($sVal, '1') == 0;
40 function fail($sError, $sUserError = false)
43 $sUserError = $sError;
45 error_log('ERROR: '.$sError);
46 var_dump($sUserError);
52 function getProcessorCount()
54 $sCPU = file_get_contents('/proc/cpuinfo');
55 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
56 return count($aMatches[0]);
60 function getTotalMemoryMB()
62 $sCPU = file_get_contents('/proc/meminfo');
63 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
64 return (int)($aMatches[1]/1024);
68 function getCacheMemoryMB()
70 $sCPU = file_get_contents('/proc/meminfo');
71 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
72 return (int)($aMatches[1]/1024);
75 function getDatabaseDate(&$oDB)
77 // Find the newest node in the DB
78 $iLastOSMID = $oDB->getOne("select max(osm_id) from place where osm_type = 'N'");
79 // Lookup the timestamp that node was created
80 $sLastNodeURL = 'https://www.openstreetmap.org/api/0.6/node/'.$iLastOSMID.'/1';
81 $sLastNodeXML = file_get_contents($sLastNodeURL);
83 if ($sLastNodeXML === false) {
87 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);
89 return $aLastNodeDate[1];
93 function byImportance($a, $b)
95 if ($a['importance'] != $b['importance']) {
96 return ($a['importance'] > $b['importance']?-1:1);
99 return $a['foundorder'] <=> $b['foundorder'];
103 function javascript_renderData($xVal, $iOptions = 0)
105 $sCallback = isset($_GET['json_callback']) ? $_GET['json_callback'] : '';
106 if ($sCallback && !preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u', $sCallback)) {
107 // Unset, we call javascript_renderData again during exception handling
108 unset($_GET['json_callback']);
109 throw new Exception('Invalid json_callback value', 400);
112 $iOptions |= JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
113 if (isset($_GET['pretty']) && in_array(strtolower($_GET['pretty']), array('1', 'true'))) {
114 $iOptions |= JSON_PRETTY_PRINT;
117 $jsonout = json_encode($xVal, $iOptions);
120 header('Content-Type: application/javascript; charset=UTF-8');
121 echo $_GET['json_callback'].'('.$jsonout.')';
123 header('Content-Type: application/json; charset=UTF-8');
128 function addQuotes($s)
133 function parseLatLon($sQuery)
139 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)) {
141 * degrees decimal minutes
142 * N 40 26.767, W 79 58.933
143 * N 40°26.767′, W 79°58.933′
146 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
147 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
148 } 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)) {
150 * degrees decimal minutes
151 * 40 26.767 N, 79 58.933 W
152 * 40° 26.767′ N 79° 58.933′ W
155 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
156 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
157 } 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)) {
159 * degrees decimal seconds
160 * N 40 26 46 W 79 58 56
161 * N 40° 26′ 46″, W 79° 58′ 56″
164 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
165 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
166 } 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)) {
168 * degrees decimal seconds
169 * 40 26 46 N 79 58 56 W
170 * 40° 26′ 46″ N, 79° 58′ 56″ W
171 * 40° 26′ 46.78″ N, 79° 58′ 56.89″ W
174 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
175 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
176 } elseif (preg_match('/\\s*([NS])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*[,\s]+([EW])[\s]+([0-9]+[0-9]*\\.[0-9]+)[°]*\\s*/', $sQuery, $aData)) {
179 * N 40.446° W 79.982°
182 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
183 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
184 } elseif (preg_match('/\\s*([0-9]+[0-9]*\\.[0-9]+)[°\s]+([NS])[,\s]+([0-9]+[0-9]*\\.[0-9]+)[°\s]+([EW])\\s*/', $sQuery, $aData)) {
187 * 40.446° N 79.982° W
190 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
191 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
192 } elseif (preg_match('/(\\s*\\[|^\\s*|\\s*)(-?[0-9]+[0-9]*\\.[0-9]+)[,\s]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]\\s*|\\s*$|\\s*)/', $sQuery, $aData)) {
200 $fQueryLat = $aData[2];
201 $fQueryLon = $aData[3];
206 return array($sFound, $fQueryLat, $fQueryLon);
209 function addressRankToGeocodeJsonType($iAddressRank)
211 if ($iAddressRank >= 29 && $iAddressRank <= 30) {
214 if ($iAddressRank >= 26 && $iAddressRank < 28) {
217 if ($iAddressRank >= 22 && $iAddressRank < 26) {
220 if ($iAddressRank >= 17 && $iAddressRank < 22) {
223 if ($iAddressRank >= 13 && $iAddressRank < 17) {
226 if ($iAddressRank >= 10 && $iAddressRank < 13) {
229 if ($iAddressRank >= 5 && $iAddressRank < 10) {
232 if ($iAddressRank >= 4 && $iAddressRank < 5) {
239 if (!function_exists('array_key_last')) {
240 function array_key_last(array $array)
242 if (!empty($array)) {
243 return key(array_slice($array, -1, 1, true));