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