Subversion Repositories eFlore/Projets.eflore-projets

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
167 jpm 1
<?php
392 jpm 2
/**
3
 * Exemple lancement:
4
 * /opt/lampp/bin/php -d memory_limit=3500M cli.php osm/mise_a_jour -a MAJ -f fichier_osm_a_analyser
5
 */
167 jpm 6
class MiseAjour extends Script {
7
	private $communes = array();
8
	private $relations_communes = array();
9
	private $relation_a_chemins = array();
10
	private $chemin_a_noeuds = array();
11
	private $noeuds = array();
12
	private $pas = 10000;
13
	private $pas_commune = 1000;
392 jpm 14
 
167 jpm 15
	protected $parametres_autorises = array(
16
	'-f' => array(true, null, 'Chemin du fichier osm à analyser'));
17
 
18
	public function executer() {
19
		$this->bdd = new Bdd();
20
 
21
		// Lancement de l'action demandée
22
		$cmd = $this->getParametre('a');
23
	    switch ($cmd) {
24
			case 'MAJ' :
25
				$this->MettreAjour();
26
				break;
27
				default :
28
				$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
29
		}
30
    }
392 jpm 31
 
32
 
167 jpm 33
	/**
34
	 * Fonction permettant de traiter et d'analyser le fichier de différence et de mettre  à jour la base de données
35
	 * en tenant compte des trois cas:Suppression, Création ,modification
36
	*/
37
	private function MettreAjour() {
38
		$lecteur = new XMLReader();
39
		if ($lecteur->open($this->getParametre('f'))) {
40
			$dom = new DomDocument();
41
			while ($lecteur->read()) {
42
				if ($lecteur->nodeType == XMLREADER::ELEMENT) {
43
					if ($lecteur->localName == 'create') {
44
						$creations = $lecteur->expand();
45
						$this->	analyserCreations($creations);
392 jpm 46
					}
167 jpm 47
					if ($lecteur->localName == 'modify') {
48
						$modifications = $lecteur->expand();
49
						$this->	analyserModifications($modifications);
392 jpm 50
					}
167 jpm 51
					if ($lecteur->localName == 'delete') {
52
						$suppressions = $lecteur->expand();
53
						$this->	analyserSuppressions($suppressions);
392 jpm 54
					}
167 jpm 55
				}
56
			}
57
		} else {
58
			$e = "Impossible d'ouvrir le fichier osm : %s";
59
			$this->traiterErreur($e, array($this->getParametre('f')));
60
		}
61
	}
392 jpm 62
 
167 jpm 63
	/**
64
	 * Fonction permettant le traitement du cas création concernant les relations, ways et les noeuds.
65
	 */
66
	private function analyserCreations($creations) {
67
		$relations = $creations->getElementsByTagName('relation');
68
		foreach ($relations as $relation) {
69
			$this-> analyserRelation($relation);
70
			if (count($this->relation_a_chemins) > $this->pas) {
71
				$this->insererRelationAChemins();
72
			}
73
			if (count($this->relations_communes) > $this->pas_commune) {
74
				$this->insererRelationsCommunes();
75
			}
76
		}
77
		$ways = $creations->getElementsByTagName('way');
78
		foreach ($ways as $way) {
79
			$this->analyserWay($way);
80
			if (count($this->chemin_a_noeuds) > $this->pas) {
81
				$this->insererCheminANoeuds();
82
			}
83
		}
84
		$noeuds = $creations->getElementsByTagName('node');
85
		foreach ($noeuds as $noeud) {
86
			$this->analyserNode($noeud);
87
			if (count($this->noeuds) > $this->pas) {
88
				$this->insererNoeuds();
89
			}
90
		}
91
		$this->insererRelationsCommunes();
92
		$this->insererRelationAChemins();
93
		$this->insererCheminANoeuds();
94
		$this->insererNoeuds();
95
	}
392 jpm 96
 
167 jpm 97
	/**
98
	 * Fonction permettant le traitement du cas modification concernant les relations, ways et les noeuds.
99
	 */
100
	private function analyserModifications($modifications) {
101
		$relations = $modifications->getElementsByTagName('relation');
102
		foreach ($relations as $relation) {
103
			$this-> analyserRelation($relation);
104
			if (count($this->relation_a_chemins) > $this->pas) {
105
				$this->modifierRelationAChemins();
106
			}
107
			if (count($this->relations_communes) > $this->pas_commune) {
108
				$this->modifierRelationsCommunes();
109
			}
110
		}
111
		$ways = $modifications->getElementsByTagName('way');
112
		foreach ($ways as $way) {
113
			$this->analyserWay($way);
114
			if (count($this->chemin_a_noeuds) > $this->pas) {
115
				$this->modifierCheminANoeuds();
116
			}
117
		}
118
		$noeuds = $modifications->getElementsByTagName('node');
119
		foreach ($noeuds as $noeud) {
120
			$this->analyserNode($noeud);
121
			if (count($this->noeuds) > $this->pas) {
122
				$this->modifierNoeuds();
123
			}
124
		}
125
		$this->modifierRelationsCommunes();
126
		$this->modifierRelationAChemins();
127
		$this->modifierCheminANoeuds();
128
		$this->modifierNoeuds();
129
	}
392 jpm 130
 
167 jpm 131
	/**
132
	 * Fonction permettant le traitement du cas suppression concernant les relations, ways et les noeuds.
133
	 */
134
	private function analyserSuppressions($suppressions) {
135
		$relations = $suppressions->getElementsByTagName('relation');
136
		foreach ($relations as $relation) {
137
			$this-> analyserRelation($relation);
138
			if (count($this->relation_a_chemins) > $this->pas) {
139
				$this->supprimerRelationAChemins();
140
			}
141
			if (count($this->relations_communes) > $this->pas_commune) {
142
				$this->supprimerRelationsCommunes();
143
			}
144
		}
145
		$ways = $suppressions->getElementsByTagName('way');
146
		foreach ($ways as $way) {
147
			$this->analyserWay($way);
148
			if (count($this->chemin_a_noeuds) > $this->pas) {
149
				$this->supprimerCheminANoeuds();
150
			}
151
		}
152
		$noeuds = $suppressions->getElementsByTagName('node');
153
		foreach ($noeuds as $noeud) {
154
			$this->analyserNode($noeud);
155
			if (count($this->noeuds) > $this->pas) {
156
				$this->supprimerNoeuds();
157
			}
158
		}
159
		$this->supprimerRelationsCommunes();
160
		$this->supprimerRelationAChemins();
161
		$this->supprimerCheminANoeuds();
162
		$this->supprimerNoeuds();
163
	}
392 jpm 164
 
165
 
167 jpm 166
	/**
167
	 * Récupère l'id commune, nom commune et le code INSEE et remplie la table `osm_relations`
392 jpm 168
	 */
167 jpm 169
	private function analyserRelation($relation) {
170
		$relation_id = $this->proteger($relation->getAttribute('id'));
171
		$chemins = $relation->getElementsByTagName('member');
172
		foreach ($chemins as $chemin) {
173
			if ($chemin->getAttribute('type') == 'way') { //écarter le noeud centrale
174
				$chemin_id = $this->proteger($chemin->getAttribute('ref'));
175
				$role = $this->proteger($chemin->getAttribute('role'));//role: null, inner, outer, exclave et enclave.
392 jpm 176
				$this->relation_a_chemins[] = array($relation_id, $chemin_id, $role);
167 jpm 177
			}
178
		}
179
		$commune_nom = null;
180
		$commune_insee = null;
181
		$tags = $relation->getElementsByTagName('tag');
182
		foreach ($tags as $tag) {
183
			$tag_cle = $tag->getAttribute('k');
184
			$tag_val = $tag->getAttribute('v');
185
			switch ($tag_cle) {
186
				case 'name' :
187
					$commune_nom = $this->proteger($tag_val);
392 jpm 188
					break;
167 jpm 189
				case 'ref:INSEE' :
190
					$commune_insee = $this->proteger($tag_val);
191
					break;
192
			}
193
			if (!is_null($commune_nom) && !is_null($commune_insee)) {
194
				$this->relations_communes[] = array($relation_id, $commune_nom, $commune_insee);
195
				if (!isset($this->communes[$commune_insee])) {
196
					$this->communes[$commune_insee] = $relation_id;
197
				} else {
198
					$e = "La relation #%s contient déjà le tag ref:INSEE avec la valeur %s.".
199
						"Veuillez corriger la carte OSM.";
200
					$this->traiterErreur($e, array($this->communes[$commune_insee], $commune_insee, $relation_id));
201
				}
202
				break;
203
			}
392 jpm 204
		}
167 jpm 205
	}
392 jpm 206
 
167 jpm 207
	/**
208
	 * Récupère l'id_way et tous les id_node de chaque way et remplie la table `osm_chemin_a_noeuds`
392 jpm 209
	 */
167 jpm 210
	private function analyserWay($way) {
211
		$chemin_id = $this->proteger($way->getAttribute('id'));
212
		$ordre = 0;
213
		$noeuds = $way->getElementsByTagName('nd');
214
		$chemin_a_noeuds = array();
215
		foreach ($noeuds as $noeud) {
216
			$noeud_id = $this->proteger($noeud->getAttribute('ref'));
217
			$ordre++;
218
	    	$this->chemin_a_noeuds[] = array($chemin_id, $noeud_id, $ordre);
219
		}
220
	}
392 jpm 221
 
222
	/**
167 jpm 223
	 * Fonction qui récupère tous les l'id_node et les valeurs(Lat/Lon) correspondantes et remplie la table `osm_noeuds`
392 jpm 224
	 */
167 jpm 225
	private function analyserNode($node) {
226
		$noeud_id = $this->proteger($node->getAttribute('id'));
227
		$lat = $this->proteger($node->getAttribute('lat'));
228
		$lon = $this->proteger($node->getAttribute('lon'));
229
		$this->noeuds[] = array($noeud_id, $lat, $lon);
230
	}
392 jpm 231
 
232
	//Insertion des relations
167 jpm 233
	private function insererRelationsCommunes() {
234
		$requete =	'INSERT INTO osm_relations (id_relation, nom, code_insee) '.
235
					'VALUES '.$this->creerValuesMultiple($this->relations_communes);
236
		$this->inserer($requete);
237
		$this->relations_communes = array();
238
	}
239
 
392 jpm 240
	//Insertion des relations à chemins
167 jpm 241
	private function insererRelationAChemins() {
242
		$requete =	'INSERT INTO osm_relation_a_chemins (id_relation, id_chemin, role) '.
243
					'VALUES '.$this->creerValuesMultiple($this->relation_a_chemins);
244
		$this->inserer($requete);
245
		$this->relation_a_chemins = array();
392 jpm 246
	}
167 jpm 247
 
392 jpm 248
	//Insertion des chemins à noeuds
167 jpm 249
	private function insererCheminANoeuds() {
250
		$requete = 	'INSERT INTO osm_chemin_a_noeuds (id_chemin, id_noeud, ordre) '.
251
					'VALUES '.$this->creerValuesMultiple($this->chemin_a_noeuds);
252
		$this->inserer($requete);
253
		$this->chemin_a_noeuds = array();
254
	}
392 jpm 255
 
256
	//Insertion des noeuds
167 jpm 257
	private function insererNoeuds() {
258
		$requete = 'INSERT INTO osm_noeuds (id_noeud, lat, `long`) '.
259
	    		   'VALUES '.$this->creerValuesMultiple($this->noeuds);
260
		$this->inserer($requete);
261
		$this->noeuds = array();
262
	}
392 jpm 263
 
264
	//Update des relations
167 jpm 265
	private function modifierRelationsCommunes() {
266
		$donnees = $this->relations_communes;
267
		foreach ($donnees as $donnee) {
392 jpm 268
		$requete = 'UPDATE osm_relations '.
269
					"SET id_relation = $donnee[0], nom = $donnee[1], code_insee = $donnee[2] ".
167 jpm 270
					"WHERE id_relation = $donnee[0]";
271
			$this->inserer($requete);
272
		}
273
		$this->relations_communes = array();
274
	}
392 jpm 275
 
167 jpm 276
	/*
392 jpm 277
	*Update des relations à chemins en supprimant l'ancienne relation et tous ses chemins
278
	*de la table osm_relation_a_chemins et insérer la nouvelle
167 jpm 279
	*/
280
	private function modifierRelationAChemins() {
281
		$relations_a_chemins_a_supp = array();
282
		$donnees = $this->relation_a_chemins;
283
		foreach ($donnees as $donnee) {
284
			$relations_a_chemins_a_supp[] = $donnee[0];
285
		}
286
		$values_chaine = '('.implode(',',array_unique($relations_a_chemins_a_supp)).')';
287
		$requete = 'DELETE FROM osm_relation_a_chemins '.
288
					"WHERE id_relation IN $values_chaine";
392 jpm 289
		$this->inserer($requete);
167 jpm 290
		foreach ($donnees as $donnee) {
291
			$requete = 'INSERT INTO osm_relation_a_chemins (id_relation, id_chemin, role) '.
292
						"VALUES ($donnee[0], $donnee[1], $donnee[2]);";
293
			$this->inserer($requete);
294
		}
295
		$this->relation_a_chemins = array();
392 jpm 296
	}
297
 
167 jpm 298
	/*
392 jpm 299
	*Update des chemins à noeuds en supprimant l'ancien chemin et tous ses noeuds
300
	*de la table osm_chemins_a_noeuds et insérer le nouveau
167 jpm 301
	*/
302
	private function modifierCheminANoeuds() {
303
		$chemin_a_noeuds_a_supp = array();
304
		$donnees = $this->chemin_a_noeuds;
305
		foreach ($donnees as $donnee) {
306
			$chemin_a_noeuds_a_supp[] = $donnee[0];
307
		}
308
		$values_chaine = '('.implode(',',array_unique($chemin_a_noeuds_a_supp)).')';
309
		$requete = 'DELETE FROM osm_chemin_a_noeuds '.
310
					"WHERE id_chemin IN $values_chaine";
392 jpm 311
		$this->inserer($requete);
167 jpm 312
		foreach ($donnees as $donnee) {
313
			$requete = 'INSERT INTO osm_chemin_a_noeuds (id_chemin, id_noeud, ordre) '.
314
						"VALUES ($donnee[0], $donnee[1], $donnee[2]);";
315
			$this->inserer($requete);
316
		}
317
		$this->chemin_a_noeuds = array();
318
	}
392 jpm 319
 
320
 
321
	//Update des noeuds
167 jpm 322
	private function modifierNoeuds() {
323
		$donnees = $this->noeuds;
324
		foreach ($donnees as $donnee) {
392 jpm 325
			$requete = 'UPDATE osm_noeuds '.
326
						"SET id_noeud = $donnee[0], lat = $donnee[1], `long` = $donnee[2] ".
167 jpm 327
						"WHERE id_noeud = $donnee[0]";
328
			$this->inserer($requete);
329
		}
330
		$this->noeuds = array();
331
	}
392 jpm 332
 
333
 
334
	//Suppressions des relations
167 jpm 335
	private function supprimerRelationsCommunes() {
336
		$donnees = $this->relations_communes;
337
		foreach ($donnees as $donnee) {
392 jpm 338
			$requete = 'DELETE FROM osm_relations '.
167 jpm 339
						"WHERE id_relation = $donnee[0]";
340
			$this->inserer($requete);
341
		}
342
		$this->relations_communes = array();
343
	}
392 jpm 344
 
345
	//Suppressions des relations à chemins
167 jpm 346
	private function supprimerRelationAChemins() {
347
		$donnees = $this->relations_communes;
348
		foreach ($donnees as $donnee) {
349
			$donnees = $this->relation_a_chemins;
392 jpm 350
			$requete = 'DELETE FROM osm_relation_a_chemins '.
167 jpm 351
						"WHERE id_relation = $donnee[0]";
352
			$this->inserer($requete);
353
		}
354
		$this->relation_a_chemins = array();
392 jpm 355
	}
356
 
357
	//Suppressions des chemins à noeuds
167 jpm 358
	private function supprimerCheminANoeuds() {
359
		$donnees = $this->chemin_a_noeuds;
360
		foreach ($donnees as $donnee) {
361
			$donnees = $this->relation_a_chemins;
392 jpm 362
			$requete = 'DELETE FROM osm_chemin_a_noeuds '.
167 jpm 363
						"WHERE id_chemin = $donnee[0]";
364
			$this->inserer($requete);
365
		}
366
		$this->chemin_a_noeuds = array();
367
	}
392 jpm 368
 
369
	//Suppressions des chemins à noeuds
167 jpm 370
	private function supprimerNoeuds() {
371
		$donnees = $this->noeuds;
372
		foreach ($donnees as $donnee) {
392 jpm 373
			$requete = 'DELETE FROM osm_noeuds '.
167 jpm 374
						"WHERE id_noeud = $donnee[0]";
375
			$this->inserer($requete);
376
		}
377
		$this->noeuds = array();
378
	}
392 jpm 379
 
167 jpm 380
	private function inserer($requete) {
381
		$this->bdd->requeter($requete);
382
	}
392 jpm 383
 
167 jpm 384
	private function proteger($chaine) {
385
		return $this->bdd->proteger($chaine);
386
	}
392 jpm 387
 
167 jpm 388
	private function creerValuesMultiple($donnees) {
389
		$values = array();
390
		foreach ($donnees as $donnee) {
391
			$values[] = implode(',', $donnee);
392
		}
393
		$values_chaine = '('.implode('),(', $values).')';
394
		return $values_chaine;
395
	}
392 jpm 396
}
167 jpm 397
?>