Subversion Repositories eFlore/Applications.cel

Rev

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

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