2743 |
aurelien |
1 |
<?php
|
|
|
2 |
class CelStreets extends Cel {
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Méthode appelée avec une requête de type GET.
|
|
|
6 |
*/
|
|
|
7 |
public function getRessource() {
|
|
|
8 |
$this->getStreetGeom();
|
|
|
9 |
}
|
|
|
10 |
|
|
|
11 |
private function getStreetGeom() {
|
|
|
12 |
|
|
|
13 |
$erreurs = $this->verifierParametres();
|
|
|
14 |
if(!empty($erreurs)) {
|
|
|
15 |
$this->envoyerMessageErreur(400, implode("\n", $erreurs));
|
|
|
16 |
exit;
|
|
|
17 |
}
|
|
|
18 |
|
|
|
19 |
$latitude_debut = $_GET['latitude_debut'];
|
|
|
20 |
$longitude_debut = $_GET['longitude_debut'];
|
|
|
21 |
$latitude_fin = $_GET['latitude_fin'];
|
|
|
22 |
$longitude_fin = $_GET['longitude_fin'];
|
|
|
23 |
|
|
|
24 |
$dist_max = round(self::haversineGreatCircleDistance($latitude_debut, $longitude_debut, $latitude_fin, $longitude_fin)/2);
|
|
|
25 |
|
|
|
26 |
$url_tpl = $this->getUrlRequeteOverPass().urlencode($this->getRequetePasLoin($latitude_debut, $longitude_debut, $latitude_fin, $longitude_fin, $dist_max));
|
|
|
27 |
$json = file_get_contents($url_tpl);
|
|
|
28 |
|
|
|
29 |
$infos = json_decode($json, true);
|
|
|
30 |
$this->filtrerNoeuds($infos, $latitude_debut, $longitude_debut, $latitude_fin, $longitude_fin);
|
|
|
31 |
|
|
|
32 |
header('Content-type: application/json');
|
|
|
33 |
echo json_encode($infos); exit;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
private function verifierParametres() {
|
|
|
37 |
$parametres_obligatoires = array('latitude_debut', 'longitude_debut', 'latitude_fin', 'longitude_fin');
|
|
|
38 |
$erreurs = array();
|
|
|
39 |
foreach($parametres_obligatoires as $param) {
|
|
|
40 |
if(empty($_GET[$param]) || !is_numeric($_GET[$param])) {
|
|
|
41 |
$erreurs[] = "Le paramètre $param est obligatoire et doit être un flottant";
|
|
|
42 |
}
|
|
|
43 |
}
|
|
|
44 |
return $erreurs;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
function getUrlRequeteOverPass() {
|
|
|
48 |
return $this->config['cel']['url_service_rue'];
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
function getRequetePasLoin($latitude_debut, $longitude_debut, $latitude_fin, $longitude_fin, $dist = 50) {
|
|
|
52 |
$req = 'way["highway"]
|
|
|
53 |
(around:'.$dist.','.$latitude_debut.','.$longitude_debut.')
|
|
|
54 |
(around:'.$dist.','.$latitude_fin.','.$longitude_fin.')
|
|
|
55 |
;
|
|
|
56 |
(
|
|
|
57 |
._;
|
|
|
58 |
>;
|
|
|
59 |
);
|
|
|
60 |
out geom;';
|
|
|
61 |
|
|
|
62 |
return $req;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
function getRequete($nord, $sud, $est, $ouest) {
|
|
|
66 |
return '(
|
|
|
67 |
way["highway"]
|
|
|
68 |
('.$sud.','.$ouest.','.$nord.','.$est.');
|
|
|
69 |
>;
|
|
|
70 |
);out geom;';
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
function filtrerNoeuds(&$infos, $latitude_debut, $longitude_debut, $latitude_fin, $longitude_fin) {
|
|
|
74 |
$distances_debut = array();
|
|
|
75 |
$distances_fin = array();
|
|
|
76 |
|
|
|
77 |
foreach($infos['elements'] as $index_e => &$element) {
|
|
|
78 |
if(empty($element['geometry'])) {
|
|
|
79 |
unset($infos['elements'][$index_e]);
|
|
|
80 |
} else {
|
|
|
81 |
$index_fin_tableau = count($element['geometry']) - 1;
|
|
|
82 |
|
|
|
83 |
$distances_debut[$index_e] = -1;
|
|
|
84 |
$distances_fin[$index_e] = -1;
|
|
|
85 |
|
|
|
86 |
$index_point_plus_proche_debut = 0;
|
|
|
87 |
$dist_point_plus_proche_debut = 999999999;
|
|
|
88 |
$index_point_plus_proche_fin = $index_fin_tableau;
|
|
|
89 |
$dist_point_plus_proche_fin = 9999999999;
|
|
|
90 |
|
|
|
91 |
$index_debut = array(0, 1);
|
|
|
92 |
$index_fin = array($index_fin_tableau -1, $index_fin_tableau);
|
|
|
93 |
|
|
|
94 |
$element['geometryOrig'] = $element['geometry'];
|
|
|
95 |
|
|
|
96 |
foreach($element['geometry'] as $index_g => &$geometry) {
|
|
|
97 |
// Calcul de la plus petite distance de la droite de rue au point
|
|
|
98 |
// pour sélectionner la rue la plus proche des deux points
|
|
|
99 |
if($index_g + 1 < count($element['geometry'])) {
|
|
|
100 |
$geom_next = $element['geometry'][$index_g + 1];
|
|
|
101 |
$this->stockerDistancePlusCourte($distances_debut, $index_debut, $index_e, $index_g, $geometry, $geom_next, $latitude_debut, $longitude_debut);
|
|
|
102 |
$this->stockerDistancePlusCourte($distances_fin, $index_fin, $index_e, $index_g, $geometry, $geom_next, $latitude_fin, $longitude_fin);
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
$dist_debut= self::haversineGreatCircleDistance($latitude_debut, $longitude_debut, $geometry['lat'], $geometry['lon']);
|
|
|
106 |
$dist_fin = self::haversineGreatCircleDistance($latitude_fin, $longitude_fin, $geometry['lat'], $geometry['lon']);
|
|
|
107 |
if($dist_point_plus_proche_debut > $dist_debut) {
|
|
|
108 |
$dist_point_plus_proche_debut = $dist_debut;
|
|
|
109 |
$index_point_plus_proche_debut = $index_g;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
if($dist_point_plus_proche_fin > $dist_fin) {
|
|
|
113 |
$dist_point_plus_proche_fin = $dist_fin;
|
|
|
114 |
$index_point_plus_proche_fin = $index_g;
|
|
|
115 |
}
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
$index_min = min($index_point_plus_proche_debut, $index_point_plus_proche_fin);
|
|
|
119 |
$index_max = max($index_point_plus_proche_debut, $index_point_plus_proche_fin);
|
|
|
120 |
|
|
|
121 |
$infos_debut = array('lat' => (float)$latitude_debut, 'lon' => (float)$longitude_debut);
|
|
|
122 |
$infos_fin = array('lat' => (float)$latitude_fin, 'lon' => (float)$longitude_fin);
|
|
|
123 |
|
|
|
124 |
// Inversion des points de début et de fin si le début donné par l'utilisateur est plus
|
|
|
125 |
// proche de la fin
|
|
|
126 |
if($index_min != $index_point_plus_proche_debut) {
|
|
|
127 |
$tmp = $infos_debut;
|
|
|
128 |
$infos_debut = $infos_fin;
|
|
|
129 |
$infos_fin = $tmp;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
// Projection des points de début et d'arrivée sur le segment de rue
|
|
|
133 |
if($index_min < $index_fin_tableau) {
|
|
|
134 |
$proj_debut = $this->getOrthoPoint($element['geometry'][$index_min], $element['geometry'][$index_min+1], $infos_debut);
|
|
|
135 |
$index_insertion_point_debut = $this->getIndexInsertionDebut($element, $index_min, $infos_debut);
|
|
|
136 |
} else {
|
|
|
137 |
$proj_debut = $infos_debut;
|
|
|
138 |
$index_insertion_point_debut = 0;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
if($index_max > 0) {
|
|
|
142 |
$proj_fin = $this->getOrthoPoint($element['geometry'][$index_max], $element['geometry'][$index_max-1], $infos_fin);
|
|
|
143 |
$index_insertion_point_fin = $this->getIndexInsertionFin($element, $index_max, $infos_fin);
|
|
|
144 |
} else {
|
|
|
145 |
$proj_fin = $infos_fin;
|
|
|
146 |
$index_insertion_point_fin = $index_fin_tableau;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
$index_insertion_point_debut = $index_insertion_point_debut < 0 ? 0 : $index_insertion_point_debut;
|
|
|
150 |
$index_insertion_point_fin = $index_insertion_point_fin > $index_fin_tableau ? $index_fin_tableau : $index_insertion_point_fin;
|
|
|
151 |
|
|
|
152 |
$rd = array();
|
|
|
153 |
for($ii = $index_insertion_point_debut+1; $ii <= $index_insertion_point_fin; $ii++) {
|
|
|
154 |
$rd[] = $element['geometry'][$ii];
|
|
|
155 |
}
|
|
|
156 |
|
2747 |
aurelien |
157 |
$proj_debut = $infos_debut;
|
|
|
158 |
$proj_fin = $infos_fin;
|
|
|
159 |
|
2743 |
aurelien |
160 |
// Insertion des points utilisateurs en début et fin de tableau
|
|
|
161 |
array_unshift($rd, $proj_debut);
|
|
|
162 |
array_push($rd, $proj_fin);
|
|
|
163 |
|
|
|
164 |
$element['geometry'] = $rd;
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
if(!empty($distances_debut)) {
|
|
|
169 |
asort($distances_debut);
|
|
|
170 |
reset($distances_debut);
|
|
|
171 |
$cle_plus_courte_debut = key($distances_debut);
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
if(!empty($distances_fin)) {
|
|
|
175 |
asort($distances_fin);
|
|
|
176 |
reset($distances_fin);
|
|
|
177 |
$cle_plus_courte_fin = key($distances_fin);
|
|
|
178 |
}
|
|
|
179 |
|
2747 |
aurelien |
180 |
if(!empty($distances_fin)) {
|
|
|
181 |
$cle_choisie = $this->choisirIndexRuePlusProchePourDebutEtFin($cle_plus_courte_debut, $cle_plus_courte_fin, $distances_debut, $distances_fin);
|
|
|
182 |
$rue_plus_proche = $distances_fin[$cle_choisie];
|
|
|
183 |
unset($distances_fin[$cle_choisie]);
|
|
|
184 |
$distances_fin = array($cle_choisie => $rue_plus_proche) + $distances_fin;
|
|
|
185 |
}
|
2743 |
aurelien |
186 |
|
|
|
187 |
$elements_fmt = array();
|
|
|
188 |
foreach($distances_fin as $index => $distance) {
|
|
|
189 |
//echo '<pre>'.print_r($infos['elements'][$index], true).'</pre>';exit;
|
|
|
190 |
$rue_proche = $infos['elements'][$index];
|
|
|
191 |
$rue_proche['dist_min'] = $distance;
|
|
|
192 |
$elements_fmt[] = $rue_proche;
|
|
|
193 |
}
|
|
|
194 |
$infos['elements'] = $elements_fmt;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
function choisirIndexRuePlusProchePourDebutEtFin($cle_plus_courte_debut, $cle_plus_courte_fin, $distances_debut, $distances_fin) {
|
|
|
198 |
|
|
|
199 |
$cle_choisie = $cle_plus_courte_fin;
|
|
|
200 |
// Si la rue la plus proche n'est pas la même pour les deux points
|
|
|
201 |
if($cle_plus_courte_debut != $cle_plus_courte_fin) {
|
|
|
202 |
// on calcule celle qui est la mieux positionnée en moyenne
|
|
|
203 |
$index_debut_tab_debut = array_search($cle_plus_courte_debut, array_keys($distances_debut));
|
|
|
204 |
$index_debut_tab_fin = array_search($cle_plus_courte_debut, array_keys($distances_fin));
|
|
|
205 |
|
|
|
206 |
$index_fin_tab_debut = array_search($cle_plus_courte_fin, array_keys($distances_debut));
|
|
|
207 |
$index_fin_tab_fin = array_search($cle_plus_courte_fin, array_keys($distances_fin));
|
|
|
208 |
|
|
|
209 |
$moy_index_debut = ($index_debut_tab_debut + $index_debut_tab_fin)/2;
|
|
|
210 |
$moy_index_fin = ($index_fin_tab_debut + $index_fin_tab_fin)/2;
|
|
|
211 |
|
|
|
212 |
$cle_choisie = ($moy_index_debut < $moy_index_fin) ? $cle_plus_courte_debut : $cle_plus_courte_fin;
|
|
|
213 |
|
|
|
214 |
// Si ça ne suffit pas on prend celle qui a la distance moyenne la plus courte aux deux points
|
|
|
215 |
if ($moy_index_debut == $moy_index_fin) {
|
|
|
216 |
$moyenne_dist_cle_debut = ($distances_debut[$cle_plus_courte_debut] + $distances_fin[$cle_plus_courte_debut])/2;
|
|
|
217 |
$moyenne_dist_cle_fin = ($distances_debut[$cle_plus_courte_fin] + $distances_fin[$cle_plus_courte_fin])/2;
|
|
|
218 |
|
|
|
219 |
$cle_choisie = ($moyenne_dist_cle_debut < $moyenne_dist_cle_fin) ? $cle_plus_courte_debut : $cle_plus_courte_fin;
|
|
|
220 |
}
|
|
|
221 |
}
|
|
|
222 |
|
|
|
223 |
return $cle_choisie;
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
function getIndexInsertionDebut($element, $index_min, $debut) {
|
2747 |
aurelien |
227 |
// le point de début est situé entre $index_min et $index_min + 1
|
2743 |
aurelien |
228 |
$proj_debut = $this->getOrthoPoint($element['geometry'][$index_min], $element['geometry'][$index_min+1], $debut);
|
|
|
229 |
$point_deb = $element['geometry'][$index_min];
|
|
|
230 |
$point_deb_plus_un = $element['geometry'][$index_min+1];
|
|
|
231 |
$distance_point_deb_plus_un_proj = self::haversineGreatCircleDistance($point_deb_plus_un['lat'], $point_deb_plus_un['lon'], $proj_debut['lat'], $proj_debut['lon']);
|
|
|
232 |
$distance_point_deb_deb_plus_un = self::haversineGreatCircleDistance($point_deb['lat'], $point_deb['lon'], $point_deb_plus_un['lat'], $point_deb_plus_un['lon']);
|
|
|
233 |
|
|
|
234 |
if($distance_point_deb_plus_un_proj < $distance_point_deb_deb_plus_un) {
|
|
|
235 |
// le point le plus proche doit être éliminé
|
|
|
236 |
return $index_min + 1;
|
|
|
237 |
} else {
|
|
|
238 |
// il doit être gardé
|
|
|
239 |
return $index_min;
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
function getIndexInsertionFin($element, $index_max, $fin) {
|
|
|
244 |
$proj_fin = $this->getOrthoPoint($element['geometry'][$index_max], $element['geometry'][$index_max-1], $fin);
|
|
|
245 |
$point_fin = $element['geometry'][$index_max];
|
|
|
246 |
$point_fin_moins_un = $element['geometry'][$index_max-1];
|
|
|
247 |
$distance_point_fin_moins_un_proj = self::haversineGreatCircleDistance($point_fin_moins_un['lat'], $point_fin_moins_un['lon'], $proj_fin['lat'], $proj_fin['lon']);
|
|
|
248 |
$distance_point_fin_fin_moins_un = self::haversineGreatCircleDistance($point_fin['lat'], $point_fin['lon'], $point_fin_moins_un['lat'], $point_fin_moins_un['lon']);
|
|
|
249 |
|
|
|
250 |
if($distance_point_fin_moins_un_proj < $distance_point_fin_fin_moins_un) {
|
|
|
251 |
// le point le plus proche doit être éliminé
|
2747 |
aurelien |
252 |
return $index_max - 1;
|
2743 |
aurelien |
253 |
} else {
|
|
|
254 |
// il doit être gardé
|
2747 |
aurelien |
255 |
return $index_max;
|
2743 |
aurelien |
256 |
}
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
function stockerDistancePlusCourte(&$distances, &$infos_index_position_point, $index_e, $index_g, $geometry, $geom_next, $latitude_point, $longitude_point) {
|
|
|
260 |
$a = $geometry['lat'];
|
|
|
261 |
$b = $geometry['lon'];
|
|
|
262 |
$c = $geom_next['lat'];
|
|
|
263 |
$d = $geom_next['lon'];
|
|
|
264 |
$x = $latitude_point ;
|
|
|
265 |
$y = $longitude_point ;
|
|
|
266 |
$dist = $this->getShortestDistance($a, $b, $c, $d, $x, $y);
|
|
|
267 |
if($distances[$index_e] == -1 || $dist < $distances[$index_e]) {
|
|
|
268 |
$distances[$index_e] = $dist;
|
|
|
269 |
$infos_index_position_point = array($index_g, $index_g+1);
|
|
|
270 |
}
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
// projette le point C sur le segment AB
|
|
|
274 |
function getOrthoPoint($a, $b, $c) {
|
|
|
275 |
$x1 = $a['lat'];
|
|
|
276 |
$y1 = $a['lon'];
|
|
|
277 |
$x2 = $b['lat'];
|
|
|
278 |
$y2 = $b['lon'];
|
|
|
279 |
$x3 = $c['lat'];
|
|
|
280 |
$y3 = $c['lon'];
|
|
|
281 |
$px = $x2-$x1;
|
|
|
282 |
$py = $y2-$y1;
|
|
|
283 |
$dAB = $px*$px + $py*$py;
|
|
|
284 |
$u = (($x3 - $x1) * $px + ($y3 - $y1) * $py) / $dAB;
|
|
|
285 |
$x = $x1 + $u * $px;
|
|
|
286 |
$y = $y1 + $u * $py;
|
|
|
287 |
return array('lat' => $x, 'lon' => $y);
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
function getShortestDistance($a, $b, $c, $d, $x, $y) {
|
|
|
291 |
//Coordinates are (a,b) and (c,d)
|
|
|
292 |
//the point (x,y) is the required point.
|
|
|
293 |
//echo 'nagi ';
|
|
|
294 |
//echo $c.' - '.$a." = ".($c-$a).'<br />';
|
|
|
295 |
if(($c-$a) == 0) {
|
|
|
296 |
return 56465465456485;
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
$m=($d-$b)/($c-$a);
|
|
|
300 |
//echo $m."\n";
|
|
|
301 |
|
|
|
302 |
//echo $y-($m*$x)-$b+($m*$a)."\n";
|
|
|
303 |
$distance=abs($y-($m*$x)-$b+($m*$a))/sqrt(1+($m*$m));
|
|
|
304 |
return $distance;
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
function haversineGreatCircleDistance(
|
|
|
308 |
$latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
|
|
|
309 |
{
|
|
|
310 |
// convert from degrees to radians
|
|
|
311 |
$latFrom = deg2rad($latitudeFrom);
|
|
|
312 |
$lonFrom = deg2rad($longitudeFrom);
|
|
|
313 |
$latTo = deg2rad($latitudeTo);
|
|
|
314 |
$lonTo = deg2rad($longitudeTo);
|
|
|
315 |
|
|
|
316 |
$latDelta = $latTo - $latFrom;
|
|
|
317 |
$lonDelta = $lonTo - $lonFrom;
|
|
|
318 |
|
|
|
319 |
$angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
|
|
|
320 |
cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
|
|
|
321 |
return $angle * $earthRadius;
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
public static function vincentyGreatCircleDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371000)
|
|
|
325 |
{
|
|
|
326 |
// convert from degrees to radians
|
|
|
327 |
$latFrom = deg2rad($latitudeFrom);
|
|
|
328 |
$lonFrom = deg2rad($longitudeFrom);
|
|
|
329 |
$latTo = deg2rad($latitudeTo);
|
|
|
330 |
$lonTo = deg2rad($longitudeTo);
|
|
|
331 |
|
|
|
332 |
$lonDelta = $lonTo - $lonFrom;
|
|
|
333 |
$a = pow(cos($latTo) * sin($lonDelta), 2) +
|
|
|
334 |
pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2);
|
|
|
335 |
$b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta);
|
|
|
336 |
|
|
|
337 |
$angle = atan2(sqrt($a), $b);
|
|
|
338 |
return $angle * $earthRadius;
|
|
|
339 |
}
|
|
|
340 |
}
|
|
|
341 |
?>
|