Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

Rev Author Line No. Line
1046 jpm 1
<?php
2
/**
3
 * Traitement de l'ordre :
4
 *
5
 * Fonction qui rajoute l'ordre et le sens de chaque way d'une relation dans la table `osm_relation_a_chemins`
6
 *
7
 * Exemple de lancement du script :
8
 * /opt/lampp/bin/php -d memory_limit=8000M cli.php osm -a ordre -m manuel
9
 *
10
 */
11
class OrdonneurDeChemins {
12
	private $conteneur;
13
	private $bdd;
14
	private $messages;
15
	private $mode;
16
 
1047 jpm 17
	private $infosRel = array();
18
	private $infosNoeuds = array();
1046 jpm 19
	private $idRelation = null;
20
	private $idChemin = null;
21
	private $idNoeud = null;
22
	private $ordre = 0;
23
 
24
	public function __construct($conteneur) {
25
		$this->conteneur = $conteneur;
26
		$this->bdd = $this->conteneur->getBdd();
27
		$this->messages = $this->conteneur->getMessages();
28
		$this->mode = $this->conteneur->getParametre('m');
29
	}
30
 
31
	public function executer() {
32
		$relations = $this->getToutesRelationsAChemins();
33
		if ($this->mode == 'manuel') {
34
			$this->messages->traiterInfo("Nombre de relations : %s", array(count($relations)));
35
		}
36
		foreach ($relations as $relation) {
37
			// Traitement de la relation courante
38
			$this->idRelation = $relation['id_relation'];
39
 
1047 jpm 40
			$this->chargerDonneesRelationActuelle();
1046 jpm 41
 
42
			// Selection du premier chemin comme chemin actuel
43
			$this->idChemin = $this->getIdPremierChemin();
44
			$this->mettreAJourChemin('directe', __LINE__);
45
 
46
			// Selection du dernier noeud comme noeud actuel
47
			$idDernierNoeud = $this->getIdDernierNoeud();
48
			if ($idDernierNoeud) {
49
				$this->idNoeud = $idDernierNoeud;
50
				$this->ordonnerChemins();
51
			}
52
 
53
			// Affichage de l'avancement
54
			if ($this->mode == 'manuel') {
55
				$this->messages->afficherAvancement("Ordone les chemins de la relation : ", 1);
56
			}
57
		}
58
	}
59
 
60
	private function getToutesRelationsAChemins() {
61
		$requete = 'SELECT DISTINCT id_relation '.
62
			'FROM osm_relation_a_chemins '.
63
			' -- '.__FILE__.' : '.__LINE__;
64
		$relations = $this->bdd->recupererTous($requete);
65
		return $relations;
66
	}
67
 
68
	private function chargerDonneesRelationActuelle() {
69
		$requete = 'SELECT cn.id_chemin, cn.id_noeud, cn.ordre '.
70
			'FROM osm_relation_a_chemins AS rc '.
1047 jpm 71
			'	INNER JOIN osm_chemin_a_noeuds AS cn ON (rc.id_chemin = cn.id_chemin) '.
1046 jpm 72
			"WHERE rc.id_relation = {$this->idRelation} ".
1047 jpm 73
			"ORDER BY cn.ordre ASC ".
1046 jpm 74
			' -- '.__FILE__.' : '.__LINE__;
75
		$infos = $this->bdd->recupererTous($requete);
76
		foreach ($infos as $info) {
1047 jpm 77
			$this->infosRel[$info['id_chemin']][$info['id_noeud']] = $info['ordre'];
78
			if (! isset($this->infosNoeuds[$info['id_noeud']][$info['id_chemin']])) {
79
				$this->infosNoeuds[$info['id_noeud']][$info['id_chemin']] = 1;
80
			} else {
81
				$this->infosNoeuds[$info['id_noeud']][$info['id_chemin']]++;
82
			}
1046 jpm 83
		}
1047 jpm 84
		//print_r($this->infosNoeuds);exit();
1046 jpm 85
		$this->ordre = 0;
86
	}
87
 
88
	private function getIdPremierChemin() {
1047 jpm 89
		$idChemin = null;
90
		if (count($this->infosRel) > 0) {
91
			reset($this->infosRel);
92
			$idChemin = key($this->infosRel);
93
		}
94
		return $idChemin;
1046 jpm 95
	}
96
 
97
	private function mettreAJourChemin($sens, $ligne, $fichier = __FILE__) {
98
		$ordre = $this->ordre++;
99
		$requete = 'UPDATE osm_relation_a_chemins '.
100
			"SET ordre = '$ordre', sens = '$sens' ".
101
			"WHERE id_relation = {$this->idRelation} ".
102
			"AND id_chemin = {$this->idChemin} ".
103
			" -- $fichier : $ligne";
104
		$retour = $this->bdd->requeter($requete);
105
		return $retour;
106
	}
107
 
108
	private function getIdDernierNoeud() {
1047 jpm 109
		$idNoeud = false;
110
		if (count($this->infosRel[$this->idChemin]) > 0) {
111
			end($this->infosRel[$this->idChemin]);
112
			$idNoeud = key($this->infosRel[$this->idChemin]);
113
		}
114
		return $idNoeud;
1046 jpm 115
	}
116
 
117
	private function ordonnerChemins() {
1047 jpm 118
		foreach ($this->infosRel as $chemin) {
1046 jpm 119
			// Selection du chemin qui possède le dernier noeud du précédent chemin et écarter l'actuel
120
			$idCheminSuivant = $this->getIdCheminSuivant();
121
			if ($idCheminSuivant) {
122
				$this->idChemin = $idCheminSuivant;
1047 jpm 123
				$idPremierNoeud = $this->getIdPremierNoeud();
124
				$idDernierNoeud = $this->getIdDernierNoeud();
125
				$sens = $this->getSens($idPremierNoeud);
126
				$this->mettreAJourChemin($sens, __LINE__);
127
				$this->idNoeud =  ($sens == 'directe') ? $idDernierNoeud : $idPremierNoeud;
1046 jpm 128
			}
129
		}
130
	}
131
 
132
	private function getIdCheminSuivant() {
1047 jpm 133
		$idCheminSuivant = false;
134
		if (isset($this->infosNoeuds[$this->idNoeud])) {
135
			$cheminsIds = array_keys($this->infosNoeuds[$this->idNoeud]);
136
			foreach ($cheminsIds as $idChemin) {
137
				if ($idChemin != $this->idChemin) {
138
					$idCheminSuivant = $idChemin;
139
					break;
140
				}
141
			}
142
		} else {
143
			$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));
145
		}
1046 jpm 146
		return $idCheminSuivant;
147
	}
148
 
1047 jpm 149
	private function getSens($idPremierNoeud) {
150
		$sens = ( strcmp($idPremierNoeud, $this->idNoeud ) == 0 ) ? 'directe' : 'indirecte';
151
		return $sens;
1046 jpm 152
	}
153
 
154
	private function getIdPremierNoeud() {
1047 jpm 155
		$idNoeud = false;
156
		if (count($this->infosRel[$this->idChemin]) > 0) {
157
			reset($this->infosRel[$this->idChemin]);
158
			$idNoeud = key($this->infosRel[$this->idChemin]);
159
		}
160
		return $idNoeud;
1046 jpm 161
	}
162
}