| 977 | jpm | 1 | <?php
 | 
        
           |  |  | 2 | class CartoGroupage {
 | 
        
           | 978 | jpm | 3 |   | 
        
           |  |  | 4 | 	private static $seuilClusterisation = 100;
 | 
        
           |  |  | 5 | 	private static $zoomDefaut = 3;
 | 
        
           |  |  | 6 | 	private static $zoomMaxClustering = 12;
 | 
        
           |  |  | 7 | 	private static $pasZoomDefaut = 1;
 | 
        
           |  |  | 8 | 	private static $pasZoomMaxClustering = 0.05;
 | 
        
           |  |  | 9 | 	private static $profondeurMin = 0;
 | 
        
           |  |  | 10 | 	private static $profondeurMax = 8;
 | 
        
           | 977 | jpm | 11 |   | 
        
           | 978 | jpm | 12 | 	private static $pasCorrectionCentre = null;
 | 
        
           |  |  | 13 | 	private static $coefficientReductionPas = null;
 | 
        
           |  |  | 14 | 	private static $coefficientProfondeurMax = null;
 | 
        
           | 979 | jpm | 15 |   | 
        
           |  |  | 16 | 	private static $nbElements = array('stations' => 0,'communes' => 0, 'observations' => 0);
 | 
        
           | 978 | jpm | 17 |   | 
        
           | 977 | jpm | 18 | 	private static $listeNoeudsSelectionnes = array();
 | 
        
           | 979 | jpm | 19 |   | 
        
           |  |  | 20 | 	private static $pointsDejaTraites = array();
 | 
        
           | 978 | jpm | 21 |   | 
        
           |  |  | 22 | 	/*
 | 
        
           |  |  | 23 | 	  +---------+---------+
 | 
        
           |  |  | 24 | 	  |         |         |
 | 
        
           |  |  | 25 | 	  |    A    |    B    |
 | 
        
           |  |  | 26 | 	  |         |         |
 | 
        
           |  |  | 27 | 	  +---------*---------+
 | 
        
           |  |  | 28 | 	  |         |         |
 | 
        
           |  |  | 29 | 	  |    D    |    C    |
 | 
        
           |  |  | 30 | 	  |         |         |
 | 
        
           |  |  | 31 | 	  +---------+---------+
 | 
        
           |  |  | 32 |   | 
        
           |  |  | 33 | 	  Quatres cadrans sont considérés par le quad tree
 | 
        
           |  |  | 34 | 	  * = centre de la fenetre
 | 
        
           |  |  | 35 | 	 */
 | 
        
           | 979 | jpm | 36 | 	public static function creerGroupesQuadtree(&$markers, $neLat, $neLng, $swLat, $swLng, $zoom = 3) {
 | 
        
           | 977 | jpm | 37 |   | 
        
           | 978 | jpm | 38 | 		if(count($markers) > self::$seuilClusterisation) {
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 | 			self::calculerProfondeurMax($zoom);
 | 
        
           |  |  | 41 | 			self::calculerPasCorrectionCentre($zoom);
 | 
        
           |  |  | 42 |   | 
        
           |  |  | 43 | 			$noeudRacine = array('nbrePoints' => count($markers), 'points' => $markers);
 | 
        
           |  |  | 44 | 			self::attribuerAuCadran($noeudRacine, $neLat, $neLng, $swLat, $swLng);
 | 
        
           | 977 | jpm | 45 |   | 
        
           | 978 | jpm | 46 | 		} else {
 | 
        
           |  |  | 47 | 			foreach($markers as $marker) {
 | 
        
           |  |  | 48 | 				$points = array($marker);
 | 
        
           |  |  | 49 | 				$noeudSimple = array('points' => $points, 'nbrePoints' => 1);
 | 
        
           |  |  | 50 | 				self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeudSimple);
 | 
        
           | 977 | jpm | 51 | 			}
 | 
        
           | 978 | jpm | 52 | 		}
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 | 		return self::$listeNoeudsSelectionnes;
 | 
        
           |  |  | 55 | 	}
 | 
        
           | 977 | jpm | 56 |   | 
        
           | 978 | jpm | 57 | 	private function calculerCoefficientReductionPas() {
 | 
        
           |  |  | 58 | 		if(self::$coefficientReductionPas == null) {
 | 
        
           |  |  | 59 | 			self::$coefficientReductionPas = (self::$pasZoomMaxClustering - self::$pasZoomDefaut)/(self::$zoomMaxClustering - self::$zoomDefaut);
 | 
        
           | 977 | jpm | 60 | 		}
 | 
        
           |  |  | 61 |   | 
        
           | 978 | jpm | 62 | 		return self::$coefficientReductionPas;
 | 
        
           | 977 | jpm | 63 | 	}
 | 
        
           |  |  | 64 |   | 
        
           | 978 | jpm | 65 | 	private function calculerPasCorrectionCentre($zoom) {
 | 
        
           |  |  | 66 | 		self::$pasCorrectionCentre = ($zoom - self::$zoomDefaut) * self::calculerCoefficientReductionPas() + self::$pasZoomDefaut;
 | 
        
           | 977 | jpm | 67 | 	}
 | 
        
           |  |  | 68 |   | 
        
           | 978 | jpm | 69 | 	private function calculerCoefficientProfondeurMax() {
 | 
        
           |  |  | 70 | 		if(self::$coefficientProfondeurMax == null) {
 | 
        
           |  |  | 71 | 			self::$coefficientProfondeurMax = (self::$profondeurMax - self::$profondeurMin)/(self::$zoomMaxClustering - self::$zoomDefaut);
 | 
        
           | 977 | jpm | 72 | 		}
 | 
        
           | 978 | jpm | 73 |   | 
        
           |  |  | 74 | 		return self::$coefficientProfondeurMax;
 | 
        
           | 977 | jpm | 75 | 	}
 | 
        
           |  |  | 76 |   | 
        
           | 978 | jpm | 77 | 	private function calculerProfondeurMax($zoom) {
 | 
        
           |  |  | 78 | 		if($zoom > self::$zoomDefaut) {
 | 
        
           |  |  | 79 | 			self::$profondeurMax = round(($zoom - self::$zoomDefaut) * self::calculerCoefficientProfondeurMax() + self::$profondeurMin,0);
 | 
        
           | 977 | jpm | 80 | 		} else {
 | 
        
           | 978 | jpm | 81 | 			self::$profondeurMax = 1;
 | 
        
           | 977 | jpm | 82 | 		}
 | 
        
           |  |  | 83 | 	}
 | 
        
           |  |  | 84 |   | 
        
           | 979 | jpm | 85 | 	public static function getNbElements() {
 | 
        
           |  |  | 86 | 		return self::$nbElements;
 | 
        
           |  |  | 87 | 	}
 | 
        
           |  |  | 88 |   | 
        
           | 978 | jpm | 89 | 	/**
 | 
        
           |  |  | 90 | 	 *
 | 
        
           |  |  | 91 | 	 * @param mixed $noeud Le noeud à traiter par le quadtree
 | 
        
           |  |  | 92 | 	 * @param float $neLat Latitude du coin nord est de la fenetre
 | 
        
           |  |  | 93 | 	 * @param float $neLng Longitude du coin nord est de la fenetre
 | 
        
           |  |  | 94 | 	 * @param float $swLat Latitude du coin sud ouest de la fenetre
 | 
        
           |  |  | 95 | 	 * @param float $swLng Longitude du coin sud ouest de la fenetre
 | 
        
           |  |  | 96 | 	 * @param int $profondeur profondeur courante de l'arbre
 | 
        
           |  |  | 97 | 	 */
 | 
        
           |  |  | 98 | 	private static function attribuerAuCadran(&$noeud, $neLat, $neLng, $swLat, $swLng, $profondeur = 0) {
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 | 		$latCentre = round((($neLat+$swLat)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
 | 
        
           |  |  | 101 | 		$lngCentre = round((($neLng+$swLng)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
 | 
        
           |  |  | 102 |   | 
        
           | 977 | jpm | 103 | 		foreach ($noeud['points'] as &$point) {
 | 
        
           | 979 | jpm | 104 | 				self::$nbElements['observations']++;
 | 
        
           |  |  | 105 | 				self::$nbElements[$point['type_emplacement']]++;
 | 
        
           |  |  | 106 | 				unset($point['type_emplacement']);
 | 
        
           |  |  | 107 | 				$cadran = self::obtenirCadranPourPoint($latCentre, $lngCentre, $point);
 | 
        
           |  |  | 108 | 				self::ajouterFils($noeud,$cadran,$point);
 | 
        
           | 977 | jpm | 109 | 		}
 | 
        
           |  |  | 110 |   | 
        
           |  |  | 111 | 		$profondeur++;
 | 
        
           |  |  | 112 |   | 
        
           | 978 | jpm | 113 | 		if($profondeur <= self::$profondeurMax) {
 | 
        
           |  |  | 114 | 			($noeud['A'] != null) ? self::attribuerAuCadran($noeud['A'], $neLat, $lngCentre , $latCentre, $lngSw, $profondeur) : '';
 | 
        
           |  |  | 115 | 			($noeud['B'] != null) ? self::attribuerAuCadran($noeud['B'], $neLat, $neLng, $latCentre, $lngCentre, $profondeur) : '';
 | 
        
           |  |  | 116 | 			($noeud['C'] != null) ? self::attribuerAuCadran($noeud['C'], $latCentre, $neLng, $swLat, $lngCentre, $profondeur) : '';
 | 
        
           |  |  | 117 | 			($noeud['D'] != null) ? self::attribuerAuCadran($noeud['D'], $latCentre, $lngCentre, $swLat, $swLng, $profondeur) : '';
 | 
        
           | 977 | jpm | 118 | 		}
 | 
        
           |  |  | 119 |   | 
        
           | 978 | jpm | 120 | 		if(self::estUnParentFeuilles($noeud)) {
 | 
        
           |  |  | 121 | 			self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeud);
 | 
        
           | 977 | jpm | 122 | 		}
 | 
        
           |  |  | 123 | 	}
 | 
        
           |  |  | 124 |   | 
        
           | 978 | jpm | 125 | 	private function obtenirCadranPourPoint($latCentre,$lngCentre, &$point) {
 | 
        
           |  |  | 126 | 		if ($point['lng'] < $lngCentre) {
 | 
        
           |  |  | 127 | 			if ($point['lat'] > $latCentre) {
 | 
        
           |  |  | 128 | 					$cadran = 'A';
 | 
        
           |  |  | 129 | 				} else {
 | 
        
           |  |  | 130 | 					$cadran = 'D';
 | 
        
           |  |  | 131 | 				}
 | 
        
           |  |  | 132 | 		} else {
 | 
        
           |  |  | 133 | 			if ($point['lat'] > $latCentre) {
 | 
        
           |  |  | 134 | 				$cadran = 'B';
 | 
        
           |  |  | 135 | 			} else {
 | 
        
           |  |  | 136 | 				$cadran = 'C';
 | 
        
           |  |  | 137 | 			}
 | 
        
           |  |  | 138 | 		}
 | 
        
           |  |  | 139 | 		return $cadran;
 | 
        
           |  |  | 140 | 	}
 | 
        
           |  |  | 141 |   | 
        
           |  |  | 142 | 	private static function ajouterFils(&$noeud, $cadran, &$point) {
 | 
        
           |  |  | 143 | 		$noeud[$cadran]['points'][] = $point;
 | 
        
           |  |  | 144 | 		$noeud[$cadran]['nbrePoints']++;
 | 
        
           |  |  | 145 | 		$noeud[$cadran]['latMoyenne'] += $point['lat'];
 | 
        
           |  |  | 146 | 		$noeud[$cadran]['lngMoyenne'] += $point['lng'];
 | 
        
           |  |  | 147 | 	}
 | 
        
           |  |  | 148 |   | 
        
           | 979 | jpm | 149 | 	private static function ajouterGroupeOuPoint(&$noeud) {
 | 
        
           | 977 | jpm | 150 | 		$groupe = array();
 | 
        
           |  |  | 151 | 		if ($noeud['nbrePoints'] > 1) {
 | 
        
           |  |  | 152 | 			$groupe['lat'] = $noeud['latMoyenne']/$noeud['nbrePoints'];
 | 
        
           |  |  | 153 | 			$groupe['lng'] = $noeud['lngMoyenne']/$noeud['nbrePoints'];
 | 
        
           | 978 | jpm | 154 | 			$groupe['id'] = 'GROUPE:'.$groupe['lat'].';'.$groupe['lng'];
 | 
        
           | 977 | jpm | 155 | 			$groupe['nbreMarqueur'] = $noeud['nbrePoints'];
 | 
        
           |  |  | 156 | 		} else {
 | 
        
           |  |  | 157 | 			$groupe = $noeud['points'][0];
 | 
        
           |  |  | 158 | 		}
 | 
        
           |  |  | 159 | 		return $groupe;
 | 
        
           |  |  | 160 | 	}
 | 
        
           |  |  | 161 |   | 
        
           | 979 | jpm | 162 | 	private static function estUnParentFeuilles(&$noeud) {
 | 
        
           | 978 | jpm | 163 | 		return  self::estUneFeuille($noeud['A']) &&
 | 
        
           |  |  | 164 | 				self::estUneFeuille($noeud['B']) &&
 | 
        
           |  |  | 165 | 				self::estUneFeuille($noeud['C']) &&
 | 
        
           |  |  | 166 | 				self::estUneFeuille($noeud['D']);
 | 
        
           |  |  | 167 | 	}
 | 
        
           |  |  | 168 |   | 
        
           | 979 | jpm | 169 | 	private static function estUneFeuille(&$noeud) {
 | 
        
           | 977 | jpm | 170 | 		return $noeud == null || ($noeud['A'] == null && $noeud['B'] == null && $noeud['C'] == null && $noeud['D'] == null);
 | 
        
           |  |  | 171 | 	}
 | 
        
           |  |  | 172 | }
 | 
        
           |  |  | 173 | ?>
 |