Commit | Line | Data |
---|---|---|
0337d704 | 1 | <?php |
2 | /*************************************************************************** | |
8d84c630 | 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
0337d704 | 4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
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. * | |
10 | * * | |
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. * | |
15 | * * | |
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 * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
0337d704 | 22 | // {{{ get_address_text($adr) |
23 | /** make the text of an address that can be read by a mailman | |
24 | * @param $adr an array with all the usual fields | |
25 | */ | |
787bb3d7 | 26 | function get_address_text($adr) |
8c4a0c30 | 27 | { |
0337d704 | 28 | $t = ""; |
5ee95138 | 29 | if (isset($adr['adr1']) && $adr['adr1']) $t.= $adr['adr1']; |
30 | if (isset($adr['adr2']) && $adr['adr2']) $t.= "\n".$adr['adr2']; | |
31 | if (isset($adr['adr3']) && $adr['adr3']) $t.= "\n".$adr['adr3']; | |
0337d704 | 32 | $l = ""; |
5ee95138 | 33 | if (isset($adr['display']) && $adr['display']) { |
80ca1b4e | 34 | $keys = explode(' ', $adr['display']); |
35 | foreach ($keys as $key) { | |
8c4a0c30 | 36 | if (isset($adr[$key])) { |
80ca1b4e | 37 | $l .= " ".$adr[$key]; |
8c4a0c30 | 38 | } else { |
80ca1b4e | 39 | $l .= " ".$key; |
8c4a0c30 | 40 | } |
80ca1b4e | 41 | } |
8c4a0c30 | 42 | if ($l) substr($l, 1); |
43 | } elseif ($adr['country'] == 'US' || $adr['country'] == 'CA' || $adr['country'] == 'GB') { | |
44 | if ($adr['city']) $l .= $adr['city'].",\n"; | |
45 | if ($adr['region']) $l .= $adr['region']." "; | |
46 | if ($adr['postcode']) $l .= $adr['postcode']; | |
47 | } else { | |
48 | if (isset($adr['postcode']) && $adr['postcode']) $l .= $adr['postcode']." "; | |
49 | if (isset($adr['city']) && $adr['city']) $l .= $adr['city']; | |
0337d704 | 50 | } |
51 | if ($l) $t .= "\n".trim($l); | |
52 | if ($adr['country'] != '00' && (!$adr['countrytxt'] || $adr['countrytxt'] == strtoupper($adr['countrytxt']))) { | |
08cce2ff | 53 | $res = XDB::query("SELECT pays FROM geoloc_pays WHERE a2 = {?}", $adr['country']); |
0337d704 | 54 | $adr['countrytxt'] = $res->fetchOneCell(); |
55 | } | |
8c4a0c30 | 56 | if (isset($adr['countrytxt']) && $adr['countrytxt']) { |
57 | $t .= "\n".$adr['countrytxt']; | |
58 | } | |
0337d704 | 59 | return trim($t); |
60 | } | |
61 | // }}} | |
0337d704 | 62 | // {{{ compare_addresses_text($a, $b) |
63 | /** compares if two address matches | |
64 | * @param $a the raw text of an address | |
65 | * @param $b the raw text of a complete valid address | |
66 | */ | |
8c4a0c30 | 67 | function compare_addresses_text($a, $b) |
68 | { | |
0337d704 | 69 | $ta = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $a)); |
70 | $tb = strtoupper(preg_replace(array("/[0-9,\"'#~:;_\- ]/", "/\r\n/"), array("", "\n"), $b)); | |
787bb3d7 | 71 | |
0337d704 | 72 | $la = explode("\n", $ta); |
73 | $lb = explode("\n", $tb); | |
74 | ||
8c4a0c30 | 75 | if (count($lb) > count($la) + 1) { |
76 | return false; | |
77 | } | |
041a5cec | 78 | foreach ($la as $i => $l) { |
37d44b3b | 79 | if (levenshtein(trim($l), trim($lb[$i])) > 3) { |
8c4a0c30 | 80 | return false; |
81 | } | |
82 | } | |
0337d704 | 83 | return true; |
84 | } | |
85 | ||
86 | // }}} | |
0337d704 | 87 | // {{{ localize_addresses($uid) |
88 | /* localize all the address of a user and modify the database | |
89 | * if the new address match with the old one | |
90 | * @param $uid the id of the user | |
91 | */ | |
8c4a0c30 | 92 | function localize_addresses($uid) |
93 | { | |
94 | $res = XDB::iterator("SELECT * | |
95 | FROM adresses | |
96 | WHERE uid = {?} and (cityid IS NULL OR cityid = 0)", $uid); | |
0337d704 | 97 | $erreur = Array(); |
98 | ||
99 | while ($a = $res->next()) { | |
100 | $new = get_address_infos($ta = get_address_text($a)); | |
101 | if (compare_addresses_text($ta, get_address_text($new))) { | |
8c4a0c30 | 102 | XDB::execute("UPDATE adresses |
103 | SET adr1 = {?}, adr2 = {?}, adr3 = {?}, | |
104 | cityid = {?}, city = {?}, postcode = {?}, | |
105 | region = {?}, regiontxt = {?}, country = {?}, | |
106 | glat = {?}, glng = {?} | |
107 | WHERE uid = {?} AND adrid = {?}", | |
108 | $new['adr1'], $new['adr2'], $new['adr3'], | |
109 | $new['cityid'], $new['city'], $new['postcode'], | |
110 | $new['region'], $new['regiontxt'], $new['country'], | |
111 | $new['precise_lat'], $new['precise_lon'], | |
112 | $uid, $a['adrid']); | |
113 | $new['store'] = true; | |
114 | if (!$new['cityid']) { | |
115 | $erreur[$a['adrid']] = $new; | |
116 | } | |
0337d704 | 117 | } else { |
118 | $new['store'] = false; | |
119 | $erreur[$a['adrid']] = $new; | |
120 | } | |
121 | } | |
122 | return $erreur; | |
123 | } | |
124 | // }}} | |
041a5cec SJ |
125 | // {{{ get_address_infos($txt) |
126 | /** retrieve the infos on a text address | |
127 | * store on the fly the info of the city concerned | |
128 | * @param $txt the raw text of an address | |
129 | */ | |
130 | ||
131 | function get_address_infos($txt) | |
132 | { | |
133 | global $globals; | |
134 | ||
135 | $url = $globals->geoloc->webservice_url."address.php?precise=1&txt=" . urlencode($txt); | |
136 | if ($globals->debug & DEBUG_BT) { | |
137 | if (!isset(PlBacktrace::$bt['Geoloc'])) { | |
138 | new PlBacktrace('Geoloc'); | |
139 | } | |
140 | PlBacktrace::$bt['Geoloc']->start($url); | |
141 | } | |
142 | $f = @fopen($url, 'r'); | |
143 | if ($f === false) { | |
144 | if ($globals->debug & DEBUG_BT) { | |
145 | PlBacktrace::$bt['Geoloc']->stop(0, 'Can\'t fetch result'); | |
146 | } | |
147 | return false; | |
148 | } | |
149 | $keys = explode('|',fgets($f)); | |
150 | $vals = explode('|',fgets($f)); | |
151 | if ($globals->debug & DEBUG_BT) { | |
152 | $data = array(); | |
153 | for ($i = 0 ; $i < count($keys) ; ++$i) { | |
154 | $data[] = array($keys[$i], $vals[$i]); | |
155 | } | |
156 | PlBacktrace::$bt['Geoloc']->stop(count($keys), null, $data); | |
157 | } | |
158 | $infos = empty_address(); | |
159 | foreach ($keys as $i=>$key) { | |
160 | if($vals[$i]) { | |
161 | if ($key == 'sql') { | |
162 | $infos[$key] = $vals[$i]; | |
163 | } else { | |
164 | $val = strtr($vals[$i], array(chr(197).chr(147) => "œ")); | |
165 | $infos[$key] = $val; | |
166 | } | |
167 | } | |
168 | } | |
169 | if (empty($infos['country'])) { | |
170 | $infos['country'] = '00'; | |
171 | } | |
172 | if (isset($infos['sql']) && $infos['sql']) { | |
173 | $sql = explode(', ', trim($infos['sql'], '()')); | |
174 | if (count($sql) == 16) { | |
175 | for ($i = 0 ; $i < 16 ; ++$i) { | |
176 | $sql[$i] = stripslashes(trim($sql[$i], ' \'')); | |
177 | } | |
178 | XDB::execute("REPLACE INTO geoloc_city | |
179 | VALUES ({?}, {?}, {?}, {?}, {?}, {?}, | |
180 | {?}, {?}, {?}, {?}, {?}, {?}, | |
181 | {?}, {?}, {?}, {?})", | |
182 | $sql[0], $sql[1], $sql[2], $sql[3], $sql[4], $sql[5], | |
183 | $sql[6], $sql[7], $sql[8], $sql[9], $sql[10], $sql[11], | |
184 | $sql[12], $sql[13], $sql[14], $sql[15]); | |
185 | } | |
186 | } | |
187 | if (isset($infos['display']) && $infos['display']) | |
188 | XDB::execute("UPDATE geoloc_pays | |
189 | SET display = {?} | |
190 | WHERE a2 = {?}", $infos['display'], $infos['country']); | |
191 | if (isset($infos['cityid'])) { | |
192 | fix_cities_not_on_map(1, $infos['cityid']); | |
193 | if (floatval($infos['precise_lat']) && floatval($infos['precise_lon'])) { | |
194 | $res = XDB::query("SELECT c.lat / 100000, c.lon / 100000 | |
195 | FROM geoloc_city AS c | |
196 | WHERE c.id = {?}", $infos['cityid']); | |
197 | if ($res->numRows()) { | |
198 | list($glat, $glng) = $res->fetchOneRow(); | |
199 | $infos['precise_lat'] = $glat; | |
200 | $infos['precise_lon'] = $glng; | |
201 | } | |
202 | } | |
203 | } | |
204 | return $infos; | |
205 | } | |
0337d704 | 206 | |
041a5cec | 207 | // }}} |
0337d704 | 208 | // {{{ synchro_city($id) |
209 | /** synchronise the local geoloc_city base to geoloc.org | |
210 | * @param $id the id of the city to synchronize | |
211 | */ | |
787bb3d7 | 212 | function synchro_city($id) |
8c4a0c30 | 213 | { |
56670b6a | 214 | global $globals; |
215 | $url = $globals->geoloc->webservice_url."cityFinder.php?method=id&id=".$id."&out=sql"; | |
8c4a0c30 | 216 | if (!($f = @fopen($url, 'r'))) { |
217 | return false; | |
218 | } | |
0337d704 | 219 | $s = fgets($f); |
8c4a0c30 | 220 | if ($s) { |
08cce2ff | 221 | return XDB::execute("REPLACE INTO geoloc_city VALUES ".$s) > 0; |
8c4a0c30 | 222 | } |
223 | } | |
0337d704 | 224 | // }}} |
56670b6a | 225 | // {{{ function fix_cities_not_on_map($limit) |
7f3ac007 | 226 | function fix_cities_not_on_map($limit=false, $cityid=false) |
56670b6a | 227 | { |
8c4a0c30 | 228 | $missing = XDB::query("SELECT c.id |
229 | FROM geoloc_city AS c | |
787bb3d7 | 230 | LEFT JOIN geoloc_city_in_maps AS m ON(c.id = m.city_id) |
8c4a0c30 | 231 | WHERE m.city_id IS NULL" |
232 | . ($cityid ? " AND c.id = '" . $cityid . "'" : "" ) | |
233 | . ($limit ? " LIMIT $limit" : "" )); | |
56670b6a | 234 | $maps = get_cities_maps($missing->fetchColumn()); |
8c4a0c30 | 235 | if ($maps) { |
56670b6a | 236 | $values = ""; |
8c4a0c30 | 237 | foreach ($maps as $cityid => $maps_c) { |
238 | foreach ($maps_c as $map_id) { | |
56670b6a | 239 | $values .= ",($cityid, $map_id, '')"; |
8c4a0c30 | 240 | } |
241 | } | |
22cc3370 FB |
242 | if (strlen($values) > 1) { |
243 | XDB::execute("REPLACE INTO geoloc_city_in_maps | |
244 | VALUES ".substr($values, 1)); | |
245 | } | |
8c4a0c30 | 246 | } else { |
56670b6a | 247 | return false; |
8c4a0c30 | 248 | } |
014c8464 | 249 | return true; |
250 | } | |
56670b6a | 251 | |
8c4a0c30 | 252 | function set_smallest_levels() |
253 | { | |
254 | $maxlengths = XDB::iterRow("SELECT MAX(LENGTH(gm.path)), gcim.city_id | |
255 | FROM geoloc_city_in_maps AS gcim | |
256 | INNER JOIN geoloc_maps AS gm USING ( map_id ) | |
257 | GROUP BY gcim.city_id"); | |
014c8464 | 258 | while (list($length, $id) = $maxlengths->next()) { |
8c4a0c30 | 259 | XDB::execute("UPDATE geoloc_city_in_maps AS gcim |
260 | INNER JOIN geoloc_maps AS gm USING(map_id) | |
261 | SET gcim.infos = IF(LENGTH(gm.path) = {?}, 'smallest', '') | |
262 | WHERE gcim.city_id = {?}", $length, $id); | |
014c8464 | 263 | } |
56670b6a | 264 | return true; |
265 | } | |
266 | // }}} | |
4c906759 SJ |
267 | // {{{ geoloc_country($current, $avail_only = false) |
268 | /** donne la liste déroulante des pays | |
269 | * @param $current pays actuellement selectionné | |
270 | */ | |
271 | function geoloc_country($current, $avail_only = false) | |
272 | { | |
273 | if ($avail_only) { | |
274 | $res = XDB::iterRow('SELECT g.a2, g.pays | |
275 | FROM geoloc_pays AS g | |
276 | INNER JOIN adresses AS a ON(a.country = g.a2) | |
277 | GROUP BY g.a2 | |
278 | ORDER BY g.pays'); | |
279 | } else { | |
280 | $res = XDB::iterRow('SELECT a2,pays FROM geoloc_pays ORDER BY pays'); | |
281 | } | |
282 | $html = ""; | |
283 | while (list($my_id, $my_pays) = $res->next()) { | |
284 | $html .= sprintf("<option value=\"%s\" %s>%s</option>\n", | |
285 | $my_id, ($current==$my_id?"selected='selected'":""), $my_pays); | |
286 | } | |
287 | return $html; | |
288 | } | |
289 | ||
290 | // }}} | |
291 | // {{{ geoloc_region($country, $current, $avail_only = false) | |
292 | /** donne la liste deroulante des regions pour un pays | |
293 | * @param $pays le pays dont on veut afficher les regions | |
294 | * @param $current la region actuellement selectionnee | |
295 | */ | |
296 | function geoloc_region($country, $current, $avail_only = false) | |
297 | { | |
298 | if ($avail_only) { | |
299 | $res = XDB::iterRow('SELECT r.region, r.name | |
300 | FROM geoloc_region AS r | |
301 | INNER JOIN adresses AS a ON (a.country = r.a2 AND a.region = r.region) | |
302 | WHERE r.a2 = {?} | |
303 | GROUP BY r.region | |
304 | ORDER BY r.name', $country); | |
305 | } else { | |
306 | $res = XDB::iterRow('SELECT region,name | |
307 | FROM geoloc_region | |
308 | WHERE a2 = {?} | |
309 | ORDER BY name', $country); | |
310 | } | |
311 | $html = "<option value=\"\"></option>"; | |
312 | while (list($regid, $regname) = $res->next()) { | |
313 | $html .= sprintf("<option value=\"%s\" %s>%s</option>\n", | |
314 | $regid, ($current==$regid?"selected='selected'":""), $regname); | |
315 | } | |
316 | return $html; | |
317 | } | |
318 | // }}} | |
319 | // {{{ get_cities_maps($array) | |
320 | /* get all the maps id of the cities contained in an array */ | |
321 | function get_cities_maps($array) | |
322 | { | |
323 | global $globals; | |
324 | implode("\n",$array); | |
325 | $url = $globals->geoloc->webservice_url."findMaps.php?datatext=".urlencode(implode("\n", $array)); | |
326 | if (!($f = @fopen($url, 'r'))) return false; | |
327 | $maps = array(); | |
328 | while (!feof($f)) | |
329 | { | |
330 | $l = trim(fgets($f)); | |
331 | $tab = explode(';', $l); | |
332 | $i = $tab[0]; | |
333 | unset($tab[0]); | |
334 | $maps[$i] = $tab; | |
335 | } | |
336 | return $maps; | |
337 | } | |
338 | // }}} | |
339 | // {{{ get_new_maps($url) | |
340 | /** set new maps from url **/ | |
341 | function get_new_maps($url) | |
342 | { | |
343 | if (!($f = @fopen($url, 'r'))) { | |
344 | return false; | |
345 | } | |
346 | XDB::query('TRUNCATE TABLE geoloc_maps'); | |
347 | $s = ''; | |
348 | while (!feof($f)) { | |
349 | $l = fgetcsv($f, 1024, ';', '"'); | |
350 | foreach ($l as $i => $val) { | |
351 | if ($val != 'NULL') { | |
352 | $l[$i] = '\''.addslashes($val).'\''; | |
353 | } | |
354 | } | |
355 | $s .= ',('.implode(',',$l).')'; | |
356 | } | |
357 | XDB::execute('INSERT INTO geoloc_maps VALUES '.substr($s, 1)); | |
358 | return true; | |
359 | } | |
360 | // }}} | |
56670b6a | 361 | |
8c4a0c30 | 362 | function geoloc_to_x($lon, $lat) |
363 | { | |
364 | return deg2rad(1) * $lon *100; | |
365 | } | |
366 | ||
367 | function geoloc_to_y($lon, $lat) | |
368 | { | |
369 | if ($lat < -75) { | |
370 | return latToY(-75); | |
371 | } | |
372 | if ($lat > 75) { | |
373 | return latToY(75); | |
374 | } | |
787bb3d7 | 375 | return -100 * log(tan(pi()/4 + deg2rad(1)/2*$lat)); |
8c4a0c30 | 376 | } |
56670b6a | 377 | |
8c4a0c30 | 378 | function size_of_city($nb) |
379 | { | |
380 | $s = round(log($nb + 1)*2,2); | |
381 | if ($s < 1) { | |
382 | return 1; | |
383 | } | |
384 | return $s; | |
56670b6a | 385 | } |
386 | ||
8c4a0c30 | 387 | function size_of_territory($nb) |
388 | { | |
389 | return size_of_city($nb); | |
390 | } | |
56670b6a | 391 | |
8c4a0c30 | 392 | function geoloc_getData_subcities($mapid, $SFields, &$cities, $direct=true) |
393 | { | |
394 | if ($SFields instanceof UserSet) { | |
395 | $set = $SFields; | |
396 | $SFields = array(); | |
397 | } else { | |
398 | $set = new UserSet(); | |
399 | } | |
400 | for ($i_mapfield=0; $i_mapfield < count($SFields) ; $i_mapfield++) { | |
401 | if ($SFields[$i_mapfield]->fieldFormName == 'mapid') { | |
402 | break; | |
403 | } | |
404 | } | |
405 | $SFields[$i_mapfield] = new MapSField('mapid', | |
406 | array('gcim.map_id'), | |
407 | array('adresses','geoloc_city_in_maps'), | |
408 | array('am','gcim'), | |
409 | array(getadr_join('am'), 'am.cityid = gcim.city_id'), | |
410 | $mapid); | |
2b105fb6 | 411 | $fields = new SFieldGroup(true, $SFields); |
412 | $where = $fields->get_where_statement(); | |
86b5c8f0 | 413 | $joins = $fields->get_select_statement(); |
8c4a0c30 | 414 | if ($where) { |
415 | $where .= ' AND '; | |
416 | } | |
417 | $cityres = $set->get('gc.id, | |
418 | gc.lon / 100000 AS x, gc.lat/100000 AS y, | |
419 | gc.name, | |
420 | COUNT(u.user_id) AS pop, | |
421 | SUM(u.promo % 2) AS yellow', | |
86b5c8f0 | 422 | "$joins |
8c4a0c30 | 423 | LEFT JOIN geoloc_city AS gc ON(gcim.city_id = gc.id)", |
424 | $where . ($direct ? "gcim.infos = 'smallest'" : '1'), | |
425 | 'gc.id, gc.alias', | |
426 | 'pop DESC'); | |
06df222b | 427 | foreach($cityres as $c) { |
8c4a0c30 | 428 | if ($c['pop'] > 0) { |
2b105fb6 | 429 | $city = $c; |
430 | $city['x'] = geoloc_to_x($c['x'], $c['y']); | |
431 | $city['y'] = geoloc_to_y($c['x'], $c['y']); | |
432 | $city['size'] = size_of_city($c['pop']); | |
433 | $cities[$c['id']] = $city; | |
434 | } | |
8c4a0c30 | 435 | } |
2b105fb6 | 436 | } |
437 | ||
787bb3d7 | 438 | function geoloc_getData_subcountries($mapid, $sin, $minentities) |
8c4a0c30 | 439 | { |
2b105fb6 | 440 | $countries = array(); |
441 | $cities = array(); | |
787bb3d7 | 442 | |
8c4a0c30 | 443 | if ($mapid === false) { |
b536b5ae | 444 | $wheremapid = "WHERE gm.parent IS NULL"; |
8c4a0c30 | 445 | } else { |
b536b5ae | 446 | $wheremapid = "WHERE gm.parent = {?}"; |
8c4a0c30 | 447 | } |
08cce2ff | 448 | $submapres = XDB::iterator( |
787bb3d7 | 449 | "SELECT gm.map_id AS id, gm.name, gm.x, gm.y, gm.xclip, gm.yclip, |
8c4a0c30 | 450 | gm.width, gm.height, gm.scale, 1 AS rat |
451 | FROM geoloc_maps AS gm | |
452 | ". $wheremapid, Env::v('mapid','')); | |
2b105fb6 | 453 | |
edfc872d | 454 | global $globals; |
455 | ||
8c4a0c30 | 456 | while ($c = $submapres->next()) { |
2b105fb6 | 457 | $country = $c; |
2b105fb6 | 458 | $country['color'] = 0xFFFFFF; |
459 | $country['swf'] = $globals->geoloc->webservice_url."maps/mercator/map_".$c['id'].".swf"; | |
460 | $countries[$c['id']] = $country; | |
461 | } | |
787bb3d7 | 462 | |
8c4a0c30 | 463 | if ($mapid === false) { |
464 | return array($countries, $cities); | |
465 | } | |
2b105fb6 | 466 | |
8c4a0c30 | 467 | geoloc_getData_subcities(Env::i('mapid'), $sin, $cities); |
b536b5ae | 468 | $nbcities = count($cities); |
469 | $nocity = $nbcities == 0; | |
8c4a0c30 | 470 | if ($sin instanceof UserSet) { |
471 | $set = $sin; | |
472 | $SFields = array(); | |
473 | } else { | |
474 | $set = new UserSet(); | |
475 | $SFields = $sin; | |
476 | } | |
2b105fb6 | 477 | |
8c4a0c30 | 478 | for ($i_mapfield=0; $i_mapfield < count($SFields) ; $i_mapfield++) { |
479 | if ($SFields[$i_mapfield]->fieldFormName == 'mapid') { | |
480 | break; | |
481 | } | |
482 | } | |
483 | $SFields[$i_mapfield] = new MapSField('mapid', | |
484 | array('map.parent'), | |
485 | array('adresses','geoloc_city_in_maps','geoloc_maps'), | |
486 | array('am','gcim','map'), | |
787bb3d7 FB |
487 | array(getadr_join('am'), |
488 | 'am.cityid = gcim.city_id', | |
8c4a0c30 | 489 | 'map.map_id = gcim.map_id')); |
b536b5ae | 490 | $fields = new SFieldGroup(true, $SFields); |
86b5c8f0 | 491 | $where = $fields->get_where_statement(); |
492 | $joins = $fields->get_select_statement(); | |
8c4a0c30 | 493 | $countryres = $set->get('map.map_id AS id, |
494 | COUNT(u.user_id) AS nbPop, | |
495 | SUM(u.promo % 2) AS yellow, | |
496 | COUNT(DISTINCT gcim.city_id) AS nbCities, | |
497 | SUM(IF(u.user_id IS NULL,0,am.glng)) AS lonPop, | |
498 | SUM(IF(u.user_id IS NULL, 0,am.glat)) AS latPop', | |
86b5c8f0 | 499 | $joins, |
500 | $where, | |
8c4a0c30 | 501 | 'map.map_id', |
502 | 'NULL'); | |
787bb3d7 | 503 | |
b536b5ae | 504 | $maxpop = 0; |
06df222b FB |
505 | $nbentities = $nbcities + count($countryres); |
506 | foreach ($countryres as $c) { | |
b536b5ae | 507 | $c['latPop'] /= $c['nbPop']; |
508 | $c['lonPop'] /= $c['nbPop']; | |
509 | $c['rad'] = size_of_territory($c['nbPop']); | |
510 | if ($maxpop < $c['nbPop']) $maxpop = $c['nbPop']; | |
511 | $c['xPop'] = geoloc_to_x($c['lonPop'], $c['latPop']); | |
512 | $c['yPop'] = geoloc_to_y($c['lonPop'], $c['latPop']); | |
86b5c8f0 | 513 | @$countries[$c['id']] = array_merge($countries[$c['id']], $c); |
787bb3d7 | 514 | |
b536b5ae | 515 | $nbcities += $c['nbCities']; |
787bb3d7 FB |
516 | } |
517 | ||
8c4a0c30 | 518 | if ($nocity && $nbcities < $minentities){ |
519 | foreach($countries as $i => $c) { | |
b536b5ae | 520 | $countries[$i]['nbPop'] = 0; |
86b5c8f0 | 521 | if (@$c['nbCities'] > 0) { |
8c4a0c30 | 522 | geoloc_getData_subcities($c['id'], $sin, $cities, false); |
787bb3d7 | 523 | } |
8c4a0c30 | 524 | } |
b536b5ae | 525 | } |
787bb3d7 | 526 | |
8c4a0c30 | 527 | foreach ($countries as $i => $c) { |
c1895524 | 528 | if (@$c['nbPop'] > 0) { |
8c4a0c30 | 529 | $lambda = pow($c['nbPop'] / $maxpop,0.3); |
530 | $countries[$i]['color'] = 0x0000FF + round((1-$lambda) * 0xFF)*0x010100; | |
531 | } | |
b536b5ae | 532 | } |
787bb3d7 FB |
533 | |
534 | return array($countries, $cities); | |
535 | } | |
138b3c8e | 536 | |
a7de4ef7 | 537 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
0337d704 | 538 | ?> |