Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1002 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1002 Rev 1004
1
<?php
1
<?php
2
/** Exemple lancement:
2
/** Exemple lancement:
3
 * /opt/lampp/bin/php -d memory_limit=3500M cli.php ifn -a chargerTous
3
 * /opt/lampp/bin/php -d memory_limit=3500M cli.php ifn -a chargerTous
4
 * Options :
4
 * Options :
5
 * -t : Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).
5
 * -t : Permet de tester le script sur un jeux réduit de données (indiquer le nombre de lignes).
6
*/
6
*/
7
class Ifn extends EfloreScript {
7
class Ifn extends EfloreScript {
8
 
8
 
9
	public function executer() {
9
	public function executer() {
10
		// Lancement de l'action demandée
10
		// Lancement de l'action demandée
11
		try {
11
		try {
12
			$this->initialiserProjet('ifn');
12
			$this->initialiserProjet('ifn');
13
 
13
 
14
			$cmd = $this->getParametre('a');
14
			$cmd = $this->getParametre('a');
15
			switch ($cmd) {
15
			switch ($cmd) {
16
				case 'chargerTous' :
16
				case 'chargerTous' :
17
					$this->chargerStructureSql();
17
					$this->chargerStructureSql();
18
					$this->chargerDonnees("documentationFlore");
18
					$this->chargerDonnees("documentationFlore");
19
					$this->chargerDonneesAnnuelles();
19
					$this->chargerDonneesAnnuelles();
20
					include_once dirname(__FILE__)."/bibliotheque/proj4php/proj4php.php";
20
					include_once dirname(__FILE__)."/bibliotheque/proj4php/proj4php.php";
21
					$this->genererCoordonneesWgs('placettesForet');
21
					$this->genererCoordonneesWgs('placettesForet');
22
					$this->genererCoordonneesWgs('placettesPeupleraie');
22
					$this->genererCoordonneesWgs('placettesPeupleraie');
-
 
23
					$this->genererCodeInsee('placettesForet');
-
 
24
					$this->genererCodeInsee('placettesPeupleraie');
-
 
25
					$this->creerVueTapir();
-
 
26
					$this->ajouterTupleEfloreOntologies();
23
					break;
27
					break;
24
				case 'chargerStructure' :
28
				case 'chargerStructure' :
25
					$this->chargerStructureSql();
29
					$this->chargerStructureSql();
26
					break;
30
					break;
27
				case 'chargerDonnees' :
31
				case 'chargerDonnees' :
28
					$this->chargerDonnees("documentationFlore");
32
					$this->chargerDonnees("documentationFlore");
29
					$this->chargerDonneesAnnuelles();
33
					$this->chargerDonneesAnnuelles();
30
					break;
34
					break;
31
				case 'genererCoordWgs' :
35
				case 'genererCoordWgs' :
32
					include_once dirname(__FILE__)."/bibliotheque/proj4php/proj4php.php";
36
					include_once dirname(__FILE__)."/bibliotheque/proj4php/proj4php.php";
33
					$this->genererCoordonneesWgs('placettesForet');
37
					$this->genererCoordonneesWgs('placettesForet');
34
					$this->genererCoordonneesWgs('placettesPeupleraie');
38
					$this->genererCoordonneesWgs('placettesPeupleraie');
35
					break;
39
					break;
36
				case 'genererCodeInsee' :
40
				case 'genererCodeInsee' :
37
					$this->genererCodeInsee('placettesForet');
41
					$this->genererCodeInsee('placettesForet');
38
					//$this->genererCodeInsee('placettesPeupleraie');
42
					$this->genererCodeInsee('placettesPeupleraie');
39
					break;
43
					break;
40
				case 'creerVueTapir' :
44
				case 'creerVueTapir' :
41
					$this->creerVueTapir();
45
					$this->creerVueTapir();
42
					$this->ajouterTupleEfloreOntologies();
46
					$this->ajouterTupleEfloreOntologies();
43
					break;
47
					break;
44
				case 'supprimerTous' :
48
				case 'supprimerTous' :
45
					$this->supprimerTous();
49
					$this->supprimerTous();
46
					break;
50
					break;
47
				default :
51
				default :
48
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
52
					throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
49
			}
53
			}
50
		} catch (Exception $e) {
54
		} catch (Exception $e) {
51
			$this->traiterErreur($e->getMessage());
55
			$this->traiterErreur($e->getMessage());
52
		}
56
		}
53
	}
57
	}
54
	
58
	
55
 
59
 
56
	private function chargerDonneesAnnuelles() {
60
	private function chargerDonneesAnnuelles() {
57
		$categories = explode(",",Config::get('categories'));
61
		$categories = explode(",",Config::get('categories'));
58
		$annees = explode(",",Config::get('versions'));
62
		$annees = explode(",",Config::get('versions'));
59
		foreach ($categories as $categorie) {
63
		foreach ($categories as $categorie) {
60
			foreach ($annees as $annee) {
64
			foreach ($annees as $annee) {
61
				$this->chargerDonnees($categorie, $annee."/");
65
				$this->chargerDonnees($categorie, $annee."/");
62
				Debug::printr($categorie." ".$annee."\n");
66
				Debug::printr($categorie." ".$annee."\n");
63
			}
67
			}
64
		}
68
		}
65
		
69
		
66
	}
70
	}
67
	
71
	
68
	private function chargerDonnees($categorie, $annee = '') {
72
	private function chargerDonnees($categorie, $annee = '') {
69
		$chemin = Config::get('dossierTsv').$annee.Config::get('fichiers.'.$categorie);
73
		$chemin = Config::get('dossierTsv').$annee.Config::get('fichiers.'.$categorie);
70
		$table = Config::get('tables.'.$categorie);
74
		$table = Config::get('tables.'.$categorie);
71
		$requete = "LOAD DATA INFILE '$chemin' ".
75
		$requete = "LOAD DATA INFILE '$chemin' ".
72
						"REPLACE INTO TABLE $table ".
76
						"REPLACE INTO TABLE $table ".
73
						'CHARACTER SET utf8 '.
77
						'CHARACTER SET utf8 '.
74
						'FIELDS '.
78
						'FIELDS '.
75
						"	TERMINATED BY ';' ".
79
						"	TERMINATED BY ';' ".
76
						"	ENCLOSED BY '' ".
80
						"	ENCLOSED BY '' ".
77
						"	ESCAPED BY '\\\' ".
81
						"	ESCAPED BY '\\\' ".
78
						'IGNORE 1 LINES';
82
						'IGNORE 1 LINES';
79
		$this->getBdd()->requeter($requete);
83
		$this->getBdd()->requeter($requete);
80
	}
84
	}
81
	
85
	
82
	private function genererNumNomBdtfx() {
86
	private function genererNumNomBdtfx() {
83
		$table = Config::get('tables.flore');
87
		$table = Config::get('tables.flore');
84
		$placettes = $this->recupererTuplesPrChpCoordonnees($table);
88
		$placettes = $this->recupererTuplesPrChpCoordonnees($table);
85
		$this->preparerTablePrCoordWgs($table);
89
		$this->preparerTablePrCoordWgs($table);
86
		foreach ($placettes as $placette) {
90
		foreach ($placettes as $placette) {
87
			$placette = $this->transformerCoordL93enWgs84($placette);
91
			$placette = $this->transformerCoordL93enWgs84($placette);
88
			$this->remplirChpCoordonnees($table, $placette);
92
			$this->remplirChpCoordonnees($table, $placette);
89
		}
93
		}
90
	}
94
	}
91
	
95
	
92
	private function creerVueTapir() {
96
	private function creerVueTapir() {
93
		$requete = "DROP VIEW ifn_tapir; CREATE VIEW ifn_tapir AS ".
97
		$requete = "DROP VIEW ifn_tapir; CREATE VIEW ifn_tapir AS ".
94
			"SELECT f.idp as observation_id, b.nom_sci as nom_scientifique_complet, b.num_nom, ".
98
			"SELECT f.idp as observation_id, b.nom_sci as nom_scientifique_complet, b.num_nom, ".
95
				" fo.lieu_commune_code_insee, fo.lieu_station_latitude, fo.lieu_station_longitude,".
99
				" fo.lieu_commune_code_insee, fo.lieu_station_latitude, fo.lieu_station_longitude,".
96
				" 'WGS84' AS geodeticDatum, e.dateeco as observation_date, '' AS observateur_nom_complet".
100
				" 'WGS84' AS geodeticDatum, e.dateeco as observation_date, '' AS observateur_nom_complet".
97
				" FROM `bdtfx_v2_02` b, ifn_flore f, ifn_ecologie e, ifn_placettes_foret fo".
101
				" FROM `bdtfx_v2_02` b, ifn_flore f, ifn_ecologie e, ifn_placettes_foret fo".
98
				" WHERE f.idp = e.idp".
102
				" WHERE f.idp = e.idp".
99
				" AND e.idp = fo.idp".
103
				" AND e.idp = fo.idp".
100
				" AND b.cd_ref = f.cd_ref";
104
				" AND b.cd_ref = f.cd_ref";
101
		$this->getBdd()->requeter($requete);
105
		$this->getBdd()->requeter($requete);
102
	}
106
	}
103
	
107
	
104
	private function ajouterTupleEfloreOntologies(){ // pour la légende
108
	private function ajouterTupleEfloreOntologies(){ // pour la légende
105
		$requete = "INSERT INTO `eflore_ontologies`(id,`classe_id`, `nom`, `description`, `code`, `complements`) VALUES ".
109
		$requete = "INSERT INTO `eflore_ontologies`(id,`classe_id`, `nom`, `description`, `code`, `complements`) VALUES ".
106
				"(24,10,'IFN',".
110
				"(24,10,'IFN',".
107
				"'données issues des données brutes mises en ligne de l\'Inventaire Forestier National',".
111
				"'données issues des données brutes mises en ligne de l\'Inventaire Forestier National',".
108
				"'ifn','legende=#F2B148')";
112
				"'ifn','legende=#F2B148')";
109
		$this->getBdd()->requeter($requete);
113
		$this->getBdd()->requeter($requete);
110
	}
114
	}
111
	
115
	
112
	private function genererCoordonneesWgs($categorie) {
116
	private function genererCoordonneesWgs($categorie) {
113
		$table = Config::get('tables.'.$categorie);
117
		$table = Config::get('tables.'.$categorie);
114
		$placettes = $this->recupererTuplesPrChpCoordonnees($table);
118
		$placettes = $this->recupererTuplesPrChpCoordonnees($table);
115
		$this->preparerTablePrCoordWgs($table);
119
		$this->preparerTablePrCoordWgs($table);
116
		foreach ($placettes as $placette) {
120
		foreach ($placettes as $placette) {
117
			$placette = $this->transformerCoordL93enWgs84($placette);
121
			$placette = $this->transformerCoordL93enWgs84($placette);
118
			$this->remplirChpCoordonnees($table, $placette);
122
			$this->remplirChpCoordonnees($table, $placette);
119
		}
123
		}
120
	}
124
	}
121
	
125
	
122
	private function transformerCoordL93enWgs84($coord) {
126
	private function transformerCoordL93enWgs84($coord) {
123
		$proj4 = new Proj4php();
127
		$proj4 = new Proj4php();
124
		$projL93 = new Proj4phpProj('EPSG:2154',$proj4);
128
		$projL93 = new Proj4phpProj('EPSG:2154',$proj4);
125
		$projWGS84 = new Proj4phpProj('EPSG:4326',$proj4);
129
		$projWGS84 = new Proj4phpProj('EPSG:4326',$proj4);
126
		$projLI = new Proj4phpProj('EPSG:27571',$proj4);
130
		$projLI = new Proj4phpProj('EPSG:27571',$proj4);
127
		$projLSud = new Proj4phpProj('EPSG:27563',$proj4);
131
		$projLSud = new Proj4phpProj('EPSG:27563',$proj4);
128
		$projL72 = new Proj4phpProj('EPSG:31370',$proj4); 
132
		$projL72 = new Proj4phpProj('EPSG:31370',$proj4); 
129
		
133
		
130
		$pointSrc = new proj4phpPoint($coord['xl93'],$coord['yl93']);
134
		$pointSrc = new proj4phpPoint($coord['xl93'],$coord['yl93']);
131
		$pointDest = $proj4->transform($projL93,$projWGS84,$pointSrc);
135
		$pointDest = $proj4->transform($projL93,$projWGS84,$pointSrc);
132
		$coord['longitude_wgs'] = '"'.number_format($pointDest->x, 6).'"';
136
		$coord['longitude_wgs'] = '"'.number_format($pointDest->x, 6).'"';
133
		$coord['latitude_wgs'] = '"'.number_format($pointDest->y, 6).'"';
137
		$coord['latitude_wgs'] = '"'.number_format($pointDest->y, 6).'"';
134
		return $coord;
138
		return $coord;
135
	}
139
	}
136
	
140
	
137
	private function recupererTuplesPrChpCoordonnees($table) {
141
	private function recupererTuplesPrChpCoordonnees($table) {
138
		$requete = 'SELECT idp, xl93, yl93 '.
142
		$requete = 'SELECT idp, xl93, yl93 '.
139
				"FROM {$table} ";
143
				"FROM {$table} ";
140
		$resultat = $this->getBdd()->recupererTous($requete);
144
		$resultat = $this->getBdd()->recupererTous($requete);
141
		return $resultat;
145
		return $resultat;
142
	}
146
	}
143
	
147
	
144
	private function remplirChpCoordonnees($table, $placette) {
148
	private function remplirChpCoordonnees($table, $placette) {
145
		$requete = "UPDATE {$table} ".
149
		$requete = "UPDATE {$table} ".
146
			" SET lieu_station_longitude = {$placette['longitude_wgs']} , lieu_station_latitude = {$placette['latitude_wgs']} ".
150
			" SET lieu_station_longitude = {$placette['longitude_wgs']} , lieu_station_latitude = {$placette['latitude_wgs']} ".
147
			" WHERE idp = {$placette['idp']} ";
151
			" WHERE idp = {$placette['idp']} ";
148
		$resultat = $this->getBdd()->requeter($requete);
152
		$resultat = $this->getBdd()->requeter($requete);
149
		if ($resultat === false) {
153
		if ($resultat === false) {
150
			throw new Exception("Erreur d'insertion pour le tuple $idp");
154
			throw new Exception("Erreur d'insertion pour le tuple $idp");
151
		}
155
		}
152
	}
156
	}
153
 
157
 
154
	private function preparerTablePrCoordWgs($table) {
158
	private function preparerTablePrCoordWgs($table) {
155
		$requete = "SHOW COLUMNS FROM {$table} LIKE 'lieu_station_latitude' ";
159
		$requete = "SHOW COLUMNS FROM {$table} LIKE 'lieu_station_latitude' ";
156
		$resultat = $this->getBdd()->recuperer($requete);
160
		$resultat = $this->getBdd()->recuperer($requete);
157
		if ($resultat === false) {
161
		if ($resultat === false) {
158
			$requete = 	"ALTER TABLE {$table} ".
162
			$requete = 	"ALTER TABLE {$table} ".
159
					' ADD `lieu_station_latitude` DECIMAL( 9, 6 ),'.
163
					' ADD `lieu_station_latitude` DECIMAL( 9, 6 ),'.
160
					' ADD `lieu_station_longitude` DECIMAL( 9, 6 )';
164
					' ADD `lieu_station_longitude` DECIMAL( 9, 6 )';
161
			$this->getBdd()->requeter($requete);
165
			$this->getBdd()->requeter($requete);
162
		}
166
		}
163
	}
167
	}
164
 
168
 
165
	private function genererCodeInsee($categorie) {
169
	private function genererCodeInsee($categorie) {
166
		$table = Config::get('tables.'.$categorie);
170
		$table = Config::get('tables.'.$categorie);
167
		$this->preparerTablePrChpCodeInsee($table);
171
		$this->preparerTablePrChpCodeInsee($table);
168
		$liste_coordonnees = $this->recupererTuplesPrChpCodeInsee($table);
172
		$liste_coordonnees = $this->recupererTuplesPrChpCodeInsee($table);
169
		foreach ($liste_coordonnees as $coordonnees) {
173
		foreach ($liste_coordonnees as $coordonnees) {
170
			$code_insee = $this->chercherCodeCommune($coordonnees['latitude'], $coordonnees['longitude']);
174
			$code_insee = $this->chercherCodeCommune($coordonnees['latitude'], $coordonnees['longitude']);
171
			if ($code_insee != "") {
175
			if ($code_insee != "") {
172
				$this->remplirChpCodeInsee($table, $coordonnees['latitude'], $coordonnees['longitude'], $code_insee);
176
				$this->remplirChpCodeInsee($table, $coordonnees['latitude'], $coordonnees['longitude'], $code_insee);
173
			}
177
			}
174
		}
178
		}
175
	}
179
	}
176
	private function preparerTablePrChpCodeInsee($table) {
180
	private function preparerTablePrChpCodeInsee($table) {
177
		$requete = "SHOW COLUMNS FROM {$table} LIKE 'lieu_commune_code_insee' ";
181
		$requete = "SHOW COLUMNS FROM {$table} LIKE 'lieu_commune_code_insee' ";
178
		$resultat = $this->getBdd()->recuperer($requete);
182
		$resultat = $this->getBdd()->recuperer($requete);
179
		if ($resultat === false) {
183
		if ($resultat === false) {
180
			$requete = 	"ALTER TABLE {$table} ".
184
			$requete = 	"ALTER TABLE {$table} ".
181
						' ADD `lieu_commune_code_insee` VARCHAR(5);';
185
						' ADD `lieu_commune_code_insee` VARCHAR(5);';
182
			$this->getBdd()->requeter($requete);
186
			$this->getBdd()->requeter($requete);
183
		}
187
		}
184
	}
188
	}
185
	private function recupererTuplesPrChpCodeInsee($table) {
189
	private function recupererTuplesPrChpCodeInsee($table) {
186
		$requete = 'SELECT distinct lieu_station_latitude as latitude, lieu_station_longitude as longitude '.
190
		$requete = 'SELECT distinct lieu_station_latitude as latitude, lieu_station_longitude as longitude '.
187
					"FROM {$table} WHERE lieu_commune_code_insee IS NULL ";
191
					"FROM {$table} WHERE lieu_commune_code_insee IS NULL ";
188
		$resultat = $this->getBdd()->recupererTous($requete);
192
		$resultat = $this->getBdd()->recupererTous($requete);
189
		return $resultat;
193
		return $resultat;
190
	}
194
	}
191
	private function remplirChpCodeInsee($table, $latitude, $longitude, $code_insee) {
195
	private function remplirChpCodeInsee($table, $latitude, $longitude, $code_insee) {
192
		$requete = "UPDATE {$table} ".
196
		$requete = "UPDATE {$table} ".
193
				" SET lieu_commune_code_insee = '{$code_insee}'".
197
				" SET lieu_commune_code_insee = '{$code_insee}'".
194
				" WHERE lieu_station_longitude = {$longitude} AND lieu_station_latitude = {$latitude} ";
198
				" WHERE lieu_station_longitude = {$longitude} AND lieu_station_latitude = {$latitude} ";
195
		$resultat = $this->getBdd()->requeter($requete);
199
		$resultat = $this->getBdd()->requeter($requete);
196
		if ($resultat === false) {
200
		if ($resultat === false) {
197
			throw new Exception("Erreur d'insertion pour le tuple $idp");
201
			throw new Exception("Erreur d'insertion pour le tuple $idp");
198
		}
202
		}
199
	}
203
	}
200
	
204
	
201
	private function chercherCodeCommune($latitude, $longitude) {
205
	private function chercherCodeCommune($latitude, $longitude) {
202
		$code_insee = '';
206
		$code_insee = '';
203
		if ($this->testerCoordonneesWgsFrance($latitude, $longitude)) {
207
		if ($this->testerCoordonneesWgsFrance($latitude, $longitude)) {
204
			$url_service = "api.tela-botanica.org/service:eflore:0.1/osm/nom-commune".
208
			$url_service = "api.tela-botanica.org/service:eflore:0.1/osm/nom-commune".
205
							"?lat={$latitude}&lon={$longitude}";
209
							"?lat={$latitude}&lon={$longitude}";
206
			$url_service = str_replace(',', '.', $url_service);
210
			$url_service = str_replace(',', '.', $url_service);
207
			$ch = curl_init($url_service);
211
			$ch = curl_init($url_service);
208
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
212
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
209
			$reponse = curl_exec($ch);
213
			$reponse = curl_exec($ch);
210
			$reponse = json_decode($reponse);
214
			$reponse = json_decode($reponse);
211
			if (isset($reponse->codeINSEE)) {
215
			if (isset($reponse->codeINSEE)) {
212
				$code_insee = $reponse->codeINSEE;
216
				$code_insee = $reponse->codeINSEE;
213
			}
217
			}
214
			curl_close($ch);
218
			curl_close($ch);
215
		}
219
		}
216
		return $code_insee;
220
		return $code_insee;
217
	}
221
	}
218
	
222
	
219
	private function testerCoordonneesWgsFrance($latitude, $longitude) {
223
	private function testerCoordonneesWgsFrance($latitude, $longitude) {
220
		$coord_france = false;
224
		$coord_france = false;
221
		if ($latitude != '' && $longitude != '') {
225
		if ($latitude != '' && $longitude != '') {
222
			if ($latitude < 51.071667 && $latitude > 41.316667) {
226
			if ($latitude < 51.071667 && $latitude > 41.316667) {
223
				if ($longitude < 9.513333 && $longitude > -5.140278) {
227
				if ($longitude < 9.513333 && $longitude > -5.140278) {
224
					$coord_france = true;
228
					$coord_france = true;
225
				}
229
				}
226
			}
230
			}
227
		}
231
		}
228
		return $coord_france;
232
		return $coord_france;
229
	}
233
	}
230
 
234
 
231
	private function supprimerTous() {
235
	private function supprimerTous() {
232
		$requete = "DROP TABLE IF EXISTS `ifn_arbres_forets`, `ifn_arbres_peupleraie`, `ifn_couverts_foret`,
236
		$requete = "DROP TABLE IF EXISTS `ifn_arbres_forets`, `ifn_arbres_peupleraie`, `ifn_couverts_foret`,
233
			 `ifn_documentation`, `ifn_documentation_flore`, `ifn_ecologie`, `ifn_flore`, `ifn_placettes_foret`,
237
			 `ifn_documentation`, `ifn_documentation_flore`, `ifn_ecologie`, `ifn_flore`, `ifn_placettes_foret`,
234
			 `ifn_placettes_peupleraie`;
238
			 `ifn_placettes_peupleraie`;
235
		";
239
		";
236
		$this->getBdd()->requeter($requete);
240
		$this->getBdd()->requeter($requete);
237
	}
241
	}
238
}
242
}
239
?>
243
?>