Subversion Repositories eFlore/Projets.eflore-projets

Rev

Blame | Last modification | View Log | RSS feed

<?php
/**
 * Traitement de l'ordre :
 *
 * Fonction qui rajoute l'ordre et le sens de chaque way d'une relation dans la table `osm_relation_a_chemins`
 *
 * Exemple de lancement du script :
 * /opt/lampp/bin/php -d memory_limit=8000M cli.php osm -a ordre -m manuel
 *
 */
class OrdonneurDeChemins {
        private $conteneur;
        private $bdd;
        private $messages;
        private $mode;

        private $idRelation = null;
        private $idChemin = null;
        private $idNoeud = null;
        private $chemins = array();
        private $ordre = 0;

        public function __construct($conteneur) {
                $this->conteneur = $conteneur;
                $this->bdd = $this->conteneur->getBdd();
                $this->messages = $this->conteneur->getMessages();
                $this->mode = $this->conteneur->getParametre('m');
        }

        public function executer() {
                $relations = $this->getToutesRelationsAChemins();
                if ($this->mode == 'manuel') {
                        $this->messages->traiterInfo("Nombre de relations : %s", array(count($relations)));
                }
                foreach ($relations as $relation) {
                        // Traitement de la relation courante
                        $this->idRelation = $relation['id_relation'];

                        $this->chargerCheminsRelationActuelle();

                        // Selection du premier chemin comme chemin actuel
                        $this->idChemin = $this->getIdPremierChemin();
                        $this->mettreAJourChemin('directe', __LINE__);

                        // Selection du dernier noeud comme noeud actuel
                        $idDernierNoeud = $this->getIdDernierNoeud();
                        if ($idDernierNoeud) {
                                $this->idNoeud = $idDernierNoeud;
                                $this->ordonnerChemins();
                        }

                        // Affichage de l'avancement
                        if ($this->mode == 'manuel') {
                                $this->messages->afficherAvancement("Ordone les chemins de la relation : ", 1);
                        }
                }
        }

        private function getToutesRelationsAChemins() {
                $requete = 'SELECT DISTINCT id_relation '.
                        'FROM osm_relation_a_chemins '.
                        ' -- '.__FILE__.' : '.__LINE__;
                $relations = $this->bdd->recupererTous($requete);
                return $relations;
        }

        private function chargerDonneesRelationActuelle() {
                $requete = 'SELECT cn.id_chemin, cn.id_noeud, cn.ordre '.
                        'FROM osm_relation_a_chemins AS rc '.
                        '       LEFT JOIN osm_chemin_a_noeuds AS cn ON (rc.id_chemin = cn.id_chemin) '.
                        "WHERE rc.id_relation = {$this->idRelation} ".
                        "ODER BY cn.ordre ASC ".
                        ' -- '.__FILE__.' : '.__LINE__;
                $infos = $this->bdd->recupererTous($requete);
                foreach ($infos as $info) {
                        $this->infosRel[$this->idRelation][$info['id_chemin']]['noeuds'][$info['id_noeud']] = $info['ordre'];
                }
                $this->chemins = ($chemins) ? $chemins : array();
                $this->ordre = 0;
        }

        private function chargerCheminsRelationActuelle() {
                $requete = 'SELECT id_chemin '.
                        'FROM osm_relation_a_chemins '.
                        "WHERE id_relation = {$this->idRelation} ".
                        ' -- '.__FILE__.' : '.__LINE__;
                $chemins = $this->bdd->recupererTous($requete);
                $this->chemins = ($chemins) ? $chemins : array();
                $this->ordre = 0;
        }

        private function getIdPremierChemin() {
                return isset($this->chemins[0]) ? $this->chemins[0]['id_chemin'] : null;
        }

        private function mettreAJourChemin($sens, $ligne, $fichier = __FILE__) {
                $ordre = $this->ordre++;
                $requete = 'UPDATE osm_relation_a_chemins '.
                        "SET ordre = '$ordre', sens = '$sens' ".
                        "WHERE id_relation = {$this->idRelation} ".
                        "AND id_chemin = {$this->idChemin} ".
                        " -- $fichier : $ligne";
                $retour = $this->bdd->requeter($requete);
                return $retour;
        }

        private function getIdDernierNoeud() {
                $requete = 'SELECT id_noeud '.
                        'FROM osm_chemin_a_noeuds '.
                        "WHERE id_chemin = {$this->idChemin} ".
                        'ORDER BY ordre DESC '.
                        'LIMIT 0,1 '.
                        ' -- '.__FILE__.' : '.__LINE__;
                $noeud = $this->bdd->recuperer($requete);
                $idDernierNoeud = ($noeud) ? $noeud['id_noeud'] : false;
                return $idDernierNoeud;
        }

        private function ordonnerChemins() {
                foreach ($this->chemins as $chemin) {
                        // Selection du chemin qui possède le dernier noeud du précédent chemin et écarter l'actuel
                        $idCheminSuivant = $this->getIdCheminSuivant();
                        if ($idCheminSuivant) {
                                $this->idChemin = $idCheminSuivant;
                                $this->comparerNoeuds();
                        } else {
                                $msg = "Impossible de trouver le chemin suivant pour la relation : %s";
                                $this->messages->traiterAvertissement($msg, array($this->idRelation));
                        }
                }
        }

        private function getIdCheminSuivant() {
                $requete = 'SELECT wn.id_chemin '.
                        'FROM osm_relation_a_chemins AS rw '.
                        '       LEFT JOIN osm_chemin_a_noeuds AS wn ON (rw.id_chemin = wn.id_chemin) '.
                        "WHERE wn.id_noeud = {$this->idNoeud} ".
                        "       AND wn.id_chemin != {$this->idChemin} ".
                        "       AND rw.id_relation = {$this->idRelation} ".
                        " LIMIT 0,1 ".
                        ' -- '.__FILE__.' : '.__LINE__;
                $chemins = $this->bdd->recuperer($requete);
                $idCheminSuivant = ($chemins && isset($chemins['id_chemin'])) ? $chemins['id_chemin'] : false;
                return $idCheminSuivant;
        }

        private function comparerNoeuds() {
                $idPremierNoeud = $this->getIdPremierNoeud();
                $idDernierNoeud = $this->getIdDernierNoeud();
                if ( strcmp($idPremierNoeud, $this->idNoeud ) == 0 ) {
                        $this->mettreAJourChemin('directe', __LINE__);
                        $this->idNoeud = $idDernierNoeud;
                } else {
                        $this->mettreAJourChemin('indirecte', __LINE__);
                        $this->idNoeud = $idPremierNoeud;
                }
        }

        private function getIdPremierNoeud() {
                $requete = 'SELECT id_noeud '.
                        'FROM osm_chemin_a_noeuds '.
                        "WHERE id_chemin = {$this->idChemin} ".
                        'ORDER BY ordre ASC '.
                        'LIMIT 0,1 '.
                        ' -- '.__FILE__.' : '.__LINE__;
                $noeud = $this->bdd->recuperer($requete);
                $idPremierNoeud = ($noeud) ? $noeud['id_noeud'] : false;
                return $idPremierNoeud;
        }
}