Subversion Repositories eFlore/Applications.cel

Rev

Details | Last modification | View Log | RSS feed

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