3 function fail($sError, $sUserError = false)
5 if (!$sUserError) $sUserError = $sError;
6 error_log('ERROR: '.$sError);
12 function getLoadAverage()
14 $sLoadAverage = file_get_contents('/proc/loadavg');
15 $aLoadAverage = explode(' ',$sLoadAverage);
16 return (float)$aLoadAverage[0];
20 function getProcessorCount()
22 $sCPU = file_get_contents('/proc/cpuinfo');
23 preg_match_all('#processor\s+: [0-9]+#', $sCPU, $aMatches);
24 return sizeof($aMatches[0]);
28 function getTotalMemoryMB()
30 $sCPU = file_get_contents('/proc/meminfo');
31 preg_match('#MemTotal: +([0-9]+) kB#', $sCPU, $aMatches);
32 return (int)($aMatches[1]/1024);
36 function getCacheMemoryMB()
38 $sCPU = file_get_contents('/proc/meminfo');
39 preg_match('#Cached: +([0-9]+) kB#', $sCPU, $aMatches);
40 return (int)($aMatches[1]/1024);
44 function bySearchRank($a, $b)
46 if ($a['iSearchRank'] == $b['iSearchRank'])
47 return strlen($a['sOperator']) + strlen($a['sHouseNumber']) - strlen($b['sOperator']) - strlen($b['sHouseNumber']);
48 return ($a['iSearchRank'] < $b['iSearchRank']?-1:1);
52 function byImportance($a, $b)
54 if ($a['importance'] != $b['importance'])
55 return ($a['importance'] > $b['importance']?-1:1);
57 return ($a['foundorder'] < $b['foundorder']?-1:1);
61 function getPreferredLanguages($sLangString=false)
65 // If we have been provided the value in $_GET it overrides browser value
66 if (isset($_GET['accept-language']) && $_GET['accept-language'])
68 $_SERVER["HTTP_ACCEPT_LANGUAGE"] = $_GET['accept-language'];
69 $sLangString = $_GET['accept-language'];
71 else if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
73 $sLangString = $_SERVER["HTTP_ACCEPT_LANGUAGE"];
77 $aLanguages = array();
80 if (preg_match_all('/(([a-z]{1,8})(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $sLangString, $aLanguagesParse, PREG_SET_ORDER))
82 foreach($aLanguagesParse as $iLang => $aLanguage)
84 $aLanguages[$aLanguage[1]] = isset($aLanguage[5])?(float)$aLanguage[5]:1 - ($iLang/100);
85 if (!isset($aLanguages[$aLanguage[2]])) $aLanguages[$aLanguage[2]] = $aLanguages[$aLanguage[1]]/10;
90 if (!sizeof($aLanguages) && CONST_Default_Language) $aLanguages = array(CONST_Default_Language=>1);
91 foreach($aLanguages as $sLangauge => $fLangauagePref)
93 $aLangPrefOrder['short_name:'.$sLangauge] = 'short_name:'.$sLangauge;
95 foreach($aLanguages as $sLangauge => $fLangauagePref)
97 $aLangPrefOrder['name:'.$sLangauge] = 'name:'.$sLangauge;
99 foreach($aLanguages as $sLangauge => $fLangauagePref)
101 $aLangPrefOrder['place_name:'.$sLangauge] = 'place_name:'.$sLangauge;
103 foreach($aLanguages as $sLangauge => $fLangauagePref)
105 $aLangPrefOrder['official_name:'.$sLangauge] = 'official_name:'.$sLangauge;
107 $aLangPrefOrder['short_name'] = 'short_name';
108 $aLangPrefOrder['name'] = 'name';
109 $aLangPrefOrder['place_name'] = 'place_name';
110 $aLangPrefOrder['official_name'] = 'official_name';
111 $aLangPrefOrder['ref'] = 'ref';
112 $aLangPrefOrder['type'] = 'type';
113 return $aLangPrefOrder;
117 function getWordSets($aWords, $iDepth)
119 $aResult = array(array(join(' ',$aWords)));
122 while(sizeof($aWords) > 1)
124 $sWord = array_shift($aWords);
125 $sFirstToken .= ($sFirstToken?' ':'').$sWord;
126 $aRest = getWordSets($aWords, $iDepth+1);
127 foreach($aRest as $aSet)
129 $aResult[] = array_merge(array($sFirstToken),$aSet);
136 function getInverseWordSets($aWords, $iDepth)
138 $aResult = array(array(join(' ',$aWords)));
142 while(sizeof($aWords) > 1)
144 $sWord = array_pop($aWords);
145 $sFirstToken = $sWord.($sFirstToken?' ':'').$sFirstToken;
146 $aRest = getInverseWordSets($aWords, $iDepth+1);
147 foreach($aRest as $aSet)
149 $aResult[] = array_merge(array($sFirstToken),$aSet);
157 function getTokensFromSets($aSets)
160 foreach($aSets as $aSet)
162 foreach($aSet as $sWord)
164 $aTokens[' '.$sWord] = ' '.$sWord;
165 $aTokens[$sWord] = $sWord;
173 GB Postcode functions
176 function gbPostcodeCalculate($sPostcode, $sPostcodeSector, $sPostcodeEnd, &$oDB)
178 // Try an exact match on the gb_postcode table
179 $sSQL = 'select \'AA\', ST_X(ST_Centroid(geometry)) as lon,ST_Y(ST_Centroid(geometry)) as lat from gb_postcode where postcode = \''.$sPostcode.'\'';
180 $aNearPostcodes = chksql($oDB->getAll($sSQL));
182 if (sizeof($aNearPostcodes))
184 $aPostcodes = array();
185 foreach($aNearPostcodes as $aPostcode)
187 $aPostcodes[] = array('lat' => $aPostcode['lat'], 'lon' => $aPostcode['lon'], 'radius' => 0.005);
197 function getClassTypes()
200 'boundary:administrative:1' => array('label'=>'Continent','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
201 'boundary:administrative:2' => array('label'=>'Country','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
202 'place:country' => array('label'=>'Country','frequency'=>0,'icon'=>'poi_boundary_administrative','defzoom'=>6, 'defdiameter' => 15,),
203 'boundary:administrative:3' => array('label'=>'State','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
204 'boundary:administrative:4' => array('label'=>'State','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
205 'place:state' => array('label'=>'State','frequency'=>0,'icon'=>'poi_boundary_administrative','defzoom'=>8, 'defdiameter' => 5.12,),
206 'boundary:administrative:5' => array('label'=>'State District','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
207 'boundary:administrative:6' => array('label'=>'County','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
208 'boundary:administrative:7' => array('label'=>'County','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
209 'place:county' => array('label'=>'County','frequency'=>108,'icon'=>'poi_boundary_administrative','defzoom'=>10, 'defdiameter' => 1.28,),
210 'boundary:administrative:8' => array('label'=>'City','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
211 'place:city' => array('label'=>'City','frequency'=>66,'icon'=>'poi_place_city','defzoom'=>12, 'defdiameter' => 0.32,),
212 'boundary:administrative:9' => array('label'=>'City District','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
213 'boundary:administrative:10' => array('label'=>'Suburb','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
214 'boundary:administrative:11' => array('label'=>'Neighbourhood','frequency'=>0,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
215 'place:region' => array('label'=>'Region','frequency'=>0,'icon'=>'poi_boundary_administrative','defzoom'=>8, 'defdiameter' => 0.04,),
216 'place:island' => array('label'=>'Island','frequency'=>288,'icon'=>'','defzoom'=>11, 'defdiameter' => 0.64,),
217 'boundary:administrative' => array('label'=>'Administrative','frequency'=>413,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
218 'boundary:postal_code' => array('label'=>'Postcode','frequency'=>413,'icon'=>'poi_boundary_administrative', 'defdiameter' => 0.32,),
219 'place:town' => array('label'=>'Town','frequency'=>1497,'icon'=>'poi_place_town','defzoom'=>14, 'defdiameter' => 0.08,),
220 'place:village' => array('label'=>'Village','frequency'=>11230,'icon'=>'poi_place_village','defzoom'=>15, 'defdiameter' => 0.04,),
221 'place:hamlet' => array('label'=>'Hamlet','frequency'=>7075,'icon'=>'poi_place_village','defzoom'=>15, 'defdiameter' => 0.04,),
222 'place:suburb' => array('label'=>'Suburb','frequency'=>2528,'icon'=>'poi_place_village', 'defdiameter' => 0.04,),
223 'place:locality' => array('label'=>'Locality','frequency'=>4113,'icon'=>'poi_place_village', 'defdiameter' => 0.02,),
224 'landuse:farm' => array('label'=>'Farm','frequency'=>1201,'icon'=>'', 'defdiameter' => 0.02,),
225 'place:farm' => array('label'=>'Farm','frequency'=>1162,'icon'=>'', 'defdiameter' => 0.02,),
227 'highway:motorway_junction' => array('label'=>'Motorway Junction','frequency'=>1126,'icon'=>'','simplelabel'=>'Junction',),
228 'highway:motorway' => array('label'=>'Motorway','frequency'=>4627,'icon'=>'','simplelabel'=>'Road',),
229 'highway:trunk' => array('label'=>'Trunk','frequency'=>23084,'icon'=>'','simplelabel'=>'Road',),
230 'highway:primary' => array('label'=>'Primary','frequency'=>32138,'icon'=>'','simplelabel'=>'Road',),
231 'highway:secondary' => array('label'=>'Secondary','frequency'=>25807,'icon'=>'','simplelabel'=>'Road',),
232 'highway:tertiary' => array('label'=>'Tertiary','frequency'=>29829,'icon'=>'','simplelabel'=>'Road',),
233 'highway:residential' => array('label'=>'Residential','frequency'=>361498,'icon'=>'','simplelabel'=>'Road',),
234 'highway:unclassified' => array('label'=>'Unclassified','frequency'=>66441,'icon'=>'','simplelabel'=>'Road',),
235 'highway:living_street' => array('label'=>'Living Street','frequency'=>710,'icon'=>'','simplelabel'=>'Road',),
236 'highway:service' => array('label'=>'Service','frequency'=>9963,'icon'=>'','simplelabel'=>'Road',),
237 'highway:track' => array('label'=>'Track','frequency'=>2565,'icon'=>'','simplelabel'=>'Road',),
238 'highway:road' => array('label'=>'Road','frequency'=>591,'icon'=>'','simplelabel'=>'Road',),
239 'highway:byway' => array('label'=>'Byway','frequency'=>346,'icon'=>'','simplelabel'=>'Road',),
240 'highway:bridleway' => array('label'=>'Bridleway','frequency'=>1556,'icon'=>'',),
241 'highway:cycleway' => array('label'=>'Cycleway','frequency'=>2419,'icon'=>'',),
242 'highway:pedestrian' => array('label'=>'Pedestrian','frequency'=>2757,'icon'=>'',),
243 'highway:footway' => array('label'=>'Footway','frequency'=>15008,'icon'=>'',),
244 'highway:steps' => array('label'=>'Steps','frequency'=>444,'icon'=>'','simplelabel'=>'Footway',),
245 'highway:motorway_link' => array('label'=>'Motorway Link','frequency'=>795,'icon'=>'','simplelabel'=>'Road',),
246 'highway:trunk_link' => array('label'=>'Trunk Link','frequency'=>1258,'icon'=>'','simplelabel'=>'Road',),
247 'highway:primary_link' => array('label'=>'Primary Link','frequency'=>313,'icon'=>'','simplelabel'=>'Road',),
249 'landuse:industrial' => array('label'=>'Industrial','frequency'=>1062,'icon'=>'',),
250 'landuse:residential' => array('label'=>'Residential','frequency'=>886,'icon'=>'',),
251 'landuse:retail' => array('label'=>'Retail','frequency'=>754,'icon'=>'',),
252 'landuse:commercial' => array('label'=>'Commercial','frequency'=>657,'icon'=>'',),
254 'place:airport' => array('label'=>'Airport','frequency'=>36,'icon'=>'transport_airport2', 'defdiameter' => 0.03,),
255 'aeroway:aerodrome' => array('label'=>'Aerodrome','frequency'=>36,'icon'=>'transport_airport2', 'defdiameter' => 0.03,),
256 'aeroway' => array('label'=>'Aeroway','frequency'=>36,'icon'=>'transport_airport2', 'defdiameter' => 0.03,),
257 'railway:station' => array('label'=>'Station','frequency'=>3431,'icon'=>'transport_train_station2', 'defdiameter' => 0.01,),
258 'amenity:place_of_worship' => array('label'=>'Place Of Worship','frequency'=>9049,'icon'=>'place_of_worship_unknown3',),
259 'amenity:pub' => array('label'=>'Pub','frequency'=>18969,'icon'=>'food_pub',),
260 'amenity:bar' => array('label'=>'Bar','frequency'=>164,'icon'=>'food_bar',),
261 'amenity:university' => array('label'=>'University','frequency'=>607,'icon'=>'education_university',),
262 'tourism:museum' => array('label'=>'Museum','frequency'=>543,'icon'=>'tourist_museum',),
263 'amenity:arts_centre' => array('label'=>'Arts Centre','frequency'=>136,'icon'=>'tourist_art_gallery2',),
264 'tourism:zoo' => array('label'=>'Zoo','frequency'=>47,'icon'=>'tourist_zoo',),
265 'tourism:theme_park' => array('label'=>'Theme Park','frequency'=>24,'icon'=>'poi_point_of_interest',),
266 'tourism:attraction' => array('label'=>'Attraction','frequency'=>1463,'icon'=>'poi_point_of_interest',),
267 'leisure:golf_course' => array('label'=>'Golf Course','frequency'=>712,'icon'=>'sport_golf',),
268 'historic:castle' => array('label'=>'Castle','frequency'=>316,'icon'=>'tourist_castle',),
269 'amenity:hospital' => array('label'=>'Hospital','frequency'=>879,'icon'=>'health_hospital',),
270 'amenity:school' => array('label'=>'School','frequency'=>8192,'icon'=>'education_school',),
271 'amenity:theatre' => array('label'=>'Theatre','frequency'=>371,'icon'=>'tourist_theatre',),
272 'amenity:public_building' => array('label'=>'Public Building','frequency'=>985,'icon'=>'',),
273 'amenity:library' => array('label'=>'Library','frequency'=>794,'icon'=>'amenity_library',),
274 'amenity:townhall' => array('label'=>'Townhall','frequency'=>242,'icon'=>'',),
275 'amenity:community_centre' => array('label'=>'Community Centre','frequency'=>157,'icon'=>'',),
276 'amenity:fire_station' => array('label'=>'Fire Station','frequency'=>221,'icon'=>'amenity_firestation3',),
277 'amenity:police' => array('label'=>'Police','frequency'=>334,'icon'=>'amenity_police2',),
278 'amenity:bank' => array('label'=>'Bank','frequency'=>1248,'icon'=>'money_bank2',),
279 'amenity:post_office' => array('label'=>'Post Office','frequency'=>859,'icon'=>'amenity_post_office',),
280 'leisure:park' => array('label'=>'Park','frequency'=>2378,'icon'=>'',),
281 'amenity:park' => array('label'=>'Park','frequency'=>53,'icon'=>'',),
282 'landuse:park' => array('label'=>'Park','frequency'=>50,'icon'=>'',),
283 'landuse:recreation_ground' => array('label'=>'Recreation Ground','frequency'=>517,'icon'=>'',),
284 'tourism:hotel' => array('label'=>'Hotel','frequency'=>2150,'icon'=>'accommodation_hotel2',),
285 'tourism:motel' => array('label'=>'Motel','frequency'=>43,'icon'=>'',),
286 'amenity:cinema' => array('label'=>'Cinema','frequency'=>277,'icon'=>'tourist_cinema',),
287 'tourism:artwork' => array('label'=>'Artwork','frequency'=>171,'icon'=>'tourist_art_gallery2',),
288 'historic:archaeological_site' => array('label'=>'Archaeological Site','frequency'=>407,'icon'=>'tourist_archaeological2',),
289 'amenity:doctors' => array('label'=>'Doctors','frequency'=>581,'icon'=>'health_doctors',),
290 'leisure:sports_centre' => array('label'=>'Sports Centre','frequency'=>767,'icon'=>'sport_leisure_centre',),
291 'leisure:swimming_pool' => array('label'=>'Swimming Pool','frequency'=>24,'icon'=>'sport_swimming_outdoor',),
292 'shop:supermarket' => array('label'=>'Supermarket','frequency'=>2673,'icon'=>'shopping_supermarket',),
293 'shop:convenience' => array('label'=>'Convenience','frequency'=>1469,'icon'=>'shopping_convenience',),
294 'amenity:restaurant' => array('label'=>'Restaurant','frequency'=>3179,'icon'=>'food_restaurant',),
295 'amenity:fast_food' => array('label'=>'Fast Food','frequency'=>2289,'icon'=>'food_fastfood',),
296 'amenity:cafe' => array('label'=>'Cafe','frequency'=>1780,'icon'=>'food_cafe',),
297 'tourism:guest_house' => array('label'=>'Guest House','frequency'=>223,'icon'=>'accommodation_bed_and_breakfast',),
298 'amenity:pharmacy' => array('label'=>'Pharmacy','frequency'=>733,'icon'=>'health_pharmacy_dispensing',),
299 'amenity:fuel' => array('label'=>'Fuel','frequency'=>1308,'icon'=>'transport_fuel',),
300 'natural:peak' => array('label'=>'Peak','frequency'=>3212,'icon'=>'poi_peak',),
301 'waterway:waterfall' => array('label'=>'Waterfall','frequency'=>24,'icon'=>'',),
302 'natural:wood' => array('label'=>'Wood','frequency'=>1845,'icon'=>'landuse_coniferous_and_deciduous',),
303 'natural:water' => array('label'=>'Water','frequency'=>1790,'icon'=>'',),
304 'landuse:forest' => array('label'=>'Forest','frequency'=>467,'icon'=>'',),
305 'landuse:cemetery' => array('label'=>'Cemetery','frequency'=>463,'icon'=>'',),
306 'landuse:allotments' => array('label'=>'Allotments','frequency'=>408,'icon'=>'',),
307 'landuse:farmyard' => array('label'=>'Farmyard','frequency'=>397,'icon'=>'',),
308 'railway:rail' => array('label'=>'Rail','frequency'=>4894,'icon'=>'',),
309 'waterway:canal' => array('label'=>'Canal','frequency'=>1723,'icon'=>'',),
310 'waterway:river' => array('label'=>'River','frequency'=>4089,'icon'=>'',),
311 'waterway:stream' => array('label'=>'Stream','frequency'=>2684,'icon'=>'',),
312 'shop:bicycle' => array('label'=>'Bicycle','frequency'=>349,'icon'=>'shopping_bicycle',),
313 'shop:clothes' => array('label'=>'Clothes','frequency'=>315,'icon'=>'shopping_clothes',),
314 'shop:hairdresser' => array('label'=>'Hairdresser','frequency'=>312,'icon'=>'shopping_hairdresser',),
315 'shop:doityourself' => array('label'=>'Doityourself','frequency'=>247,'icon'=>'shopping_diy',),
316 'shop:estate_agent' => array('label'=>'Estate Agent','frequency'=>162,'icon'=>'shopping_estateagent2',),
317 'shop:car' => array('label'=>'Car','frequency'=>159,'icon'=>'shopping_car',),
318 'shop:garden_centre' => array('label'=>'Garden Centre','frequency'=>143,'icon'=>'shopping_garden_centre',),
319 'shop:car_repair' => array('label'=>'Car Repair','frequency'=>141,'icon'=>'shopping_car_repair',),
320 'shop:newsagent' => array('label'=>'Newsagent','frequency'=>132,'icon'=>'',),
321 'shop:bakery' => array('label'=>'Bakery','frequency'=>129,'icon'=>'shopping_bakery',),
322 'shop:furniture' => array('label'=>'Furniture','frequency'=>124,'icon'=>'',),
323 'shop:butcher' => array('label'=>'Butcher','frequency'=>105,'icon'=>'shopping_butcher',),
324 'shop:apparel' => array('label'=>'Apparel','frequency'=>98,'icon'=>'shopping_clothes',),
325 'shop:electronics' => array('label'=>'Electronics','frequency'=>96,'icon'=>'',),
326 'shop:department_store' => array('label'=>'Department Store','frequency'=>86,'icon'=>'',),
327 'shop:books' => array('label'=>'Books','frequency'=>85,'icon'=>'',),
328 'shop:yes' => array('label'=>'Shop','frequency'=>68,'icon'=>'',),
329 'shop:outdoor' => array('label'=>'Outdoor','frequency'=>67,'icon'=>'',),
330 'shop:mall' => array('label'=>'Mall','frequency'=>63,'icon'=>'',),
331 'shop:florist' => array('label'=>'Florist','frequency'=>61,'icon'=>'',),
332 'shop:charity' => array('label'=>'Charity','frequency'=>60,'icon'=>'',),
333 'shop:hardware' => array('label'=>'Hardware','frequency'=>59,'icon'=>'',),
334 'shop:laundry' => array('label'=>'Laundry','frequency'=>51,'icon'=>'shopping_laundrette',),
335 'shop:shoes' => array('label'=>'Shoes','frequency'=>49,'icon'=>'',),
336 'shop:beverages' => array('label'=>'Beverages','frequency'=>48,'icon'=>'shopping_alcohol',),
337 'shop:dry_cleaning' => array('label'=>'Dry Cleaning','frequency'=>46,'icon'=>'',),
338 'shop:carpet' => array('label'=>'Carpet','frequency'=>45,'icon'=>'',),
339 'shop:computer' => array('label'=>'Computer','frequency'=>44,'icon'=>'',),
340 'shop:alcohol' => array('label'=>'Alcohol','frequency'=>44,'icon'=>'shopping_alcohol',),
341 'shop:optician' => array('label'=>'Optician','frequency'=>55,'icon'=>'health_opticians',),
342 'shop:chemist' => array('label'=>'Chemist','frequency'=>42,'icon'=>'health_pharmacy',),
343 'shop:gallery' => array('label'=>'Gallery','frequency'=>38,'icon'=>'tourist_art_gallery2',),
344 'shop:mobile_phone' => array('label'=>'Mobile Phone','frequency'=>37,'icon'=>'',),
345 'shop:sports' => array('label'=>'Sports','frequency'=>37,'icon'=>'',),
346 'shop:jewelry' => array('label'=>'Jewelry','frequency'=>32,'icon'=>'shopping_jewelry',),
347 'shop:pet' => array('label'=>'Pet','frequency'=>29,'icon'=>'',),
348 'shop:beauty' => array('label'=>'Beauty','frequency'=>28,'icon'=>'',),
349 'shop:stationery' => array('label'=>'Stationery','frequency'=>25,'icon'=>'',),
350 'shop:shopping_centre' => array('label'=>'Shopping Centre','frequency'=>25,'icon'=>'',),
351 'shop:general' => array('label'=>'General','frequency'=>25,'icon'=>'',),
352 'shop:electrical' => array('label'=>'Electrical','frequency'=>25,'icon'=>'',),
353 'shop:toys' => array('label'=>'Toys','frequency'=>23,'icon'=>'',),
354 'shop:jeweller' => array('label'=>'Jeweller','frequency'=>23,'icon'=>'',),
355 'shop:betting' => array('label'=>'Betting','frequency'=>23,'icon'=>'',),
356 'shop:household' => array('label'=>'Household','frequency'=>21,'icon'=>'',),
357 'shop:travel_agency' => array('label'=>'Travel Agency','frequency'=>21,'icon'=>'',),
358 'shop:hifi' => array('label'=>'Hifi','frequency'=>21,'icon'=>'',),
359 'amenity:shop' => array('label'=>'Shop','frequency'=>61,'icon'=>'',),
360 'tourism:information' => array('label'=>'Information','frequency'=>224,'icon'=>'amenity_information',),
362 'place:house' => array('label'=>'House','frequency'=>2086,'icon'=>'','defzoom'=>18,),
363 'place:house_name' => array('label'=>'House','frequency'=>2086,'icon'=>'','defzoom'=>18,),
364 'place:house_number' => array('label'=>'House Number','frequency'=>2086,'icon'=>'','defzoom'=>18,),
365 'place:country_code' => array('label'=>'Country Code','frequency'=>2086,'icon'=>'','defzoom'=>18,),
369 'leisure:pitch' => array('label'=>'Pitch','frequency'=>762,'icon'=>'',),
370 'highway:unsurfaced' => array('label'=>'Unsurfaced','frequency'=>492,'icon'=>'',),
371 'historic:ruins' => array('label'=>'Ruins','frequency'=>483,'icon'=>'tourist_ruin',),
372 'amenity:college' => array('label'=>'College','frequency'=>473,'icon'=>'education_school',),
373 'historic:monument' => array('label'=>'Monument','frequency'=>470,'icon'=>'tourist_monument',),
374 'railway:subway' => array('label'=>'Subway','frequency'=>385,'icon'=>'',),
375 'historic:memorial' => array('label'=>'Memorial','frequency'=>382,'icon'=>'tourist_monument',),
376 'leisure:nature_reserve' => array('label'=>'Nature Reserve','frequency'=>342,'icon'=>'',),
377 'leisure:common' => array('label'=>'Common','frequency'=>322,'icon'=>'',),
378 'waterway:lock_gate' => array('label'=>'Lock Gate','frequency'=>321,'icon'=>'',),
379 'natural:fell' => array('label'=>'Fell','frequency'=>308,'icon'=>'',),
380 'amenity:nightclub' => array('label'=>'Nightclub','frequency'=>292,'icon'=>'',),
381 'highway:path' => array('label'=>'Path','frequency'=>287,'icon'=>'',),
382 'leisure:garden' => array('label'=>'Garden','frequency'=>285,'icon'=>'',),
383 'landuse:reservoir' => array('label'=>'Reservoir','frequency'=>276,'icon'=>'',),
384 'leisure:playground' => array('label'=>'Playground','frequency'=>264,'icon'=>'',),
385 'leisure:stadium' => array('label'=>'Stadium','frequency'=>212,'icon'=>'',),
386 'historic:mine' => array('label'=>'Mine','frequency'=>193,'icon'=>'poi_mine',),
387 'natural:cliff' => array('label'=>'Cliff','frequency'=>193,'icon'=>'',),
388 'tourism:caravan_site' => array('label'=>'Caravan Site','frequency'=>183,'icon'=>'accommodation_caravan_park',),
389 'amenity:bus_station' => array('label'=>'Bus Station','frequency'=>181,'icon'=>'transport_bus_station',),
390 'amenity:kindergarten' => array('label'=>'Kindergarten','frequency'=>179,'icon'=>'',),
391 'highway:construction' => array('label'=>'Construction','frequency'=>176,'icon'=>'',),
392 'amenity:atm' => array('label'=>'Atm','frequency'=>172,'icon'=>'money_atm2',),
393 'amenity:emergency_phone' => array('label'=>'Emergency Phone','frequency'=>164,'icon'=>'',),
394 'waterway:lock' => array('label'=>'Lock','frequency'=>146,'icon'=>'',),
395 'waterway:riverbank' => array('label'=>'Riverbank','frequency'=>143,'icon'=>'',),
396 'natural:coastline' => array('label'=>'Coastline','frequency'=>142,'icon'=>'',),
397 'tourism:viewpoint' => array('label'=>'Viewpoint','frequency'=>140,'icon'=>'tourist_view_point',),
398 'tourism:hostel' => array('label'=>'Hostel','frequency'=>140,'icon'=>'',),
399 'tourism:bed_and_breakfast' => array('label'=>'Bed And Breakfast','frequency'=>140,'icon'=>'accommodation_bed_and_breakfast',),
400 'railway:halt' => array('label'=>'Halt','frequency'=>135,'icon'=>'',),
401 'railway:platform' => array('label'=>'Platform','frequency'=>134,'icon'=>'',),
402 'railway:tram' => array('label'=>'Tram','frequency'=>130,'icon'=>'transport_tram_stop',),
403 'amenity:courthouse' => array('label'=>'Courthouse','frequency'=>129,'icon'=>'amenity_court',),
404 'amenity:recycling' => array('label'=>'Recycling','frequency'=>126,'icon'=>'amenity_recycling',),
405 'amenity:dentist' => array('label'=>'Dentist','frequency'=>124,'icon'=>'health_dentist',),
406 'natural:beach' => array('label'=>'Beach','frequency'=>121,'icon'=>'tourist_beach',),
407 'place:moor' => array('label'=>'Moor','frequency'=>118,'icon'=>'',),
408 'amenity:grave_yard' => array('label'=>'Grave Yard','frequency'=>110,'icon'=>'',),
409 'waterway:drain' => array('label'=>'Drain','frequency'=>108,'icon'=>'',),
410 'landuse:grass' => array('label'=>'Grass','frequency'=>106,'icon'=>'',),
411 'landuse:village_green' => array('label'=>'Village Green','frequency'=>106,'icon'=>'',),
412 'natural:bay' => array('label'=>'Bay','frequency'=>102,'icon'=>'',),
413 'railway:tram_stop' => array('label'=>'Tram Stop','frequency'=>101,'icon'=>'transport_tram_stop',),
414 'leisure:marina' => array('label'=>'Marina','frequency'=>98,'icon'=>'',),
415 'highway:stile' => array('label'=>'Stile','frequency'=>97,'icon'=>'',),
416 'natural:moor' => array('label'=>'Moor','frequency'=>95,'icon'=>'',),
417 'railway:light_rail' => array('label'=>'Light Rail','frequency'=>91,'icon'=>'',),
418 'railway:narrow_gauge' => array('label'=>'Narrow Gauge','frequency'=>90,'icon'=>'',),
419 'natural:land' => array('label'=>'Land','frequency'=>86,'icon'=>'',),
420 'amenity:village_hall' => array('label'=>'Village Hall','frequency'=>82,'icon'=>'',),
421 'waterway:dock' => array('label'=>'Dock','frequency'=>80,'icon'=>'',),
422 'amenity:veterinary' => array('label'=>'Veterinary','frequency'=>79,'icon'=>'',),
423 'landuse:brownfield' => array('label'=>'Brownfield','frequency'=>77,'icon'=>'',),
424 'leisure:track' => array('label'=>'Track','frequency'=>76,'icon'=>'',),
425 'railway:historic_station' => array('label'=>'Historic Station','frequency'=>74,'icon'=>'',),
426 'landuse:construction' => array('label'=>'Construction','frequency'=>72,'icon'=>'',),
427 'amenity:prison' => array('label'=>'Prison','frequency'=>71,'icon'=>'amenity_prison',),
428 'landuse:quarry' => array('label'=>'Quarry','frequency'=>71,'icon'=>'',),
429 'amenity:telephone' => array('label'=>'Telephone','frequency'=>70,'icon'=>'',),
430 'highway:traffic_signals' => array('label'=>'Traffic Signals','frequency'=>66,'icon'=>'',),
431 'natural:heath' => array('label'=>'Heath','frequency'=>62,'icon'=>'',),
432 'historic:house' => array('label'=>'House','frequency'=>61,'icon'=>'',),
433 'amenity:social_club' => array('label'=>'Social Club','frequency'=>61,'icon'=>'',),
434 'landuse:military' => array('label'=>'Military','frequency'=>61,'icon'=>'',),
435 'amenity:health_centre' => array('label'=>'Health Centre','frequency'=>59,'icon'=>'',),
436 'historic:building' => array('label'=>'Building','frequency'=>58,'icon'=>'',),
437 'amenity:clinic' => array('label'=>'Clinic','frequency'=>57,'icon'=>'',),
438 'highway:services' => array('label'=>'Services','frequency'=>56,'icon'=>'',),
439 'amenity:ferry_terminal' => array('label'=>'Ferry Terminal','frequency'=>55,'icon'=>'',),
440 'natural:marsh' => array('label'=>'Marsh','frequency'=>55,'icon'=>'',),
441 'natural:hill' => array('label'=>'Hill','frequency'=>54,'icon'=>'',),
442 'highway:raceway' => array('label'=>'Raceway','frequency'=>53,'icon'=>'',),
443 'amenity:taxi' => array('label'=>'Taxi','frequency'=>47,'icon'=>'',),
444 'amenity:take_away' => array('label'=>'Take Away','frequency'=>45,'icon'=>'',),
445 'amenity:car_rental' => array('label'=>'Car Rental','frequency'=>44,'icon'=>'',),
446 'place:islet' => array('label'=>'Islet','frequency'=>44,'icon'=>'',),
447 'amenity:nursery' => array('label'=>'Nursery','frequency'=>44,'icon'=>'',),
448 'amenity:nursing_home' => array('label'=>'Nursing Home','frequency'=>43,'icon'=>'',),
449 'amenity:toilets' => array('label'=>'Toilets','frequency'=>38,'icon'=>'',),
450 'amenity:hall' => array('label'=>'Hall','frequency'=>38,'icon'=>'',),
451 'waterway:boatyard' => array('label'=>'Boatyard','frequency'=>36,'icon'=>'',),
452 'highway:mini_roundabout' => array('label'=>'Mini Roundabout','frequency'=>35,'icon'=>'',),
453 'historic:manor' => array('label'=>'Manor','frequency'=>35,'icon'=>'',),
454 'tourism:chalet' => array('label'=>'Chalet','frequency'=>34,'icon'=>'',),
455 'amenity:bicycle_parking' => array('label'=>'Bicycle Parking','frequency'=>34,'icon'=>'',),
456 'amenity:hotel' => array('label'=>'Hotel','frequency'=>34,'icon'=>'',),
457 'waterway:weir' => array('label'=>'Weir','frequency'=>33,'icon'=>'',),
458 'natural:wetland' => array('label'=>'Wetland','frequency'=>33,'icon'=>'',),
459 'natural:cave_entrance' => array('label'=>'Cave Entrance','frequency'=>32,'icon'=>'',),
460 'amenity:crematorium' => array('label'=>'Crematorium','frequency'=>31,'icon'=>'',),
461 'tourism:picnic_site' => array('label'=>'Picnic Site','frequency'=>31,'icon'=>'',),
462 'landuse:wood' => array('label'=>'Wood','frequency'=>30,'icon'=>'',),
463 'landuse:basin' => array('label'=>'Basin','frequency'=>30,'icon'=>'',),
464 'natural:tree' => array('label'=>'Tree','frequency'=>30,'icon'=>'',),
465 'leisure:slipway' => array('label'=>'Slipway','frequency'=>29,'icon'=>'',),
466 'landuse:meadow' => array('label'=>'Meadow','frequency'=>29,'icon'=>'',),
467 'landuse:piste' => array('label'=>'Piste','frequency'=>28,'icon'=>'',),
468 'amenity:care_home' => array('label'=>'Care Home','frequency'=>28,'icon'=>'',),
469 'amenity:club' => array('label'=>'Club','frequency'=>28,'icon'=>'',),
470 'amenity:medical_centre' => array('label'=>'Medical Centre','frequency'=>27,'icon'=>'',),
471 'historic:roman_road' => array('label'=>'Roman Road','frequency'=>27,'icon'=>'',),
472 'historic:fort' => array('label'=>'Fort','frequency'=>26,'icon'=>'',),
473 'railway:subway_entrance' => array('label'=>'Subway Entrance','frequency'=>26,'icon'=>'',),
474 'historic:yes' => array('label'=>'Historic','frequency'=>25,'icon'=>'',),
475 'highway:gate' => array('label'=>'Gate','frequency'=>25,'icon'=>'',),
476 'leisure:fishing' => array('label'=>'Fishing','frequency'=>24,'icon'=>'',),
477 'historic:museum' => array('label'=>'Museum','frequency'=>24,'icon'=>'',),
478 'amenity:car_wash' => array('label'=>'Car Wash','frequency'=>24,'icon'=>'',),
479 'railway:level_crossing' => array('label'=>'Level Crossing','frequency'=>23,'icon'=>'',),
480 'leisure:bird_hide' => array('label'=>'Bird Hide','frequency'=>23,'icon'=>'',),
481 'natural:headland' => array('label'=>'Headland','frequency'=>21,'icon'=>'',),
482 'tourism:apartments' => array('label'=>'Apartments','frequency'=>21,'icon'=>'',),
483 'amenity:shopping' => array('label'=>'Shopping','frequency'=>21,'icon'=>'',),
484 'natural:scrub' => array('label'=>'Scrub','frequency'=>20,'icon'=>'',),
485 'natural:fen' => array('label'=>'Fen','frequency'=>20,'icon'=>'',),
486 'building:yes' => array('label'=>'Building','frequency'=>200,'icon'=>'',),
487 'mountain_pass:yes' => array('label'=>'Mountain Pass','frequency'=>200,'icon'=>'',),
489 'amenity:parking' => array('label'=>'Parking','frequency'=>3157,'icon'=>'',),
490 'highway:bus_stop' => array('label'=>'Bus Stop','frequency'=>35777,'icon'=>'transport_bus_stop2',),
491 'place:postcode' => array('label'=>'Postcode','frequency'=>27267,'icon'=>'',),
492 'amenity:post_box' => array('label'=>'Post Box','frequency'=>9613,'icon'=>'',),
494 'place:houses' => array('label'=>'Houses','frequency'=>85,'icon'=>'',),
495 'railway:preserved' => array('label'=>'Preserved','frequency'=>227,'icon'=>'',),
496 'waterway:derelict_canal' => array('label'=>'Derelict Canal','frequency'=>21,'icon'=>'',),
497 'amenity:dead_pub' => array('label'=>'Dead Pub','frequency'=>20,'icon'=>'',),
498 'railway:disused_station' => array('label'=>'Disused Station','frequency'=>114,'icon'=>'',),
499 'railway:abandoned' => array('label'=>'Abandoned','frequency'=>641,'icon'=>'',),
500 'railway:disused' => array('label'=>'Disused','frequency'=>72,'icon'=>'',),
505 function getClassTypesWithImportance()
507 $aOrders = getClassTypes();
509 foreach($aOrders as $sID => $a)
511 $aOrders[$sID]['importance'] = $i++;
516 function getResultDiameter($aResult)
518 $aClassType = getClassTypes();
522 if (isset($aResult['class'])
523 && isset($aResult['type'])
524 && isset($aResult['admin_level'])
525 && isset($aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
526 && $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'])
528 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type'].':'.$aResult['admin_level']]['defdiameter'];
530 elseif (isset($aResult['class'])
531 && isset($aResult['type'])
532 && isset($aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
533 && $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'])
535 $fDiameter = $aClassType[$aResult['class'].':'.$aResult['type']]['defdiameter'];
542 function javascript_renderData($xVal, $iOptions = 0)
544 if (defined('PHP_VERSION_ID') && PHP_VERSION_ID > 50400)
545 $iOptions |= JSON_UNESCAPED_UNICODE;
546 $jsonout = json_encode($xVal, $iOptions);
548 if( ! isset($_GET['json_callback']))
550 header("Content-Type: application/json; charset=UTF-8");
554 if (preg_match('/^[$_\p{L}][$_\p{L}\p{Nd}.[\]]*$/u',$_GET['json_callback']))
556 header("Content-Type: application/javascript; charset=UTF-8");
557 echo $_GET['json_callback'].'('.$jsonout.')';
561 header('HTTP/1.0 400 Bad Request');
567 function _debugDumpGroupedSearches($aData, $aTokens)
569 $aWordsIDs = array();
572 foreach($aTokens as $sToken => $aWords)
576 foreach($aWords as $aToken)
578 $aWordsIDs[$aToken['word_id']] = $sToken.'('.$aToken['word_id'].')';
583 echo "<table border=\"1\">";
584 echo "<tr><th>rank</th><th>Name Tokens</th><th>Name Not</th><th>Address Tokens</th><th>Address Not</th><th>country</th><th>operator</th><th>class</th><th>type</th><th>house#</th><th>Lat</th><th>Lon</th><th>Radius</th></tr>";
585 foreach($aData as $iRank => $aRankedSet)
587 foreach($aRankedSet as $aRow)
590 echo "<td>$iRank</td>";
594 foreach($aRow['aName'] as $iWordID)
596 echo $sSep.'#'.$aWordsIDs[$iWordID].'#';
603 foreach($aRow['aNameNonSearch'] as $iWordID)
605 echo $sSep.'#'.$aWordsIDs[$iWordID].'#';
612 foreach($aRow['aAddress'] as $iWordID)
614 echo $sSep.'#'.$aWordsIDs[$iWordID].'#';
621 foreach($aRow['aAddressNonSearch'] as $iWordID)
623 echo $sSep.'#'.$aWordsIDs[$iWordID].'#';
628 echo "<td>".$aRow['sCountryCode']."</td>";
630 echo "<td>".$aRow['sOperator']."</td>";
631 echo "<td>".$aRow['sClass']."</td>";
632 echo "<td>".$aRow['sType']."</td>";
634 echo "<td>".$aRow['sHouseNumber']."</td>";
636 echo "<td>".$aRow['fLat']."</td>";
637 echo "<td>".$aRow['fLon']."</td>";
638 echo "<td>".$aRow['fRadius']."</td>";
647 function getAddressDetails(&$oDB, $sLanguagePrefArraySQL, $iPlaceID, $sCountryCode = false, $housenumber = -1, $bRaw = false)
649 $sSQL = "select *,get_name_by_language(name,$sLanguagePrefArraySQL) as localname from get_addressdata($iPlaceID, $housenumber)";
650 if (!$bRaw) $sSQL .= " WHERE isaddress OR type = 'country_code'";
651 $sSQL .= " order by rank_address desc,isaddress desc";
653 $aAddressLines = chksql($oDB->getAll($sSQL));
654 if ($bRaw) return $aAddressLines;
656 //var_dump($aAddressLines);
658 $aFallback = array();
659 $aClassType = getClassTypes();
660 foreach($aAddressLines as $aLine)
664 if (isset($aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type'].':'.$aLine['admin_level']];
665 elseif (isset($aClassType[$aLine['class'].':'.$aLine['type']])) $aTypeLabel = $aClassType[$aLine['class'].':'.$aLine['type']];
666 elseif (isset($aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))]))
668 $aTypeLabel = $aClassType['boundary:administrative:'.((int)($aLine['rank_address']/2))];
673 $aTypeLabel = array('simplelabel'=>'address'.$aLine['rank_address']);
676 if ($aTypeLabel && ((isset($aLine['localname']) && $aLine['localname']) || (isset($aLine['housenumber']) && $aLine['housenumber'])))
678 $sTypeLabel = strtolower(isset($aTypeLabel['simplelabel'])?$aTypeLabel['simplelabel']:$aTypeLabel['label']);
679 $sTypeLabel = str_replace(' ','_',$sTypeLabel);
680 if (!isset($aAddress[$sTypeLabel]) || (isset($aFallback[$sTypeLabel]) && $aFallback[$sTypeLabel]) || $aLine['class'] == 'place')
682 $aAddress[$sTypeLabel] = $aLine['localname']?$aLine['localname']:$aLine['housenumber'];
684 $aFallback[$sTypeLabel] = $bFallback;
691 function geocodeReverse($fLat, $fLon, $iZoom=18)
695 $sPointSQL = "ST_SetSRID(ST_Point($fLon,$fLat),4326)";
697 // Zoom to rank, this could probably be calculated but a lookup gives fine control
699 0 => 2, // Continent / Sea
711 12 => 18, // Town / Village
715 16 => 26, // Street, TODO: major street?
717 18 => 30, // or >, Building
718 19 => 30, // or >, Building
720 $iMaxRank = isset($aZoomRank[$iZoom])?$aZoomRank[$iZoom]:28;
722 // Find the nearest point
723 $fSearchDiam = 0.0001;
726 $fMaxAreaDistance = 1;
727 while(!$iPlaceID && $fSearchDiam < $fMaxAreaDistance)
729 $fSearchDiam = $fSearchDiam * 2;
731 // If we have to expand the search area by a large amount then we need a larger feature
732 // then there is a limit to how small the feature should be
733 if ($fSearchDiam > 2 && $iMaxRank > 4) $iMaxRank = 4;
734 if ($fSearchDiam > 1 && $iMaxRank > 9) $iMaxRank = 8;
735 if ($fSearchDiam > 0.8 && $iMaxRank > 10) $iMaxRank = 10;
736 if ($fSearchDiam > 0.6 && $iMaxRank > 12) $iMaxRank = 12;
737 if ($fSearchDiam > 0.2 && $iMaxRank > 17) $iMaxRank = 17;
738 if ($fSearchDiam > 0.1 && $iMaxRank > 18) $iMaxRank = 18;
739 if ($fSearchDiam > 0.008 && $iMaxRank > 22) $iMaxRank = 22;
740 if ($fSearchDiam > 0.001 && $iMaxRank > 26) $iMaxRank = 26;
742 $sSQL = 'select place_id,parent_place_id from placex';
743 $sSQL .= ' WHERE ST_DWithin('.$sPointSQL.', geometry, '.$fSearchDiam.')';
744 $sSQL .= ' and rank_search != 28 and rank_search >= '.$iMaxRank;
745 $sSQL .= ' and (name is not null or housenumber is not null)';
746 $sSQL .= ' and class not in (\'waterway\')';
747 $sSQL .= ' and (ST_GeometryType(geometry) not in (\'ST_Polygon\',\'ST_MultiPolygon\') ';
748 $sSQL .= ' OR ST_DWithin('.$sPointSQL.', ST_Centroid(geometry), '.$fSearchDiam.'))';
749 $sSQL .= ' ORDER BY ST_distance('.$sPointSQL.', geometry) ASC limit 1';
751 $aPlace = chksql($oDB->getRow($sSQL));
752 $iPlaceID = $aPlace['place_id'];
755 // The point we found might be too small - use the address to find what it is a child of
758 $sSQL = "select address_place_id from place_addressline where cached_rank_address <= $iMaxRank and place_id = $iPlaceID order by cached_rank_address desc,isaddress desc,distance desc limit 1";
759 $iPlaceID = chksql($oDB->getOne($sSQL));
761 if ($iPlaceID && $aPlace['place_id'] && $iMaxRank < 28)
763 $sSQL = "select address_place_id from place_addressline where cached_rank_address <= $iMaxRank and place_id = ".$aPlace['place_id']." order by cached_rank_address desc,isaddress desc,distance desc";
764 $iPlaceID = chksql($oDB->getOne($sSQL));
768 $iPlaceID = $aPlace['place_id'];
775 function addQuotes($s)
781 function validLatLon($fLat,$fLon)
783 return ($fLat <= 90.1 && $fLat >= -90.1 && $fLon <= 180.1 && $fLon >= -180.1);
786 // Do we have anything that looks like a lat/lon pair?
787 // returns array(lat,lon,query_with_lat_lon_removed)
789 function looksLikeLatLonPair($sQuery)
795 // degrees decimal minutes
796 // N 40 26.767, W 79 58.933
797 // N 40°26.767′, W 79°58.933′
799 if (preg_match('/\\b([NS])[ ]+([0-9]+[0-9.]*)[° ]+([0-9.]+)?[′\']*[, ]+([EW])[ ]+([0-9]+)[° ]+([0-9]+[0-9.]*)[′\']*?\\b/', $sQuery, $aData))
802 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60);
803 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[5] + $aData[6]/60);
805 // degrees decimal minutes
806 // 40 26.767 N, 79 58.933 W
807 // 40° 26.767′ N 79° 58.933′ W
809 elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\']*[ ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+[0-9.]*)?[′\' ]+([EW])\\b/', $sQuery, $aData))
812 $fQueryLat = ($aData[3]=='N'?1:-1) * ($aData[1] + $aData[2]/60);
813 $fQueryLon = ($aData[6]=='E'?1:-1) * ($aData[4] + $aData[5]/60);
815 // degrees decimal seconds
816 // N 40 26 46 W 79 58 56
817 // N 40° 26′ 46″, W 79° 58′ 56″
819 elseif (preg_match('/\\b([NS])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*[, ]+([EW])[ ]([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″"]*\\b/', $sQuery, $aData))
822 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2] + $aData[3]/60 + $aData[4]/3600);
823 $fQueryLon = ($aData[5]=='E'?1:-1) * ($aData[6] + $aData[7]/60 + $aData[8]/3600);
825 // degrees decimal seconds
826 // 40 26 46 N 79 58 56 W
827 // 40° 26′ 46″ N, 79° 58′ 56″ W
829 elseif (preg_match('/\\b([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([NS])[, ]+([0-9]+)[° ]+([0-9]+)[′\' ]+([0-9]+)[″" ]+([EW])\\b/', $sQuery, $aData))
832 $fQueryLat = ($aData[4]=='N'?1:-1) * ($aData[1] + $aData[2]/60 + $aData[3]/3600);
833 $fQueryLon = ($aData[8]=='E'?1:-1) * ($aData[5] + $aData[6]/60 + $aData[7]/3600);
836 // N 40.446° W 79.982°
838 elseif (preg_match('/\\b([NS])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*[, ]+([EW])[ ]([0-9]+[0-9]*\\.[0-9]+)[°]*\\b/', $sQuery, $aData))
841 $fQueryLat = ($aData[1]=='N'?1:-1) * ($aData[2]);
842 $fQueryLon = ($aData[3]=='E'?1:-1) * ($aData[4]);
845 // 40.446° N 79.982° W
847 elseif (preg_match('/\\b([0-9]+[0-9]*\\.[0-9]+)[° ]+([NS])[, ]+([0-9]+[0-9]*\\.[0-9]+)[° ]+([EW])\\b/', $sQuery, $aData))
850 $fQueryLat = ($aData[2]=='N'?1:-1) * ($aData[1]);
851 $fQueryLon = ($aData[4]=='E'?1:-1) * ($aData[3]);
857 elseif (preg_match('/(\\[|^|\\b)(-?[0-9]+[0-9]*\\.[0-9]+)[, ]+(-?[0-9]+[0-9]*\\.[0-9]+)(\\]|$|\\b)/', $sQuery, $aData))
860 $fQueryLat = $aData[2];
861 $fQueryLon = $aData[3];
864 if (!validLatLon($fQueryLat, $fQueryLon)) return;
865 $sQuery = trim(str_replace($sFound, ' ', $sQuery));
867 return array('lat' => $fQueryLat, 'lon' => $fQueryLon, 'query' => $sQuery);
871 function geometryText2Points($geometry_as_text, $fRadius)
874 if (preg_match('#POLYGON\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch))
876 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
878 elseif (preg_match('#LINESTRING\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch))
880 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
882 elseif (preg_match('#MULTIPOLYGON\\(\\(\\(([- 0-9.,]+)#', $geometry_as_text, $aMatch))
884 preg_match_all('/(-?[0-9.]+) (-?[0-9.]+)/', $aMatch[1], $aPolyPoints, PREG_SET_ORDER);
886 elseif (preg_match('#POINT\\((-?[0-9.]+) (-?[0-9.]+)\\)#', $geometry_as_text, $aMatch))
888 $aPolyPoints = createPointsAroundCenter($aMatch[1], $aMatch[2], $fRadius);
891 if (isset($aPolyPoints))
893 $aResultPoints = array();
894 foreach($aPolyPoints as $aPoint)
896 $aResultPoints[] = array($aPoint[1], $aPoint[2]);
898 return $aResultPoints;
904 function createPointsAroundCenter($fLon, $fLat, $fRadius)
906 $iSteps = max(8, min(100, ($fRadius * 40000)^2));
907 $fStepSize = (2*pi())/$iSteps;
908 $aPolyPoints = array();
909 for($f = 0; $f < 2*pi(); $f += $fStepSize)
911 $aPolyPoints[] = array('', $fLon+($fRadius*sin($f)), $fLat+($fRadius*cos($f)) );