Subversion Repositories eFlore/Applications.cel

Rev

Rev 2460 | Details | Compare with Previous | Last modification | View Log | RSS feed

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