X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=include%2Fgeoloc.inc.php;h=00e8fa979da05ff89c76a68ccda997ba0cc52a9a;hb=981d6bb2fbf8a249dd5cde339da8bc029b23794b;hp=89ecd450531206731487a6bf63fd488900f157dd;hpb=be6ab042d1dc34d9e42557281b48dd8da400231b;p=platal.git diff --git a/include/geoloc.inc.php b/include/geoloc.inc.php index 89ecd45..00e8fa9 100644 --- a/include/geoloc.inc.php +++ b/include/geoloc.inc.php @@ -1,6 +1,6 @@ geoloc->webservice_url."address.php?precise=1&txt=" . urlencode($txt); - if (!($f = @fopen($url, 'r'))) return false; + if ($globals->debug & DEBUG_BT) { + if (!isset(PlBacktrace::$bt['Geoloc'])) { + new PlBacktrace('Geoloc'); + } + PlBacktrace::$bt['Geoloc']->start($url); + } + $f = @fopen($url, 'r'); + if ($f === false) { + if ($globals->debug & DEBUG_BT) { + PlBacktrace::$bt['Geoloc']->stop(0, 'Can\'t fetch result'); + } + return false; + } $keys = explode('|',fgets($f)); $vals = explode('|',fgets($f)); - $infos = array(); + if ($globals->debug & DEBUG_BT) { + $data = array(); + for ($i = 0 ; $i < count($keys) ; ++$i) { + $data[] = array($keys[$i], $vals[$i]); + } + PlBacktrace::$bt['Geoloc']->stop(count($keys), null, $data); + } + $infos = empty_address(); foreach ($keys as $i=>$key) { if($vals[$i]) { if ($key == 'sql') { @@ -93,12 +113,41 @@ function get_address_infos($txt) } } } - if (isset($infos['sql']) && $infos['sql']) - XDB::execute("REPLACE INTO geoloc_city VALUES ".$infos['sql']); + if (empty($infos['country'])) { + $infos['country'] = '00'; + } + if (isset($infos['sql']) && $infos['sql']) { + $sql = explode(', ', trim($infos['sql'], '()')); + if (count($sql) == 16) { + for ($i = 0 ; $i < 16 ; ++$i) { + $sql[$i] = stripslashes(trim($sql[$i], ' \'')); + } + XDB::execute("REPLACE INTO geoloc_city + VALUES ({?}, {?}, {?}, {?}, {?}, {?}, + {?}, {?}, {?}, {?}, {?}, {?}, + {?}, {?}, {?}, {?})", + $sql[0], $sql[1], $sql[2], $sql[3], $sql[4], $sql[5], + $sql[6], $sql[7], $sql[8], $sql[9], $sql[10], $sql[11], + $sql[12], $sql[13], $sql[14], $sql[15]); + } + } if (isset($infos['display']) && $infos['display']) - XDB::execute("UPDATE geoloc_pays SET display = {?} WHERE a2 = {?}", $infos['display'], $infos['country']); - if (isset($infos['cityid'])) + XDB::execute("UPDATE geoloc_pays + SET display = {?} + WHERE a2 = {?}", $infos['display'], $infos['country']); + if (isset($infos['cityid'])) { fix_cities_not_on_map(1, $infos['cityid']); + if (floatval($infos['precise_lat']) && floatval($infos['precise_lon'])) { + $res = XDB::query("SELECT c.lat / 100000, c.lon / 100000 + FROM geoloc_city AS c + WHERE c.id = {?}", $infos['cityid']); + if ($res->numRows()) { + list($glat, $glng) = $res->fetchOneRow(); + $infos['precise_lat'] = $glat; + $infos['precise_lon'] = $glng; + } + } + } return $infos; } // }}} @@ -225,7 +274,9 @@ function empty_address() { "region" => "", "regiontxt" => "", "country" => "00", - "countrytxt" => ""); + "countrytxt" => "", + "precise_lat" => "", + "precise_lon" => ""); } // create a simple address from a text without geoloc @@ -310,8 +361,10 @@ function fix_cities_not_on_map($limit=false, $cityid=false) $values .= ",($cityid, $map_id, '')"; } } - XDB::execute("REPLACE INTO geoloc_city_in_maps - VALUES ".substr($values, 1)); + if (strlen($values) > 1) { + XDB::execute("REPLACE INTO geoloc_city_in_maps + VALUES ".substr($values, 1)); + } } else { return false; } @@ -400,7 +453,7 @@ function geoloc_getData_subcities($mapid, $SFields, &$cities, $direct=true) $where . ($direct ? "gcim.infos = 'smallest'" : '1'), 'gc.id, gc.alias', 'pop DESC'); - while ($c = $cityres->next()) { + foreach($cityres as $c) { if ($c['pop'] > 0) { $city = $c; $city['x'] = geoloc_to_x($c['x'], $c['y']); @@ -478,8 +531,8 @@ function geoloc_getData_subcountries($mapid, $sin, $minentities) 'NULL'); $maxpop = 0; - $nbentities = $nbcities + $countryres->total(); - while ($c = $countryres->next()) { + $nbentities = $nbcities + count($countryres); + foreach ($countryres as $c) { $c['latPop'] /= $c['nbPop']; $c['lonPop'] /= $c['nbPop']; $c['rad'] = size_of_territory($c['nbPop']); @@ -501,7 +554,7 @@ function geoloc_getData_subcountries($mapid, $sin, $minentities) } foreach ($countries as $i => $c) { - if ($c['nbPop'] > 0) { + if (@$c['nbPop'] > 0) { $lambda = pow($c['nbPop'] / $maxpop,0.3); $countries[$i]['color'] = 0x0000FF + round((1-$lambda) * 0xFF)*0x010100; }