Subversion Repositories eFlore/Applications.cel

Rev

Rev 2461 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2461 Rev 2462
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/**
3
/**
4
 * Service renvoyant les observations présentent au sein d'un cercle de rayon donné.
4
 * Service renvoyant les observations présentent au sein d'un cercle de rayon donné.
5
 *
5
 *
6
 * @internal   Mininum PHP version : 5.2
6
 * @internal   Mininum PHP version : 5.2
7
 * @category   CEL
7
 * @category   CEL
8
 * @package    Services
8
 * @package    Services
9
 * @subpackage Bibliothèques
9
 * @subpackage Cartes
10
 * @version    0.1
10
 * @version    0.1
11
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
11
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
12
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
12
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
13
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
13
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
14
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
14
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
15
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
15
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
16
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
16
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
17
 */
17
 */
18
class CelRadiusPoints extends Cel {
18
class CelRadiusPoints extends Cel {
19
 
19
 
20
	private $champs = array('id_observation', 'nom_sel', 'latitude', 'longitude', 'COUNT(id_observation) AS nb_obs');
20
	private $champs = array('id_observation', 'nom_sel', 'latitude', 'longitude', 'COUNT(id_observation) AS nb_obs');
21
	private $champs_min = array('latitude', 'longitude');
21
	private $champs_min = array('latitude', 'longitude');
22
 
22
 
23
	private $champs_mode = null;
23
	private $champs_mode = null;
24
 
24
 
25
	/**
25
	/**
26
	 * Méthode appelée avec une requête de type GET.
26
	 * Méthode appelée avec une requête de type GET.
27
	 */
27
	 */
28
	public function getRessource() {
28
	public function getRessource() {
29
		$this->champs_mode = $this->champs_min;
29
		$this->champs_mode = $this->champs_min;
30
		return $this->getElement(array());
30
		return $this->getElement(array());
31
	}
31
	}
32
 
32
 
33
	/**
33
	/**
34
	 * Méthode appelée avec une requête de type GET.
34
	 * Méthode appelée avec une requête de type GET.
35
	 */
35
	 */
36
	public function getElement($params) {
36
	public function getElement($params) {
37
		$lat_centre = str_replace(',', '.', round(floatval($_GET['lat']), 3));
37
		$lat_centre = str_replace(',', '.', round(floatval($_GET['lat']), 3));
38
		$lon_centre = str_replace(',', '.', round(floatval($_GET['lon']), 3));
38
		$lon_centre = str_replace(',', '.', round(floatval($_GET['lon']), 3));
39
		$radius = str_replace(',', '.', floatval($_GET['radius']/1000));
39
		$radius = str_replace(',', '.', floatval($_GET['radius']/1000));
40
 
40
 
41
		$retour = array();
41
		$retour = array();
42
 
42
 
43
		$retour['points'] = $this->obtenirPointsPourCentreEtRadius($lat_centre, $lon_centre, $radius);
43
		$retour['points'] = $this->obtenirPointsPourCentreEtRadius($lat_centre, $lon_centre, $radius);
44
		if (empty($retour['points'])) {
44
		if (empty($retour['points'])) {
45
			$retour['plus_proche'] = $this->obtenirPointPlusProche($lat_centre, $lon_centre);
45
			$retour['plus_proche'] = $this->obtenirPointPlusProche($lat_centre, $lon_centre);
46
		}
46
		}
47
		$this->envoyerJson($retour);
47
		$this->envoyerJson($retour);
48
	}
48
	}
49
 
49
 
50
	public function obtenirPointsPourCentreEtRadius($lat_centre, $lon_centre, $radius) {
50
	public function obtenirPointsPourCentreEtRadius($lat_centre, $lon_centre, $radius) {
51
		$requete = 'SELECT '.implode(', ', $this->champs_mode).' '.
51
		$requete = 'SELECT '.implode(', ', $this->champs_mode).' '.
52
			'FROM cel_obs '.
52
			'FROM cel_obs '.
53
			'WHERE latitude != 0 AND longitude != 0 '.
53
			'WHERE latitude != 0 AND longitude != 0 '.
54
			'AND '.$this->renvoyerDistanceSql($lat_centre, $lon_centre)." < $radius ".
54
			'AND '.$this->renvoyerDistanceSql($lat_centre, $lon_centre)." < $radius ".
55
			'GROUP BY latitude, longitude '.
55
			'GROUP BY latitude, longitude '.
56
			' -- '.__FILE__.':'.__LINE__;
56
			' -- '.__FILE__.':'.__LINE__;
57
		$points = Cel::db()->requeter($requete);
57
		$points = Cel::db()->requeter($requete);
58
		return $points;
58
		return $points;
59
	}
59
	}
60
 
60
 
61
	private function renvoyerDistanceSql($lat_centre, $lon_centre) {
61
	private function renvoyerDistanceSql($lat_centre, $lon_centre) {
62
		$sous_requete =
62
		$sous_requete =
63
			"( ".
63
			"( ".
64
				"6371 * acos ( ".
64
				"6371 * acos ( ".
65
					"cos ( radians($lat_centre) ) ".
65
					"cos ( radians($lat_centre) ) ".
66
					"* cos( radians( latitude ) ) ".
66
					"* cos( radians( latitude ) ) ".
67
					"* cos( radians( longitude ) - radians($lon_centre) ) ".
67
					"* cos( radians( longitude ) - radians($lon_centre) ) ".
68
					"+ sin ( radians($lat_centre) ) ".
68
					"+ sin ( radians($lat_centre) ) ".
69
					"* sin( radians( latitude ) ) ".
69
					"* sin( radians( latitude ) ) ".
70
				") ".
70
				") ".
71
			") ";
71
			") ";
72
		return $sous_requete;
72
		return $sous_requete;
73
	}
73
	}
74
 
74
 
75
	public function obtenirPointPlusProche($lat_centre, $lon_centre) {
75
	public function obtenirPointPlusProche($lat_centre, $lon_centre) {
76
		// TODO: faire moins moche et plus efficace
76
		// TODO: faire moins moche et plus efficace
77
		$requete = 'SELECT '.
77
		$requete = 'SELECT '.
78
			implode(", ", $this->champs_mode).", ".$this->renvoyerDistanceSql($lat_centre, $lon_centre)." AS distance ".
78
			implode(", ", $this->champs_mode).", ".$this->renvoyerDistanceSql($lat_centre, $lon_centre)." AS distance ".
79
			'FROM cel_obs '.
79
			'FROM cel_obs '.
80
			'WHERE latitude != 0 AND longitude != 0 '.
80
			'WHERE latitude != 0 AND longitude != 0 '.
81
			'GROUP BY latitude, longitude '.
81
			'GROUP BY latitude, longitude '.
82
			'ORDER BY distance '.
82
			'ORDER BY distance '.
83
			'LIMIT 1 '.
83
			'LIMIT 1 '.
84
			' -- '.__FILE__.':'.__LINE__;
84
			' -- '.__FILE__.':'.__LINE__;
85
		$point = Cel::db()->requeterLigne($requete);
85
		$point = Cel::db()->requeterLigne($requete);
86
		return $point;
86
		return $point;
87
	}
87
	}
88
}
88
}