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