Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev Author Line No. Line
148 jpm 1
<?php
2
//declare(encoding='UTF-8');
3
/**
4
 * Classe permettant de :
5
 *  - convertir la latitude et longitude en degre, et 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 lion1906 -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 Lion1906 extends EfloreScript {
20
 
21
	public function executer() {
150 jpm 22
		try {
23
			$this->initialiserProjet('lion1906');
148 jpm 24
 
150 jpm 25
			// Lancement de l'action demandée
26
			$cmd = $this->getParametre('a');
27
			switch ($cmd) {
28
				case 'chargerTous' :
29
					$this->chargerStructureSql();
30
					$this->chargerMetaDonnees();
31
					$this->chargerLion1906();
32
					$this->preparerTable();
33
					$this->convertirRadianEnDegre();
34
					break;
35
				case 'convertir' :
36
					$this->preparerTable();
37
					$this->convertirRadianEnDegre();
38
					break;
39
				case 'supprimerTous' :
40
					$this->supprimerTous();
41
					break;
42
				default :
43
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
44
			}
45
		} catch (Exception $e) {
46
			$this->traiterErreur($e->getMessage());
148 jpm 47
		}
150 jpm 48
	}
148 jpm 49
 
150 jpm 50
	protected function chargerMetaDonnees() {
51
		$contenuSql = $this->recupererContenu(Config::get('chemins.lion1906Meta'));
52
		$this->executerScripSql($contenuSql);
53
	}
148 jpm 54
 
150 jpm 55
	private function chargerLion1906() {
56
		$chemin = Config::get('chemins.lion1906');
57
		$table = Config::get('tables.lion1906');
58
		$requete = "LOAD DATA INFILE '$chemin' ".
59
					"REPLACE INTO TABLE $table ".
60
					'CHARACTER SET latin1 '.
61
					'FIELDS '.
62
					"	TERMINATED BY ';' ".
63
					"	ENCLOSED BY '' ".
64
					"	ESCAPED BY '\\\' ".
65
					'IGNORE 1 LINES';
66
		$this->getBdd()->requeter($requete);
67
	}
148 jpm 68
 
69
	private function convertirRadianEnDegre() {
70
		$table = Config::get('tables.lion1906');
71
		$requete = 	'SELECT insee, latitude_radian, longitude_radian '.
72
					"FROM $table ";
73
		$LatLons = $this->getBdd()->recupererTous($requete);
74
 
75
		foreach ($LatLons as $LatLon) {
76
			$insee = $LatLon['insee'] ;
77
			$latitude_degre = $LatLon['latitude_radian'] * 180 / pi();
78
			$longitude_degre = $LatLon['longitude_radian'] * 180 / pi();
79
			$latitude_degre = str_replace(',', '.', $latitude_degre);
80
			$longitude_degre = str_replace(',', '.', $longitude_degre);
81
			$this->formerPointCentre($latitude_degre, $longitude_degre, $insee);
82
			$this->afficherAvancement('Analyse des communes Lion1906');
83
		}
84
	}
85
 
86
	private function preparerTable() {
87
		$table = Config::get('tables.lion1906');
88
		$requete = 	"ALTER TABLE $table ".
150 jpm 89
					'DROP latitude_degre, '.
90
					'DROP longitude_degre, '.
91
					'DROP centroide, '.
92
					'DROP INDEX insee ';
148 jpm 93
		$this->getBdd()->requeter($requete);
94
 
95
		$requete = 	"ALTER TABLE $table ".
96
					'	ADD latitude_degre double NOT NULL , '.
97
					'	ADD longitude_degre double NOT NULL , '.
98
					'	ADD centroide point NOT NULL ';
99
		$this->getBdd()->requeter($requete);
100
 
101
		$requete = 	"ALTER TABLE $table ".
102
					'ADD INDEX insee (insee, latitude_degre, longitude_degre, centroide) ';
103
		$this->getBdd()->requeter($requete);
104
	}
105
 
106
	private function formerPointCentre($latitude_degre, $longitude_degre, $insee) {
107
		$centre = "$latitude_degre $longitude_degre" ;
108
		$table = Config::get('tables.lion1906');
109
		$requete = "UPDATE $table ".
110
					"SET latitude_degre = '$latitude_degre', longitude_degre = '$longitude_degre', ".
150 jpm 111
					"	centroide = POINTFROMTEXT('POINT($centre)') ".
112
					"WHERE insee = '$insee' ";
148 jpm 113
		$this->getBdd()->requeter($requete);
114
	}
115
 
116
	private function supprimerTous() {
349 jpm 117
		$requete = "DROP TABLE IF EXISTS lion1906_meta, lion1906_communes_v2008";
148 jpm 118
		$this->getBdd()->requeter($requete);
119
	}
120
 
121
}
122
?>