29 |
alex |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
class Maillage {
|
|
|
5 |
|
|
|
6 |
private $bbox;
|
|
|
7 |
private $zoom;
|
|
|
8 |
|
|
|
9 |
private $indexLongitude;
|
|
|
10 |
private $indexLatitude;
|
|
|
11 |
private $mailles;
|
|
|
12 |
private $origine;
|
|
|
13 |
|
|
|
14 |
private $bdd = null;
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
public function __construct($bbox, $zoom) {
|
|
|
19 |
$this->bbox = $bbox;
|
|
|
20 |
$this->zoom = $zoom;
|
|
|
21 |
$this->indexLongitude = array();
|
|
|
22 |
$this->indexLatitude = array();
|
|
|
23 |
$this->mailles = array();
|
|
|
24 |
$this->origine = Config::get('origine');
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
public function __destruct() {
|
|
|
28 |
while (count($this->indexLatitude) > 0) {
|
|
|
29 |
array_pop($this->indexLatitude);
|
|
|
30 |
}
|
|
|
31 |
while (count($this->indexLongitude) > 0) {
|
|
|
32 |
array_pop($this->indexLongitude);
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
while (count($this->mailles) > 0) {
|
|
|
36 |
array_pop($this->mailles);
|
|
|
37 |
}
|
|
|
38 |
unset($this);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
|
|
|
42 |
public function genererMaillesVides() {
|
|
|
43 |
$this->recupererIndexMaillesDansBbox();
|
|
|
44 |
foreach ($this->indexLatitude as $indexLat => $intervalleLat) {
|
|
|
45 |
$ligne = array();
|
|
|
46 |
foreach ($this->indexLongitude as $indexLng => $intervalleLng) {
|
|
|
47 |
$ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1],
|
|
|
48 |
$intervalleLng[1], $indexLat, $indexLng);
|
|
|
49 |
}
|
|
|
50 |
$this->mailles[] = $ligne;
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
private function recupererIndexMaillesDansBbox() {
|
|
|
56 |
$tableIndex = Config::get('table_index_mailles');
|
|
|
57 |
$requete =
|
|
|
58 |
"SELECT axe, position, debut, fin FROM {$tableIndex} WHERE zoom={$this->zoom} AND (".
|
|
|
59 |
"(axe='lat' AND debut<{$this->bbox['nord']} AND fin>{$this->bbox['sud']}) ".
|
|
|
60 |
"OR (axe='lng' AND debut<{$this->bbox['est']} AND fin>{$this->bbox['ouest']})".
|
|
|
61 |
") ORDER BY axe, position";
|
|
|
62 |
$indexMailles = $this->getBdd()->recupererTous($requete);
|
|
|
63 |
|
|
|
64 |
foreach ($indexMailles as $index) {
|
|
|
65 |
if ($index['axe'] == 'lng') {
|
|
|
66 |
$this->indexLongitude[$index['position']] = array($index['debut'], $index['fin']);
|
|
|
67 |
} else {
|
|
|
68 |
$this->indexLatitude[$index['position']] = array($index['debut'], $index['fin']);
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
private function getBdd() {
|
|
|
74 |
if (is_null($this->bdd)) {
|
|
|
75 |
$this->bdd = new Bdd();
|
|
|
76 |
}
|
|
|
77 |
return $this->bdd;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
public function ajouterPoints($points) {
|
|
|
82 |
foreach ($points as $point) {
|
|
|
83 |
$longitude = $point['longitude'];
|
|
|
84 |
$latitude = $point['latitude'];
|
|
|
85 |
list($indexLongitude, $indexLatitude) = $this->rechercherIndexMaille($longitude, $latitude);
|
|
|
86 |
$this->mailles[$indexLatitude][$indexLongitude]->ajouterPoint($point);
|
|
|
87 |
}
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
private function rechercherIndexMaille($longitude, $latitude) {
|
|
|
91 |
$indexLatitude = 0;
|
|
|
92 |
$indexLongitude = 0;
|
|
|
93 |
while ($indexLatitude < count($this->indexLatitude) - 1
|
|
|
94 |
&& $this->mailles[$indexLatitude][0]->getLatitudeNord() < $latitude) {
|
|
|
95 |
$indexLatitude ++;
|
|
|
96 |
}
|
|
|
97 |
while ($indexLongitude < count($this->indexLongitude) - 1
|
|
|
98 |
&& $this->mailles[$indexLatitude][$indexLongitude]->getLongitudeEst() < $longitude) {
|
|
|
99 |
$indexLongitude ++;
|
|
|
100 |
}
|
|
|
101 |
return array($indexLongitude, $indexLatitude);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
public function formaterPourInsertionBdd() {
|
|
|
105 |
$mailles_resume = array();
|
|
|
106 |
foreach ($this->mailles as $ligne) {
|
|
|
107 |
foreach ($ligne as $maille) {
|
|
|
108 |
if ($maille->getNombrePoints() > 0) {
|
|
|
109 |
$mailles_resume[] = array(
|
|
|
110 |
'zoom' => $this->zoom,
|
|
|
111 |
'sud' => $maille->getLatitudeSud(),
|
|
|
112 |
'ouest' => $maille->getLongitudeOuest(),
|
|
|
113 |
'nord' => $maille->getLatitudeNord(),
|
|
|
114 |
'est' => $maille->getLongitudeEst(),
|
|
|
115 |
'indexLat' => $maille->getIndexLatitude(),
|
|
|
116 |
'indexLng' => $maille->getIndexLongitude(),
|
|
|
117 |
'points' => $maille->getNombrePoints(),
|
|
|
118 |
'observations' => $maille->getNombreObservations()
|
|
|
119 |
);
|
|
|
120 |
}
|
|
|
121 |
}
|
|
|
122 |
}
|
|
|
123 |
return $mailles_resume;
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
private function formaterPourQuadtree() {
|
|
|
127 |
$mailles_resume = array();
|
|
|
128 |
foreach ($this->mailles as $ligne) {
|
|
|
129 |
foreach ($ligne as $maille) {
|
|
|
130 |
if ($maille->getNombrePoints() > 0) {
|
|
|
131 |
$mailles_resume[] = array(
|
|
|
132 |
'zoom' => $this->zoom,
|
|
|
133 |
'sud' => $maille->getLatitudeSud(),
|
|
|
134 |
'ouest' => $maille->getLongitudeOuest(),
|
|
|
135 |
'nord' => $maille->getLatitudeNord(),
|
|
|
136 |
'est' => $maille->getLongitudeEst(),
|
|
|
137 |
'indexLat' => $maille->getIndexLatitude(),
|
|
|
138 |
'indexLng' => $maille->getIndexLongitude(),
|
|
|
139 |
'points' => $maille->getPoints()
|
|
|
140 |
);
|
|
|
141 |
}
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
return $mailles_resume;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
public function redecouperMailles() {
|
|
|
148 |
$mailles = $this->formaterPourQuadtree();
|
|
|
149 |
$this->viderMailles();
|
|
|
150 |
while (count($mailles)>0) {
|
|
|
151 |
$nouvellesMailles = $this->genererMaillesParQuadtree($mailles[count($mailles)-1]);
|
|
|
152 |
foreach ($nouvellesMailles as $maille) {
|
|
|
153 |
$this->ajouterMaille($maille);
|
|
|
154 |
}
|
|
|
155 |
array_pop($mailles);
|
|
|
156 |
}
|
|
|
157 |
$this->zoom += 1;
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
private function viderMailles() {
|
|
|
161 |
while (count($this->indexLatitude) > 0) {
|
|
|
162 |
array_pop($this->indexLatitude);
|
|
|
163 |
}
|
|
|
164 |
while (count($this->indexLongitude) > 0) {
|
|
|
165 |
array_pop($this->indexLongitude);
|
|
|
166 |
}
|
|
|
167 |
while (count($this->mailles) > 0) {
|
|
|
168 |
array_pop($this->mailles);
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
private function genererMaillesParQuadtree(& $maille) {
|
|
|
173 |
$ouest = $maille['ouest'];
|
|
|
174 |
$est = $maille['est'];
|
|
|
175 |
$sud = $maille['sud'];
|
|
|
176 |
$nord = $maille['nord'];
|
|
|
177 |
$indexLat = $maille['indexLat'];
|
|
|
178 |
$indexLng = $maille['indexLng'];
|
|
|
179 |
$milieuLongitude = $this->convertirFloatToString(($ouest + $est)/2);
|
|
|
180 |
$milieuLatitude = $this->calculerMilieuLatitudes($sud, $nord);
|
|
|
181 |
// sens des mailles : NordOuest, NordEst, SudOuest, SudEst
|
|
|
182 |
$mailles = array();
|
|
|
183 |
$mailles[] = new Maille($milieuLatitude, $ouest, $nord, $milieuLongitude, $indexLat*2, $indexLng*2);
|
|
|
184 |
$mailles[] = new Maille($milieuLatitude, $milieuLongitude, $nord, $est, $indexLat*2, $indexLng*2+1);
|
|
|
185 |
$mailles[] = new Maille($sud, $ouest, $milieuLatitude, $milieuLongitude, $indexLat*2+1, $indexLng*2);
|
|
|
186 |
$mailles[] = new Maille($sud, $milieuLongitude, $milieuLatitude, $est, $indexLat*2+1, $indexLng*2+1);
|
|
|
187 |
$this->deplacerPoints($maille, $mailles);
|
|
|
188 |
return $mailles;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
private function calculerMilieuLatitudes($latitudeSud, $latitudeNord) {
|
|
|
192 |
$ySud = log(tan((90 + $latitudeSud) * M_PI / 360.0 )) / (M_PI / 180.0) * ($this->origine/180.0);
|
|
|
193 |
$yNord = log(tan((90 + $latitudeNord) * M_PI / 360.0 )) / (M_PI / 180.0) * ($this->origine/180.0);
|
|
|
194 |
$yMilieu = ($ySud+$yNord) / 2;
|
|
|
195 |
$latitude = ($yMilieu / $this->origine) * 180.0;
|
|
|
196 |
$latitude = 180 / M_PI * (2 * atan(exp($latitude * M_PI / 180.0)) - M_PI / 2.0);
|
|
|
197 |
return $this->convertirFloatToString($latitude);
|
|
|
198 |
}
|
|
|
199 |
|
|
|
200 |
private function deplacerPoints(& $maille, & $sousMailles) {
|
|
|
201 |
$nombrePoints = count($maille['points']);
|
|
|
202 |
for ($index = 0; $index < $nombrePoints; $index ++) {
|
|
|
203 |
$position = ($maille['points'][$index]['latitude']>=$sousMailles[0]->getLatitudeSud()) ? 0 : 2;
|
|
|
204 |
$position += ($maille['points'][$index]['longitude']<$sousMailles[$position]->getLongitudeEst()) ? 0 : 1;
|
|
|
205 |
$sousMailles[$position]->ajouterPoint($maille['points'][$index]);
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
private function ajouterMaille($maille) {
|
|
|
210 |
$indexLat = $maille->getIndexLatitude();
|
|
|
211 |
$indexLng = $maille->getIndexLongitude();
|
|
|
212 |
if (!isset($this->indexLongitude[$indexLng])) {
|
|
|
213 |
$this->indexLongitude[$indexLng] = array($maille->getLongitudeOuest(), $maille->getLongitudeEst());
|
|
|
214 |
$this->mailles[$indexLng] = array();
|
|
|
215 |
}
|
|
|
216 |
if (!isset($this->indexLongitude[$indexLat])) {
|
|
|
217 |
$this->indexLatitude[$indexLat] = array($maille->getLatitudeSud(), $maille->getLatitudeNord());
|
|
|
218 |
}
|
|
|
219 |
$this->mailles[$indexLng][$indexLat] = $maille;
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
private function convertirFloatToString($float) {
|
|
|
223 |
return str_replace(',', '.', strval($float));
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
?>
|