Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1047 | 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 $premierNoeudPolygon = null;
        private $infosRel = array();
        private $infosNoeuds = array();
        private $idRelation = null;
        private $idChemin = null;
        private $idNoeud = null;
        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->chargerDonneesRelationActuelle();

                        $numPolygon = 1;
                        while(count($this->chemins) > 0) {
                                reset($this->chemins);
                                $idChemin = key($this->chemins);
                                echo "--------------POLY : $numPolygon : $idChemin\n";
                                $this->ordonnerCheminsRelation($numPolygon, $idChemin);
                                $this->mettreAJourRelationAChemin();
                                $numPolygon++;
                        }

                        // 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 '.
                        'WHERE id_relation = 403823 '.
                        ' -- '.__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 '.
                        '       INNER JOIN osm_chemin_a_noeuds AS cn ON (rc.id_chemin = cn.id_chemin) '.
                        "WHERE rc.id_relation = {$this->idRelation} ".
                        "ORDER BY rc.ordre ASC, cn.ordre ASC ".
                        ' -- '.__FILE__.' : '.__LINE__;
                $infos = $this->bdd->recupererTous($requete);
                $noeuds = array();
                foreach ($infos as $info) {
                        $noeuds[$info['id_noeud']][$info['id_chemin']] = $info['ordre'];
                        if (! isset($this->chemins[$info['id_chemin']]) ) {
                                $this->chemins[$info['id_chemin']]['max'] == info['ordre'];
                                if ($info['ordre'] == 1) {
                                        $this->chemins[$info['id_chemin']][0] == $info['id_noeud'];
                                }
                        } else {

                        }
                        if (count($noeuds[$info['id_noeud']]) == 2) {
                                list($premierChemin, $dernierChemin) = array_keys($noeuds[$info['id_noeud']]);
                                $this->chemins[$premierChemin][] = $info['id_noeud'];
                                $this->chemins[$dernierChemin][] = $info['id_noeud'];
                                $this->noeuds[$info['id_noeud']][$premierChemin] = $dernierChemin;
                                $this->noeuds[$info['id_noeud']][$dernierChemin] = $premierChemin;
                        }
                }
                //print_r($this->chemins);exit();
        }

        private function ordonnerCheminsRelation($numPolygon, $idChemin, $idCheminPrecedent = null, $ordre = 1) {
                $this->infosRel[$idChemin] = array($ordre, $numPolygon);

                list($premierNoeud, $dernierNoeud) = $this->chemins[$idChemin];

                if ($idCheminPrecedent == null) {// Premier chemin à tester
                        $this->premierNoeudPolygon = $premierNoeud;
                        $noeudSuivant = ($dernierNoeud == $premierNoeud) ? $premierNoeud : $dernierNoeud;
                        echo "Chemin $idChemin :: premierNoeud: $premierNoeud, dernierNoeud: $dernierNoeud \n";
                } else {
                        list($premierNoeudPrec, $dernierNoeudPrec) = $this->chemins[$idCheminPrecedent];
                        unset($this->chemins[$idCheminPrecedent]);
                        $noeudSuivant = ($dernierNoeudPrec == $dernierNoeud || $premierNoeudPrec == $dernierNoeud) ? $premierNoeud : $dernierNoeud;
                        echo "Chemin $idChemin :: premierNoeudPrec: $premierNoeudPrec, dernierNoeudPrec: $dernierNoeudPrec, premierNoeud: $premierNoeud, dernierNoeud: $dernierNoeud \n";
                }

                if ($this->premierNoeudPolygon != $noeudSuivant) {
                        if (isset($this->noeuds[$noeudSuivant][$idChemin])) {
                                $idCheminSuivant = $this->noeuds[$noeudSuivant][$idChemin];
                                if (! isset($this->infosRel[$idCheminSuivant])) {
                                        $this->ordonnerCheminsRelation($numPolygon, $idCheminSuivant, $idChemin, ++$ordre);
                                } else {
                                        $this->messages->traiterAvertissement("Chemins %s déjà pris en comtpe", array($idCheminSuivant));
                                }
                        } else {
                                $this->messages->traiterAvertissement("Relation %s a un polygone incomplet", array($this->idRelation));
                        }
                } else {
                        unset($this->chemins[$idChemin]);
                }
                return null;
        }

        private function mettreAJourRelationAChemin() {
                if (count($this->infosRel) > 0) {
                        foreach ($this->infosRel as $idC => $donnees) {
                                list($ordre, $numPolygon) = $this->bdd->proteger($donnees);
                                $requete = 'UPDATE osm_relation_a_chemins '.
                                        "SET sens = $ordre, num_poly = $numPolygon ".
                                        "WHERE id_relation = {$this->idRelation} ".
                                        "AND id_chemin = $idC ".
                                        ' -- '.__FILE__.' : '.__LINE__;
                                $this->bdd->requeter($requete);
                        }
                        $this->infosRel = array();
                }
        }

        private function creerSet($donnees) {
                $values = array();
                foreach ($donnees as $infos) {
                        $infosP = $this->bdd->proteger($infos);
                        $values[] = implode(',', $infosP);
                }
                $valuesClause = '('.implode('),(', $values).')';
                return $valuesClause;
        }

        private function getIdPremierChemin() {
                $idChemin = null;
                if (count($this->infosRel) > 0) {
                        reset($this->infosRel);
                        $idChemin = key($this->infosRel);
                }
                return $idChemin;
        }

        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() {
                $idNoeud = false;
                if (count($this->infosRel[$this->idChemin]) > 0) {
                        end($this->infosRel[$this->idChemin]);
                        $idNoeud = current($this->infosRel[$this->idChemin]);
                }
                return $idNoeud;
        }

        private function ordonnerChemins() {
                foreach ($this->infosRel 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;
                                $idPremierNoeud = $this->getIdPremierNoeud();
                                $idDernierNoeud = $this->getIdDernierNoeud();
                                $sens = $this->getSens($idPremierNoeud);
                                $this->mettreAJourChemin($sens, __LINE__);
                                $this->idNoeud =  ($sens == 'directe') ? $idDernierNoeud : $idPremierNoeud;
                        }
                }
        }

        private function getIdCheminSuivant() {
                $idCheminSuivant = false;
                if (isset($this->infosNoeuds[$this->idNoeud])) {
                        $idCheminSuivant = next($this->infosNoeuds[$this->idNoeud]);
                        //echo $this->idChemin.'-'.$idCheminSuivant."\n".print_r($this->infosNoeuds[$this->idNoeud]); exit();
                } else {
                        $msg = "Impossible de trouver le chemin suivant pour la relation : %s, le chemin %s et le noeud %s";
                        $this->messages->traiterAvertissement($msg, array($this->idRelation, $this->idChemin, $this->idNoeud));
                }
                return $idCheminSuivant;
        }

        private function getSens($idPremierNoeud) {
                $sens = ( strcmp($idPremierNoeud, $this->idNoeud ) == 0 ) ? 'directe' : 'indirecte';
                return $sens;
        }

        private function getIdPremierNoeud() {
                $idNoeud = false;
                if (count($this->infosRel[$this->idChemin]) > 0) {
                        reset($this->infosRel[$this->idChemin]);
                        $idNoeud = current($this->infosRel[$this->idChemin]);
                }
                return $idNoeud;
        }
}