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 ".
|
|
|
32 |
"id_observation, nom_sel, ( ".
|
|
|
33 |
"6371 * acos ( ".
|
|
|
34 |
"cos ( radians(".$lat_centre.") ) ".
|
|
|
35 |
"* cos( radians( latitude ) ) ".
|
|
|
36 |
"* cos( radians( longitude ) - radians(".$lon_centre.") ) ".
|
|
|
37 |
"+ sin ( radians(".$lat_centre.") ) ".
|
|
|
38 |
"* sin( radians( latitude ) ) ".
|
|
|
39 |
") ".
|
|
|
40 |
") AS distance, latitude, longitude, COUNT(id_observation) as nb_obs ".
|
|
|
41 |
"FROM cel_obs ".
|
|
|
42 |
"WHERE latitude != 0 AND longitude != 0 ".
|
|
|
43 |
"GROUP BY latitude, longitude ".
|
|
|
44 |
"HAVING distance < ".$radius." ".
|
|
|
45 |
"ORDER BY distance ";
|
|
|
46 |
|
|
|
47 |
$points = Cel::db()->requeter($requete);
|
|
|
48 |
return $points;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public function obtenirPointPlusProche($lat_centre, $lon_centre) {
|
|
|
52 |
$requete = "SELECT ".
|
|
|
53 |
"id_observation, nom_sel, ( ".
|
|
|
54 |
"6371 * acos ( ".
|
|
|
55 |
"cos ( radians(".$lat_centre.") ) ".
|
|
|
56 |
"* cos( radians( latitude ) ) ".
|
|
|
57 |
"* cos( radians( longitude ) - radians(".$lon_centre.") ) ".
|
|
|
58 |
"+ sin ( radians(".$lat_centre.") ) ".
|
|
|
59 |
"* sin( radians( latitude ) ) ".
|
|
|
60 |
") ".
|
|
|
61 |
") AS distance, latitude, longitude, COUNT(id_observation) as nb_obs ".
|
|
|
62 |
"FROM cel_obs ".
|
|
|
63 |
"WHERE latitude != 0 AND longitude != 0 ".
|
|
|
64 |
"GROUP BY latitude, longitude ".
|
|
|
65 |
"HAVING distance = MIN(distance) ".
|
|
|
66 |
"ORDER BY distance ";
|
|
|
67 |
|
|
|
68 |
$point = Cel::db()->requeterLigne($requete);
|
|
|
69 |
return $point;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
}
|