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
 
1142 aurelien 4
	const MARQUEUR_GROUPE = 'GROUPE';
5
	const MARQUEUR_COMMUNE = 'COMMUNE';
6
	const MARQUEUR_STATION = 'STATION';
7
 
1032 aurelien 8
	private static $seuilClusterisation = 200;
978 jpm 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;
1032 aurelien 15
 
978 jpm 16
	private static $pasCorrectionCentre = null;
1032 aurelien 17
	private static $coefficientReductionPas = null;
978 jpm 18
	private static $coefficientProfondeurMax = null;
1032 aurelien 19
 
1172 aurelien 20
	private static $nbElements = array('stations' => 0,'communes' => 0, 'points' => 0);
1142 aurelien 21
	private static $listeNoeudsSelectionnes = array();
1106 aurelien 22
	private static $bornesMax = array('latMin' => null, 'lngMin' => null, 'latMax' => null, 'lngMax' => null);
1142 aurelien 23
	private static $id_traites = array();
978 jpm 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 {
1142 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
				}
1172 aurelien 57
				self::$nbElements['points']++;
977 jpm 58
			}
978 jpm 59
		}
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) {
1142 aurelien 96
 
97
		if(!is_numeric($point['lat']) || abs($point['lat']) > 90) return;
98
		if(!is_numeric($point['lng']) || abs($point['lng']) > 180) return;
99
 
100
		self::$bornesMax['latMin'] = ($point['lat'] < self::$bornesMax['latMin'] || self::$bornesMax['latMin'] == null) ? $point['lat'] : self::$bornesMax['latMin'] ;
101
		self::$bornesMax['lngMin'] = ($point['lng'] < self::$bornesMax['lngMin'] || self::$bornesMax['lngMin'] == null) ? $point['lng'] : self::$bornesMax['lngMin'] ;
102
		self::$bornesMax['latMax'] = ($point['lat'] > self::$bornesMax['latMax'] || self::$bornesMax['latMax'] == null) ? $point['lat'] : self::$bornesMax['latMax'] ;
103
		self::$bornesMax['lngMax'] = ($point['lng'] > self::$bornesMax['lngMax'] || self::$bornesMax['lngMax'] == null) ? $point['lng'] : self::$bornesMax['lngMax'] ;
1106 aurelien 104
	}
105
 
106
	public static function getBornes() {
107
		return self::$bornesMax;
108
	}
109
 
978 jpm 110
	/**
1032 aurelien 111
	 *
978 jpm 112
	 * @param mixed $noeud Le noeud à traiter par le quadtree
1032 aurelien 113
	 * @param float $neLat Latitude du coin nord est de la fenetre
114
	 * @param float $neLng Longitude du coin nord est de la fenetre
115
	 * @param float $swLat Latitude du coin sud ouest de la fenetre
116
	 * @param float $swLng Longitude du coin sud ouest de la fenetre
978 jpm 117
	 * @param int $profondeur profondeur courante de l'arbre
118
	 */
119
	private static function attribuerAuCadran(&$noeud, $neLat, $neLng, $swLat, $swLng, $profondeur = 0) {
1032 aurelien 120
 
978 jpm 121
		$latCentre = round((($neLat+$swLat)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
122
		$lngCentre = round((($neLng+$swLng)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
1032 aurelien 123
 
1142 aurelien 124
		foreach ($noeud['points'] as &$point) {
125
			if(!self::estUnPointAExclure($point)) {
126
				$emplacement = self::formaterPointPourAjout(&$point);
1106 aurelien 127
				self::mettreAJourBornes(&$point);
979 jpm 128
				$cadran = self::obtenirCadranPourPoint($latCentre, $lngCentre, $point);
129
				self::ajouterFils($noeud,$cadran,$point);
1142 aurelien 130
				self::$nbElements[$emplacement]++;
131
			}
1172 aurelien 132
			self::$nbElements['points']++;
977 jpm 133
		}
1032 aurelien 134
 
977 jpm 135
		$profondeur++;
1032 aurelien 136
 
137
		if($profondeur <= self::$profondeurMax) {
138
			(isset($noeud['A']) && $noeud['A'] != null) ? self::attribuerAuCadran($noeud['A'], $neLat, $lngCentre , $latCentre, $swLng, $profondeur) : '';
139
			(isset($noeud['B']) && $noeud['B'] != null) ? self::attribuerAuCadran($noeud['B'], $neLat, $neLng, $latCentre, $lngCentre, $profondeur) : '';
140
			(isset($noeud['C']) && $noeud['C'] != null) ? self::attribuerAuCadran($noeud['C'], $latCentre, $neLng, $swLat, $lngCentre, $profondeur) : '';
141
			(isset($noeud['D']) && $noeud['D'] != null) ? self::attribuerAuCadran($noeud['D'], $latCentre, $lngCentre, $swLat, $swLng, $profondeur) : '';
977 jpm 142
		}
1032 aurelien 143
 
978 jpm 144
		if(self::estUnParentFeuilles($noeud)) {
145
			self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeud);
977 jpm 146
		}
147
	}
1032 aurelien 148
 
1142 aurelien 149
	private static function estUnPointAExclure(&$point) {
150
		return self::estSensible($point) &&
151
		self::coordonneesCommuneSontNulles($point);
152
	}
153
 
154
	private static function coordonneesCommuneSontNulles(&$point) {
155
		$coord_nulles = ($point['wgs84_latitude'] == null ||
156
		$point['wgs84_latitude'] == '' ||
157
		$point['wgs84_longitude'] == null ||
158
		$point['wgs84_longitude'] == '');
159
		return $coord_nulles;
160
	}
161
 
162
	private static function coordonneesSontNulles(&$point) {
163
 
1409 aurelien 164
		$coord_nulles = ($point['latitude'] == '000null' ||
165
				$point['latitude'] == 0 ||
1339 aurelien 166
				$point['latitude'] == '' ||
167
				$point['longitude'] == '000null' ||
1409 aurelien 168
				$point['longitude'] == 0 ||
1339 aurelien 169
				$point['longitude'] == '');
1142 aurelien 170
		return $coord_nulles;
171
	}
172
 
173
	private static function estSensible(&$point) {
174
		$sensible = isset($point['mots_cles_texte']) && substr_count($point['mots_cles_texte'], 'sensible') != 0;
175
		return $sensible;
176
	}
177
 
1341 aurelien 178
	private static function formaterNomStation(&$point, $type_emplacement) {
1142 aurelien 179
		$station = '';
1341 aurelien 180
		if($type_emplacement == 'stations' && $point['station'] != '' && $point['station'] != '000null') {
1142 aurelien 181
			$station = $point['station'];
182
		} else {
1472 aurelien 183
			$id_zone_geo = $point['ce_zone_geo'];
184
			$station = $point['zone_geo'].(($id_zone_geo != '' && $id_zone_geo != '000null') ? ' ('.self::formaterNomZoneGeo($id_zone_geo).')' : '');
1032 aurelien 185
		}
1142 aurelien 186
 
187
		return $station;
1032 aurelien 188
	}
189
 
1339 aurelien 190
	private static function formaterNomZoneGeo($zone_geo) {
191
		$zone_geo = str_replace('INSEE-C:', '', $zone_geo);
192
		$zone_geo = strlen($zone_geo) >= 2 ? substr($zone_geo, 0, 2) : $zone_geo;
193
		return $zone_geo;
194
	}
195
 
1142 aurelien 196
	private static function formaterPointPourAjout(&$point) {
197
 
198
		if(isset($point['type_emplacement'])) {
199
			return $point['type_emplacement'];
200
		}
201
 
202
		if(self::coordonneesSontNulles($point) || self::estSensible($point)) {
1144 aurelien 203
			$point_allege = array();
204
			$point_allege['id'] = self::MARQUEUR_COMMUNE.':'.$point['wgs84_latitude'].'|'.$point['wgs84_longitude'];
205
			$point_allege['type_emplacement'] = 'communes';
1341 aurelien 206
			$point_allege['nom'] = self::formaterNomStation($point, 'communes');
1144 aurelien 207
			$point_allege['lat'] = (float)$point['wgs84_latitude'];
208
			$point_allege['lng'] = (float)$point['wgs84_longitude'];
209
 
210
			$point = $point_allege;
1142 aurelien 211
		} else {
212
			$point_allege = array();
1339 aurelien 213
			$point_allege['id'] = self::MARQUEUR_STATION.':'.$point['latitude'].'|'.$point['longitude'];
1142 aurelien 214
			$point_allege['type_emplacement'] = 'stations';
1341 aurelien 215
			$point_allege['nom'] = self::formaterNomStation($point, 'stations');
1339 aurelien 216
			$point_allege['lat'] = (float)$point['latitude'];
217
			$point_allege['lng'] = (float)$point['longitude'];
1142 aurelien 218
 
219
			$point = $point_allege;
220
		}
221
 
222
		return $point['type_emplacement'];
223
	}
224
 
978 jpm 225
	private function obtenirCadranPourPoint($latCentre,$lngCentre, &$point) {
226
		if ($point['lng'] < $lngCentre) {
227
			if ($point['lat'] > $latCentre) {
228
					$cadran = 'A';
229
				} else {
230
					$cadran = 'D';
231
				}
232
		} else {
233
			if ($point['lat'] > $latCentre) {
234
				$cadran = 'B';
235
			} else {
236
				$cadran = 'C';
237
			}
1032 aurelien 238
		}
978 jpm 239
		return $cadran;
240
	}
1032 aurelien 241
 
1003 jpm 242
	private static function ajouterFils(&$noeud, $cadran, &$point) {
1032 aurelien 243
		if(!isset($noeud[$cadran])) {
244
			$noeud[$cadran] = array('points' => array(),'nbrePoints' => 0, 'latMoyenne' => 0, 'lngMoyenne' => 0);
245
		}
246
		$noeud[$cadran]['points'][] = $point;
247
		$noeud[$cadran]['nbrePoints']++;
248
		$noeud[$cadran]['latMoyenne'] += $point['lat'];
249
		$noeud[$cadran]['lngMoyenne'] += $point['lng'];
978 jpm 250
	}
1032 aurelien 251
 
979 jpm 252
	private static function ajouterGroupeOuPoint(&$noeud) {
977 jpm 253
		$groupe = array();
1142 aurelien 254
		if ($noeud['nbrePoints'] > 1 && isset($noeud['latMoyenne']) && isset($noeud['lngMoyenne'])) {
977 jpm 255
			$groupe['lat'] = $noeud['latMoyenne']/$noeud['nbrePoints'];
256
			$groupe['lng'] = $noeud['lngMoyenne']/$noeud['nbrePoints'];
978 jpm 257
			$groupe['id'] = 'GROUPE:'.$groupe['lat'].';'.$groupe['lng'];
977 jpm 258
			$groupe['nbreMarqueur'] = $noeud['nbrePoints'];
259
		} else {
260
			$groupe = $noeud['points'][0];
261
		}
262
		return $groupe;
263
	}
1032 aurelien 264
 
1148 aurelien 265
 
979 jpm 266
	private static function estUnParentFeuilles(&$noeud) {
1032 aurelien 267
		return  self::estUneFeuille($noeud['A']) &&
268
				self::estUneFeuille($noeud['B']) &&
269
				self::estUneFeuille($noeud['C']) &&
978 jpm 270
				self::estUneFeuille($noeud['D']);
271
	}
1032 aurelien 272
 
273
	private static function estUneFeuille(&$noeud) {
274
		return $noeud == null ||
275
		(!isset($noeud['A']) || $noeud['A'] == null) &&
276
		(!isset($noeud['B']) || $noeud['B'] == null) &&
277
		(!isset($noeud['C']) || $noeud['C'] == null) &&
278
		(!isset($noeud['D']) || $noeud['D'] == null);
977 jpm 279
	}
280
}
281
?>