Subversion Repositories eFlore/Applications.coel

Compare Revisions

Ignore whitespace Rev 1651 → Rev 1652

/trunk/scripts/modules/201310-bulk-coord-update.php
New file
0,0 → 1,65
<?php
/*
cat >> /etc/php.d/php.ini <<EOF
pdo.dsn.dbcoel = "mysql:dbname=tb_coel;host=localhost"
mysql.default_user = root
mysql.default_password =
EOF
*/
 
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('dbcoel',
ini_get('mysql.default_user'),
ini_get('mysql.default_password'),
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
 
$cache = @unserialize(file_get_contents(_GEO_STORE_CACHE));
 
/* 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)) );
*/
 
 
// smooth:
$q = "SELECT * FROM coel_structure".
" WHERE cs_latitude IS NULL OR cs_latitude = '' OR cs_longitude IS NULL OR cs_longitude = '' LIMIT 85";
 
foreach($db->query($q)->fetchAll(PDO::FETCH_ASSOC) AS $params) {
$id = $params['cs_id_structure'];
if(array_key_exists($id, $cache)) {
// TODO: UPDATE
continue;
}
if(array_key_exists($id, @$cache['failed'])) {
// TODO: UPDATE
continue;
}
 
$lonlat = array();
if(Coel::coordGuess(Coel::addrReStruct($params), $lonlat, array('countrycodes' => 'fr,de,ae'))) {
$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));
}