Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 26 | 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 $point) {
                        list($longitude, $latitude) = $this->obtenirCoordonneesPoint($point);
                        list($indexLongitude, $indexLatitude) = $this->rechercherIndexMaille($longitude, $latitude);
                        $this->mailles[$indexLatitude][$indexLongitude]->ajouterPoint($point);
                }
        }
        
        public function ajouterMailles(& $mailles) {
                foreach ($mailles as $maille) {
                        $longitude = ($maille->longitudeOuest + $maille->longitudeEst) / 2;
                        $latitude  = ($maille->latitudeSud    + $maille->latitudeNord) / 2;
                        list($indexLongitude, $indexLatitude) = $this->rechercherIndexMaille($longitude, $latitude);
                        $this->mailles[$indexLatitude][$indexLongitude]->combinerMailles($maille);
                }
        }
        
        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($toutesLesMailles = false) {
                $mailles_resume = array();
                foreach ($this->mailles as $ligne) {
                        foreach ($ligne as $maille) {
                                $nombrePoints = $maille->getNombrePoints();
                                if ($nombrePoints > 0 || $toutesLesMailles) {
                                        $mailles_resume[] = array(
                                                'zoom'      => $this->zoom,
                                                'sud'       => $maille->getLatitudeSud(),
                                                'ouest'     => $maille->getLongitudeOuest(),
                                                'nord'      => $maille->getLatitudeNord(),
                                                'est'       => $maille->getLongitudeEst(),
                                                'points'    => $nombrePoints,
                                                'observations' => $maille->getNombreObservations(),
                                                'type_site' => 'MAILLE'
                                        );
                                }
                        }
                }
                if (count($mailles_resume) == 0 || count($mailles_resume[0]) == 0)
                        return array();
                return $mailles_resume;
        }

        
}

?>