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