Subversion Repositories eFlore/Applications.coel

Rev

Rev 1652 | Rev 1666 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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