2452 |
aurelien |
1 |
<?php
|
|
|
2 |
class CelRadiusPoints extends Cel {
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Méthode appelée avec une requête de type GET.
|
|
|
6 |
*/
|
|
|
7 |
public function getRessource() {
|
|
|
8 |
return $this->getElement(array());
|
|
|
9 |
}
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* Méthode appelée avec une requête de type GET.
|
|
|
13 |
*/
|
|
|
14 |
public function getElement($params) {
|
|
|
15 |
|
2453 |
aurelien |
16 |
$lat_centre = str_replace(',', '.', round(floatval($_GET['lat']), 3));
|
|
|
17 |
$lon_centre = str_replace(',', '.', round(floatval($_GET['lon']), 3));
|
2452 |
aurelien |
18 |
$radius = str_replace(',', '.', floatval($_GET['radius']/1000));
|
|
|
19 |
|
|
|
20 |
$retour = array();
|
|
|
21 |
|
|
|
22 |
$retour['points'] = $this->obtenirPointsPourCentreEtRadius($lat_centre, $lon_centre, $radius);
|
|
|
23 |
if(empty($retour['points'])) {
|
|
|
24 |
$retour['plus_proche'] = $this->obtenirPointPlusProche($lat_centre, $lon_centre);
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
$this->envoyerJson($retour);
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
public function obtenirPointsPourCentreEtRadius($lat_centre, $lon_centre, $radius) {
|
|
|
31 |
$requete = "SELECT ".
|
2454 |
aurelien |
32 |
"id_observation, nom_sel, latitude, longitude, COUNT(id_observation) as nb_obs ".
|
2452 |
aurelien |
33 |
"FROM cel_obs ".
|
|
|
34 |
"WHERE latitude != 0 AND longitude != 0 ".
|
2454 |
aurelien |
35 |
"AND ".$this->renvoyerDistanceSql($lat_centre, $lon_centre)." < ".$radius." ".
|
|
|
36 |
"GROUP BY latitude, longitude ";
|
2452 |
aurelien |
37 |
|
|
|
38 |
$points = Cel::db()->requeter($requete);
|
|
|
39 |
return $points;
|
|
|
40 |
}
|
|
|
41 |
|
2454 |
aurelien |
42 |
private function renvoyerDistanceSql($lat_centre, $lon_centre) {
|
|
|
43 |
$sous_requete =
|
|
|
44 |
"( ".
|
|
|
45 |
"6371 * acos ( ".
|
|
|
46 |
"cos ( radians(".$lat_centre.") ) ".
|
|
|
47 |
"* cos( radians( latitude ) ) ".
|
|
|
48 |
"* cos( radians( longitude ) - radians(".$lon_centre.") ) ".
|
|
|
49 |
"+ sin ( radians(".$lat_centre.") ) ".
|
|
|
50 |
"* sin( radians( latitude ) ) ".
|
|
|
51 |
") ".
|
|
|
52 |
") ";
|
|
|
53 |
return $sous_requete;
|
|
|
54 |
}
|
|
|
55 |
|
2452 |
aurelien |
56 |
public function obtenirPointPlusProche($lat_centre, $lon_centre) {
|
|
|
57 |
$requete = "SELECT ".
|
2454 |
aurelien |
58 |
"id_observation, nom_sel, ".$this->renvoyerDistanceSql($lat_centre, $lon_centre)." AS distance, ".
|
|
|
59 |
"latitude, longitude, COUNT(id_observation) as nb_obs ".
|
2452 |
aurelien |
60 |
"FROM cel_obs ".
|
|
|
61 |
"WHERE latitude != 0 AND longitude != 0 ".
|
|
|
62 |
"GROUP BY latitude, longitude ".
|
|
|
63 |
"HAVING distance = MIN(distance) ".
|
|
|
64 |
"ORDER BY distance ";
|
|
|
65 |
|
|
|
66 |
$point = Cel::db()->requeterLigne($requete);
|
|
|
67 |
return $point;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
|
|
|
71 |
}
|