Subversion Repositories eFlore/Applications.cel

Rev

Rev 1032 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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