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