]> git.openstreetmap.org Git - nominatim.git/blob - lib/lib.php
Merge pull request #487 from lonvia/unify-reverse
[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 sizeof($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
36         function bySearchRank($a, $b)
37         {
38                 if ($a['iSearchRank'] == $b['iSearchRank'])
39                         return strlen($a['sOperator']) + strlen($a['sHouseNumber']) - strlen($b['sOperator']) - strlen($b['sHouseNumber']);
40                 return ($a['iSearchRank'] < $b['iSearchRank']?-1:1);
41         }
42
43
44         function byImportance($a, $b)
45         {
46                 if ($a['importance'] != $b['importance'])
47                         return ($a['importance'] > $b['importance']?-1:1);
48
49                 return ($a['foundorder'] < $b['foundorder']?-1:1);
50         }
51
52
53         function getPreferredLanguages($sLangString=false)
54         {
55                 if (!$sLangString)
56                 {
57                         // If we have been provided the value in $_GET it overrides browser value
58                         if (isset($_GET['accept-language']) && $_GET['accept-language'])
59                         {
60                                 $_SERVER["HTTP_ACCEPT_LANGUAGE"] = $_GET['accept-language'];
61                                 $sLangString = $_GET['accept-language'];
62                         }
63                         else if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
64                         {
65                                 $sLangString = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
66                         }
67                 }
68
69                 $aLanguages = array();
70                 if ($sLangString)
71                 {
72                         if (preg_match_all('/(([a-z]{1,8})(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $sLangString, $aLanguagesParse, PREG_SET_ORDER))
73                         {
74                                 foreach($aLanguagesParse as $iLang => $aLanguage)
75                                 {
76                                         $aLanguages[$aLanguage[1]] = isset($aLanguage[5])?(float)$aLanguage[5]:1 - ($iLang/100);
77                                         if (!isset($aLanguages[$aLanguage[2]])) $aLanguages[$aLanguage[2]] = $aLanguages[$aLanguage[1]]/10;
78                                 }
79                                 arsort($aLanguages);
80                         }
81                 }
82                 if (!sizeof($aLanguages) && CONST_Default_Language) $aLanguages = array(CONST_Default_Language=>1);
83                 foreach($aLanguages as $sLangauge => $fLangauagePref)
84                 {
85                         $aLangPrefOrder['short_name:'.$sLangauge] = 'short_name:'.$sLangauge;
86                 }
87                 foreach($aLanguages as $sLangauge => $fLangauagePref)
88                 {
89                         $aLangPrefOrder['name:'.$sLangauge] = 'name:'.$sLangauge;
90                 }
91                 foreach($aLanguages as $sLangauge => $fLangauagePref)
92                 {
93                         $aLangPrefOrder['place_name:'.$sLangauge] = 'place_name:'.$sLangauge;
94                 }
95                 foreach($aLanguages as $sLangauge => $fLangauagePref)
96                 {
97                         $aLangPrefOrder['official_name:'.$sLangauge] = 'official_name:'.$sLangauge;
98                 }
99                 $aLangPrefOrder['short_name'] = 'short_name';
100                 $aLangPrefOrder['name'] = 'name';
101                 $aLangPrefOrder['place_name'] = 'place_name';
102                 $aLangPrefOrder['official_name'] = 'official_name';
103                 $aLangPrefOrder['ref'] = 'ref';
104                 $aLangPrefOrder['type'] = 'type';
105                 return $aLangPrefOrder;
106         }
107
108
109         function getWordSets($aWords, $iDepth)
110         {
111                 $aResult = array(array(join(' ',$aWords)));
112                 $sFirstToken = '';
113                 if ($iDepth < 8) {
114                         while(sizeof($aWords) > 1)
115                         {
116                                 $sWord = array_shift($aWords);
117                                 $sFirstToken .= ($sFirstToken?' ':'').$sWord;
118                                 $aRest = getWordSets($aWords, $iDepth+1);
119                                 foreach($aRest as $aSet)
120                                 {
121                                         $aResult[] = array_merge(array($sFirstToken),$aSet);
122                                 }
123                         }
124                 }
125                 return $aResult;
126         }
127
128         function getInverseWordSets($aWords, $iDepth)
129         {
130                 $aResult = array(array(join(' ',$aWords)));
131                 $sFirstToken = '';
132                 if ($iDepth < 8)
133                 {
134                         while(sizeof($aWords) > 1)
135                         {
136                                 $sWord = array_pop($aWords);
137                                 $sFirstToken = $sWord.($sFirstToken?' ':'').$sFirstToken;
138                                 $aRest = getInverseWordSets($aWords, $iDepth+1);
139                                 foreach($aRest as $aSet)
140                                 {
141                                         $aResult[] = array_merge(array($sFirstToken),$aSet);
142                                 }
143                         }
144                 }
145                 return $aResult;
146         }
147
148
149         function getTokensFromSets($aSets)
150         {
151                 $aTokens = array();
152                 foreach($aSets as $aSet)
153                 {
154                         foreach($aSet as $sWord)
155                         {
156                                 $aTokens[' '.$sWord] = ' '.$sWord;
157                                 $aTokens[$sWord] = $sWord;
158                         }
159                 }
160                 return $aTokens;
161         }
162
163
164         /*
165            GB Postcode functions
166          */
167
168         function gbPostcodeCalculate($sPostcode, $sPostcodeSector, $sPostcodeEnd, &$oDB)
169         {
170                 // Try an exact match on the gb_postcode table
171                 $sSQL = 'select \'AA\', ST_X(ST_Centroid(geometry)) as lon,ST_Y(ST_Centroid(geometry)) as lat from gb_postcode where postcode = \''.$sPostcode.'\'';
172                 $aNearPostcodes = chksql($oDB->getAll($sSQL));
173
174                 if (sizeof($aNearPostcodes))
175                 {
176                         $aPostcodes = array();
177                         foreach($aNearPostcodes as $aPostcode)
178                         {
179                                 $aPostcodes[] = array('lat' => $aPostcode['lat'], 'lon' => $aPostcode['lon'], 'radius' => 0.005);
180                         }
181
182                         return $aPostcodes;
183                 }
184
185                 return false;
186         }
187
188
189         function getClassTypes()
190         {
191                 return array(
192  'boundary:administrative:1' => array('label'=>'Continent','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
193  'boundary:administrative:2' => array('label'=>'Country','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
194  'place:country' => array('label'=>'Country','frequency'=>0,'icon'=>'poi_boundary_administrative','defzoom'=>6, 'defdiameter' => 15,),
195  'boundary:administrative:3' => array('label'=>'State','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
196  'boundary:administrative:4' => array('label'=>'State','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
197  'place:state' => array('label'=>'State','frequency'=>0,'icon'=>'poi_boundary_administrative','defzoom'=>8, 'defdiameter' => 5.12,),
198  'boundary:administrative:5' => array('label'=>'State District','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
199  'boundary:administrative:6' => array('label'=>'County','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
200  'boundary:administrative:7' => array('label'=>'County','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
201  'place:county' => array('label'=>'County','frequency'=>108,'icon'=>'poi_boundary_administrative','defzoom'=>10, 'defdiameter' => 1.28,),
202  'boundary:administrative:8' => array('label'=>'City','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
203  'place:city' => array('label'=>'City','frequency'=>66,'icon'=>'poi_place_city','defzoom'=>12, 'defdiameter' => 0.32,),
204  'boundary:administrative:9' => array('label'=>'City District','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
205  'boundary:administrative:10' => array('label'=>'Suburb','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
206  'boundary:administrative:11' => array('label'=>'Neighbourhood','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
207  'place:region' => array('label'=>'Region','frequency'=>0,'icon'=>'poi_boundary_administrative','defzoom'=>8, 'defdiameter' => 0.04,),
208  'place:island' => array('label'=>'Island','frequency'=>288,'icon'=>'','defzoom'=>11, 'defdiameter' => 0.64,),
209  'boundary:administrative' => array('label'=>'Administrative','frequency'=>413,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
210  'boundary:postal_code' => array('label'=>'Postcode','frequency'=>413,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
211  'place:town' => array('label'=>'Town','frequency'=>1497,'icon'=>'poi_place_town','defzoom'=>14, 'defdiameter' => 0.08,),
212  'place:village' => array('label'=>'Village','frequency'=>11230,'icon'=>'poi_place_village','defzoom'=>15, 'defdiameter' => 0.04,),
213  'place:hamlet' => array('label'=>'Hamlet','frequency'=>7075,'icon'=>'poi_place_village','defzoom'=>15, 'defdiameter' => 0.04,),
214  'place:suburb' => array('label'=>'Suburb','frequency'=>2528,'icon'=>'poi_place_village', 'defdiameter' => 0.04,),
215  'place:locality' => array('label'=>'Locality','frequency'=>4113,'icon'=>'poi_place_village', 'defdiameter' => 0.02,),
216  'landuse:farm' => array('label'=>'Farm','frequency'=>1201,'icon'=>'', 'defdiameter' => 0.02,),
217  'place:farm' => array('label'=>'Farm','frequency'=>1162,'icon'=>'', 'defdiameter' => 0.02,),
218
219  'highway:motorway_junction' => array('label'=>'Motorway Junction','frequency'=>1126,'icon'=>'','simplelabel'=>'Junction',),
220  'highway:motorway' => array('label'=>'Motorway','frequency'=>4627,'icon'=>'','simplelabel'=>'Road',),
221  'highway:trunk' => array('label'=>'Trunk','frequency'=>23084,'icon'=>'','simplelabel'=>'Road',),
222  'highway:primary' => array('label'=>'Primary','frequency'=>32138,'icon'=>'','simplelabel'=>'Road',),
223  'highway:secondary' => array('label'=>'Secondary','frequency'=>25807,'icon'=>'','simplelabel'=>'Road',),
224  'highway:tertiary' => array('label'=>'Tertiary','frequency'=>29829,'icon'=>'','simplelabel'=>'Road',),
225  'highway:residential' => array('label'=>'Residential','frequency'=>361498,'icon'=>'','simplelabel'=>'Road',),
226  'highway:unclassified' => array('label'=>'Unclassified','frequency'=>66441,'icon'=>'','simplelabel'=>'Road',),
227  'highway:living_street' => array('label'=>'Living Street','frequency'=>710,'icon'=>'','simplelabel'=>'Road',),
228  'highway:service' => array('label'=>'Service','frequency'=>9963,'icon'=>'','simplelabel'=>'Road',),
229  'highway:track' => array('label'=>'Track','frequency'=>2565,'icon'=>'','simplelabel'=>'Road',),
230  'highway:road' => array('label'=>'Road','frequency'=>591,'icon'=>'','simplelabel'=>'Road',),
231  'highway:byway' => array('label'=>'Byway','frequency'=>346,'icon'=>'','simplelabel'=>'Road',),
232  'highway:bridleway' => array('label'=>'Bridleway','frequency'=>1556,'icon'=>'',),
233  'highway:cycleway' => array('label'=>'Cycleway','frequency'=>2419,'icon'=>'',),
234  'highway:pedestrian' => array('label'=>'Pedestrian','frequency'=>2757,'icon'=>'',),
235  'highway:footway' => array('label'=>'Footway','frequency'=>15008,'icon'=>'',),
236  'highway:steps' => array('label'=>'Steps','frequency'=>444,'icon'=>'','simplelabel'=>'Footway',),
237  'highway:motorway_link' => array('label'=>'Motorway Link','frequency'=>795,'icon'=>'','simplelabel'=>'Road',),
238  'highway:trunk_link' => array('label'=>'Trunk Link','frequency'=>1258,'icon'=>'','simplelabel'=>'Road',),
239  'highway:primary_link' => array('label'=>'Primary Link','frequency'=>313,'icon'=>'','simplelabel'=>'Road',),
240
241  'landuse:industrial' => array('label'=>'Industrial','frequency'=>1062,'icon'=>'',),
242  'landuse:residential' => array('label'=>'Residential','frequency'=>886,'icon'=>'',),
243  'landuse:retail' => array('label'=>'Retail','frequency'=>754,'icon'=>'',),
244  'landuse:commercial' => array('label'=>'Commercial','frequency'=>657,'icon'=>'',),
245
246  'place:airport' => array('label'=>'Airport','frequency'=>36,'icon'=>'transport_airport2', 'defdiameter' => 0.03,),
247  'aeroway:aerodrome' => array('label'=>'Aerodrome','frequency'=>36,'icon'=>'transport_airport2', 'defdiameter' => 0.03,),
248  'aeroway' => array('label'=>'Aeroway','frequency'=>36,'icon'=>'transport_airport2', 'defdiameter' => 0.03,),
249  'railway:station' => array('label'=>'Station','frequency'=>3431,'icon'=>'transport_train_station2', 'defdiameter' => 0.01,),
250  'amenity:place_of_worship' => array('label'=>'Place Of Worship','frequency'=>9049,'icon'=>'place_of_worship_unknown3',),
251  'amenity:pub' => array('label'=>'Pub','frequency'=>18969,'icon'=>'food_pub',),
252  'amenity:bar' => array('label'=>'Bar','frequency'=>164,'icon'=>'food_bar',),
253  'amenity:university' => array('label'=>'University','frequency'=>607,'icon'=>'education_university',),
254  'tourism:museum' => array('label'=>'Museum','frequency'=>543,'icon'=>'tourist_museum',),
255  'amenity:arts_centre' => array('label'=>'Arts Centre','frequency'=>136,'icon'=>'tourist_art_gallery2',),
256  'tourism:zoo' => array('label'=>'Zoo','frequency'=>47,'icon'=>'tourist_zoo',),
257  'tourism:theme_park' => array('label'=>'Theme Park','frequency'=>24,'icon'=>'poi_point_of_interest',),
258  'tourism:attraction' => array('label'=>'Attraction','frequency'=>1463,'icon'=>'poi_point_of_interest',),
259  'leisure:golf_course' => array('label'=>'Golf Course','frequency'=>712,'icon'=>'sport_golf',),
260  'historic:castle' => array('label'=>'Castle','frequency'=>316,'icon'=>'tourist_castle',),
261  'amenity:hospital' => array('label'=>'Hospital','frequency'=>879,'icon'=>'health_hospital',),
262  'amenity:school' => array('label'=>'School','frequency'=>8192,'icon'=>'education_school',),
263  'amenity:theatre' => array('label'=>'Theatre','frequency'=>371,'icon'=>'tourist_theatre',),
264  'amenity:public_building' => array('label'=>'Public Building','frequency'=>985,'icon'=>'',),
265  'amenity:library' => array('label'=>'Library','frequency'=>794,'icon'=>'amenity_library',),
266  'amenity:townhall' => array('label'=>'Townhall','frequency'=>242,'icon'=>'',),
267  'amenity:community_centre' => array('label'=>'Community Centre','frequency'=>157,'icon'=>'',),
268  'amenity:fire_station' => array('label'=>'Fire Station','frequency'=>221,'icon'=>'amenity_firestation3',),
269  'amenity:police' => array('label'=>'Police','frequency'=>334,'icon'=>'amenity_police2',),
270  'amenity:bank' => array('label'=>'Bank','frequency'=>1248,'icon'=>'money_bank2',),
271  'amenity:post_office' => array('label'=>'Post Office','frequency'=>859,'icon'=>'amenity_post_office',),
272  'leisure:park' => array('label'=>'Park','frequency'=>2378,'icon'=>'',),
273  'amenity:park' => array('label'=>'Park','frequency'=>53,'icon'=>'',),
274  'landuse:park' => array('label'=>'Park','frequency'=>50,'icon'=>'',),
275  'landuse:recreation_ground' => array('label'=>'Recreation Ground','frequency'=>517,'icon'=>'',),
276  'tourism:hotel' => array('label'=>'Hotel','frequency'=>2150,'icon'=>'accommodation_hotel2',),
277  'tourism:motel' => array('label'=>'Motel','frequency'=>43,'icon'=>'',),
278  'amenity:cinema' => array('label'=>'Cinema','frequency'=>277,'icon'=>'tourist_cinema',),
279  'tourism:artwork' => array('label'=>'Artwork','frequency'=>171,'icon'=>'tourist_art_gallery2',),
280  'historic:archaeological_site' => array('label'=>'Archaeological Site','frequency'=>407,'icon'=>'tourist_archaeological2',),
281  'amenity:doctors' => array('label'=>'Doctors','frequency'=>581,'icon'=>'health_doctors',),
282  'leisure:sports_centre' => array('label'=>'Sports Centre','frequency'=>767,'icon'=>'sport_leisure_centre',),
283  'leisure:swimming_pool' => array('label'=>'Swimming Pool','frequency'=>24,'icon'=>'sport_swimming_outdoor',),
284  'shop:supermarket' => array('label'=>'Supermarket','frequency'=>2673,'icon'=>'shopping_supermarket',),
285  'shop:convenience' => array('label'=>'Convenience','frequency'=>1469,'icon'=>'shopping_convenience',),
286  'amenity:restaurant' => array('label'=>'Restaurant','frequency'=>3179,'icon'=>'food_restaurant',),
287  'amenity:fast_food' => array('label'=>'Fast Food','frequency'=>2289,'icon'=>'food_fastfood',),
288  'amenity:cafe' => array('label'=>'Cafe','frequency'=>1780,'icon'=>'food_cafe',),
289  'tourism:guest_house' => array('label'=>'Guest House','frequency'=>223,'icon'=>'accommodation_bed_and_breakfast',),
290  'amenity:pharmacy' => array('label'=>'Pharmacy','frequency'=>733,'icon'=>'health_pharmacy_dispensing',),
291  'amenity:fuel' => array('label'=>'Fuel','frequency'=>1308,'icon'=>'transport_fuel',),
292  'natural:peak' => array('label'=>'Peak','frequency'=>3212,'icon'=>'poi_peak',),
293  'waterway:waterfall' => array('label'=>'Waterfall','frequency'=>24,'icon'=>'',),
294  'natural:wood' => array('label'=>'Wood','frequency'=>1845,'icon'=>'landuse_coniferous_and_deciduous',),
295  'natural:water' => array('label'=>'Water','frequency'=>1790,'icon'=>'',),
296  'landuse:forest' => array('label'=>'Forest','frequency'=>467,'icon'=>'',),
297  'landuse:cemetery' => array('label'=>'Cemetery','frequency'=>463,'icon'=>'',),
298  'landuse:allotments' => array('label'=>'Allotments','frequency'=>408,'icon'=>'',),
299  'landuse:farmyard' => array('label'=>'Farmyard','frequency'=>397,'icon'=>'',),
300  'railway:rail' => array('label'=>'Rail','frequency'=>4894,'icon'=>'',),
301  'waterway:canal' => array('label'=>'Canal','frequency'=>1723,'icon'=>'',),
302  'waterway:river' => array('label'=>'River','frequency'=>4089,'icon'=>'',),
303  'waterway:stream' => array('label'=>'Stream','frequency'=>2684,'icon'=>'',),
304  'shop:bicycle' => array('label'=>'Bicycle','frequency'=>349,'icon'=>'shopping_bicycle',),
305  'shop:clothes' => array('label'=>'Clothes','frequency'=>315,'icon'=>'shopping_clothes',),
306  'shop:hairdresser' => array('label'=>'Hairdresser','frequency'=>312,'icon'=>'shopping_hairdresser',),
307  'shop:doityourself' => array('label'=>'Doityourself','frequency'=>247,'icon'=>'shopping_diy',),
308  'shop:estate_agent' => array('label'=>'Estate Agent','frequency'=>162,'icon'=>'shopping_estateagent2',),
309  'shop:car' => array('label'=>'Car','frequency'=>159,'icon'=>'shopping_car',),
310  'shop:garden_centre' => array('label'=>'Garden Centre','frequency'=>143,'icon'=>'shopping_garden_centre',),
311  'shop:car_repair' => array('label'=>'Car Repair','frequency'=>141,'icon'=>'shopping_car_repair',),
312  'shop:newsagent' => array('label'=>'Newsagent','frequency'=>132,'icon'=>'',),
313  'shop:bakery' => array('label'=>'Bakery','frequency'=>129,'icon'=>'shopping_bakery',),
314  'shop:furniture' => array('label'=>'Furniture','frequency'=>124,'icon'=>'',),
315  'shop:butcher' => array('label'=>'Butcher','frequency'=>105,'icon'=>'shopping_butcher',),
316  'shop:apparel' => array('label'=>'Apparel','frequency'=>98,'icon'=>'shopping_clothes',),
317  'shop:electronics' => array('label'=>'Electronics','frequency'=>96,'icon'=>'',),
318  'shop:department_store' => array('label'=>'Department Store','frequency'=>86,'icon'=>'',),
319  'shop:books' => array('label'=>'Books','frequency'=>85,'icon'=>'',),
320  'shop:yes' => array('label'=>'Shop','frequency'=>68,'icon'=>'',),
321  'shop:outdoor' => array('label'=>'Outdoor','frequency'=>67,'icon'=>'',),
322  'shop:mall' => array('label'=>'Mall','frequency'=>63,'icon'=>'',),
323  'shop:florist' => array('label'=>'Florist','frequency'=>61,'icon'=>'',),
324  'shop:charity' => array('label'=>'Charity','frequency'=>60,'icon'=>'',),
325  'shop:hardware' => array('label'=>'Hardware','frequency'=>59,'icon'=>'',),
326  'shop:laundry' => array('label'=>'Laundry','frequency'=>51,'icon'=>'shopping_laundrette',),
327  'shop:shoes' => array('label'=>'Shoes','frequency'=>49,'icon'=>'',),
328  'shop:beverages' => array('label'=>'Beverages','frequency'=>48,'icon'=>'shopping_alcohol',),
329  'shop:dry_cleaning' => array('label'=>'Dry Cleaning','frequency'=>46,'icon'=>'',),
330  'shop:carpet' => array('label'=>'Carpet','frequency'=>45,'icon'=>'',),
331  'shop:computer' => array('label'=>'Computer','frequency'=>44,'icon'=>'',),
332  'shop:alcohol' => array('label'=>'Alcohol','frequency'=>44,'icon'=>'shopping_alcohol',),
333  'shop:optician' => array('label'=>'Optician','frequency'=>55,'icon'=>'health_opticians',),
334  'shop:chemist' => array('label'=>'Chemist','frequency'=>42,'icon'=>'health_pharmacy',),
335  'shop:gallery' => array('label'=>'Gallery','frequency'=>38,'icon'=>'tourist_art_gallery2',),
336  'shop:mobile_phone' => array('label'=>'Mobile Phone','frequency'=>37,'icon'=>'',),
337  'shop:sports' => array('label'=>'Sports','frequency'=>37,'icon'=>'',),
338  'shop:jewelry' => array('label'=>'Jewelry','frequency'=>32,'icon'=>'shopping_jewelry',),
339  'shop:pet' => array('label'=>'Pet','frequency'=>29,'icon'=>'',),
340  'shop:beauty' => array('label'=>'Beauty','frequency'=>28,'icon'=>'',),
341  'shop:stationery' => array('label'=>'Stationery','frequency'=>25,'icon'=>'',),
342  'shop:shopping_centre' => array('label'=>'Shopping Centre','frequency'=>25,'icon'=>'',),
343  'shop:general' => array('label'=>'General','frequency'=>25,'icon'=>'',),
344  'shop:electrical' => array('label'=>'Electrical','frequency'=>25,'icon'=>'',),
345  'shop:toys' => array('label'=>'Toys','frequency'=>23,'icon'=>'',),
346  'shop:jeweller' => array('label'=>'Jeweller','frequency'=>23,'icon'=>'',),
347  'shop:betting' => array('label'=>'Betting','frequency'=>23,'icon'=>'',),
348  'shop:household' => array('label'=>'Household','frequency'=>21,'icon'=>'',),
349  'shop:travel_agency' => array('label'=>'Travel Agency','frequency'=>21,'icon'=>'',),
350  'shop:hifi' => array('label'=>'Hifi','frequency'=>21,'icon'=>'',),
351  'amenity:shop' => array('label'=>'Shop','frequency'=>61,'icon'=>'',),
352  'tourism:information' => array('label'=>'Information','frequency'=>224,'icon'=>'amenity_information',),
353
354  'place:house' => array('label'=>'House','frequency'=>2086,'icon'=>'','defzoom'=>18,),
355  'place:house_name' => array('label'=>'House','frequency'=>2086,'icon'=>'','defzoom'=>18,),
356  'place:house_number' => array('label'=>'House Number','frequency'=>2086,'icon'=>'','defzoom'=>18,),
357  'place:country_code' => array('label'=>'Country Code','frequency'=>2086,'icon'=>'','defzoom'=>18,),
358
359  //
360
361  'leisure:pitch' => array('label'=>'Pitch','frequency'=>762,'icon'=>'',),
362  'highway:unsurfaced' => array('label'=>'Unsurfaced','frequency'=>492,'icon'=>'',),
363  'historic:ruins' => array('label'=>'Ruins','frequency'=>483,'icon'=>'tourist_ruin',),
364  'amenity:college' => array('label'=>'College','frequency'=>473,'icon'=>'education_school',),
365  'historic:monument' => array('label'=>'Monument','frequency'=>470,'icon'=>'tourist_monument',),
366  'railway:subway' => array('label'=>'Subway','frequency'=>385,'icon'=>'',),
367  'historic:memorial' => array('label'=>'Memorial','frequency'=>382,'icon'=>'tourist_monument',),
368  'leisure:nature_reserve' => array('label'=>'Nature Reserve','frequency'=>342,'icon'=>'',),
369  'leisure:common' => array('label'=>'Common','frequency'=>322,'icon'=>'',),
370  'waterway:lock_gate' => array('label'=>'Lock Gate','frequency'=>321,'icon'=>'',),
371  'natural:fell' => array('label'=>'Fell','frequency'=>308,'icon'=>'',),
372  'amenity:nightclub' => array('label'=>'Nightclub','frequency'=>292,'icon'=>'',),
373  'highway:path' => array('label'=>'Path','frequency'=>287,'icon'=>'',),
374  'leisure:garden' => array('label'=>'Garden','frequency'=>285,'icon'=>'',),
375  'landuse:reservoir' => array('label'=>'Reservoir','frequency'=>276,'icon'=>'',),
376  'leisure:playground' => array('label'=>'Playground','frequency'=>264,'icon'=>'',),
377  'leisure:stadium' => array('label'=>'Stadium','frequency'=>212,'icon'=>'',),
378  'historic:mine' => array('label'=>'Mine','frequency'=>193,'icon'=>'poi_mine',),
379  'natural:cliff' => array('label'=>'Cliff','frequency'=>193,'icon'=>'',),
380  'tourism:caravan_site' => array('label'=>'Caravan Site','frequency'=>183,'icon'=>'accommodation_caravan_park',),
381  'amenity:bus_station' => array('label'=>'Bus Station','frequency'=>181,'icon'=>'transport_bus_station',),
382  'amenity:kindergarten' => array('label'=>'Kindergarten','frequency'=>179,'icon'=>'',),
383  'highway:construction' => array('label'=>'Construction','frequency'=>176,'icon'=>'',),
384  'amenity:atm' => array('label'=>'Atm','frequency'=>172,'icon'=>'money_atm2',),
385  'amenity:emergency_phone' => array('label'=>'Emergency Phone','frequency'=>164,'icon'=>'',),
386  'waterway:lock' => array('label'=>'Lock','frequency'=>146,'icon'=>'',),
387  'waterway:riverbank' => array('label'=>'Riverbank','frequency'=>143,'icon'=>'',),
388  'natural:coastline' => array('label'=>'Coastline','frequency'=>142,'icon'=>'',),
389  'tourism:viewpoint' => array('label'=>'Viewpoint','frequency'=>140,'icon'=>'tourist_view_point',),
390  'tourism:hostel' => array('label'=>'Hostel','frequency'=>140,'icon'=>'',),
391  'tourism:bed_and_breakfast' => array('label'=>'Bed And Breakfast','frequency'=>140,'icon'=>'accommodation_bed_and_breakfast',),
392  'railway:halt' => array('label'=>'Halt','frequency'=>135,'icon'=>'',),
393  'railway:platform' => array('label'=>'Platform','frequency'=>134,'icon'=>'',),
394  'railway:tram' => array('label'=>'Tram','frequency'=>130,'icon'=>'transport_tram_stop',),
395  'amenity:courthouse' => array('label'=>'Courthouse','frequency'=>129,'icon'=>'amenity_court',),
396  'amenity:recycling' => array('label'=>'Recycling','frequency'=>126,'icon'=>'amenity_recycling',),
397  'amenity:dentist' => array('label'=>'Dentist','frequency'=>124,'icon'=>'health_dentist',),
398  'natural:beach' => array('label'=>'Beach','frequency'=>121,'icon'=>'tourist_beach',),
399  'place:moor' => array('label'=>'Moor','frequency'=>118,'icon'=>'',),
400  'amenity:grave_yard' => array('label'=>'Grave Yard','frequency'=>110,'icon'=>'',),
401  'waterway:drain' => array('label'=>'Drain','frequency'=>108,'icon'=>'',),
402  'landuse:grass' => array('label'=>'Grass','frequency'=>106,'icon'=>'',),
403  'landuse:village_green' => array('label'=>'Village Green','frequency'=>106,'icon'=>'',),
404  'natural:bay' => array('label'=>'Bay','frequency'=>102,'icon'=>'',),
405  'railway:tram_stop' => array('label'=>'Tram Stop','frequency'=>101,'icon'=>'transport_tram_stop',),
406  'leisure:marina' => array('label'=>'Marina','frequency'=>98,'icon'=>'',),
407  'highway:stile' => array('label'=>'Stile','frequency'=>97,'icon'=>'',),
408  'natural:moor' => array('label'=>'Moor','frequency'=>95,'icon'=>'',),
409  'railway:light_rail' => array('label'=>'Light Rail','frequency'=>91,'icon'=>'',),
410  'railway:narrow_gauge' => array('label'=>'Narrow Gauge','frequency'=>90,'icon'=>'',),
411  'natural:land' => array('label'=>'Land','frequency'=>86,'icon'=>'',),
412  'amenity:village_hall' => array('label'=>'Village Hall','frequency'=>82,'icon'=>'',),
413  'waterway:dock' => array('label'=>'Dock','frequency'=>80,'icon'=>'',),
414  'amenity:veterinary' => array('label'=>'Veterinary','frequency'=>79,'icon'=>'',),
415  'landuse:brownfield' => array('label'=>'Brownfield','frequency'=>77,'icon'=>'',),
416  'leisure:track' => array('label'=>'Track','frequency'=>76,'icon'=>'',),
417  'railway:historic_station' => array('label'=>'Historic Station','frequency'=>74,'icon'=>'',),
418  'landuse:construction' => array('label'=>'Construction','frequency'=>72,'icon'=>'',),
419  'amenity:prison' => array('label'=>'Prison','frequency'=>71,'icon'=>'amenity_prison',),
420  'landuse:quarry' => array('label'=>'Quarry','frequency'=>71,'icon'=>'',),
421  'amenity:telephone' => array('label'=>'Telephone','frequency'=>70,'icon'=>'',),
422  'highway:traffic_signals' => array('label'=>'Traffic Signals','frequency'=>66,'icon'=>'',),
423  'natural:heath' => array('label'=>'Heath','frequency'=>62,'icon'=>'',),
424  'historic:house' => array('label'=>'House','frequency'=>61,'icon'=>'',),
425  'amenity:social_club' => array('label'=>'Social Club','frequency'=>61,'icon'=>'',),
426  'landuse:military' => array('label'=>'Military','frequency'=>61,'icon'=>'',),
427  'amenity:health_centre' => array('label'=>'Health Centre','frequency'=>59,'icon'=>'',),
428  'historic:building' => array('label'=>'Building','frequency'=>58,'icon'=>'',),
429  'amenity:clinic' => array('label'=>'Clinic','frequency'=>57,'icon'=>'',),
430  'highway:services' => array('label'=>'Services','frequency'=>56,'icon'=>'',),
431  'amenity:ferry_terminal' => array('label'=>'Ferry Terminal','frequency'=>55,'icon'=>'',),
432  'natural:marsh' => array('label'=>'Marsh','frequency'=>55,'icon'=>'',),
433  'natural:hill' => array('label'=>'Hill','frequency'=>54,'icon'=>'',),
434  'highway:raceway' => array('label'=>'Raceway','frequency'=>53,'icon'=>'',),
435  'amenity:taxi' => array('label'=>'Taxi','frequency'=>47,'icon'=>'',),
436  'amenity:take_away' => array('label'=>'Take Away','frequency'=>45,'icon'=>'',),
437  'amenity:car_rental' => array('label'=>'Car Rental','frequency'=>44,'icon'=>'',),
438  'place:islet' => array('label'=>'Islet','frequency'=>44,'icon'=>'',),
439  'amenity:nursery' => array('label'=>'Nursery','frequency'=>44,'icon'=>'',),
440  'amenity:nursing_home' => array('label'=>'Nursing Home','frequency'=>43,'icon'=>'',),
441  'amenity:toilets' => array('label'=>'Toilets','frequency'=>38,'icon'=>'',),
442  'amenity:hall' => array('label'=>'Hall','frequency'=>38,'icon'=>'',),
443  'waterway:boatyard' => array('label'=>'Boatyard','frequency'=>36,'icon'=>'',),
444  'highway:mini_roundabout' => array('label'=>'Mini Roundabout','frequency'=>35,'icon'=>'',),
445  'historic:manor' => array('label'=>'Manor','frequency'=>35,'icon'=>'',),
446  'tourism:chalet' => array('label'=>'Chalet','frequency'=>34,'icon'=>'',),
447  'amenity:bicycle_parking' => array('label'=>'Bicycle Parking','frequency'=>34,'icon'=>'',),
448  'amenity:hotel' => array('label'=>'Hotel','frequency'=>34,'icon'=>'',),
449  'waterway:weir' => array('label'=>'Weir','frequency'=>33,'icon'=>'',),
450  'natural:wetland' => array('label'=>'Wetland','frequency'=>33,'icon'=>'',),
451  'natural:cave_entrance' => array('label'=>'Cave Entrance','frequency'=>32,'icon'=>'',),
452  'amenity:crematorium' => array('label'=>'Crematorium','frequency'=>31,'icon'=>'',),
453  'tourism:picnic_site' => array('label'=>'Picnic Site','frequency'=>31,'icon'=>'',),
454  'landuse:wood' => array('label'=>'Wood','frequency'=>30,'icon'=>'',),
455  'landuse:basin' => array('label'=>'Basin','frequency'=>30,'icon'=>'',),
456  'natural:tree' => array('label'=>'Tree','frequency'=>30,'icon'=>'',),
457  'leisure:slipway' => array('label'=>'Slipway','frequency'=>29,'icon'=>'',),
458  'landuse:meadow' => array('label'=>'Meadow','frequency'=>29,'icon'=>'',),
459  'landuse:piste' => array('label'=>'Piste','frequency'=>28,'icon'=>'',),
460  'amenity:care_home' => array('label'=>'Care Home','frequency'=>28,'icon'=>'',),
461  'amenity:club' => array('label'=>'Club','frequency'=>28,'icon'=>'',),
462  'amenity:medical_centre' => array('label'=>'Medical Centre','frequency'=>27,'icon'=>'',),
463  'historic:roman_road' => array('label'=>'Roman Road','frequency'=>27,'icon'=>'',),
464  'historic:fort' => array('label'=>'Fort','frequency'=>26,'icon'=>'',),
465  'railway:subway_entrance' => array('label'=>'Subway Entrance','frequency'=>26,'icon'=>'',),
466  'historic:yes' => array('label'=>'Historic','frequency'=>25,'icon'=>'',),
467  'highway:gate' => array('label'=>'Gate','frequency'=>25,'icon'=>'',),
468  'leisure:fishing' => array('label'=>'Fishing','frequency'=>24,'icon'=>'',),
469  'historic:museum' => array('label'=>'Museum','frequency'=>24,'icon'=>'',),
470  'amenity:car_wash' => array('label'=>'Car Wash','frequency'=>24,'icon'=>'',),
471  'railway:level_crossing' => array('label'=>'Level Crossing','frequency'=>23,'icon'=>'',),
472  'leisure:bird_hide' => array('label'=>'Bird Hide','frequency'=>23,'icon'=>'',),
473  'natural:headland' => array('label'=>'Headland','frequency'=>21,'icon'=>'',),
474  'tourism:apartments' => array('label'=>'Apartments','frequency'=>21,'icon'=>'',),
475  'amenity:shopping' => array('label'=>'Shopping','frequency'=>21,'icon'=>'',),
476  'natural:scrub' => array('label'=>'Scrub','frequency'=>20,'icon'=>'',),
477  'natural:fen' => array('label'=>'Fen','frequency'=>20,'icon'=>'',),
478  'building:yes' => array('label'=>'Building','frequency'=>200,'icon'=>'',),
479  'mountain_pass:yes' => array('label'=>'Mountain Pass','frequency'=>200,'icon'=>'',),
480
481  'amenity:parking' => array('label'=>'Parking','frequency'=>3157,'icon'=>'',),
482  'highway:bus_stop' => array('label'=>'Bus Stop','frequency'=>35777,'icon'=>'transport_bus_stop2',),
483  'place:postcode' => array('label'=>'Postcode','frequency'=>27267,'icon'=>'',),
484  'amenity:post_box' => array('label'=>'Post Box','frequency'=>9613,'icon'=>'',),
485
486  'place:houses' => array('label'=>'Houses','frequency'=>85,'icon'=>'',),
487  'railway:preserved' => array('label'=>'Preserved','frequency'=>227,'icon'=>'',),
488  'waterway:derelict_canal' => array('label'=>'Derelict Canal','frequency'=>21,'icon'=>'',),
489  'amenity:dead_pub' => array('label'=>'Dead Pub','frequency'=>20,'icon'=>'',),
490  'railway:disused_station' => array('label'=>'Disused Station','frequency'=>114,'icon'=>'',),
491  'railway:abandoned' => array('label'=>'Abandoned','frequency'=>641,'icon'=>'',),
492  'railway:disused' => array('label'=>'Disused','frequency'=>72,'icon'=>'',),
493                                 );
494         }
495
496
497         function getClassTypesWithImportance()
498         {
499                 $aOrders = getClassTypes();
500                 $i = 1;
501                 foreach($aOrders as $sID => $a)
502                 {
503                         $aOrders[$sID]['importance'] = $i++;
504                 }
505                 return $aOrders;
506         }
507
508         function getResultDiameter($aResult)
509         {
510                 $aClassType = getClassTypes();
511
512                 $fDiameter = 0.0001;
513
514                 if (isset($aResult['class'])
515                           && isset($aResult['type'])
516                           && isset($aResult['admin_level'])
517                           && isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
518                                 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
519                 {
520                         $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'];
521                 }
522                 elseif (isset($aResult['class'])
523                           && isset($aResult['type'])
524                           && isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
525                                 && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
526                 {
527                         $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
528                 }
529
530                 return $fDiameter;
531         }
532
533
534         function javascript_renderData($xVal, $iOptions = 0)
535         {
536                 if (defined('PHP_VERSION_ID') && PHP_VERSION_ID > 50400)
537                         $iOptions |= JSON_UNESCAPED_UNICODE;
538                 $jsonout = json_encode($xVal, $iOptions);
539
540                 if( ! isset($_GET['json_callback']))
541                 {
542                         header("Content-Type: application/json; charset=UTF-8");
543                         echo $jsonout;
544                 } else
545                 {
546                         if (preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u',$_GET['json_callback']))
547                         {
548                                 header("Content-Type: application/javascript; charset=UTF-8");
549                                 echo $_GET['json_callback'].'('.$jsonout.')';
550                         }
551                         else
552                         {
553                                 header('HTTP/1.0 400 Bad Request');
554                         }
555                 }
556         }
557
558
559         function _debugDumpGroupedSearches($aData, $aTokens)
560         {
561                 $aWordsIDs = array();
562                 if ($aTokens)
563                 {
564                         foreach($aTokens as $sToken => $aWords)
565                         {
566                                 if ($aWords)
567                                 {
568                                         foreach($aWords as $aToken)
569                                         {
570                                                 $aWordsIDs[$aToken['word_id']] = $sToken.'('.$aToken['word_id'].')';
571                                         }
572                                 }
573                         }
574                 }
575                 echo "<table border=\"1\">";
576                 echo "<tr><th>rank</th><th>Name Tokens</th><th>Name Not</th><th>Address Tokens</th><th>Address Not</th><th>country</th><th>operator</th><th>class</th><th>type</th><th>house#</th><th>Lat</th><th>Lon</th><th>Radius</th></tr>";
577                 foreach($aData as $iRank => $aRankedSet)
578                 {
579                         foreach($aRankedSet as $aRow)
580                         {
581                                 echo "<tr>";
582                                 echo "<td>$iRank</td>";
583
584                                 echo "<td>";
585                                 $sSep = '';
586                                 foreach($aRow['aName'] as $iWordID)
587                                 {
588                                         echo $sSep.'#'.$aWordsIDs[$iWordID].'#';
589                                         $sSep = ', ';
590                                 }
591                                 echo "</td>";
592
593                                 echo "<td>";
594                                 $sSep = '';
595                                 foreach($aRow['aNameNonSearch'] as $iWordID)
596                                 {
597                                         echo $sSep.'#'.$aWordsIDs[$iWordID].'#';
598                                         $sSep = ', ';
599                                 }
600                                 echo "</td>";
601
602                                 echo "<td>";
603                                 $sSep = '';
604                                 foreach($aRow['aAddress'] as $iWordID)
605                                 {
606                                         echo $sSep.'#'.$aWordsIDs[$iWordID].'#';
607                                         $sSep = ', ';
608                                 }
609                                 echo "</td>";
610
611                                 echo "<td>";
612                                 $sSep = '';
613                                 foreach($aRow['aAddressNonSearch'] as $iWordID)
614                                 {
615                                         echo $sSep.'#'.$aWordsIDs[$iWordID].'#';
616                                         $sSep = ', ';
617                                 }
618                                 echo "</td>";
619
620                                 echo "<td>".$aRow['sCountryCode']."</td>";
621
622                                 echo "<td>".$aRow['sOperator']."</td>";
623                                 echo "<td>".$aRow['sClass']."</td>";
624                                 echo "<td>".$aRow['sType']."</td>";
625
626                                 echo "<td>".$aRow['sHouseNumber']."</td>";
627
628                                 echo "<td>".$aRow['fLat']."</td>";
629                                 echo "<td>".$aRow['fLon']."</td>";
630                                 echo "<td>".$aRow['fRadius']."</td>";
631
632                                 echo "</tr>";
633                         }
634                 }
635                 echo "</table>";
636         }
637
638
639         function getAddressDetails(&$oDB, $sLanguagePrefArraySQL, $iPlaceID, $sCountryCode = false, $housenumber = -1, $bRaw = false)
640         {
641                 $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata($iPlaceID, $housenumber)";
642                 if (!$bRaw) $sSQL .= " WHERE isaddress OR type = 'country_code'";
643                 $sSQL .= " order by rank_address desc,isaddress desc";
644
645                 $aAddressLines = chksql($oDB->getAll($sSQL));
646                 if ($bRaw) return $aAddressLines;
647                 //echo "<pre>";
648                 //var_dump($aAddressLines);
649                 $aAddress = array();
650                 $aFallback = array();
651                 $aClassType = getClassTypes();
652                 foreach($aAddressLines as $aLine)
653                 {
654                         $bFallback = false;
655                         $aTypeLabel = false;
656                         if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
657                         elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
658                         elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
659                         {
660                                 $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
661                                 $bFallback = true;
662                         }
663                         else
664                         {
665                                 $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
666                                 $bFallback = true;
667                         }
668                         if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
669                         {
670                                 $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
671                                 $sTypeLabel = str_replace(' ','_',$sTypeLabel);
672                                 if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place')
673                                 {
674                                         $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
675                                 }
676                                 $aFallback[$sTypeLabel] = $bFallback;
677                         }
678                 }
679                 return $aAddress;
680         }
681
682
683         function addQuotes($s)
684         {
685                 return "'".$s."'";
686         }
687
688         // returns boolean
689         function validLatLon($fLat,$fLon)
690         {
691                 return ($fLat <= 90.1 && $fLat >= -90.1 && $fLon <= 180.1 && $fLon >= -180.1);
692         }
693
694         // Do we have anything that looks like a lat/lon pair?
695         // returns array(lat,lon,query_with_lat_lon_removed)
696         // or null
697         function looksLikeLatLonPair($sQuery)
698         {
699                 $sFound    = null;
700                 $fQueryLat = null;
701                 $fQueryLon = null;
702
703                 // degrees decimal minutes
704                 // N 40 26.767, W 79 58.933
705                 // N 40°26.767′, W 79°58.933′
706                 //                  1         2                   3                  4         5            6
707                 if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*?\\b/', $sQuery, $aData))
708                 {
709                         $sFound    = $aData[0];
710                         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
711                         $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
712                 }
713                 // degrees decimal minutes
714                 // 40 26.767 N, 79 58.933 W
715                 // 40° 26.767′ N 79° 58.933′ W
716                 //                      1             2                      3          4            5                    6
717                 elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\b/', $sQuery, $aData))
718                 {
719                         $sFound    = $aData[0];
720                         $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
721                         $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
722                 }
723                 // degrees decimal seconds
724                 // N 40 26 46 W 79 58 56
725                 // N 40° 26′ 46″, W 79° 58′ 56″
726                 //                      1        2            3            4                5        6            7            8
727                 elseif (preg_match('/\\b([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\b/', $sQuery, $aData))
728                 {
729                         $sFound    = $aData[0];
730                         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
731                         $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
732                 }
733                 // degrees decimal seconds
734                 // 40 26 46 N 79 58 56 W
735                 // 40° 26′ 46″ N, 79° 58′ 56″ W
736                 //                      1            2            3            4          5            6            7            8
737                 elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([EW])\\b/', $sQuery, $aData))
738                 {
739                         $sFound    = $aData[0];
740                         $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
741                         $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
742                 }
743                 // degrees decimal
744                 // N 40.446° W 79.982°
745                 //                      1        2                               3        4
746                 elseif (preg_match('/\\b([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\b/', $sQuery, $aData))
747                 {
748                         $sFound    = $aData[0];
749                         $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
750                         $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
751                 }
752                 // degrees decimal
753                 // 40.446° N 79.982° W
754                 //                      1                           2          3                           4
755                 elseif (preg_match('/\\b([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\b/', $sQuery, $aData))
756                 {
757                         $sFound    = $aData[0];
758                         $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
759                         $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
760                 }
761                 // degrees decimal
762                 // 12.34, 56.78
763                 // [12.456,-78.90]
764                 //                   1          2                             3                        4
765                 elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData))
766                 {
767                         $sFound    = $aData[0];
768                         $fQueryLat = $aData[2];
769                         $fQueryLon = $aData[3];
770                 }
771
772                 if (!validLatLon($fQueryLat, $fQueryLon)) return;
773                 $sQuery = trim(str_replace($sFound, ' ', $sQuery));
774
775                 return array('lat' => $fQueryLat, 'lon' => $fQueryLon, 'query' => $sQuery);
776         }
777
778
779         function geometryText2Points($geometry_as_text, $fRadius)
780         {
781                 $aPolyPoints = NULL;
782                 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch))
783                 {
784                         preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
785                 }
786                 elseif (preg_match('#LINESTRING\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch))
787                 {
788                         preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
789                 }
790                 elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch))
791                 {
792                         preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
793                 }
794                 elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#', $geometry_as_text, $aMatch))
795                 {
796                         $aPolyPoints = createPointsAroundCenter($aMatch[1], $aMatch[2], $fRadius);
797                 }
798
799                 if (isset($aPolyPoints))
800                 {
801                         $aResultPoints = array();
802                         foreach($aPolyPoints as $aPoint)
803                         {
804                                 $aResultPoints[] = array($aPoint[1], $aPoint[2]);
805                         }
806                         return $aResultPoints;
807                 }
808
809                 return;
810         }
811
812         function createPointsAroundCenter($fLon, $fLat, $fRadius)
813         {
814                         $iSteps = max(8, min(100, ($fRadius * 40000)^2));
815                         $fStepSize = (2*pi())/$iSteps;
816                         $aPolyPoints = array();
817                         for($f = 0; $f < 2*pi(); $f += $fStepSize)
818                         {
819                                 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );
820                         }
821                         return $aPolyPoints;
822         }