Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev Author Line No. Line
156 jpm 1
<?php
2
//declare(encoding='UTF-8');
3
/**
4
 * Classe permettant de :
5
 *  - rajouter l'objet point centroide de la commune.
6
 *  - charger la bdd
7
 * Exemple de lancement du script : :
8
 * /opt/lampp/bin/php cli.php wikipedia -a chargerTous
9
 *
10
 * @category	php 5.2
11
 * @package		eFlore/Scripts
12
 * @author		Mohcen BENMOUNAH <mohcen@tela-botanica.org>
13
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
14
 * @copyright	Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
15
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
16
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
17
 * @version		$Id$
18
 */
19
class Wikipedia extends EfloreScript {
20
	private $tableMeta = '';
21
	private $cheminFichierMeta = '';
22
	private $tableCommunes = '';
23
	private $cheminFichierCommunes = '';
24
 
25
	public function executer() {
26
		try {
27
			$this->initialiserProjet('wikipedia');
28
			$this->tableMeta = Config::get('tables.wikipediaMeta');
29
			$this->cheminFichierMeta = Config::get('chemins.wikipediaMeta');
30
			$this->tableCommunes = Config::get('tables.wikipediaCommunes');
31
			$this->cheminFichierCommunes = Config::get('chemins.wikipediaCommunes');
32
 
33
			// Lancement de l'action demandée
34
			$cmd = $this->getParametre('a');
35
			switch ($cmd) {
36
				case 'chargerTous' :
37
					$this->chargerStructureSql();
38
					$this->chargerMetaDonnees();
39
					$this->chargerWikipediaCommunes();
40
					$this->preparerTable();
41
					$this->recupererPoints();
42
				case 'points' :
43
					$this->preparerTable();
44
					$this->recupererPoints();
45
					break;
46
				case 'supprimerTous' :
47
					$this->supprimerTous();
48
					break;
49
				default :
50
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
51
			}
52
		} catch (Exception $e) {
53
			$this->traiterErreur($e->getMessage());
54
		}
55
	}
56
 
57
	protected function chargerMetaDonnees() {
58
		$contenuSql = $this->recupererContenu($this->cheminFichierMeta);
59
		$this->executerScripSql($contenuSql);
60
	}
61
 
62
	private function chargerWikipediaCommunes() {
63
		$requete = "LOAD DATA INFILE '{$this->cheminFichierCommunes}' ".
64
			"REPLACE INTO TABLE {$this->tableCommunes} ".
65
			'CHARACTER SET utf8 '.
66
			'FIELDS '.
67
			"	TERMINATED BY ',' ".
68
			"	ENCLOSED BY '\"' ".
69
			"	ESCAPED BY '\\\' ".
70
			'IGNORE 1 LINES';
71
		$this->getBdd()->requeter($requete);
72
	}
73
 
74
	private function preparerTable() {
75
		$requete = 	"ALTER TABLE {$this->tableCommunes} ".
76
					'DROP centroide ';
77
		$this->getBdd()->requeter($requete);
78
 
79
		$requete = 	"ALTER TABLE {$this->tableCommunes} ".
80
					'	ADD centroide point NOT NULL ';
81
		$this->getBdd()->requeter($requete);
82
 
83
		$requete = 	"ALTER TABLE {$this->tableCommunes} ".
84
					'	ADD INDEX (centroide) ';
85
		$this->getBdd()->requeter($requete);
86
	}
87
 
88
	private function recupererPoints() {
89
		$requete = 	'SELECT * '.
90
					"FROM {$this->tableCommunes} ";
91
		$LatLons = $this->getBdd()->recupererTous($requete);
92
 
93
		foreach ($LatLons as $LatLon) {
94
			$latitude_degre = $LatLon['latitude'];
95
			$longitude_degre = $LatLon['longitude'];
96
			$insee = $LatLon['code_insee'];
97
			$this->formerPointCentre($latitude_degre, $longitude_degre, $insee);
98
			$this->afficherAvancement('Analyse des communes Wikipedia');
99
		}
100
	}
101
 
102
	private function formerPointCentre($latitude_degre, $longitude_degre, $insee) {
103
		$centre = "$latitude_degre $longitude_degre";
104
		$requete = "UPDATE {$this->tableCommunes} ".
105
					"SET centroide = POINTFROMTEXT('POINT($centre)') ".
106
					"WHERE code_insee = $insee ";
107
		$this->getBdd()->requeter($requete);
108
	}
109
 
110
	private function supprimerTous() {
349 jpm 111
		$requete = "DROP TABLE IF EXISTS {$this->tableMeta}, {$this->tableCommunes}";
156 jpm 112
		$this->getBdd()->requeter($requete);
113
	}
114
}
115
?>