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