+// {{{ function fix_cities_not_on_map($limit)
+function fix_cities_not_on_map($limit=false, $cityid=false)
+{
+ $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" : "" ));
+ $maps = get_cities_maps($missing->fetchColumn());
+ if ($maps) {
+ $values = "";
+ foreach ($maps as $cityid => $maps_c) {
+ foreach ($maps_c as $map_id) {
+ $values .= ",($cityid, $map_id, '')";
+ }
+ }
+ XDB::execute("REPLACE INTO geoloc_city_in_maps
+ VALUES ".substr($values, 1));
+ } else {
+ return false;
+ }
+ return true;
+}
+
+function set_smallest_levels()
+{
+ $maxlengths = XDB::iterRow("SELECT MAX(LENGTH(gm.path)), gcim.city_id
+ FROM geoloc_city_in_maps AS gcim
+ INNER JOIN geoloc_maps AS gm USING ( map_id )
+ GROUP BY gcim.city_id");
+ while (list($length, $id) = $maxlengths->next()) {
+ XDB::execute("UPDATE geoloc_city_in_maps AS gcim
+ INNER JOIN geoloc_maps AS gm USING(map_id)
+ SET gcim.infos = IF(LENGTH(gm.path) = {?}, 'smallest', '')
+ WHERE gcim.city_id = {?}", $length, $id);
+ }
+ return true;
+}
+// }}}
+
+
+function geoloc_to_x($lon, $lat)
+{
+ return deg2rad(1) * $lon *100;
+}
+
+function geoloc_to_y($lon, $lat)
+{
+ if ($lat < -75) {
+ return latToY(-75);
+ }
+ if ($lat > 75) {
+ return latToY(75);
+ }
+ return -100 * log(tan(pi()/4 + deg2rad(1)/2*$lat));
+}
+
+function size_of_city($nb)
+{
+ $s = round(log($nb + 1)*2,2);
+ if ($s < 1) {
+ return 1;
+ }
+ return $s;
+}
+
+function size_of_territory($nb)
+{
+ return size_of_city($nb);
+}
+
+function geoloc_getData_subcities($mapid, $SFields, &$cities, $direct=true)
+{
+ if ($SFields instanceof UserSet) {
+ $set = $SFields;
+ $SFields = array();
+ } else {
+ $set = new UserSet();
+ }
+ for ($i_mapfield=0; $i_mapfield < count($SFields) ; $i_mapfield++) {
+ if ($SFields[$i_mapfield]->fieldFormName == 'mapid') {
+ break;
+ }
+ }
+ $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);
+ $fields = new SFieldGroup(true, $SFields);
+ $where = $fields->get_where_statement();
+ $joins = $fields->get_select_statement();
+ if ($where) {
+ $where .= ' AND ';
+ }
+ $cityres = $set->get('gc.id,
+ gc.lon / 100000 AS x, gc.lat/100000 AS y,
+ gc.name,
+ COUNT(u.user_id) AS pop,
+ SUM(u.promo % 2) AS yellow',
+ "$joins
+ LEFT JOIN geoloc_city AS gc ON(gcim.city_id = gc.id)",
+ $where . ($direct ? "gcim.infos = 'smallest'" : '1'),
+ 'gc.id, gc.alias',
+ 'pop DESC');
+ foreach($cityres as $c) {
+ if ($c['pop'] > 0) {
+ $city = $c;
+ $city['x'] = geoloc_to_x($c['x'], $c['y']);
+ $city['y'] = geoloc_to_y($c['x'], $c['y']);
+ $city['size'] = size_of_city($c['pop']);
+ $cities[$c['id']] = $city;
+ }
+ }
+}
+
+function geoloc_getData_subcountries($mapid, $sin, $minentities)
+{
+ $countries = array();
+ $cities = array();
+
+ if ($mapid === false) {
+ $wheremapid = "WHERE gm.parent IS NULL";
+ } else {
+ $wheremapid = "WHERE gm.parent = {?}";
+ }
+ $submapres = XDB::iterator(
+ "SELECT gm.map_id AS id, gm.name, gm.x, gm.y, gm.xclip, gm.yclip,
+ gm.width, gm.height, gm.scale, 1 AS rat
+ FROM geoloc_maps AS gm
+ ". $wheremapid, Env::v('mapid',''));
+
+ global $globals;
+
+ while ($c = $submapres->next()) {
+ $country = $c;
+ $country['color'] = 0xFFFFFF;
+ $country['swf'] = $globals->geoloc->webservice_url."maps/mercator/map_".$c['id'].".swf";
+ $countries[$c['id']] = $country;
+ }
+
+ if ($mapid === false) {
+ return array($countries, $cities);
+ }
+
+ geoloc_getData_subcities(Env::i('mapid'), $sin, $cities);
+ $nbcities = count($cities);
+ $nocity = $nbcities == 0;
+ if ($sin instanceof UserSet) {
+ $set = $sin;
+ $SFields = array();
+ } else {
+ $set = new UserSet();
+ $SFields = $sin;
+ }
+
+ for ($i_mapfield=0; $i_mapfield < count($SFields) ; $i_mapfield++) {
+ if ($SFields[$i_mapfield]->fieldFormName == 'mapid') {
+ break;
+ }
+ }
+ $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'));
+ $fields = new SFieldGroup(true, $SFields);
+ $where = $fields->get_where_statement();
+ $joins = $fields->get_select_statement();
+ $countryres = $set->get('map.map_id AS id,
+ COUNT(u.user_id) AS nbPop,
+ SUM(u.promo % 2) AS yellow,
+ COUNT(DISTINCT gcim.city_id) AS nbCities,
+ SUM(IF(u.user_id IS NULL,0,am.glng)) AS lonPop,
+ SUM(IF(u.user_id IS NULL, 0,am.glat)) AS latPop',
+ $joins,
+ $where,
+ 'map.map_id',
+ 'NULL');
+
+ $maxpop = 0;
+ $nbentities = $nbcities + count($countryres);
+ foreach ($countryres as $c) {
+ $c['latPop'] /= $c['nbPop'];
+ $c['lonPop'] /= $c['nbPop'];
+ $c['rad'] = size_of_territory($c['nbPop']);
+ if ($maxpop < $c['nbPop']) $maxpop = $c['nbPop'];
+ $c['xPop'] = geoloc_to_x($c['lonPop'], $c['latPop']);
+ $c['yPop'] = geoloc_to_y($c['lonPop'], $c['latPop']);
+ @$countries[$c['id']] = array_merge($countries[$c['id']], $c);
+
+ $nbcities += $c['nbCities'];
+ }
+
+ if ($nocity && $nbcities < $minentities){
+ foreach($countries as $i => $c) {
+ $countries[$i]['nbPop'] = 0;
+ if (@$c['nbCities'] > 0) {
+ geoloc_getData_subcities($c['id'], $sin, $cities, false);
+ }
+ }
+ }
+
+ foreach ($countries as $i => $c) {
+ if (@$c['nbPop'] > 0) {
+ $lambda = pow($c['nbPop'] / $maxpop,0.3);
+ $countries[$i]['color'] = 0x0000FF + round((1-$lambda) * 0xFF)*0x010100;
+ }
+ }
+
+ return array($countries, $cities);
+}
+// }}}
+
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: