Subversion Repositories eFlore/Applications.cel

Rev

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

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