Subversion Repositories eFlore/Applications.cel

Rev

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

<?php
class CartoGroupage {

        private static $seuilClusterisation = 200;
        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();
        
        private static $bornesMax = array('latMin' => null, 'lngMin' => null, 'latMax' => null, 'lngMax' => null);

        /*
          +---------+---------+
          |         |         |
          |    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) {                          
                                self::mettreAJourBornes(&$marker);
                                $points = array($marker);
                                $noeudSimple = array('points' => $points, 'nbrePoints' => 1);
                                self::$nbElements['observations']++;
                                $emplacement = isset($marker['type_emplacement']) ? $marker['type_emplacement'] : self::obtenirTypeEmplacementParId(&$marker);
                                self::$nbElements[$emplacement]++;
                                unset($marker['type_emplacement']);
                                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;
        }
        
        public static function mettreAJourBornes(&$point) {
                self::$bornesMax['latMin'] = (is_numeric($point['lat']) && $point['lat'] < self::$bornesMax['latMin'] || self::$bornesMax['latMin'] == null) ? $point['lat'] : self::$bornesMax['latMin'] ;
                self::$bornesMax['lngMin'] = (is_numeric($point['lng']) && $point['lng'] < self::$bornesMax['lngMin'] || self::$bornesMax['lngMin'] == null) ? $point['lng'] : self::$bornesMax['lngMin'] ;
                self::$bornesMax['latMax'] = (is_numeric($point['lat']) && $point['lat'] > self::$bornesMax['latMax'] || self::$bornesMax['latMax'] == null) ? $point['lat'] : self::$bornesMax['latMax'] ;
                self::$bornesMax['lngMax'] = (is_numeric($point['lng']) && $point['lng'] > self::$bornesMax['lngMax'] || self::$bornesMax['lngMax'] == null) ? $point['lng'] : self::$bornesMax['lngMax'] ;
        }
        
        public static function getBornes() {
                return self::$bornesMax;
        }
        
        /**
         * 
         * @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::mettreAJourBornes(&$point);
                                self::$nbElements['observations']++;
                                $emplacement = isset($point['type_emplacement']) ? $point['type_emplacement'] : self::obtenirTypeEmplacementParId(&$point);
                                self::$nbElements[$emplacement]++;
                                unset($point['type_emplacement']);
                                $cadran = self::obtenirCadranPourPoint($latCentre, $lngCentre, $point);
                                self::ajouterFils($noeud,$cadran,$point);
                }
                
                $profondeur++;
                
                if($profondeur <= self::$profondeurMax) {
                        (isset($noeud['A']) && $noeud['A'] != null) ? self::attribuerAuCadran($noeud['A'], $neLat, $lngCentre , $latCentre, $swLng, $profondeur) : '';
                        (isset($noeud['B']) && $noeud['B'] != null) ? self::attribuerAuCadran($noeud['B'], $neLat, $neLng, $latCentre, $lngCentre, $profondeur) : '';
                        (isset($noeud['C']) && $noeud['C'] != null) ? self::attribuerAuCadran($noeud['C'], $latCentre, $neLng, $swLat, $lngCentre, $profondeur) : '';
                        (isset($noeud['D']) && $noeud['D'] != null) ? self::attribuerAuCadran($noeud['D'], $latCentre, $lngCentre, $swLat, $swLng, $profondeur) : '';
                }
                
                if(self::estUnParentFeuilles($noeud)) {
                        self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeud);
                }
        }
        
        private static function obtenirTypeEmplacementParId($point) {
                $tableau_point_id = explode(':',$point['id'],2);
                switch($tableau_point_id[0]) {
                        case 'STATION':
                                $type_emplacement = 'stations';
                        break;
                        case 'COMMUNE':
                                $type_emplacement = 'communes';
                        break;
                }
                return $type_emplacement;
        }
        
        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) {
                if(!isset($noeud[$cadran])) {
                        $noeud[$cadran] = array('points' => array(),'nbrePoints' => 0, 'latMoyenne' => 0, 'lngMoyenne' => 0);
                }       
                $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 || 
                (!isset($noeud['A']) || $noeud['A'] == null) && 
                (!isset($noeud['B']) || $noeud['B'] == null) && 
                (!isset($noeud['C']) || $noeud['C'] == null) && 
                (!isset($noeud['D']) || $noeud['D'] == null);
        }
}
?>