Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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