Subversion Repositories eFlore/Applications.coel

Rev

Rev 1713 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1652 raphael 1
<?php
2
/*
1696 raphael 3
  cat >> /etc/php.d/php.ini // /etc/php5/cli/conf.d/ZZ-mycustom.ini <<EOF
1666 raphael 4
pdo.dsn.testcoel = "mysql:dbname=tb_coel_test;host=localhost"
1696 raphael 5
mysql.default_user = telabotap
1652 raphael 6
mysql.default_password =
7
EOF
8
*/
1666 raphael 9
if(PHP_SAPI !== 'cli') exit(1);
1652 raphael 10
 
1666 raphael 11
define('DOUBLE_CHECK', FALSE);
12
 
1652 raphael 13
date_default_timezone_set('Europe/Paris');
14
$DIR = dirname(__FILE__); // XXX: PHP-5.3
15
define("_GEO_STORE_CACHE", $DIR . "/nominatim-coords.cache.ser");
16
 
17
require_once($DIR . '/../../jrest/services/Coel.php');
1666 raphael 18
$db = new PDO('testcoel',
19
			  @$argv[1] ? $argv[1] : ini_get('mysql.default_user'),
20
			  @$argv[2] ? $argv[2] : ini_get('mysql.default_password'),
21
			  array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'")) or die('no db');
1652 raphael 22
$cache = @unserialize(file_get_contents(_GEO_STORE_CACHE));
23
 
1658 raphael 24
if(function_exists('pcntl_signal')) {
25
	// SIGUSR1 handler to save the result during a run
26
	function savecache($signal = FALSE) {
27
		global $cache;
28
		if($cache) {
29
			ksort($cache, SORT_NUMERIC);
30
			file_put_contents(_GEO_STORE_CACHE, serialize($cache));
31
		}
32
		echo "saved " . count($cache) . " results\n";
33
		if($signal == SIGINT) exit;
34
	}
35
	declare(ticks = 1);
36
	pcntl_signal(SIGUSR1, 'savecache');
37
	pcntl_signal(SIGINT, 'savecache');
38
	echo '$ kill -USR1 ' . posix_getpid() . ' # to flush results to disk' . "\n";
39
}
1652 raphael 40
 
1658 raphael 41
printf("cache already contains %d entries, will start in 4 seconds:\n", count($cache));
42
sleep(4);
1652 raphael 43
 
44
// smooth:
45
$q = "SELECT * FROM coel_structure".
1658 raphael 46
	" WHERE cs_latitude IS NULL OR cs_latitude = '' OR cs_longitude IS NULL".
47
	" OR cs_longitude = ''";
1652 raphael 48
foreach($db->query($q)->fetchAll(PDO::FETCH_ASSOC) AS $params) {
1658 raphael 49
	$params_override = array('countrycodes' => 'fr,de,ae');
1652 raphael 50
	$id = $params['cs_id_structure'];
51
	if(array_key_exists($id, $cache)) {
1666 raphael 52
		$db->query(sprintf("UPDATE coel_structure SET cs_latitude = %.9f, cs_longitude = %.9f WHERE cs_id_structure = %d",
53
						   $cache[$id]['lat'], $cache[$id]['lon'], $id));
1652 raphael 54
		continue;
55
	}
56
	if(array_key_exists($id, @$cache['failed'])) {
1666 raphael 57
		if(!DOUBLE_CHECK) continue;
58
 
1658 raphael 59
		// mode texte-libre (plus efficace pour les cas tordus)
60
		$params_override['force-q'] = TRUE;
61
		// cf (très) mauvaise gestion des CP par Nominatim
62
		if($params['cs_ville']) {
63
			unset($params['cs_code_postal']);
64
		}
65
		if($params['cs_adresse_01']) {
1666 raphael 66
			$params['cs_adresse_01'] = preg_replace(
67
				'/.*(?:impasse|chemin|route|rue|avenue|boulevard|place|ville)(?:\s(?:du|des|de)\s)?(la\s)?/i',
68
				'',
69
				$params['cs_adresse_01']);
1658 raphael 70
		}
1652 raphael 71
	}
72
 
73
	$lonlat = array();
1658 raphael 74
	if(Coel::coordGuess(Coel::addrReStruct($params), $lonlat, $params_override)) {
75
		unset($cache['failed'][$id]);
1652 raphael 76
		$lat = $lonlat['lat'];
77
		$lon = $lonlat['lon'];
78
		$cache[$id] = $lonlat;
79
	}
80
	else {
81
		$cache['failed'][$id] = FALSE;
82
	}
83
 
84
	echo "sleep\n";
85
	sleep(1.5);
86
}
87
if($cache) {
88
	ksort($cache, SORT_NUMERIC);
89
	file_put_contents(_GEO_STORE_CACHE, serialize($cache));
1658 raphael 90
	echo "saved " . count($cache) . " results\n";
1652 raphael 91
}
1658 raphael 92
 
93
 
94
 
95
/* raw:
96
   $q = "SELECT cs_id_structure AS id, cs_adresse_01 AS addr, cs_code_postal AS cp, cs_ville AS city".
97
   " FROM coel_structure".
98
   " WHERE cs_latitude IS NULL OR cs_latitude = '' OR cs_longitude IS NULL OR cs_longitude = ''";
99
   $query = http_build_query(array('q' => implode(',',$q),'accept_language' => 'fr', 'format' => 'json', 'limit' => 1));
100
   var_dump( json_decode(file_get_contents('http://nominatim.openstreetmap.org/search.php?' . $query)) );
101
*/