Subversion Repositories eFlore/Applications.coel

Rev

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