2 /***************************************************************************
3 * Copyright (C) 2003-2006 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // {{{ liste les pays ou les régions d'un pays
23 /** donne la liste déroulante des pays
24 * @param $current pays actuellement selectionné
26 function geoloc_country($current) {
27 $res = XDB
::iterRow('SELECT a2,pays FROM geoloc_pays ORDER BY pays');
29 while (list($my_id, $my_pays) = $res->next()) {
30 $html .= sprintf("<option value=\"%s\" %s>%s</option>\n",
31 $my_id, ($current==$my_id?
"selected='selected'":""), $my_pays);
36 function _geoloc_country_smarty($params){
37 if(!isset($params['country']))
39 return geoloc_country($params['country']);
41 $GLOBALS['page']->register_function('geoloc_country', '_geoloc_country_smarty');
43 /** donne la liste deroulante des regions pour un pays
44 * @param $pays le pays dont on veut afficher les regions
45 * @param $current la region actuellement selectionnee
47 function geoloc_region($country,$current) {
48 $res = XDB
::iterRow('SELECT region,name FROM geoloc_region where a2={?} ORDER BY name', $country);
49 $html = "<option value=\"\"></option>";
50 while (list($regid, $regname) = $res->next()) {
51 $html .= sprintf("<option value=\"%s\" %s>%s</option>\n",
52 $regid, ($current==$regid?
"selected='selected'":""), $regname);
57 function _geoloc_region_smarty($params){
58 if(!isset($params['country']))
60 if(!isset($params['region']))
62 return geoloc_region($params['country'], $params['region']);
64 $GLOBALS['page']->register_function('geoloc_region', '_geoloc_region_smarty');
67 function geoloc_is_utf8($text)
69 return (iconv('utf-8', 'utf-8', $text) == $text);
72 function geoloc_utf8_decode($text)
74 return geoloc_is_utf8($text) ?
utf8_decode($text) : $text;
77 // {{{ get_address_infos($txt)
78 /** retrieve the infos on a text address
79 * store on the fly the info of the city concerned
80 * @param $txt the raw text of an address
82 function get_address_infos($txt) {
84 $url = $globals->geoloc
->webservice_url
."address.php?precise=1&txt=".urlencode(utf8_encode($txt));
85 if (!($f = @fopen
($url, 'r'))) return false
;
86 $keys = explode('|',fgets($f));
87 $vals = explode('|',fgets($f));
89 foreach ($keys as $i=>$key) {
92 $infos[$key] = $vals[$i];
94 $val = strtr($vals[$i], array(chr(197).chr(147) => "œ"));
95 $infos[$key] = geoloc_utf8_decode($val);
99 if (isset($infos['sql']) && $infos['sql'])
100 XDB
::execute("REPLACE INTO geoloc_city VALUES ".$infos['sql']);
101 if (isset($infos['display']) && $infos['display'])
102 XDB
::execute("UPDATE geoloc_pays SET display = {?} WHERE a2 = {?}", $infos['display'], $infos['country']);
103 if (isset($infos['cityid']))
104 fix_cities_not_on_map(1, $infos['cityid']);
109 // {{{ get_cities_maps($array)
110 /* get all the maps id of the cities contained in an array */
111 function get_cities_maps($array)
114 implode("\n",$array);
115 $url = $globals->geoloc
->webservice_url
."findMaps.php?datatext=".urlencode(utf8_encode(implode("\n", $array)));
116 if (!($f = @fopen
($url, 'r'))) return false
;
120 $l = trim(fgets($f));
121 $tab = explode(';', $l);
130 // {{{ get_new_maps($url)
131 /** set new maps from url **/
132 function get_new_maps($url)
134 if (!($f = @fopen
($url, 'r'))) {
137 XDB
::query('TRUNCATE TABLE geoloc_maps');
140 $l = fgetcsv($f, 1024, ';', '"');
141 foreach ($l as $i => $val) {
142 if ($val != 'NULL') {
143 $l[$i] = '\''.addslashes($val).'\'';
146 $s .= ',('.implode(',',$l).')';
148 XDB
::execute('INSERT INTO geoloc_maps VALUES '.substr($s, 1));
153 // {{{ get_address_text($adr)
154 /** make the text of an address that can be read by a mailman
155 * @param $adr an array with all the usual fields
157 function get_address_text($adr) {
159 if (isset($adr['adr1']) && $adr['adr1']) $t.= $adr['adr1'];
160 if (isset($adr['adr2']) && $adr['adr2']) $t.= "\n".$adr['adr2'];
161 if (isset($adr['adr3']) && $adr['adr3']) $t.= "\n".$adr['adr3'];
163 if (isset($adr['display']) && $adr['display']) {
164 $keys = explode(' ', $adr['display']);
165 foreach ($keys as $key) {
166 if (isset($adr[$key]))
167 $l .= " ".$adr[$key];
171 if ($l) $l = substr($l, 1);
175 if ($adr['country'] == 'US' ||
$adr['country'] == 'CA' ||
$adr['country'] == 'GB') {
176 if ($adr['city']) $l .= $adr['city'].",\n";
177 if ($adr['region']) $l .= $adr['region']." ";
178 if ($adr['postcode']) $l .= $adr['postcode'];
180 if (isset($adr['postcode']) && $adr['postcode']) $l .= $adr['postcode']." ";
181 if (isset($adr['city']) && $adr['city']) $l .= $adr['city'];
184 if ($l) $t .= "\n".trim($l);
185 if ($adr['country'] != '00' && (!$adr['countrytxt'] ||
$adr['countrytxt'] == strtoupper($adr['countrytxt']))) {
186 $res = XDB
::query("SELECT pays FROM geoloc_pays WHERE a2 = {?}", $adr['country']);
187 $adr['countrytxt'] = $res->fetchOneCell();
189 if (isset($adr['countrytxt']) && $adr['countrytxt']) $t .= "\n".$adr['countrytxt'];
194 // {{{ compare_addresses_text($a, $b)
195 /** compares if two address matches
196 * @param $a the raw text of an address
197 * @param $b the raw text of a complete valid address
199 function compare_addresses_text($a, $b) {
200 $ta = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $a));
201 $tb = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $b));
203 $la = explode("\n", $ta);
204 $lb = explode("\n", $tb);
206 if (count($lb) > count($la) +
1) return false
;
207 foreach ($la as $i=>$l) if (levenshtein($l, $lb[$i]) > 3) return false
;
213 function empty_address() {
227 // create a simple address from a text without geoloc
228 function cut_address($txt) {
229 $txt = str_replace("\r\n", "\n", $txt);
230 ereg("^([^\n]*)(\n([^\n]*)(\n(.*))?)?$", trim($txt), $a);
231 return array("adr1" => trim($a[1]), "adr2" => trim($a[3]), "adr3" => trim(str_replace("\n", " ", $a[5])));
234 // {{{ localize_addresses($uid)
235 /* localize all the address of a user and modify the database
236 * if the new address match with the old one
237 * @param $uid the id of the user
239 function localize_addresses($uid) {
240 $res = XDB
::iterator("SELECT * FROM adresses WHERE uid = {?} and (cityid IS NULL OR cityid = 0)", $uid);
243 while ($a = $res->next()) {
244 $new = get_address_infos($ta = get_address_text($a));
245 if (compare_addresses_text($ta, get_address_text($new))) {
246 XDB
::execute("UPDATE adresses SET
247 adr1 = {?}, adr2 = {?}, adr3 = {?},
248 cityid = {?}, city = {?}, postcode = {?},
249 region = {?}, regiontxt = {?}, country = {?},
250 glat = {?}, glng = {?}
251 WHERE uid = {?} AND adrid = {?}",
252 $new['adr1'], $new['adr2'], $new['adr3'],
253 $new['cityid'], $new['city'], $new['postcode'],
254 $new['region'], $new['regiontxt'], $new['country'],
255 $new['precise_lat'], $new['precise_lon'],
257 $new['store'] = true
;
258 if (!$new['cityid']) $erreur[$a['adrid']] = $new;
260 $new['store'] = false
;
261 $erreur[$a['adrid']] = $new;
268 // {{{ synchro_city($id)
269 /** synchronise the local geoloc_city base to geoloc.org
270 * @param $id the id of the city to synchronize
272 function synchro_city($id) {
274 $url = $globals->geoloc
->webservice_url
."cityFinder.php?method=id&id=".$id."&out=sql";
275 if (!($f = @fopen
($url, 'r'))) return false
;
278 return XDB
::execute("REPLACE INTO geoloc_city VALUES ".$s) > 0;
282 // {{{ function fix_cities_not_on_map($limit)
283 function fix_cities_not_on_map($limit=false
, $cityid=false
)
285 $missing = XDB
::query("SELECT c.id FROM geoloc_city AS c LEFT JOIN geoloc_city_in_maps AS m ON(c.id = m.city_id) WHERE m.city_id IS NULL".($cityid?
(" AND c.id = '".$cityid."'"):"").($limit?
" LIMIT $limit":""));
286 $maps = get_cities_maps($missing->fetchColumn());
290 foreach ($maps as $cityid => $maps_c)
291 foreach ($maps_c as $map_id)
292 $values .= ",($cityid, $map_id, '')";
293 XDB
::execute("REPLACE INTO geoloc_city_in_maps VALUES ".substr($values, 1));
300 function set_smallest_levels() {
301 $maxlengths = XDB
::iterRow("SELECT MAX(LENGTH(gm.path)), gcim.city_id
302 FROM geoloc_city_in_maps AS gcim
303 INNER JOIN geoloc_maps AS gm
305 GROUP BY gcim.city_id
307 while (list($length, $id) = $maxlengths->next()) {
308 XDB
::execute("UPDATE geoloc_city_in_maps AS gcim
309 INNER JOIN geoloc_maps AS gm USING(map_id)
310 SET gcim.infos = IF(LENGTH(gm.path) = {?}, 'smallest', '')
311 WHERE gcim.city_id = {?}", $length, $id);
318 function geoloc_to_x($lon, $lat) { return deg2rad(1) * $lon *100; }
320 function geoloc_to_y($lon, $lat) {
321 if ($lat < -75) return latToY(-75);
322 if ($lat > 75) return latToY(75);
323 return -100 * log(tan(pi()/4 +
deg2rad(1)/2*$lat));
326 function size_of_city($nb) { $s = round(log($nb +
1)*2,2); if ($s < 1) return 1; return $s; }
327 function size_of_territory($nb) { return size_of_city($nb); }
329 function geoloc_getData_subcities($mapid, $SFields, &$cities, $direct=true
) {
330 for ($i_mapfield=0; $i_mapfield < count($SFields) ; $i_mapfield++
) if ($SFields[$i_mapfield]->fieldFormName
== 'mapid') break;
331 $SFields[$i_mapfield] = new MapSField('mapid', array('gcim.map_id'), array('adresses','geoloc_city_in_maps'), array('am','gcim'), array(getadr_join('am'), 'am.cityid = gcim.city_id'), $mapid);
333 $fields = new SFieldGroup(true
, $SFields);
334 $where = $fields->get_where_statement();
335 if ($where) $where = " AND ".$where;
337 $cityres = XDB
::iterator("
339 gc.lon / 100000 AS x, gc.lat/100000 AS y,
341 COUNT(u.user_id) AS pop,
342 SUM(u.promo % 2) AS yellow
343 FROM auth_user_md5 AS u
344 INNER JOIN auth_user_quick AS q ON(u.user_id = q.user_id)
345 ".$fields->get_select_statement()."
346 LEFT JOIN geoloc_city AS gc ON(gcim.city_id = gc.id)
347 WHERE ".($direct?
"gcim.infos = 'smallest'":"1")."
349 GROUP BY gc.id,gc.alias ORDER BY pop DESC");
350 while ($c = $cityres->next())
354 // $city['name'] = geoloc_utf8_decode($city['name']);
355 if (!geoloc_is_utf8($city['name'])) {
356 $city['name'] = utf8_encode($city['name']);
358 $city['x'] = geoloc_to_x($c['x'], $c['y']);
359 $city['y'] = geoloc_to_y($c['x'], $c['y']);
360 $city['size'] = size_of_city($c['pop']);
361 $cities[$c['id']] = $city;
365 function geoloc_getData_subcountries($mapid, $SFields, $minentities) {
366 $countries = array();
369 if ($mapid === false
)
370 $wheremapid = "WHERE gm.parent IS NULL";
372 $wheremapid = "WHERE gm.parent = {?}";
373 $submapres = XDB
::iterator(
374 "SELECT gm.map_id AS id, gm.name, gm.x, gm.y, gm.xclip, gm.yclip,
375 gm.width, gm.height, gm.scale, 1 AS rat
376 FROM geoloc_maps AS gm
377 ".$wheremapid, Env
::v('mapid',''));
381 while ($c = $submapres->next())
384 $country['name'] = utf8_decode($country['name']);
385 $country['color'] = 0xFFFFFF;
386 $country['swf'] = $globals->geoloc
->webservice_url
."maps/mercator/map_".$c['id'].".swf";
387 $countries[$c['id']] = $country;
390 if ($mapid === false
) return array($countries, $cities);
392 geoloc_getData_subcities(Env
::i('mapid'), $SFields, $cities);
393 $nbcities = count($cities);
394 $nocity = $nbcities == 0;
396 for ($i_mapfield=0; $i_mapfield < count($SFields) ; $i_mapfield++
) if ($SFields[$i_mapfield]->fieldFormName
== 'mapid') break;
397 $SFields[$i_mapfield] = new MapSField('mapid', array('map.parent'), array('adresses','geoloc_city_in_maps','geoloc_maps'), array('am','gcim','map'), array(getadr_join('am'), 'am.cityid = gcim.city_id', 'map.map_id = gcim.map_id'));
399 $fields = new SFieldGroup(true
, $SFields);
400 $where = $fields->get_where_statement();
401 if ($where) $where = " WHERE ".$where;
403 $countryres = XDB
::iterator("
404 SELECT map.map_id AS id,
405 COUNT(u.user_id) AS nbPop,
406 SUM(u.promo % 2) AS yellow,
407 COUNT(DISTINCT gcim.city_id) AS nbCities,
408 SUM(IF(u.user_id IS NULL,0,am.glng)) AS lonPop,
409 SUM(IF(u.user_id IS NULL, 0,am.glat)) AS latPop
410 FROM auth_user_md5 AS u
411 INNER JOIN auth_user_quick AS q ON(u.user_id = q.user_id)
412 ".$fields->get_select_statement()."
414 GROUP BY map.map_id ORDER BY NULL", $hierarchy);
417 $nbentities = $nbcities +
$countryres->total();
418 while ($c = $countryres->next())
420 $c['latPop'] /= $c['nbPop'];
421 $c['lonPop'] /= $c['nbPop'];
422 $c['rad'] = size_of_territory($c['nbPop']);
423 if ($maxpop < $c['nbPop']) $maxpop = $c['nbPop'];
424 $c['xPop'] = geoloc_to_x($c['lonPop'], $c['latPop']);
425 $c['yPop'] = geoloc_to_y($c['lonPop'], $c['latPop']);
426 $countries[$c['id']] = array_merge($countries[$c['id']], $c);
428 $nbcities +
= $c['nbCities'];
431 if ($nocity && $nbcities < $minentities)
433 foreach($countries as $i => $c)
435 $countries[$i]['nbPop'] = 0;
436 if ($c['nbCities'] > 0)
437 geoloc_getData_subcities($c['id'], $SFields, $cities, false
);
441 foreach ($countries as $i => $c) if ($c['nbPop'] > 0)
443 $lambda = pow($c['nbPop'] / $maxpop,0.3);
444 $countries[$i]['color'] = 0x0000FF +
round((1-$lambda) * 0xFF)*0x010100;
447 return array($countries, $cities);
451 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker: