Subversion Repositories eFlore/Applications.coel

Rev

Rev 1692 | Rev 1713 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
/*
  cat >> /etc/php.d/php.ini // /etc/php5/cli/conf.d/ZZ-mycustom.ini <<EOF
pdo.dsn.testcoel = "mysql:dbname=tb_coel_test;host=localhost"
mysql.default_user = telabotap
mysql.default_password =
EOF
*/
if(PHP_SAPI !== 'cli') exit(1);

define('DOUBLE_CHECK', FALSE);

date_default_timezone_set('Europe/Paris');
$DIR = dirname(__FILE__); // XXX: PHP-5.3
define("_GEO_STORE_CACHE", $DIR . "/nominatim-coords.cache.ser");

require_once($DIR . '/../../jrest/services/Coel.php');
$db = new PDO('testcoel',
                          @$argv[1] ? $argv[1] : ini_get('mysql.default_user'),
                          @$argv[2] ? $argv[2] : ini_get('mysql.default_password'),
                          array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'")) or die('no db');
$cache = @unserialize(file_get_contents(_GEO_STORE_CACHE));

if(function_exists('pcntl_signal')) {
        // SIGUSR1 handler to save the result during a run
        function savecache($signal = FALSE) {
                global $cache;
                if($cache) {
                        ksort($cache, SORT_NUMERIC);
                        file_put_contents(_GEO_STORE_CACHE, serialize($cache));
                }
                echo "saved " . count($cache) . " results\n";
                if($signal == SIGINT) exit;
        }
        declare(ticks = 1);
        pcntl_signal(SIGUSR1, 'savecache');
        pcntl_signal(SIGINT, 'savecache');
        echo '$ kill -USR1 ' . posix_getpid() . ' # to flush results to disk' . "\n";
}

printf("cache already contains %d entries, will start in 4 seconds:\n", count($cache));
sleep(4);

// correction
$db->query('UPDATE coel_structure SET cs_longitude = REPLACE(cs_longitude, ",", "."), cs_latitude = REPLACE(cs_latitude, ",", ".")');
$db->query('ALTER TABLE coel_structure MODIFY cs_longitude DOUBLE(12,9) NULL DEFAULT NULL');
$db->query('ALTER TABLE coel_structure MODIFY cs_latitude DOUBLE(12,9) NULL DEFAULT NULL');
$db->query('UPDATE coel_structure SET cs_latitude = NULL, cs_longitude = NULL WHERE cs_latitude = "0" AND cs_longitude = "0"');
$db->query('UPDATE coel_structure SET cs_nbre_personne = NULL WHERE cs_nbre_personne = 0');
// smooth:
$q = "SELECT * FROM coel_structure".
        " WHERE cs_latitude IS NULL OR cs_latitude = '' OR cs_longitude IS NULL".
        " OR cs_longitude = ''";
foreach($db->query($q)->fetchAll(PDO::FETCH_ASSOC) AS $params) {
        $params_override = array('countrycodes' => 'fr,de,ae');
        $id = $params['cs_id_structure'];
        if(array_key_exists($id, $cache)) {
                $db->query(sprintf("UPDATE coel_structure SET cs_latitude = %.9f, cs_longitude = %.9f WHERE cs_id_structure = %d",
                                                   $cache[$id]['lat'], $cache[$id]['lon'], $id));
                continue;
        }
        if(array_key_exists($id, @$cache['failed'])) {
                if(!DOUBLE_CHECK) continue;

                // mode texte-libre (plus efficace pour les cas tordus)
                $params_override['force-q'] = TRUE;
                // cf (très) mauvaise gestion des CP par Nominatim
                if($params['cs_ville']) {
                        unset($params['cs_code_postal']);
                }
                if($params['cs_adresse_01']) {
                        $params['cs_adresse_01'] = preg_replace(
                                '/.*(?:impasse|chemin|route|rue|avenue|boulevard|place|ville)(?:\s(?:du|des|de)\s)?(la\s)?/i',
                                '',
                                $params['cs_adresse_01']);
                }
        }

        $lonlat = array();
        if(Coel::coordGuess(Coel::addrReStruct($params), $lonlat, $params_override)) {
                unset($cache['failed'][$id]);
                $lat = $lonlat['lat'];
                $lon = $lonlat['lon'];
                $cache[$id] = $lonlat;
        }
        else {
                $cache['failed'][$id] = FALSE;
        }

        echo "sleep\n";
        sleep(1.5);
}
if($cache) {
        ksort($cache, SORT_NUMERIC);
        file_put_contents(_GEO_STORE_CACHE, serialize($cache));
        echo "saved " . count($cache) . " results\n";
}



/* raw:
   $q = "SELECT cs_id_structure AS id, cs_adresse_01 AS addr, cs_code_postal AS cp, cs_ville AS city".
   " FROM coel_structure".
   " WHERE cs_latitude IS NULL OR cs_latitude = '' OR cs_longitude IS NULL OR cs_longitude = ''";
   $query = http_build_query(array('q' => implode(',',$q),'accept_language' => 'fr', 'format' => 'json', 'limit' => 1));
   var_dump( json_decode(file_get_contents('http://nominatim.openstreetmap.org/search.php?' . $query)) );
*/