26 |
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 |
|
|
|
13 |
private $bdd = null;
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
public function __construct($bbox, $zoom) {
|
|
|
18 |
$this->bbox = $bbox;
|
|
|
19 |
$this->zoom = $zoom;
|
|
|
20 |
$this->indexLongitude = array();
|
|
|
21 |
$this->indexLatitude = array();
|
|
|
22 |
$this->mailles = array();
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
public function __destruct() {
|
|
|
26 |
while (count($this->indexLatitude) > 0) {
|
|
|
27 |
array_pop($this->indexLatitude);
|
|
|
28 |
}
|
|
|
29 |
while (count($this->indexLongitude) > 0) {
|
|
|
30 |
array_pop($this->indexLongitude);
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
while (count($this->mailles) > 0) {
|
|
|
34 |
array_pop($this->mailles);
|
|
|
35 |
}
|
|
|
36 |
unset($this);
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
public function genererMaillesVides() {
|
|
|
41 |
$this->recupererIndexMaillesDansBbox();
|
|
|
42 |
foreach ($this->indexLatitude as $indexLat => $intervalleLat) {
|
|
|
43 |
$ligne = array();
|
|
|
44 |
foreach ($this->indexLongitude as $indexLng => $intervalleLng) {
|
|
|
45 |
$ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1],
|
|
|
46 |
$intervalleLng[1], $indexLat, $indexLng);
|
|
|
47 |
}
|
|
|
48 |
$this->mailles[] = $ligne;
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
private function recupererIndexMaillesDansBbox() {
|
|
|
54 |
// recuperer toutes les mailles qui couvrent l'espace a requeter
|
|
|
55 |
$conditionsLongitude = "";
|
|
|
56 |
if ($this->bbox['ouest'] > $this->bbox['est']) {
|
|
|
57 |
$conditionsLongitude = "NOT(debut>=".$this->bbox['ouest']." AND fin<=".$this->bbox['est'].")";
|
|
|
58 |
} else {
|
|
|
59 |
$conditionsLongitude = "fin>=".$this->bbox['ouest']." AND debut <=".$this->bbox['est'];
|
|
|
60 |
}
|
|
|
61 |
$requete =
|
|
|
62 |
"SELECT axe, position, debut, fin FROM mailles_index WHERE zoom=".$this->zoom." ".
|
|
|
63 |
"AND (".
|
|
|
64 |
"(axe='lat' AND fin>=".$this->bbox['sud']." AND debut<=".$this->bbox['nord'].") ".
|
|
|
65 |
"OR (axe='lng' AND {$conditionsLongitude})".
|
|
|
66 |
") ORDER BY axe, position";
|
|
|
67 |
$indexMailles = $this->getBdd()->recupererTous($requete);
|
|
|
68 |
|
|
|
69 |
foreach ($indexMailles as $index) {
|
|
|
70 |
if ($index['axe'] == 'lng') {
|
|
|
71 |
$this->indexLongitude[$index['position']] = array($index['debut'], $index['fin']);
|
|
|
72 |
} else {
|
|
|
73 |
$this->indexLatitude[$index['position']] = array($index['debut'], $index['fin']);
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
private function getBdd() {
|
|
|
79 |
if (is_null($this->bdd)) {
|
|
|
80 |
$this->bdd = new Bdd();
|
|
|
81 |
}
|
|
|
82 |
$nomBdd = Config::get('bdd_eflore');
|
|
|
83 |
if (is_null($nomBdd)) {
|
|
|
84 |
$nomBdd = Config::get('bdd_nom');
|
|
|
85 |
}
|
|
|
86 |
$this->bdd->requeter("USE ".$nomBdd);
|
|
|
87 |
return $this->bdd;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
|
|
|
91 |
public function ajouterPoints(& $points) {
|
|
|
92 |
foreach ($points as $index => $point) {
|
|
|
93 |
list($longitude, $latitude) = $this->obtenirCoordonneesPoint($point);
|
|
|
94 |
list($indexLongitude, $indexLatitude) = $this->rechercherIndexMaille($longitude, $latitude);
|
|
|
95 |
$this->mailles[$indexLatitude][$indexLongitude]->ajouterPoint($point);
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
private function obtenirCoordonneesPoint($point) {
|
|
|
100 |
$longitude = 0;
|
|
|
101 |
$latitude = 0;
|
|
|
102 |
if (!isset($point['type_site']) || $point['type_site'] == 'STATION') {
|
|
|
103 |
$longitude = $point['longitude'];
|
|
|
104 |
$latitude = $point['latitude'];
|
|
|
105 |
} else {
|
|
|
106 |
$longitude = $point['lng_commune'];
|
|
|
107 |
$latitude = $point['lat_commune'];
|
|
|
108 |
}
|
|
|
109 |
return array($longitude, $latitude);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
private function rechercherIndexMaille($longitude, $latitude) {
|
|
|
113 |
$indexLatitude = 0;
|
|
|
114 |
$indexLongitude = 0;
|
|
|
115 |
while ($indexLatitude < count($this->indexLatitude) - 1
|
|
|
116 |
&& $this->mailles[$indexLatitude][0]->getLatitudeNord() < $latitude) {
|
|
|
117 |
$indexLatitude ++;
|
|
|
118 |
}
|
|
|
119 |
while ($indexLongitude < count($this->indexLongitude) - 1
|
|
|
120 |
&& $this->mailles[$indexLatitude][$indexLongitude]->getLongitudeEst() < $longitude) {
|
|
|
121 |
$indexLongitude ++;
|
|
|
122 |
}
|
|
|
123 |
return array($indexLongitude, $indexLatitude);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
public function formaterSortie() {
|
|
|
127 |
$mailles_resume = array();
|
|
|
128 |
foreach ($this->mailles as $ligne) {
|
|
|
129 |
foreach ($ligne as $maille) {
|
|
|
130 |
$nombrePoints = $maille->getNombrePoints();
|
|
|
131 |
if ($nombrePoints == 0)
|
|
|
132 |
continue;
|
|
|
133 |
$mailles_resume[] = array(
|
|
|
134 |
'zoom' => $this->zoom,
|
|
|
135 |
'sud' => $maille->getLatitudeSud(),
|
|
|
136 |
'ouest' => $maille->getLongitudeOuest(),
|
|
|
137 |
'nord' => $maille->getLatitudeNord(),
|
|
|
138 |
'est' => $maille->getLongitudeEst(),
|
|
|
139 |
'points' => $nombrePoints,
|
|
|
140 |
'type_site' => 'MAILLE'
|
|
|
141 |
);
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
if (count($mailles_resume) == 0 || count($mailles_resume[0]) == 0)
|
|
|
145 |
return array();
|
|
|
146 |
return $mailles_resume;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
public function formaterPourInsertionBdd() {
|
|
|
150 |
$mailles_resume = array();
|
|
|
151 |
foreach ($this->mailles as $ligne) {
|
|
|
152 |
foreach ($ligne as $maille) {
|
|
|
153 |
if ($maille->getNombrePoints() > 0) {
|
|
|
154 |
$mailles_resume[] = array(
|
|
|
155 |
'zoom' => $this->zoom,
|
|
|
156 |
'sud' => $maille->getLatitudeSud(),
|
|
|
157 |
'ouest' => $maille->getLongitudeOuest(),
|
|
|
158 |
'nord' => $maille->getLatitudeNord(),
|
|
|
159 |
'est' => $maille->getLongitudeEst(),
|
|
|
160 |
'indexLat' => $maille->getIndexLatitude(),
|
|
|
161 |
'indexLng' => $maille->getIndexLongitude(),
|
|
|
162 |
'points' => $maille->getNombrePoints()
|
|
|
163 |
);
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
if (count($mailles_resume[0]) == 0)
|
|
|
168 |
return array();
|
|
|
169 |
return $mailles_resume;
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
|
|
|
173 |
public function zoomer() {
|
|
|
174 |
$this->zoom += 1;
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
// redecoupage des mailles en 4 (fonction quadtree)
|
|
|
178 |
// TODO : revoir le fonctionnement de cette methode (pour utilisation par les scripts uniquement)
|
|
|
179 |
public function redecouperMailles() {
|
|
|
180 |
$bdd = new Bdd();
|
|
|
181 |
while (count($this->indexLatitude) > 0) {
|
|
|
182 |
array_pop($this->indexLatitude);
|
|
|
183 |
}
|
|
|
184 |
while (count($this->indexLongitude) > 0) {
|
|
|
185 |
array_pop($this->indexLongitude);
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
$mailles = $this->resumePourInsertionBdd();
|
|
|
189 |
while (count($this->mailles) > 0) {
|
|
|
190 |
array_pop($this->mailles);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
foreach ($mailles as $maille) {
|
|
|
194 |
$indexLat = 2 * $maille['indexLat'];
|
|
|
195 |
$indexLng = 2 * $maille['indexLng'];
|
|
|
196 |
// rechercher les nouvelles coordonnees des mailles au niveau de zoom inferieur
|
|
|
197 |
$requete = "SELECT axe,position,debut,fin FROM mailles_index WHERE zoom=". $this->zoom ." AND ((axe='lat'"
|
|
|
198 |
. " AND (position={$indexLat} OR position=" . ($indexLat+1) . ")) OR (axe='lng' AND"
|
|
|
199 |
. " (position={$indexLng} OR position=" . ($indexLng+1) . "))) ORDER BY If(axe='lat',0,1)";
|
|
|
200 |
$resultats = $bdd->recupererTous($requete);
|
|
|
201 |
|
|
|
202 |
$this->indexLatitude[$indexLat] = array($resultats[0]['debut'], $resultats[0]['fin']);
|
|
|
203 |
$this->indexLatitude[$indexLat+1] = array($resultats[1]['debut'], $resultats[1]['fin']);
|
|
|
204 |
$this->indexLongitude[$indexLng] = array($resultats[2]['debut'], $resultats[2]['fin']);
|
|
|
205 |
$this->indexLongitude[$indexLng+1] = array($resultats[3]['debut'], $resultats[3]['fin']);
|
|
|
206 |
}
|
|
|
207 |
ksort($this->indexLatitude);
|
|
|
208 |
ksort($this->indexLongitude);
|
|
|
209 |
|
|
|
210 |
// creer et ajouter les nouvelles mailles a partir des nouveaux index
|
|
|
211 |
foreach ($this->indexLatitude as $indexLat => $intervalleLat) {
|
|
|
212 |
$ligne = array();
|
|
|
213 |
foreach ($this->indexLongitude as $indexLng => $intervalleLng) {
|
|
|
214 |
$ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1],
|
|
|
215 |
$intervalleLng[1], $indexLat, $indexLng);
|
|
|
216 |
}
|
|
|
217 |
$this->mailles[] = $ligne;
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
}
|
|
|
221 |
|
|
|
222 |
public function getNombreMailles() {
|
|
|
223 |
return count($this->mailles);
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
?>
|