Subversion Repositories eFlore/Applications.cel

Rev

Rev 978 | Blame | Last modification | View Log | RSS feed

<?php
class CartoGroupage {

        private static $seuilClusterisation = 100;
        private static $zoomDefaut = 3;
        private static $zoomMaxClustering = 12;
        private static $pasZoomDefaut = 1;
        private static $pasZoomMaxClustering = 0.05;
        private static $profondeurMin = 0;
        private static $profondeurMax = 8;
        
        private static $pasCorrectionCentre = null;
        private static $coefficientReductionPas = null; 
        private static $coefficientProfondeurMax = null;
        
        private static $nbElements = array('stations' => 0,'communes' => 0, 'observations' => 0);

        private static $listeNoeudsSelectionnes = array();
        
        private static $pointsDejaTraites = array();

        /*
          +---------+---------+
          |         |         |
          |    A    |    B    |
          |         |         |
          +---------*---------+
          |         |         |
          |    D    |    C    |
          |         |         |
          +---------+---------+
          
          Quatres cadrans sont considérés par le quad tree
          * = centre de la fenetre 
         */
        public static function creerGroupesQuadtree(&$markers, $neLat, $neLng, $swLat, $swLng, $zoom = 3) {
                
                if(count($markers) > self::$seuilClusterisation) {
                                                
                        self::calculerProfondeurMax($zoom);
                        self::calculerPasCorrectionCentre($zoom);

                        $noeudRacine = array('nbrePoints' => count($markers), 'points' => $markers);
                        self::attribuerAuCadran($noeudRacine, $neLat, $neLng, $swLat, $swLng);  
                        
                } else {
                        foreach($markers as $marker) {
                                $points = array($marker);
                                $noeudSimple = array('points' => $points, 'nbrePoints' => 1);
                                self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeudSimple);
                        }
                }
                        
                return self::$listeNoeudsSelectionnes;
        }
        
        private function calculerCoefficientReductionPas() {
                if(self::$coefficientReductionPas == null) {
                        self::$coefficientReductionPas = (self::$pasZoomMaxClustering - self::$pasZoomDefaut)/(self::$zoomMaxClustering - self::$zoomDefaut);
                }
                
                return self::$coefficientReductionPas;
        }
        
        private function calculerPasCorrectionCentre($zoom) {
                self::$pasCorrectionCentre = ($zoom - self::$zoomDefaut) * self::calculerCoefficientReductionPas() + self::$pasZoomDefaut;
        }
        
        private function calculerCoefficientProfondeurMax() {
                if(self::$coefficientProfondeurMax == null) {
                        self::$coefficientProfondeurMax = (self::$profondeurMax - self::$profondeurMin)/(self::$zoomMaxClustering - self::$zoomDefaut);
                }
                
                return self::$coefficientProfondeurMax;
        }
        
        private function calculerProfondeurMax($zoom) { 
                if($zoom > self::$zoomDefaut) { 
                        self::$profondeurMax = round(($zoom - self::$zoomDefaut) * self::calculerCoefficientProfondeurMax() + self::$profondeurMin,0);
                } else {
                        self::$profondeurMax = 1;
                }
        }
        
        public static function getNbElements() {
                return self::$nbElements;
        }
        
        /**
         * 
         * @param mixed $noeud Le noeud à traiter par le quadtree
         * @param float $neLat Latitude du coin nord est de la fenetre 
         * @param float $neLng Longitude du coin nord est de la fenetre 
         * @param float $swLat Latitude du coin sud ouest de la fenetre 
         * @param float $swLng Longitude du coin sud ouest de la fenetre 
         * @param int $profondeur profondeur courante de l'arbre
         */
        private static function attribuerAuCadran(&$noeud, $neLat, $neLng, $swLat, $swLng, $profondeur = 0) {
                        
                $latCentre = round((($neLat+$swLat)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
                $lngCentre = round((($neLng+$swLng)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
                                                
                foreach ($noeud['points'] as &$point) {
                                self::$nbElements['observations']++;
                                self::$nbElements[$point['type_emplacement']]++;
                                unset($point['type_emplacement']);
                                $cadran = self::obtenirCadranPourPoint($latCentre, $lngCentre, $point);
                                self::ajouterFils($noeud,$cadran,$point);
                }
                
                $profondeur++;
                
                if($profondeur <= self::$profondeurMax) {
                        ($noeud['A'] != null) ? self::attribuerAuCadran($noeud['A'], $neLat, $lngCentre , $latCentre, $lngSw, $profondeur) : '';
                        ($noeud['B'] != null) ? self::attribuerAuCadran($noeud['B'], $neLat, $neLng, $latCentre, $lngCentre, $profondeur) : '';
                        ($noeud['C'] != null) ? self::attribuerAuCadran($noeud['C'], $latCentre, $neLng, $swLat, $lngCentre, $profondeur) : '';
                        ($noeud['D'] != null) ? self::attribuerAuCadran($noeud['D'], $latCentre, $lngCentre, $swLat, $swLng, $profondeur) : '';
                }
                
                if(self::estUnParentFeuilles($noeud)) {
                        self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeud);
                }
        }
        
        private function obtenirCadranPourPoint($latCentre,$lngCentre, &$point) {
                if ($point['lng'] < $lngCentre) {
                        if ($point['lat'] > $latCentre) {
                                        $cadran = 'A';
                                } else {
                                        $cadran = 'D';
                                }
                } else {
                        if ($point['lat'] > $latCentre) {
                                $cadran = 'B';
                        } else {
                                $cadran = 'C';
                        }
                }               
                return $cadran;
        }
        
        private static function ajouterFils(&$noeud, $cadran, &$point) {        
                $noeud[$cadran]['points'][] = $point;
                $noeud[$cadran]['nbrePoints']++;
                $noeud[$cadran]['latMoyenne'] += $point['lat'];
                $noeud[$cadran]['lngMoyenne'] += $point['lng'];
        }
        
        private static function ajouterGroupeOuPoint(&$noeud) {
                $groupe = array();
                if ($noeud['nbrePoints'] > 1) {
                        $groupe['lat'] = $noeud['latMoyenne']/$noeud['nbrePoints'];
                        $groupe['lng'] = $noeud['lngMoyenne']/$noeud['nbrePoints'];
                        $groupe['id'] = 'GROUPE:'.$groupe['lat'].';'.$groupe['lng'];
                        $groupe['nbreMarqueur'] = $noeud['nbrePoints'];
                } else {
                        $groupe = $noeud['points'][0];
                }
                return $groupe;
        }
        
        private static function estUnParentFeuilles(&$noeud) {
                return  self::estUneFeuille($noeud['A']) && 
                                self::estUneFeuille($noeud['B']) && 
                                self::estUneFeuille($noeud['C']) && 
                                self::estUneFeuille($noeud['D']);
        }
        
        private static function estUneFeuille(&$noeud) {                
                return $noeud == null || ($noeud['A'] == null && $noeud['B'] == null && $noeud['C'] == null && $noeud['D'] == null);
        }
}
?>