Subversion Repositories eFlore/Applications.coel

Rev

Rev 1692 | Go to most recent revision | 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
 
1666 raphael 44
// correction
45
$db->query('UPDATE coel_structure SET cs_longitude = REPLACE(cs_longitude, ",", "."), cs_latitude = REPLACE(cs_latitude, ",", ".")');
46
$db->query('ALTER TABLE coel_structure MODIFY cs_longitude DOUBLE(12,9) NULL DEFAULT NULL');
47
$db->query('ALTER TABLE coel_structure MODIFY cs_latitude DOUBLE(12,9) NULL DEFAULT NULL');
48
$db->query('UPDATE coel_structure SET cs_latitude = NULL, cs_longitude = NULL WHERE cs_latitude = "0" AND cs_longitude = "0"');
1692 raphael 49
$db->query('UPDATE coel_structure SET cs_nbre_personne = NULL WHERE cs_nbre_personne = 0');
1652 raphael 50
// smooth:
51
$q = "SELECT * FROM coel_structure".
1658 raphael 52
	" WHERE cs_latitude IS NULL OR cs_latitude = '' OR cs_longitude IS NULL".
53
	" OR cs_longitude = ''";
1652 raphael 54
foreach($db->query($q)->fetchAll(PDO::FETCH_ASSOC) AS $params) {
1658 raphael 55
	$params_override = array('countrycodes' => 'fr,de,ae');
1652 raphael 56
	$id = $params['cs_id_structure'];
57
	if(array_key_exists($id, $cache)) {
1666 raphael 58
		$db->query(sprintf("UPDATE coel_structure SET cs_latitude = %.9f, cs_longitude = %.9f WHERE cs_id_structure = %d",
59
						   $cache[$id]['lat'], $cache[$id]['lon'], $id));
1652 raphael 60
		continue;
61
	}
62
	if(array_key_exists($id, @$cache['failed'])) {
1666 raphael 63
		if(!DOUBLE_CHECK) continue;
64
 
1658 raphael 65
		// mode texte-libre (plus efficace pour les cas tordus)
66
		$params_override['force-q'] = TRUE;
67
		// cf (très) mauvaise gestion des CP par Nominatim
68
		if($params['cs_ville']) {
69
			unset($params['cs_code_postal']);
70
		}
71
		if($params['cs_adresse_01']) {
1666 raphael 72
			$params['cs_adresse_01'] = preg_replace(
73
				'/.*(?:impasse|chemin|route|rue|avenue|boulevard|place|ville)(?:\s(?:du|des|de)\s)?(la\s)?/i',
74
				'',
75
				$params['cs_adresse_01']);
1658 raphael 76
		}
1652 raphael 77
	}
78
 
79
	$lonlat = array();
1658 raphael 80
	if(Coel::coordGuess(Coel::addrReStruct($params), $lonlat, $params_override)) {
81
		unset($cache['failed'][$id]);
1652 raphael 82
		$lat = $lonlat['lat'];
83
		$lon = $lonlat['lon'];
84
		$cache[$id] = $lonlat;
85
	}
86
	else {
87
		$cache['failed'][$id] = FALSE;
88
	}
89
 
90
	echo "sleep\n";
91
	sleep(1.5);
92
}
93
if($cache) {
94
	ksort($cache, SORT_NUMERIC);
95
	file_put_contents(_GEO_STORE_CACHE, serialize($cache));
1658 raphael 96
	echo "saved " . count($cache) . " results\n";
1652 raphael 97
}
1658 raphael 98
 
99
 
100
 
101
/* raw:
102
   $q = "SELECT cs_id_structure AS id, cs_adresse_01 AS addr, cs_code_postal AS cp, cs_ville AS city".
103
   " FROM coel_structure".
104
   " WHERE cs_latitude IS NULL OR cs_latitude = '' OR cs_longitude IS NULL OR cs_longitude = ''";
105
   $query = http_build_query(array('q' => implode(',',$q),'accept_language' => 'fr', 'format' => 'json', 'limit' => 1));
106
   var_dump( json_decode(file_get_contents('http://nominatim.openstreetmap.org/search.php?' . $query)) );
107
*/