Subversion Repositories eFlore/Applications.coel

Rev

Rev 1652 | Rev 1666 | 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
4
pdo.dsn.dbcoel = "mysql:dbname=tb_coel;host=localhost"
5
mysql.default_user = root
6
mysql.default_password =
7
EOF
8
*/
9
 
10
date_default_timezone_set('Europe/Paris');
11
$DIR = dirname(__FILE__); // XXX: PHP-5.3
12
define("_GEO_STORE_CACHE", $DIR . "/nominatim-coords.cache.ser");
13
 
14
require_once($DIR . '/../../jrest/services/Coel.php');
15
$db = new PDO('dbcoel',
16
			  ini_get('mysql.default_user'),
17
			  ini_get('mysql.default_password'),
18
			  array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
19
$cache = @unserialize(file_get_contents(_GEO_STORE_CACHE));
20
 
1658 raphael 21
if(function_exists('pcntl_signal')) {
22
	// SIGUSR1 handler to save the result during a run
23
	function savecache($signal = FALSE) {
24
		global $cache;
25
		if($cache) {
26
			ksort($cache, SORT_NUMERIC);
27
			file_put_contents(_GEO_STORE_CACHE, serialize($cache));
28
		}
29
		echo "saved " . count($cache) . " results\n";
30
		if($signal == SIGINT) exit;
31
	}
32
	declare(ticks = 1);
33
	pcntl_signal(SIGUSR1, 'savecache');
34
	pcntl_signal(SIGINT, 'savecache');
35
	echo '$ kill -USR1 ' . posix_getpid() . ' # to flush results to disk' . "\n";
36
}
1652 raphael 37
 
1658 raphael 38
printf("cache already contains %d entries, will start in 4 seconds:\n", count($cache));
39
sleep(4);
1652 raphael 40
 
41
// smooth:
42
$q = "SELECT * FROM coel_structure".
1658 raphael 43
	" WHERE cs_latitude IS NULL OR cs_latitude = '' OR cs_longitude IS NULL".
44
	" OR cs_longitude = ''";
1652 raphael 45
foreach($db->query($q)->fetchAll(PDO::FETCH_ASSOC) AS $params) {
1658 raphael 46
	$params_override = array('countrycodes' => 'fr,de,ae');
1652 raphael 47
	$id = $params['cs_id_structure'];
48
	if(array_key_exists($id, $cache)) {
49
		// TODO: UPDATE
50
		continue;
51
	}
52
	if(array_key_exists($id, @$cache['failed'])) {
1658 raphael 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']);
63
		}
64
		else {
65
			continue;
66
		}
1652 raphael 67
		// TODO: UPDATE
1658 raphael 68
		//continue;
1652 raphael 69
	}
70
 
71
	$lonlat = array();
1658 raphael 72
	if(Coel::coordGuess(Coel::addrReStruct($params), $lonlat, $params_override)) {
73
		unset($cache['failed'][$id]);
1652 raphael 74
		$lat = $lonlat['lat'];
75
		$lon = $lonlat['lon'];
76
		$cache[$id] = $lonlat;
77
	}
78
	else {
79
		$cache['failed'][$id] = FALSE;
80
	}
81
 
82
	echo "sleep\n";
83
	sleep(1.5);
84
}
85
if($cache) {
86
	ksort($cache, SORT_NUMERIC);
87
	file_put_contents(_GEO_STORE_CACHE, serialize($cache));
1658 raphael 88
	echo "saved " . count($cache) . " results\n";
1652 raphael 89
}
1658 raphael 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
*/