Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev 1047 Rev 1055
1
<?php
1
<?php
2
/**
2
/**
3
 * Traitement de l'ordre :
3
 * Traitement de l'ordre :
4
 *
4
 *
5
 * Fonction qui rajoute l'ordre et le sens de chaque way d'une relation dans la table `osm_relation_a_chemins`
5
 * Fonction qui rajoute l'ordre et le sens de chaque way d'une relation dans la table `osm_relation_a_chemins`
6
 *
6
 *
7
 * Exemple de lancement du script :
7
 * Exemple de lancement du script :
8
 * /opt/lampp/bin/php -d memory_limit=8000M cli.php osm -a ordre -m manuel
8
 * /opt/lampp/bin/php -d memory_limit=8000M cli.php osm -a ordre -m manuel
9
 *
9
 *
10
 */
10
 */
11
class OrdonneurDeChemins {
11
class OrdonneurDeChemins {
12
	private $conteneur;
12
	private $conteneur;
13
	private $bdd;
13
	private $bdd;
14
	private $messages;
14
	private $messages;
15
	private $mode;
15
	private $mode;
-
 
16
 
16
 
17
	private $premierNoeudPolygon = null;
17
	private $infosRel = array();
18
	private $infosRel = array();
18
	private $infosNoeuds = array();
19
	private $infosNoeuds = array();
19
	private $idRelation = null;
20
	private $idRelation = null;
20
	private $idChemin = null;
21
	private $idChemin = null;
21
	private $idNoeud = null;
22
	private $idNoeud = null;
22
	private $ordre = 0;
23
	private $ordre = 0;
23
 
24
 
24
	public function __construct($conteneur) {
25
	public function __construct($conteneur) {
25
		$this->conteneur = $conteneur;
26
		$this->conteneur = $conteneur;
26
		$this->bdd = $this->conteneur->getBdd();
27
		$this->bdd = $this->conteneur->getBdd();
27
		$this->messages = $this->conteneur->getMessages();
28
		$this->messages = $this->conteneur->getMessages();
28
		$this->mode = $this->conteneur->getParametre('m');
29
		$this->mode = $this->conteneur->getParametre('m');
29
	}
30
	}
30
 
31
 
31
	public function executer() {
32
	public function executer() {
32
		$relations = $this->getToutesRelationsAChemins();
33
		$relations = $this->getToutesRelationsAChemins();
33
		if ($this->mode == 'manuel') {
34
		if ($this->mode == 'manuel') {
34
			$this->messages->traiterInfo("Nombre de relations : %s", array(count($relations)));
35
			$this->messages->traiterInfo("Nombre de relations : %s", array(count($relations)));
35
		}
36
		}
36
		foreach ($relations as $relation) {
37
		foreach ($relations as $relation) {
37
			// Traitement de la relation courante
38
			// Traitement de la relation courante
38
			$this->idRelation = $relation['id_relation'];
39
			$this->idRelation = $relation['id_relation'];
39
 
40
 
40
			$this->chargerDonneesRelationActuelle();
41
			$this->chargerDonneesRelationActuelle();
41
 
42
 
42
			// Selection du premier chemin comme chemin actuel
43
			$numPolygon = 1;
43
			$this->idChemin = $this->getIdPremierChemin();
44
			while(count($this->chemins) > 0) {
44
			$this->mettreAJourChemin('directe', __LINE__);
-
 
45
 
45
				reset($this->chemins);
46
			// Selection du dernier noeud comme noeud actuel
46
				$idChemin = key($this->chemins);
47
			$idDernierNoeud = $this->getIdDernierNoeud();
47
				echo "--------------POLY : $numPolygon : $idChemin\n";
48
			if ($idDernierNoeud) {
48
				$this->ordonnerCheminsRelation($numPolygon, $idChemin);
49
				$this->idNoeud = $idDernierNoeud;
49
				$this->mettreAJourRelationAChemin();
50
				$this->ordonnerChemins();
50
				$numPolygon++;
51
			}
51
			}
52
 
52
 
53
			// Affichage de l'avancement
53
			// Affichage de l'avancement
54
			if ($this->mode == 'manuel') {
54
			if ($this->mode == 'manuel') {
55
				$this->messages->afficherAvancement("Ordone les chemins de la relation : ", 1);
55
				$this->messages->afficherAvancement("Ordone les chemins de la relation : ", 1);
56
			}
56
			}
57
		}
57
		}
58
	}
58
	}
59
 
59
 
60
	private function getToutesRelationsAChemins() {
60
	private function getToutesRelationsAChemins() {
61
		$requete = 'SELECT DISTINCT id_relation '.
61
		$requete = 'SELECT DISTINCT id_relation '.
62
			'FROM osm_relation_a_chemins '.
62
			'FROM osm_relation_a_chemins '.
-
 
63
			'WHERE id_relation = 403823 '.
63
			' -- '.__FILE__.' : '.__LINE__;
64
			' -- '.__FILE__.' : '.__LINE__;
64
		$relations = $this->bdd->recupererTous($requete);
65
		$relations = $this->bdd->recupererTous($requete);
65
		return $relations;
66
		return $relations;
66
	}
67
	}
67
 
68
 
68
	private function chargerDonneesRelationActuelle() {
69
	private function chargerDonneesRelationActuelle() {
69
		$requete = 'SELECT cn.id_chemin, cn.id_noeud, cn.ordre '.
70
		$requete = 'SELECT cn.id_chemin, cn.id_noeud, cn.ordre '.
70
			'FROM osm_relation_a_chemins AS rc '.
71
			'FROM osm_relation_a_chemins AS rc '.
71
			'	INNER JOIN osm_chemin_a_noeuds AS cn ON (rc.id_chemin = cn.id_chemin) '.
72
			'	INNER JOIN osm_chemin_a_noeuds AS cn ON (rc.id_chemin = cn.id_chemin) '.
72
			"WHERE rc.id_relation = {$this->idRelation} ".
73
			"WHERE rc.id_relation = {$this->idRelation} ".
73
			"ORDER BY cn.ordre ASC ".
74
			"ORDER BY rc.ordre ASC, cn.ordre ASC ".
74
			' -- '.__FILE__.' : '.__LINE__;
75
			' -- '.__FILE__.' : '.__LINE__;
75
		$infos = $this->bdd->recupererTous($requete);
76
		$infos = $this->bdd->recupererTous($requete);
-
 
77
		$noeuds = array();
76
		foreach ($infos as $info) {
78
		foreach ($infos as $info) {
77
			$this->infosRel[$info['id_chemin']][$info['id_noeud']] = $info['ordre'];
79
			$noeuds[$info['id_noeud']][$info['id_chemin']] = $info['ordre'];
78
			if (! isset($this->infosNoeuds[$info['id_noeud']][$info['id_chemin']])) {
80
			if (! isset($this->chemins[$info['id_chemin']]) ) {
-
 
81
				$this->chemins[$info['id_chemin']]['max'] == info['ordre'];
-
 
82
				if ($info['ordre'] == 1) {
79
				$this->infosNoeuds[$info['id_noeud']][$info['id_chemin']] = 1;
83
					$this->chemins[$info['id_chemin']][0] == $info['id_noeud'];
-
 
84
				}
80
			} else {
85
			} else {
-
 
86
 
-
 
87
			}
-
 
88
			if (count($noeuds[$info['id_noeud']]) == 2) {
-
 
89
				list($premierChemin, $dernierChemin) = array_keys($noeuds[$info['id_noeud']]);
-
 
90
				$this->chemins[$premierChemin][] = $info['id_noeud'];
-
 
91
				$this->chemins[$dernierChemin][] = $info['id_noeud'];
-
 
92
				$this->noeuds[$info['id_noeud']][$premierChemin] = $dernierChemin;
81
				$this->infosNoeuds[$info['id_noeud']][$info['id_chemin']]++;
93
				$this->noeuds[$info['id_noeud']][$dernierChemin] = $premierChemin;
82
			}
94
			}
83
		}
95
		}
84
		//print_r($this->infosNoeuds);exit();
96
		//print_r($this->chemins);exit();
-
 
97
	}
-
 
98
 
-
 
99
	private function ordonnerCheminsRelation($numPolygon, $idChemin, $idCheminPrecedent = null, $ordre = 1) {
-
 
100
		$this->infosRel[$idChemin] = array($ordre, $numPolygon);
-
 
101
 
-
 
102
		list($premierNoeud, $dernierNoeud) = $this->chemins[$idChemin];
-
 
103
 
-
 
104
		if ($idCheminPrecedent == null) {// Premier chemin à tester
-
 
105
			$this->premierNoeudPolygon = $premierNoeud;
-
 
106
			$noeudSuivant = ($dernierNoeud == $premierNoeud) ? $premierNoeud : $dernierNoeud;
-
 
107
			echo "Chemin $idChemin :: premierNoeud: $premierNoeud, dernierNoeud: $dernierNoeud \n";
-
 
108
		} else {
-
 
109
			list($premierNoeudPrec, $dernierNoeudPrec) = $this->chemins[$idCheminPrecedent];
-
 
110
			unset($this->chemins[$idCheminPrecedent]);
-
 
111
			$noeudSuivant = ($dernierNoeudPrec == $dernierNoeud || $premierNoeudPrec == $dernierNoeud) ? $premierNoeud : $dernierNoeud;
-
 
112
			echo "Chemin $idChemin :: premierNoeudPrec: $premierNoeudPrec, dernierNoeudPrec: $dernierNoeudPrec, premierNoeud: $premierNoeud, dernierNoeud: $dernierNoeud \n";
-
 
113
		}
-
 
114
 
-
 
115
		if ($this->premierNoeudPolygon != $noeudSuivant) {
-
 
116
			if (isset($this->noeuds[$noeudSuivant][$idChemin])) {
-
 
117
				$idCheminSuivant = $this->noeuds[$noeudSuivant][$idChemin];
-
 
118
				if (! isset($this->infosRel[$idCheminSuivant])) {
-
 
119
					$this->ordonnerCheminsRelation($numPolygon, $idCheminSuivant, $idChemin, ++$ordre);
-
 
120
				} else {
-
 
121
					$this->messages->traiterAvertissement("Chemins %s déjà pris en comtpe", array($idCheminSuivant));
-
 
122
				}
-
 
123
			} else {
-
 
124
				$this->messages->traiterAvertissement("Relation %s a un polygone incomplet", array($this->idRelation));
-
 
125
			}
-
 
126
		} else {
-
 
127
			unset($this->chemins[$idChemin]);
-
 
128
		}
-
 
129
		return null;
-
 
130
	}
-
 
131
 
-
 
132
	private function mettreAJourRelationAChemin() {
-
 
133
		if (count($this->infosRel) > 0) {
-
 
134
			foreach ($this->infosRel as $idC => $donnees) {
-
 
135
				list($ordre, $numPolygon) = $this->bdd->proteger($donnees);
-
 
136
				$requete = 'UPDATE osm_relation_a_chemins '.
-
 
137
					"SET sens = $ordre, num_poly = $numPolygon ".
-
 
138
					"WHERE id_relation = {$this->idRelation} ".
-
 
139
					"AND id_chemin = $idC ".
-
 
140
					' -- '.__FILE__.' : '.__LINE__;
-
 
141
				$this->bdd->requeter($requete);
-
 
142
			}
85
		$this->ordre = 0;
143
			$this->infosRel = array();
-
 
144
		}
-
 
145
	}
-
 
146
 
-
 
147
	private function creerSet($donnees) {
-
 
148
		$values = array();
-
 
149
		foreach ($donnees as $infos) {
-
 
150
			$infosP = $this->bdd->proteger($infos);
-
 
151
			$values[] = implode(',', $infosP);
-
 
152
		}
-
 
153
		$valuesClause = '('.implode('),(', $values).')';
-
 
154
		return $valuesClause;
86
	}
155
	}
87
 
156
 
88
	private function getIdPremierChemin() {
157
	private function getIdPremierChemin() {
89
		$idChemin = null;
158
		$idChemin = null;
90
		if (count($this->infosRel) > 0) {
159
		if (count($this->infosRel) > 0) {
91
			reset($this->infosRel);
160
			reset($this->infosRel);
92
			$idChemin = key($this->infosRel);
161
			$idChemin = key($this->infosRel);
93
		}
162
		}
94
		return $idChemin;
163
		return $idChemin;
95
	}
164
	}
96
 
165
 
97
	private function mettreAJourChemin($sens, $ligne, $fichier = __FILE__) {
166
	private function mettreAJourChemin($sens, $ligne, $fichier = __FILE__) {
98
		$ordre = $this->ordre++;
167
		$ordre = $this->ordre++;
99
		$requete = 'UPDATE osm_relation_a_chemins '.
168
		$requete = 'UPDATE osm_relation_a_chemins '.
100
			"SET ordre = '$ordre', sens = '$sens' ".
169
			"SET ordre = '$ordre', sens = '$sens' ".
101
			"WHERE id_relation = {$this->idRelation} ".
170
			"WHERE id_relation = {$this->idRelation} ".
102
			"AND id_chemin = {$this->idChemin} ".
171
			"AND id_chemin = {$this->idChemin} ".
103
			" -- $fichier : $ligne";
172
			" -- $fichier : $ligne";
104
		$retour = $this->bdd->requeter($requete);
173
		$retour = $this->bdd->requeter($requete);
105
		return $retour;
174
		return $retour;
106
	}
175
	}
107
 
176
 
108
	private function getIdDernierNoeud() {
177
	private function getIdDernierNoeud() {
109
		$idNoeud = false;
178
		$idNoeud = false;
110
		if (count($this->infosRel[$this->idChemin]) > 0) {
179
		if (count($this->infosRel[$this->idChemin]) > 0) {
111
			end($this->infosRel[$this->idChemin]);
180
			end($this->infosRel[$this->idChemin]);
112
			$idNoeud = key($this->infosRel[$this->idChemin]);
181
			$idNoeud = current($this->infosRel[$this->idChemin]);
113
		}
182
		}
114
		return $idNoeud;
183
		return $idNoeud;
115
	}
184
	}
116
 
185
 
117
	private function ordonnerChemins() {
186
	private function ordonnerChemins() {
118
		foreach ($this->infosRel as $chemin) {
187
		foreach ($this->infosRel as $chemin) {
119
			// Selection du chemin qui possède le dernier noeud du précédent chemin et écarter l'actuel
188
			// Selection du chemin qui possède le dernier noeud du précédent chemin et écarter l'actuel
120
			$idCheminSuivant = $this->getIdCheminSuivant();
189
			$idCheminSuivant = $this->getIdCheminSuivant();
121
			if ($idCheminSuivant) {
190
			if ($idCheminSuivant) {
122
				$this->idChemin = $idCheminSuivant;
191
				$this->idChemin = $idCheminSuivant;
123
				$idPremierNoeud = $this->getIdPremierNoeud();
192
				$idPremierNoeud = $this->getIdPremierNoeud();
124
				$idDernierNoeud = $this->getIdDernierNoeud();
193
				$idDernierNoeud = $this->getIdDernierNoeud();
125
				$sens = $this->getSens($idPremierNoeud);
194
				$sens = $this->getSens($idPremierNoeud);
126
				$this->mettreAJourChemin($sens, __LINE__);
195
				$this->mettreAJourChemin($sens, __LINE__);
127
				$this->idNoeud =  ($sens == 'directe') ? $idDernierNoeud : $idPremierNoeud;
196
				$this->idNoeud =  ($sens == 'directe') ? $idDernierNoeud : $idPremierNoeud;
128
			}
197
			}
129
		}
198
		}
130
	}
199
	}
131
 
200
 
132
	private function getIdCheminSuivant() {
201
	private function getIdCheminSuivant() {
133
		$idCheminSuivant = false;
202
		$idCheminSuivant = false;
134
		if (isset($this->infosNoeuds[$this->idNoeud])) {
203
		if (isset($this->infosNoeuds[$this->idNoeud])) {
135
			$cheminsIds = array_keys($this->infosNoeuds[$this->idNoeud]);
204
			$idCheminSuivant = next($this->infosNoeuds[$this->idNoeud]);
136
			foreach ($cheminsIds as $idChemin) {
-
 
137
				if ($idChemin != $this->idChemin) {
205
			//echo $this->idChemin.'-'.$idCheminSuivant."\n".print_r($this->infosNoeuds[$this->idNoeud]); exit();
138
					$idCheminSuivant = $idChemin;
-
 
139
					break;
-
 
140
				}
-
 
141
			}
-
 
142
		} else {
206
		} else {
143
			$msg = "Impossible de trouver le chemin suivant pour la relation : %s, le chemin %s et le noeud %s";
207
			$msg = "Impossible de trouver le chemin suivant pour la relation : %s, le chemin %s et le noeud %s";
144
			$this->messages->traiterAvertissement($msg, array($this->idRelation, $this->idChemin, $this->idNoeud));
208
			$this->messages->traiterAvertissement($msg, array($this->idRelation, $this->idChemin, $this->idNoeud));
145
		}
209
		}
146
		return $idCheminSuivant;
210
		return $idCheminSuivant;
147
	}
211
	}
148
 
212
 
149
	private function getSens($idPremierNoeud) {
213
	private function getSens($idPremierNoeud) {
150
		$sens = ( strcmp($idPremierNoeud, $this->idNoeud ) == 0 ) ? 'directe' : 'indirecte';
214
		$sens = ( strcmp($idPremierNoeud, $this->idNoeud ) == 0 ) ? 'directe' : 'indirecte';
151
		return $sens;
215
		return $sens;
152
	}
216
	}
153
 
217
 
154
	private function getIdPremierNoeud() {
218
	private function getIdPremierNoeud() {
155
		$idNoeud = false;
219
		$idNoeud = false;
156
		if (count($this->infosRel[$this->idChemin]) > 0) {
220
		if (count($this->infosRel[$this->idChemin]) > 0) {
157
			reset($this->infosRel[$this->idChemin]);
221
			reset($this->infosRel[$this->idChemin]);
158
			$idNoeud = key($this->infosRel[$this->idChemin]);
222
			$idNoeud = current($this->infosRel[$this->idChemin]);
159
		}
223
		}
160
		return $idNoeud;
224
		return $idNoeud;
161
	}
225
	}
162
}
226
}