Subversion Repositories eFlore/Applications.cel

Rev

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

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