Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 31 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php


class Maillage {
        
        private $bbox;
        private $zoom;
        
        private $indexLongitude;
        private $indexLatitude;
        private $mailles;
        
        private $bdd = null;    
        
        
        
        public function __construct($bbox, $zoom) {
                $this->bbox = $bbox;
                $this->zoom = $zoom;
                $this->indexLongitude = array();
                $this->indexLatitude = array();
                $this->mailles = array();
        }
        
        public function __destruct() {
                while (count($this->indexLatitude) > 0) {
                        array_pop($this->indexLatitude);
                }
                while (count($this->indexLongitude) > 0) {
                        array_pop($this->indexLongitude);
                }
                
                while (count($this->mailles) > 0) {
                        array_pop($this->mailles);
                }
                unset($this);
        }
        
        
        public function genererMaillesVides() {
                $this->recupererIndexMaillesDansBbox();
                foreach ($this->indexLatitude as $indexLat => $intervalleLat) {
                        $ligne = array();
                        foreach ($this->indexLongitude as $indexLng => $intervalleLng) {
                                $ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1],
                                                $intervalleLng[1], $indexLat, $indexLng);
                        }
                        $this->mailles[] = $ligne;
                }
        }
        
        
        private function recupererIndexMaillesDansBbox() {
                // recuperer toutes les mailles qui couvrent l'espace a requeter
                $conditionsLongitude = "";
                if ($this->bbox['ouest'] >  $this->bbox['est']) {
                        $conditionsLongitude = "NOT(debut>=".$this->bbox['ouest']." AND fin<=".$this->bbox['est'].")";
                } else {
                        $conditionsLongitude = "fin>=".$this->bbox['ouest']." AND debut <=".$this->bbox['est'];
                }
                $requete =
                        "SELECT axe, position, debut, fin FROM mailles_index WHERE zoom=".$this->zoom." ".
                        "AND (".
                                "(axe='lat' AND fin>=".$this->bbox['sud']." AND debut<=".$this->bbox['nord'].") ".
                                "OR (axe='lng' AND {$conditionsLongitude})".
                        ") ORDER BY axe, position";
                $indexMailles = $this->getBdd()->recupererTous($requete);
                
                foreach ($indexMailles as $index) {
                        if ($index['axe'] == 'lng') {
                                $this->indexLongitude[$index['position']] = array($index['debut'], $index['fin']);
                        } else {
                                $this->indexLatitude[$index['position']]  = array($index['debut'], $index['fin']);
                        }
                }
        }       
        
        private function getBdd() {
                if (is_null($this->bdd)) {
                        $this->bdd = new Bdd();
                }
                $nomBdd = Config::get('bdd_eflore');
                if (is_null($nomBdd)) {
                        $nomBdd = Config::get('bdd_nom');
                }
                $this->bdd->requeter("USE ".$nomBdd);
                return $this->bdd;
        }
        
        
        public function ajouterPoints(& $points) {
                foreach ($points as $index => $point) {
                        list($longitude, $latitude) = $this->obtenirCoordonneesPoint($point);
                        list($indexLongitude, $indexLatitude) = $this->rechercherIndexMaille($longitude, $latitude);
                        $this->mailles[$indexLatitude][$indexLongitude]->ajouterPoint($point);
                }
        }
        
        private function obtenirCoordonneesPoint($point) {
                $longitude = 0;
                $latitude = 0;
                if (!isset($point['type_site']) || $point['type_site'] == 'STATION') {
                        $longitude = $point['longitude'];
                        $latitude = $point['latitude'];
                } else {
                        $longitude = $point['lng_commune'];
                        $latitude = $point['lat_commune'];
                }
                return array($longitude, $latitude);
        }
        
        private function rechercherIndexMaille($longitude, $latitude) {
                $indexLatitude = 0;
                $indexLongitude = 0;
                while ($indexLatitude < count($this->indexLatitude) - 1
                        && $this->mailles[$indexLatitude][0]->getLatitudeNord() < $latitude) {
                        $indexLatitude ++;
                }
                while ($indexLongitude < count($this->indexLongitude) - 1
                        && $this->mailles[$indexLatitude][$indexLongitude]->getLongitudeEst() < $longitude) {
                        $indexLongitude ++;
                }
                return array($indexLongitude, $indexLatitude);
        }

        public function formaterSortie() {
                $mailles_resume = array();
                foreach ($this->mailles as $ligne) {
                        foreach ($ligne as $maille) {
                                $nombrePoints = $maille->getNombrePoints();
                                if ($nombrePoints == 0)
                                        continue;
                                $mailles_resume[] = array(
                                        'zoom'      => $this->zoom,
                                        'sud'       => $maille->getLatitudeSud(),
                                        'ouest'     => $maille->getLongitudeOuest(),
                                        'nord'      => $maille->getLatitudeNord(),
                                        'est'       => $maille->getLongitudeEst(),
                                        'points'    => $nombrePoints,
                                        'type_site' => 'MAILLE'
                                );
                        }
                }
                if (count($mailles_resume) == 0 || count($mailles_resume[0]) == 0)
                        return array();
                return $mailles_resume;
        }
        
        public function formaterPourInsertionBdd() {
                $mailles_resume = array();
                foreach ($this->mailles as $ligne) {
                        foreach ($ligne as $maille) {
                                if ($maille->getNombrePoints() > 0) {
                                        $mailles_resume[] = array(
                                                'zoom'     => $this->zoom,
                                                'sud'      => $maille->getLatitudeSud(),
                                                'ouest'    => $maille->getLongitudeOuest(),
                                                'nord'     => $maille->getLatitudeNord(),
                                                'est'      => $maille->getLongitudeEst(),
                                                'indexLat' => $maille->getIndexLatitude(),
                                                'indexLng' => $maille->getIndexLongitude(),
                                                'points'   => $maille->getNombrePoints()
                                        );
                                }
                        }
                }
                if (count($mailles_resume[0]) == 0)
                        return array();
                return $mailles_resume;
        }
        

        public function zoomer() {
                $this->zoom += 1;
        }
        
        // redecoupage des mailles en 4 (fonction quadtree)
        // TODO : revoir le fonctionnement de cette methode (pour utilisation par les scripts uniquement)
        public function redecouperMailles() {
                $bdd = new Bdd();
                while (count($this->indexLatitude) > 0) {
                        array_pop($this->indexLatitude);
                }
                while (count($this->indexLongitude) > 0) {
                        array_pop($this->indexLongitude);
                }
                
                $mailles = $this->resumePourInsertionBdd();
                while (count($this->mailles) > 0) {
                        array_pop($this->mailles);
                }
                
                foreach ($mailles as $maille) {
                        $indexLat = 2 * $maille['indexLat'];
                        $indexLng = 2 * $maille['indexLng'];
                        // rechercher les nouvelles coordonnees des mailles au niveau de zoom inferieur
                        $requete = "SELECT axe,position,debut,fin FROM mailles_index WHERE zoom=". $this->zoom ." AND ((axe='lat'"
                                . " AND (position={$indexLat} OR position=" . ($indexLat+1) . ")) OR (axe='lng' AND"
                                . " (position={$indexLng}  OR position=" . ($indexLng+1) . "))) ORDER BY If(axe='lat',0,1)";
                        $resultats = $bdd->recupererTous($requete);
                        
                        $this->indexLatitude[$indexLat]    = array($resultats[0]['debut'], $resultats[0]['fin']);
                        $this->indexLatitude[$indexLat+1]  = array($resultats[1]['debut'], $resultats[1]['fin']);
                        $this->indexLongitude[$indexLng]   = array($resultats[2]['debut'], $resultats[2]['fin']);
                        $this->indexLongitude[$indexLng+1] = array($resultats[3]['debut'], $resultats[3]['fin']);
                }
                ksort($this->indexLatitude);
                ksort($this->indexLongitude);
                
                // creer et ajouter les nouvelles mailles a partir des nouveaux index
                foreach ($this->indexLatitude as $indexLat => $intervalleLat) {
                        $ligne = array();
                        foreach ($this->indexLongitude as $indexLng => $intervalleLng) {
                                $ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1],
                                        $intervalleLng[1], $indexLat, $indexLng);
                        }
                        $this->mailles[] = $ligne;
                }

        }
        
        public function getNombreMailles() {
                return count($this->mailles);
        }
        
        
}

?>