Subversion Repositories eFlore/Applications.cel

Rev

Rev 2462 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2462 Rev 2535
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 Cartes
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_max = array('id_observation', 'nom_sel', 'latitude', 'longitude', 'COUNT(id_observation) AS nb_obs', 'GROUP_CONCAT(id_observation) as ids_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
		
-
 
41
 
-
 
42
		if (isset($_GET['format'])) {
-
 
43
			if ($_GET['format'] == 'max') {
-
 
44
				$this->champs_mode = $this->champs_max;
-
 
45
			} else if($_GET['format'] == 'min') {
-
 
46
				$this->champs_mode = $this->champs_min;
-
 
47
			}
-
 
48
		}
40
 
49
 
41
		$retour = array();
50
		$retour = array();
42
 
51
 
43
		$retour['points'] = $this->obtenirPointsPourCentreEtRadius($lat_centre, $lon_centre, $radius);
52
		$retour['points'] = $this->obtenirPointsPourCentreEtRadius($lat_centre, $lon_centre, $radius);
44
		if (empty($retour['points'])) {
53
		if (empty($retour['points'])) {
45
			$retour['plus_proche'] = $this->obtenirPointPlusProche($lat_centre, $lon_centre);
54
			$retour['plus_proche'] = $this->obtenirPointPlusProche($lat_centre, $lon_centre);
46
		}
55
		}
47
		$this->envoyerJson($retour);
56
		$this->envoyerJson($retour);
48
	}
57
	}
49
 
58
 
50
	public function obtenirPointsPourCentreEtRadius($lat_centre, $lon_centre, $radius) {
59
	public function obtenirPointsPourCentreEtRadius($lat_centre, $lon_centre, $radius) {
51
		$requete = 'SELECT '.implode(', ', $this->champs_mode).' '.
60
		$requete = 'SELECT '.implode(', ', $this->champs_mode).' '.
52
			'FROM cel_obs '.
61
			'FROM cel_obs '.
53
			'WHERE latitude != 0 AND longitude != 0 '.
62
			'WHERE latitude != 0 AND longitude != 0 '.
54
			'AND '.$this->renvoyerDistanceSql($lat_centre, $lon_centre)." < $radius ".
63
			'AND '.$this->renvoyerDistanceSql($lat_centre, $lon_centre)." < $radius ".
55
			'GROUP BY latitude, longitude '.
64
			'GROUP BY latitude, longitude '.
56
			' -- '.__FILE__.':'.__LINE__;
65
			' -- '.__FILE__.':'.__LINE__;
57
		$points = Cel::db()->requeter($requete);
66
		$points = Cel::db()->requeter($requete);
58
		return $points;
67
		return $points;
59
	}
68
	}
60
 
69
 
61
	private function renvoyerDistanceSql($lat_centre, $lon_centre) {
70
	private function renvoyerDistanceSql($lat_centre, $lon_centre) {
62
		$sous_requete =
71
		$sous_requete =
63
			"( ".
72
			"( ".
64
				"6371 * acos ( ".
73
				"6371 * acos ( ".
65
					"cos ( radians($lat_centre) ) ".
74
					"cos ( radians($lat_centre) ) ".
66
					"* cos( radians( latitude ) ) ".
75
					"* cos( radians( latitude ) ) ".
67
					"* cos( radians( longitude ) - radians($lon_centre) ) ".
76
					"* cos( radians( longitude ) - radians($lon_centre) ) ".
68
					"+ sin ( radians($lat_centre) ) ".
77
					"+ sin ( radians($lat_centre) ) ".
69
					"* sin( radians( latitude ) ) ".
78
					"* sin( radians( latitude ) ) ".
70
				") ".
79
				") ".
71
			") ";
80
			") ";
72
		return $sous_requete;
81
		return $sous_requete;
73
	}
82
	}
74
 
83
 
75
	public function obtenirPointPlusProche($lat_centre, $lon_centre) {
84
	public function obtenirPointPlusProche($lat_centre, $lon_centre) {
76
		// TODO: faire moins moche et plus efficace
85
		// TODO: faire moins moche et plus efficace
77
		$requete = 'SELECT '.
86
		$requete = 'SELECT '.
78
			implode(", ", $this->champs_mode).", ".$this->renvoyerDistanceSql($lat_centre, $lon_centre)." AS distance ".
87
			implode(", ", $this->champs_mode).", ".$this->renvoyerDistanceSql($lat_centre, $lon_centre)." AS distance ".
79
			'FROM cel_obs '.
88
			'FROM cel_obs '.
80
			'WHERE latitude != 0 AND longitude != 0 '.
89
			'WHERE latitude != 0 AND longitude != 0 '.
81
			'GROUP BY latitude, longitude '.
90
			'GROUP BY latitude, longitude '.
82
			'ORDER BY distance '.
91
			'ORDER BY distance '.
83
			'LIMIT 1 '.
92
			'LIMIT 1 '.
84
			' -- '.__FILE__.':'.__LINE__;
93
			' -- '.__FILE__.':'.__LINE__;
85
		$point = Cel::db()->requeterLigne($requete);
94
		$point = Cel::db()->requeterLigne($requete);
86
		return $point;
95
		return $point;
87
	}
96
	}
88
}
97
}