Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 31 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php


class Maillage {
        
        private $bbox = array();
        private $zoom = '';
        private $source = '';
        
        private $indexLongitude = array();
        private $indexLatitude  = array();
        private $mailles = array();
        
        private $bdd = null;
        
        
        
        public function __construct($bbox, $zoom, $source) {
                $this->bbox   = $this->calculerLimiteBboxGlobale($bbox);
                $this->zoom   = $zoom;
                $this->source = $source;
                $this->indexLongitude = array();
                $this->indexLatitude  = array();
                $this->mailles = array();
        }
        
        private function calculerLimiteBboxGlobale($bbox) {
                $bboxGlobale = $bbox[0];
                for ($index = 1; $index < count($bbox); $index ++) {
                        if ($bbox[$index]['ouest'] < $bboxGlobale['ouest']) {
                                $bboxGlobale['ouest'] = $bbox[$index]['ouest'];
                        }
                        if ($bbox[$index]['sud'] < $bboxGlobale['sud']) {
                                $bboxGlobale['sud'] = $bbox[$index]['sud'];
                        }
                        if ($bbox[$index]['est'] > $bboxGlobale['est']) {
                                $bboxGlobale['est'] = $bbox[$index]['est'];
                        }
                        if ($bbox[$index]['nord'] > $bboxGlobale['nord']) {
                                $bboxGlobale['nord'] = $bbox[$index]['nord'];
                        }
                }
                return $bboxGlobale;
        }
                
        
        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() {
                $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_nom_eflore');
                $this->bdd->requeter("USE ".$nomBdd);
                return $this->bdd;
        }
        
        
        public function ajouterStations(& $stations) {
                foreach ($stations as $station) {
                        $longitude = $station['longitude'];
                        $latitude = $station['latitude'];
                        list($indexLongitude, $indexLatitude) = $this->rechercherIndexMaille($longitude, $latitude);
                        $this->mailles[$indexLatitude][$indexLongitude]->ajouterStation($station, $this->source);
                }
        }
        
        public function ajouterMailles(& $mailles) {
                foreach ($mailles as $maille) {
                        $longitude = ($maille['ouest'] + $maille['est'])  / 2;
                        $latitude  = ($maille['sud']   + $maille['nord']) / 2;
                        list($indexLongitude, $indexLatitude) = $this->rechercherIndexMaille($longitude, $latitude);
                        $this->mailles[$indexLatitude][$indexLongitude]->combinerMailles($maille, $this->source);
                }
        }
                
        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) {
                                $nombreStations = $maille->getNombrestations();
                                if ($nombreStations > 0) {
                                        $mailles_resume[] = array(
                                                'type_site' => 'MAILLE',
                                                'sud'       => $maille->getLatitudeSud(),
                                                'ouest'     => $maille->getLongitudeOuest(),
                                                'nord'      => $maille->getLatitudeNord(),
                                                'est'       => $maille->getLongitudeEst(),
                                                'stations'     => $maille->getStations(),
                                                'observations' => $maille->getObservations()
                                        );
                                }
                        }
                }
                if (count($mailles_resume) == 0 || count($mailles_resume[0]) == 0)
                        return array();
                return $mailles_resume;
        }

}

?>