Subversion Repositories eFlore/Applications.cel

Rev

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

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