Subversion Repositories eFlore/Applications.cel

Rev

Rev 1121 | Rev 1139 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1121 aurelien 1
<?php
2
class CartoGroupage {
3
 
1138 aurelien 4
	const MARQUEUR_GROUPE = 'GROUPE';
5
	const MARQUEUR_COMMUNE = 'COMMUNE';
6
	const MARQUEUR_STATION = 'STATION';
7
 
1121 aurelien 8
	private static $seuilClusterisation = 200;
9
	private static $zoomDefaut = 3;
10
	private static $zoomMaxClustering = 12;
11
	private static $pasZoomDefaut = 1;
12
	private static $pasZoomMaxClustering = 0.05;
13
	private static $profondeurMin = 0;
14
	private static $profondeurMax = 8;
15
 
16
	private static $pasCorrectionCentre = null;
17
	private static $coefficientReductionPas = null;
18
	private static $coefficientProfondeurMax = null;
19
 
20
	private static $nbElements = array('stations' => 0,'communes' => 0, 'observations' => 0);
1138 aurelien 21
	private static $listeNoeudsSelectionnes = array();
1121 aurelien 22
	private static $bornesMax = array('latMin' => null, 'lngMin' => null, 'latMax' => null, 'lngMax' => null);
1138 aurelien 23
	private static $id_traites = array();
1121 aurelien 24
	/*
25
	  +---------+---------+
26
	  |         |         |
27
	  |    A    |    B    |
28
	  |         |         |
29
	  +---------*---------+
30
	  |         |         |
31
	  |    D    |    C    |
32
	  |         |         |
33
	  +---------+---------+
34
 
35
	  Quatres cadrans sont considérés par le quad tree
36
	  * = centre de la fenetre
37
	 */
38
	public static function creerGroupesQuadtree(&$markers, $neLat, $neLng, $swLat, $swLng, $zoom = 3) {
39
		if(count($markers) > self::$seuilClusterisation) {
40
 
41
			self::calculerProfondeurMax($zoom);
42
			self::calculerPasCorrectionCentre($zoom);
43
 
44
			$noeudRacine = array('nbrePoints' => count($markers), 'points' => $markers);
45
			self::attribuerAuCadran($noeudRacine, $neLat, $neLng, $swLat, $swLng);
46
 
47
		} else {
1138 aurelien 48
			foreach($markers as &$marker) {
49
				if(!self::estUnPointAExclure($marker)) {
50
					$emplacement = self::formaterPointPourAjout(&$marker);
51
					self::mettreAJourBornes(&$marker);
52
					$points = array($marker);
53
					$noeudSimple = array('points' => $points, 'nbrePoints' => 1);
54
					self::$nbElements[$emplacement]++;
55
					self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeudSimple);
56
				}
1121 aurelien 57
				self::$nbElements['observations']++;
58
			}
59
		}
60
		return self::$listeNoeudsSelectionnes;
61
	}
62
 
63
	private function calculerCoefficientReductionPas() {
64
		if(self::$coefficientReductionPas == null) {
65
			self::$coefficientReductionPas = (self::$pasZoomMaxClustering - self::$pasZoomDefaut)/(self::$zoomMaxClustering - self::$zoomDefaut);
66
		}
67
 
68
		return self::$coefficientReductionPas;
69
	}
70
 
71
	private function calculerPasCorrectionCentre($zoom) {
72
		self::$pasCorrectionCentre = ($zoom - self::$zoomDefaut) * self::calculerCoefficientReductionPas() + self::$pasZoomDefaut;
73
	}
74
 
75
	private function calculerCoefficientProfondeurMax() {
76
		if(self::$coefficientProfondeurMax == null) {
77
			self::$coefficientProfondeurMax = (self::$profondeurMax - self::$profondeurMin)/(self::$zoomMaxClustering - self::$zoomDefaut);
78
		}
79
 
80
		return self::$coefficientProfondeurMax;
81
	}
82
 
83
	private function calculerProfondeurMax($zoom) {
84
		if($zoom > self::$zoomDefaut) {
85
			self::$profondeurMax = round(($zoom - self::$zoomDefaut) * self::calculerCoefficientProfondeurMax() + self::$profondeurMin,0);
86
		} else {
87
			self::$profondeurMax = 1;
88
		}
89
	}
90
 
91
	public static function getNbElements() {
92
		return self::$nbElements;
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
	}
105
 
106
	/**
107
	 *
108
	 * @param mixed $noeud Le noeud à traiter par le quadtree
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
113
	 * @param int $profondeur profondeur courante de l'arbre
114
	 */
115
	private static function attribuerAuCadran(&$noeud, $neLat, $neLng, $swLat, $swLng, $profondeur = 0) {
116
 
117
		$latCentre = round((($neLat+$swLat)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
118
		$lngCentre = round((($neLng+$swLng)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
119
 
1138 aurelien 120
		foreach ($noeud['points'] as &$point) {
121
			if(!self::estUnPointAExclure($point)) {
122
				$emplacement = self::formaterPointPourAjout(&$point);
1121 aurelien 123
				self::mettreAJourBornes(&$point);
124
				$cadran = self::obtenirCadranPourPoint($latCentre, $lngCentre, $point);
125
				self::ajouterFils($noeud,$cadran,$point);
1138 aurelien 126
				self::$nbElements[$emplacement]++;
127
			}
128
			self::$nbElements['observations']++;
1121 aurelien 129
		}
130
 
131
		$profondeur++;
132
 
133
		if($profondeur <= self::$profondeurMax) {
134
			(isset($noeud['A']) && $noeud['A'] != null) ? self::attribuerAuCadran($noeud['A'], $neLat, $lngCentre , $latCentre, $swLng, $profondeur) : '';
135
			(isset($noeud['B']) && $noeud['B'] != null) ? self::attribuerAuCadran($noeud['B'], $neLat, $neLng, $latCentre, $lngCentre, $profondeur) : '';
136
			(isset($noeud['C']) && $noeud['C'] != null) ? self::attribuerAuCadran($noeud['C'], $latCentre, $neLng, $swLat, $lngCentre, $profondeur) : '';
137
			(isset($noeud['D']) && $noeud['D'] != null) ? self::attribuerAuCadran($noeud['D'], $latCentre, $lngCentre, $swLat, $swLng, $profondeur) : '';
138
		}
139
 
140
		if(self::estUnParentFeuilles($noeud)) {
141
			self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeud);
142
		}
143
	}
144
 
1138 aurelien 145
	private static function estUnPointAExclure(&$point) {
146
		return self::estSensible($point) &&
147
		self::coordonneesCommuneSontNulles($point);
148
	}
149
 
150
	private static function coordonneesCommuneSontNulles(&$point) {
151
		$coord_nulles = ($point['wgs84_latitude'] == null ||
152
		$point['wgs84_latitude'] == '' ||
153
		$point['wgs84_longitude'] == null ||
154
		$point['wgs84_longitude'] == '');
155
		return $coord_nulles;
156
	}
157
 
158
	private static function coordonneesSontNulles(&$point) {
159
 
160
		$coord_nulles = ($point['coord_x'] == '000null' ||
161
				$point['coord_x'] == '' ||
162
				$point['coord_y'] == '000null' ||
163
				$point['coord_y'] == '');
164
		return $coord_nulles;
165
	}
166
 
167
	private static function estSensible(&$point) {
168
		$sensible = isset($point['mots_cles_texte']) && substr_count($point['mots_cles_texte'], 'sensible') != 0;
169
		return $sensible;
170
	}
171
 
172
	private static function formaterNomStation(&$point) {
173
		$station = '';
174
		if($point['station'] != '' && $point['station'] != '000null') {
175
			$station = $point['station'];
176
		} else {
177
			$station = $point['location'].(($point['id_location'] != '' && $point['id_location'] != '000null') ? '('.$point['id_location'].')' : '');
1121 aurelien 178
		}
1138 aurelien 179
 
180
		return $station;
1121 aurelien 181
	}
182
 
1138 aurelien 183
	private static function formaterPointPourAjout(&$point) {
184
 
185
		if(isset($point['type_emplacement'])) {
186
			return $point['type_emplacement'];
187
		}
188
 
189
		if(self::coordonneesSontNulles($point) || self::estSensible($point)) {
190
				$point['id'] = self::MARQUEUR_COMMUNE.':'.$point['wgs84_latitude'].'|'.$point['wgs84_longitude'];
191
				$point['type_emplacement'] = 'communes';
192
				$point['lat'] = (float)$point['wgs84_latitude'];
193
				$point['lng'] = (float)$point['wgs84_longitude'];
194
		} else {
195
			$point_allege = array();
196
			$point_allege['id'] = self::MARQUEUR_STATION.':'.$point['coord_x'].'|'.$point['coord_y'];
197
			$point_allege['type_emplacement'] = 'stations';
198
			$point_allege['nom'] = self::formaterNomStation($point);
199
			$point_allege['lat'] = (float)$point['coord_x'];
200
			$point_allege['lng'] = (float)$point['coord_y'];
201
 
202
			$point = $point_allege;
203
		}
204
 
205
		return $point['type_emplacement'];
206
	}
207
 
1121 aurelien 208
	private function obtenirCadranPourPoint($latCentre,$lngCentre, &$point) {
209
		if ($point['lng'] < $lngCentre) {
210
			if ($point['lat'] > $latCentre) {
211
					$cadran = 'A';
212
				} else {
213
					$cadran = 'D';
214
				}
215
		} else {
216
			if ($point['lat'] > $latCentre) {
217
				$cadran = 'B';
218
			} else {
219
				$cadran = 'C';
220
			}
221
		}
222
		return $cadran;
223
	}
224
 
225
	private static function ajouterFils(&$noeud, $cadran, &$point) {
226
		if(!isset($noeud[$cadran])) {
227
			$noeud[$cadran] = array('points' => array(),'nbrePoints' => 0, 'latMoyenne' => 0, 'lngMoyenne' => 0);
228
		}
229
		$noeud[$cadran]['points'][] = $point;
230
		$noeud[$cadran]['nbrePoints']++;
231
		$noeud[$cadran]['latMoyenne'] += $point['lat'];
232
		$noeud[$cadran]['lngMoyenne'] += $point['lng'];
233
	}
234
 
235
	private static function ajouterGroupeOuPoint(&$noeud) {
236
		$groupe = array();
1138 aurelien 237
		if ($noeud['nbrePoints'] > 1 && isset($noeud['latMoyenne']) && isset($noeud['lngMoyenne'])) {
1121 aurelien 238
			$groupe['lat'] = $noeud['latMoyenne']/$noeud['nbrePoints'];
239
			$groupe['lng'] = $noeud['lngMoyenne']/$noeud['nbrePoints'];
240
			$groupe['id'] = 'GROUPE:'.$groupe['lat'].';'.$groupe['lng'];
241
			$groupe['nbreMarqueur'] = $noeud['nbrePoints'];
242
		} else {
243
			$groupe = $noeud['points'][0];
244
		}
245
		return $groupe;
246
	}
247
 
248
	private static function estUnParentFeuilles(&$noeud) {
249
		return  self::estUneFeuille($noeud['A']) &&
250
				self::estUneFeuille($noeud['B']) &&
251
				self::estUneFeuille($noeud['C']) &&
252
				self::estUneFeuille($noeud['D']);
253
	}
254
 
255
	private static function estUneFeuille(&$noeud) {
256
		return $noeud == null ||
257
		(!isset($noeud['A']) || $noeud['A'] == null) &&
258
		(!isset($noeud['B']) || $noeud['B'] == null) &&
259
		(!isset($noeud['C']) || $noeud['C'] == null) &&
260
		(!isset($noeud['D']) || $noeud['D'] == null);
261
	}
262
}
263
?>