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
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);
1142 aurelien 139
				self::$nbElements[$emplacement]++;
140
			}
1172 aurelien 141
			self::$nbElements['points']++;
977 jpm 142
		}
1585 jpm 143
 
977 jpm 144
		$profondeur++;
1585 jpm 145
 
1032 aurelien 146
		if($profondeur <= self::$profondeurMax) {
147
			(isset($noeud['A']) && $noeud['A'] != null) ? self::attribuerAuCadran($noeud['A'], $neLat, $lngCentre , $latCentre, $swLng, $profondeur) : '';
148
			(isset($noeud['B']) && $noeud['B'] != null) ? self::attribuerAuCadran($noeud['B'], $neLat, $neLng, $latCentre, $lngCentre, $profondeur) : '';
149
			(isset($noeud['C']) && $noeud['C'] != null) ? self::attribuerAuCadran($noeud['C'], $latCentre, $neLng, $swLat, $lngCentre, $profondeur) : '';
150
			(isset($noeud['D']) && $noeud['D'] != null) ? self::attribuerAuCadran($noeud['D'], $latCentre, $lngCentre, $swLat, $swLng, $profondeur) : '';
977 jpm 151
		}
1585 jpm 152
 
978 jpm 153
		if(self::estUnParentFeuilles($noeud)) {
154
			self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeud);
977 jpm 155
		}
156
	}
1585 jpm 157
 
1142 aurelien 158
	private static function estUnPointAExclure(&$point) {
1585 jpm 159
		return self::estSensible($point) &&
1142 aurelien 160
		self::coordonneesCommuneSontNulles($point);
161
	}
1585 jpm 162
 
1142 aurelien 163
	private static function coordonneesCommuneSontNulles(&$point) {
164
		$coord_nulles = ($point['wgs84_latitude'] == null ||
165
		$point['wgs84_latitude'] == '' ||
166
		$point['wgs84_longitude'] == null ||
167
		$point['wgs84_longitude'] == '');
168
		return $coord_nulles;
169
	}
1585 jpm 170
 
1142 aurelien 171
	private static function coordonneesSontNulles(&$point) {
1409 aurelien 172
		$coord_nulles = ($point['latitude'] == '000null' ||
2460 jpm 173
			$point['latitude'] == 0 ||
174
			$point['latitude'] == '' ||
175
			$point['longitude'] == '000null' ||
176
			$point['longitude'] == 0 ||
177
			$point['longitude'] == '');
1142 aurelien 178
		return $coord_nulles;
179
	}
1585 jpm 180
 
1142 aurelien 181
	private static function estSensible(&$point) {
1483 aurelien 182
		$sensible = isset($point['mots_cles_texte']) && substr_count(strtolower($point['mots_cles_texte']), 'sensible') != 0;
1142 aurelien 183
		return $sensible;
184
	}
1585 jpm 185
 
1341 aurelien 186
	private static function formaterNomStation(&$point, $type_emplacement) {
1142 aurelien 187
		$station = '';
2460 jpm 188
		if ($type_emplacement == 'stations' && $point['station'] != '' && $point['station'] != '000null') {
1142 aurelien 189
			$station = $point['station'];
190
		} else {
1585 jpm 191
			$id_zone_geo = $point['ce_zone_geo'];
1472 aurelien 192
			$station = $point['zone_geo'].(($id_zone_geo != '' && $id_zone_geo != '000null') ? ' ('.self::formaterNomZoneGeo($id_zone_geo).')' : '');
1032 aurelien 193
		}
1585 jpm 194
 
1142 aurelien 195
		return $station;
1032 aurelien 196
	}
1585 jpm 197
 
1339 aurelien 198
	private static function formaterNomZoneGeo($zone_geo) {
199
		$zone_geo = str_replace('INSEE-C:', '', $zone_geo);
200
		$zone_geo = strlen($zone_geo) >= 2 ? substr($zone_geo, 0, 2) : $zone_geo;
201
		return $zone_geo;
202
	}
1585 jpm 203
 
1142 aurelien 204
	private static function formaterPointPourAjout(&$point) {
2460 jpm 205
		if (isset($point['type_emplacement'])) {
1142 aurelien 206
			return $point['type_emplacement'];
207
		}
1585 jpm 208
 
2460 jpm 209
		if (self::coordonneesSontNulles($point) || self::estSensible($point)) {
1144 aurelien 210
			$point_allege = array();
211
			$point_allege['id'] = self::MARQUEUR_COMMUNE.':'.$point['wgs84_latitude'].'|'.$point['wgs84_longitude'];
212
			$point_allege['type_emplacement'] = 'communes';
1341 aurelien 213
			$point_allege['nom'] = self::formaterNomStation($point, 'communes');
1144 aurelien 214
			$point_allege['lat'] = (float)$point['wgs84_latitude'];
215
			$point_allege['lng'] = (float)$point['wgs84_longitude'];
2170 mathias 216
			$point_allege['zonegeo'] = $point['ce_zone_geo'];
1585 jpm 217
 
1144 aurelien 218
			$point = $point_allege;
1142 aurelien 219
		} else {
220
			$point_allege = array();
1339 aurelien 221
			$point_allege['id'] = self::MARQUEUR_STATION.':'.$point['latitude'].'|'.$point['longitude'];
1142 aurelien 222
			$point_allege['type_emplacement'] = 'stations';
1341 aurelien 223
			$point_allege['nom'] = self::formaterNomStation($point, 'stations');
1339 aurelien 224
			$point_allege['lat'] = (float)$point['latitude'];
2179 mathias 225
			$point_allege['lng'] = (float)$point['longitude'];
2170 mathias 226
			$point_allege['zonegeo'] = $point['ce_zone_geo'];
1585 jpm 227
 
1142 aurelien 228
			$point = $point_allege;
229
		}
230
		return $point['type_emplacement'];
231
	}
1585 jpm 232
 
978 jpm 233
	private function obtenirCadranPourPoint($latCentre,$lngCentre, &$point) {
234
		if ($point['lng'] < $lngCentre) {
235
			if ($point['lat'] > $latCentre) {
236
					$cadran = 'A';
237
				} else {
238
					$cadran = 'D';
239
				}
240
		} else {
241
			if ($point['lat'] > $latCentre) {
242
				$cadran = 'B';
243
			} else {
244
				$cadran = 'C';
245
			}
1585 jpm 246
		}
978 jpm 247
		return $cadran;
248
	}
1585 jpm 249
 
1003 jpm 250
	private static function ajouterFils(&$noeud, $cadran, &$point) {
1032 aurelien 251
		if(!isset($noeud[$cadran])) {
252
			$noeud[$cadran] = array('points' => array(),'nbrePoints' => 0, 'latMoyenne' => 0, 'lngMoyenne' => 0);
1585 jpm 253
		}
1032 aurelien 254
		$noeud[$cadran]['points'][] = $point;
255
		$noeud[$cadran]['nbrePoints']++;
256
		$noeud[$cadran]['latMoyenne'] += $point['lat'];
257
		$noeud[$cadran]['lngMoyenne'] += $point['lng'];
978 jpm 258
	}
1585 jpm 259
 
979 jpm 260
	private static function ajouterGroupeOuPoint(&$noeud) {
977 jpm 261
		$groupe = array();
1142 aurelien 262
		if ($noeud['nbrePoints'] > 1 && isset($noeud['latMoyenne']) && isset($noeud['lngMoyenne'])) {
977 jpm 263
			$groupe['lat'] = $noeud['latMoyenne']/$noeud['nbrePoints'];
264
			$groupe['lng'] = $noeud['lngMoyenne']/$noeud['nbrePoints'];
978 jpm 265
			$groupe['id'] = 'GROUPE:'.$groupe['lat'].';'.$groupe['lng'];
977 jpm 266
			$groupe['nbreMarqueur'] = $noeud['nbrePoints'];
267
		} else {
268
			$groupe = $noeud['points'][0];
269
		}
270
		return $groupe;
271
	}
1585 jpm 272
 
979 jpm 273
	private static function estUnParentFeuilles(&$noeud) {
1585 jpm 274
		return  self::estUneFeuille($noeud['A']) &&
275
				self::estUneFeuille($noeud['B']) &&
276
				self::estUneFeuille($noeud['C']) &&
978 jpm 277
				self::estUneFeuille($noeud['D']);
278
	}
1585 jpm 279
 
280
	private static function estUneFeuille(&$noeud) {
281
		return $noeud == null ||
282
		(!isset($noeud['A']) || $noeud['A'] == null) &&
283
		(!isset($noeud['B']) || $noeud['B'] == null) &&
284
		(!isset($noeud['C']) || $noeud['C'] == null) &&
1032 aurelien 285
		(!isset($noeud['D']) || $noeud['D'] == null);
977 jpm 286
	}
2460 jpm 287
}