Subversion Repositories eFlore/Applications.moissonnage

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

<?php


class Maillage {
        
        /**
         * @var array les coordonnees des limites de l'espace de recherche
         */
        private $bbox;
        
        /**
         * @var int le niveau actuel de zoom sur la carte (cote navigateur client)
         */
        private $zoom;
        
        /**
         * @var array le tableau indiquant la coordonnee de debut de maille sur la longitude
         */
        private $indexLongitude;
        
        /**
         * @var array le tableau indiquant la coordonnee de debut de maille sur la latitude
         */
        private $indexLatitude;
        
        /**
         * @var array le tableau a deux dimensions recouvrant l'espace de la bbox de mailles
         */
        private $mailles;
                
        
        
        
        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->getIndexDansBbox();
                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 getIndexDansBbox() {
                // recuperer toutes les mailles qui couvrent l'espace a requeter
                $bdd = new Bdd();
                $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']. ")";
                if ($this->bbox['ouest'] >  $this->bbox['est']) {
                        $requete .= " OR (axe='lng' AND NOT(debut >= " . $this->bbox['ouest'] . " AND fin <= "
                                .  $this->bbox['est'] . ")))";
                } else {
                        $requete .= " OR (axe='lng' AND fin >= " . $this->bbox['ouest'] . " AND debut <= "
                                .  $this->bbox['est'] . "))";
                }
                $requete .= " ORDER BY axe, position";
                $bdd->requeter("USE tb_eflore");
                $indexMailles = $bdd->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']);
                        }
                }
        }       
        
        
        public function ajouterPoints($points) {
                foreach ($points as $index =>$point) {
                        if (!isset($point['type_site']) || $point['type_site'] == 'STATION') {
                                $longitude = $point['longitude'];
                                $latitude = $point['latitude'];
                        } else {
                                $longitude = $point['lng_commune'];
                                $latitude = $point['lat_commune'];
                        }

                        // recherche de l'index de la maille dans lequel placer le point
                        $indexLat = 0;
                        $indexLng = 0;
                        while ($indexLat < count($this->indexLatitude) - 1
                                && $this->mailles[$indexLat][0]->getLatitudeNord() < $latitude) {
                                $indexLat++;
                        }
                        while ($indexLng < count($this->indexLongitude) - 1
                                && $this->mailles[$indexLat][$indexLng]->getLongitudeEst() < $longitude) {
                                $indexLng++;
                        }
                        // ajout du point dans la maille correspondante
                        $this->mailles[$indexLat][$indexLng]->ajouterPoint($point);
                }
        }

        
        
        public function resumePourReponseAJAX() {
                $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 resumePourInsertionBdd() {
                $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)
        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);
        }
        
        
}

?>