Subversion Repositories eFlore/Applications.cel

Rev

Rev 1003 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1003 Rev 1004
1
<?php
1
<?php
2
class CartoGroupage {
2
class CartoGroupage {
3
 
3
 
4
	private static $seuilClusterisation = 100;
4
	private static $seuilClusterisation = 100;
5
	private static $zoomDefaut = 3;
5
	private static $zoomDefaut = 3;
6
	private static $zoomMaxClustering = 12;
6
	private static $zoomMaxClustering = 12;
7
	private static $pasZoomDefaut = 1;
7
	private static $pasZoomDefaut = 1;
8
	private static $pasZoomMaxClustering = 0.05;
8
	private static $pasZoomMaxClustering = 0.05;
9
	private static $profondeurMin = 0;
9
	private static $profondeurMin = 0;
10
	private static $profondeurMax = 8;
10
	private static $profondeurMax = 8;
11
 
11
 
12
	private static $pasCorrectionCentre = null;
12
	private static $pasCorrectionCentre = null;
13
	private static $coefficientReductionPas = null;
13
	private static $coefficientReductionPas = null;
14
	private static $coefficientProfondeurMax = null;
14
	private static $coefficientProfondeurMax = null;
15
 
15
 
16
	private static $nbElements = array('stations' => 0,'communes' => 0, 'observations' => 0);
16
	private static $nbElements = array('stations' => 0,'communes' => 0, 'observations' => 0);
17
 
17
 
18
	private static $listeNoeudsSelectionnes = array();
18
	private static $listeNoeudsSelectionnes = array();
19
 
19
 
20
	private static $pointsDejaTraites = array();
20
	private static $pointsDejaTraites = array();
21
 
21
 
22
	/*
22
	/*
23
	  +---------+---------+
23
	  +---------+---------+
24
	  |         |         |
24
	  |         |         |
25
	  |    A    |    B    |
25
	  |    A    |    B    |
26
	  |         |         |
26
	  |         |         |
27
	  +---------*---------+
27
	  +---------*---------+
28
	  |         |         |
28
	  |         |         |
29
	  |    D    |    C    |
29
	  |    D    |    C    |
30
	  |         |         |
30
	  |         |         |
31
	  +---------+---------+
31
	  +---------+---------+
32
 
32
 
33
	  Quatres cadrans sont considérés par le quad tree
33
	  Quatres cadrans sont considérés par le quad tree
34
	  * = centre de la fenetre
34
	  * = centre de la fenetre
35
	 */
35
	 */
36
	public static function creerGroupesQuadtree(&$markers, $neLat, $neLng, $swLat, $swLng, $zoom = 3) {
36
	public static function creerGroupesQuadtree(&$markers, $neLat, $neLng, $swLat, $swLng, $zoom = 3) {
37
 
-
 
38
		if(count($markers) > self::$seuilClusterisation) {
37
		if (count($markers) > self::$seuilClusterisation) {
39
 
-
 
40
			self::calculerProfondeurMax($zoom);
38
			self::calculerProfondeurMax($zoom);
41
			self::calculerPasCorrectionCentre($zoom);
39
			self::calculerPasCorrectionCentre($zoom);
42
 
40
 
43
			$noeudRacine = array('nbrePoints' => count($markers), 'points' => $markers);
41
			$noeudRacine = array('nbrePoints' => count($markers), 'points' => $markers);
44
			self::attribuerAuCadran($noeudRacine, $neLat, $neLng, $swLat, $swLng);
42
			self::attribuerAuCadran($noeudRacine, $neLat, $neLng, $swLat, $swLng);
45
 
-
 
46
		} else {
43
		} else {
-
 
44
 
47
			foreach($markers as $marker) {
45
			foreach($markers as $marker) {
48
				$points = array($marker);
46
				$points = array($marker);
49
				$noeudSimple = array('points' => $points, 'nbrePoints' => 1);
47
				$noeudSimple = array('points' => $points, 'nbrePoints' => 1);
50
				self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeudSimple);
48
				self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeudSimple);
51
			}
49
			}
52
		}
50
		}
53
 
51
 
54
		return self::$listeNoeudsSelectionnes;
52
		return self::$listeNoeudsSelectionnes;
55
	}
53
	}
56
 
54
 
57
	private function calculerCoefficientReductionPas() {
55
	private function calculerCoefficientReductionPas() {
58
		if(self::$coefficientReductionPas == null) {
56
		if(self::$coefficientReductionPas == null) {
59
			self::$coefficientReductionPas = (self::$pasZoomMaxClustering - self::$pasZoomDefaut)/(self::$zoomMaxClustering - self::$zoomDefaut);
57
			self::$coefficientReductionPas = (self::$pasZoomMaxClustering - self::$pasZoomDefaut)/(self::$zoomMaxClustering - self::$zoomDefaut);
60
		}
58
		}
61
 
59
 
62
		return self::$coefficientReductionPas;
60
		return self::$coefficientReductionPas;
63
	}
61
	}
64
 
62
 
65
	private function calculerPasCorrectionCentre($zoom) {
63
	private function calculerPasCorrectionCentre($zoom) {
66
		self::$pasCorrectionCentre = ($zoom - self::$zoomDefaut) * self::calculerCoefficientReductionPas() + self::$pasZoomDefaut;
64
		self::$pasCorrectionCentre = ($zoom - self::$zoomDefaut) * self::calculerCoefficientReductionPas() + self::$pasZoomDefaut;
67
	}
65
	}
68
 
66
 
69
	private function calculerCoefficientProfondeurMax() {
67
	private function calculerCoefficientProfondeurMax() {
70
		if(self::$coefficientProfondeurMax == null) {
68
		if(self::$coefficientProfondeurMax == null) {
71
			self::$coefficientProfondeurMax = (self::$profondeurMax - self::$profondeurMin)/(self::$zoomMaxClustering - self::$zoomDefaut);
69
			self::$coefficientProfondeurMax = (self::$profondeurMax - self::$profondeurMin)/(self::$zoomMaxClustering - self::$zoomDefaut);
72
		}
70
		}
73
 
71
 
74
		return self::$coefficientProfondeurMax;
72
		return self::$coefficientProfondeurMax;
75
	}
73
	}
76
 
74
 
77
	private function calculerProfondeurMax($zoom) {
75
	private function calculerProfondeurMax($zoom) {
78
		if($zoom > self::$zoomDefaut) {
76
		if($zoom > self::$zoomDefaut) {
79
			//self::$profondeurMax = round(($zoom - self::$zoomDefaut) * self::calculerCoefficientProfondeurMax() + self::$profondeurMin,0);
77
			//self::$profondeurMax = round(($zoom - self::$zoomDefaut) * self::calculerCoefficientProfondeurMax() + self::$profondeurMin,0);
80
			self::$profondeurMax = $zoom-1;
78
			self::$profondeurMax = $zoom-1;
81
		} else {
79
		} else {
82
			self::$profondeurMax = 1;
80
			self::$profondeurMax = 1;
83
		}
81
		}
84
	}
82
	}
85
 
83
 
86
	public static function getNbElements() {
84
	public static function getNbElements() {
87
		return self::$nbElements;
85
		return self::$nbElements;
88
	}
86
	}
89
 
87
 
90
	/**
88
	/**
91
	 *
89
	 *
92
	 * @param mixed $noeud Le noeud à traiter par le quadtree
90
	 * @param mixed $noeud Le noeud à traiter par le quadtree
93
	 * @param float $neLat Latitude du coin nord est de la fenetre
91
	 * @param float $neLat Latitude du coin nord est de la fenetre
94
	 * @param float $neLng Longitude du coin nord est de la fenetre
92
	 * @param float $neLng Longitude du coin nord est de la fenetre
95
	 * @param float $swLat Latitude du coin sud ouest de la fenetre
93
	 * @param float $swLat Latitude du coin sud ouest de la fenetre
96
	 * @param float $swLng Longitude du coin sud ouest de la fenetre
94
	 * @param float $swLng Longitude du coin sud ouest de la fenetre
97
	 * @param int $profondeur profondeur courante de l'arbre
95
	 * @param int $profondeur profondeur courante de l'arbre
98
	 */
96
	 */
99
	private static function attribuerAuCadran(&$noeud, $neLat, $neLng, $swLat, $swLng, $profondeur = 0) {
97
	private static function attribuerAuCadran(&$noeud, $neLat, $neLng, $swLat, $swLng, $profondeur = 0) {
100
		$latCentre = round((($neLat+$swLat)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
98
		$latCentre = round((($neLat+$swLat)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
101
		$lngCentre = round((($neLng+$swLng)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
99
		$lngCentre = round((($neLng+$swLng)/2)/self::$pasCorrectionCentre,0)*self::$pasCorrectionCentre;
102
 
100
 
103
		foreach ($noeud['points'] as &$point) {
101
		foreach ($noeud['points'] as &$point) {
104
				self::$nbElements['observations']++;
102
				self::$nbElements['observations']++;
105
				if (isset($point['type_emplacement'])) {
103
				if (isset($point['type_emplacement'])) {
106
					self::$nbElements[$point['type_emplacement']]++;
104
					self::$nbElements[$point['type_emplacement']]++;
107
				}
105
				}
108
				unset($point['type_emplacement']);
106
				unset($point['type_emplacement']);
109
				$cadran = self::obtenirCadranPourPoint($latCentre, $lngCentre, $point);
107
				$cadran = self::obtenirCadranPourPoint($latCentre, $lngCentre, $point);
110
				self::ajouterFils($noeud,$cadran,$point);
108
				self::ajouterFils($noeud,$cadran,$point);
111
		}
109
		}
112
 
110
 
113
		$profondeur++;
111
		$profondeur++;
114
 
112
 
115
		if ($profondeur <= self::$profondeurMax) {
113
		if ($profondeur <= self::$profondeurMax) {
116
			(isset($noeud['A'])) ? self::attribuerAuCadran($noeud['A'], $neLat, $lngCentre , $latCentre, $swLng, $profondeur) : '';
114
			(isset($noeud['A'])) ? self::attribuerAuCadran($noeud['A'], $neLat, $lngCentre , $latCentre, $swLng, $profondeur) : '';
117
			(isset($noeud['B'])) ? self::attribuerAuCadran($noeud['B'], $neLat, $neLng, $latCentre, $lngCentre, $profondeur) : '';
115
			(isset($noeud['B'])) ? self::attribuerAuCadran($noeud['B'], $neLat, $neLng, $latCentre, $lngCentre, $profondeur) : '';
118
			(isset($noeud['C'])) ? self::attribuerAuCadran($noeud['C'], $latCentre, $neLng, $swLat, $lngCentre, $profondeur) : '';
116
			(isset($noeud['C'])) ? self::attribuerAuCadran($noeud['C'], $latCentre, $neLng, $swLat, $lngCentre, $profondeur) : '';
119
			(isset($noeud['D'])) ? self::attribuerAuCadran($noeud['D'], $latCentre, $lngCentre, $swLat, $swLng, $profondeur) : '';
117
			(isset($noeud['D'])) ? self::attribuerAuCadran($noeud['D'], $latCentre, $lngCentre, $swLat, $swLng, $profondeur) : '';
120
		}
118
		}
121
 
119
 
122
		if(self::estUnParentFeuilles($noeud)) {
120
		if(self::estUnParentFeuilles($noeud)) {
123
			self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeud);
121
			self::$listeNoeudsSelectionnes[] = self::ajouterGroupeOuPoint($noeud);
124
		}
122
		}
125
	}
123
	}
126
 
124
 
127
	private function obtenirCadranPourPoint($latCentre,$lngCentre, &$point) {
125
	private function obtenirCadranPourPoint($latCentre,$lngCentre, &$point) {
128
		if ($point['lng'] < $lngCentre) {
126
		if ($point['lng'] < $lngCentre) {
129
			if ($point['lat'] > $latCentre) {
127
			if ($point['lat'] > $latCentre) {
130
					$cadran = 'A';
128
					$cadran = 'A';
131
				} else {
129
				} else {
132
					$cadran = 'D';
130
					$cadran = 'D';
133
				}
131
				}
134
		} else {
132
		} else {
135
			if ($point['lat'] > $latCentre) {
133
			if ($point['lat'] > $latCentre) {
136
				$cadran = 'B';
134
				$cadran = 'B';
137
			} else {
135
			} else {
138
				$cadran = 'C';
136
				$cadran = 'C';
139
			}
137
			}
140
		}
138
		}
141
		return $cadran;
139
		return $cadran;
142
	}
140
	}
143
 
141
 
144
	private static function ajouterFils(&$noeud, $cadran, &$point) {
142
	private static function ajouterFils(&$noeud, $cadran, &$point) {
145
		$fils = array('points' => array(), 'nbrePoints' => 1, 'latMoyenne' => 0, 'lngMoyenne' => 0);
143
		$fils = array('points' => array(), 'nbrePoints' => 0, 'latMoyenne' => 0, 'lngMoyenne' => 0);
-
 
144
		if (isset($noeud[$cadran])) {
-
 
145
			$fils = $noeud[$cadran];
-
 
146
		}
-
 
147
 
146
		$fils['points'][] = $point;
148
		$fils['points'][] = $point;
147
		$fils['nbrePoints'] = isset($noeud[$cadran]['nbrePoints']) ? $noeud[$cadran]['nbrePoints']++ : 1;
149
		$fils['nbrePoints']++;
148
		$fils['latMoyenne'] += $point['lat'];
150
		$fils['latMoyenne'] += $point['lat'];
149
		$fils['lngMoyenne'] += $point['lng'];
151
		$fils['lngMoyenne'] += $point['lng'];
150
		$noeud[$cadran] = $fils;
152
		$noeud[$cadran] = $fils;
151
	}
153
	}
152
 
154
 
153
	private static function ajouterGroupeOuPoint(&$noeud) {
155
	private static function ajouterGroupeOuPoint(&$noeud) {
154
		$groupe = array();
156
		$groupe = array();
155
		if ($noeud['nbrePoints'] > 1) {
157
		if ($noeud['nbrePoints'] > 1) {
156
			$groupe['lat'] = $noeud['latMoyenne']/$noeud['nbrePoints'];
158
			$groupe['lat'] = $noeud['latMoyenne']/$noeud['nbrePoints'];
157
			$groupe['lng'] = $noeud['lngMoyenne']/$noeud['nbrePoints'];
159
			$groupe['lng'] = $noeud['lngMoyenne']/$noeud['nbrePoints'];
158
			$groupe['id'] = 'GROUPE:'.$groupe['lat'].';'.$groupe['lng'];
160
			$groupe['id'] = 'GROUPE:'.$groupe['lat'].';'.$groupe['lng'];
159
			$groupe['nbreMarqueur'] = $noeud['nbrePoints'];
161
			$groupe['nbreMarqueur'] = $noeud['nbrePoints'];
160
		} else {
162
		} else {
161
			$groupe = $noeud['points'][0];
163
			$groupe = $noeud['points'][0];
162
		}
164
		}
163
		return $groupe;
165
		return $groupe;
164
	}
166
	}
165
 
167
 
166
	private static function estUnParentFeuilles(&$noeud) {
168
	private static function estUnParentFeuilles(&$noeud) {
167
		return  self::estUneFeuille($noeud['A']) &&
169
		return  self::estUneFeuille($noeud['A']) &&
168
				self::estUneFeuille($noeud['B']) &&
170
				self::estUneFeuille($noeud['B']) &&
169
				self::estUneFeuille($noeud['C']) &&
171
				self::estUneFeuille($noeud['C']) &&
170
				self::estUneFeuille($noeud['D']);
172
				self::estUneFeuille($noeud['D']);
171
	}
173
	}
172
 
174
 
173
	private static function estUneFeuille(&$noeud) {
175
	private static function estUneFeuille(&$noeud) {
174
		return $noeud == null || (!isset($noeud['A']) && !isset($noeud['B']) && !isset($noeud['C']) && !isset($noeud['D']));
176
		return $noeud == null || (!isset($noeud['A']) && !isset($noeud['B']) && !isset($noeud['C']) && !isset($noeud['D']));
175
	}
177
	}
176
}
178
}
177
?>
179
?>