Subversion Repositories eFlore/Applications.moissonnage

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 delphine 1
<?php
2
 
3
/**
4
 * Recuperer les stations ou se sont realisees des observations effectuees
5
 * par les utilisateurs du Carnet En Ligne (http://tela-botanica.org/page:cel
6
 *
7
 * @author Alexandre GALIBERT
8
 * @copyright 2013 Tela Botanica (accueil@tela-botanica.org)
9
 *
10
 */
11
 
12
 
13
 
14
class Stations {
15
 
16
	private $zoom = null;
17
	private $bbox = null;
18
 
19
	private $utilisateur = '';
20
	private $departement = '';
21
	private $taxon = null;
22
	private $referentiel = '';
23
	private $sousTaxons = array();
24
 
25
	private $bdd;
26
 
27
 
28
	public function __construct() {}
29
 
30
	public function consulter($ressources, $parametres) {
31
		extract($parametres);
32
		$zoomMaxMaillage = Config::get('zoom_maximal_maillage');
33
		$seuilMaillage   = Config::get('seuil_maillage');
34
		$this->utilisateur = $utilisateur != '*' ? $utilisateur : '';
35
		$this->departement = $dept != '*' ? $dept : '';
36
 
37
		// recuperation, traitement de tous les parametres et vertification du zoom
38
		// et des coordonnees des limites de l'espace de recherche (parametre bbox)
39
		$statutParametresEspace = $this->traiterParametresEspace($parametres);
40
		if ($statutParametresEspace == false) {
41
			$message = "Recherche des stations impossible : parametres de l'espace de recherche incorrects";
42
			return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
43
		}
44
 
45
		// recherche du taxon dans les referentiels bdtfx et bdtxa et verification de sa validite
46
		if ($referentiel != '*') {
47
			$this->referentiel = $this->verifierExistenceReferentiel($referentiel);
48
			if (is_null($this->referentiel)) {
49
				$message = "Recherche des stations impossible : referentiel inconnu\n"
50
					+ "Voici les referentiels disponibles : "
51
					+ implode(', ', Referentiel::listeReferentielsDisponibles());
52
				return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
53
			}
54
		}
55
 
56
		if ($num_taxon != '*') {
57
			$this->traiterParametresTaxon($num_taxon);
58
			if (is_null($this->taxon)) {
59
				$message = "Recherche des stations impossible : taxon non trouve dans les referentiels";
60
				return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE);
61
			}
62
			// recuperer les sous-taxons
63
			$referentiel = new Referentiel($this->referentiel);
64
			$this->sousTaxons = $referentiel->recupererSousTaxons();
65
		}
66
 
67
		// recuperer les informations sur les stations repondant a ces parametres
68
		$stations = $this->recupererStations();
69
		if (count($stations) > $seuilMaillage && intval($this->zoom)<= $zoomMaxMaillage) {
70
			// partitionnement des donnees en mailles
71
			$maillage = new Maillage($this->bbox, $this->zoom);
72
			$maillage->genererMaillesVides();
73
			$maillage->ajouterPoints($stations);
74
			$stations = $maillage->resumePourReponseAJAX();
75
		}
76
 
77
		// mettre en forme les informations au format JSON
78
		$formateurJSON = new FormateurJson();
79
		$donneesFormatees = $formateurJSON->formaterStations($stations);
80
		return $donneesFormatees;
81
	}
82
 
83
 
84
 
85
	//-------------------------------------------------------------------------------
86
	//    Recuperation et verification des parametres
87
	//-------------------------------------------------------------------------------
88
 
89
	private function traiterParametresEspace($parametres) {
90
		$this->zoom = $this->verifierZoom($parametres['zoom']);
91
		$this->bbox = $this->verifierCoordonneesBbox($parametres['bbox']);
92
		return (!is_null($this->zoom) && !is_null($this->bbox));
93
	}
94
 
95
	private function traiterParametresTaxon($numeroTaxon) {
96
		if ($numeroTaxon != '*') {
97
			if ($this->referentiel != '') {
98
				$referentiel = new Referentiel($this->referentiel);
99
				$this->taxon = $referentiel->recupererTaxon($numeroTaxon);
100
			} else {
101
				$this->taxon = $this->verifierExistenceTaxon($numeroTaxon);
102
			}
103
		}
104
	}
105
 
106
	private function verifierZoom($niveauZoom) {
107
		$zoom = intval($niveauZoom);
108
		$limitesZoom = array(
109
			'min' => intval(Config::get('carte.zoom_minimal')),
110
			'max' => intval(Config::get('carte.zoom_maximal'))
111
		);
112
		if ($zoom < $limitesZoom['min'] || $zoom > $limitesZoom['max']) {
113
			return null;
114
		}
115
		return $zoom;
116
	}
117
 
118
	private function verifierCoordonneesBbox($limitesBbox) {
119
		$coordonnees = explode(',', $limitesBbox);
120
		if (count($coordonnees) != 4) {
121
			return null;
122
		}
123
 
124
		// index du tableau des coordonnees : ouest/sud/est/nord
125
		$nomsIndexBbox = array("ouest", "sud", "est", "nord");
126
		$bbox = array();
127
		for ($i = 0; $i < count($coordonnees); $i ++) {
128
			$bbox[$nomsIndexBbox[$i]] = $coordonnees[$i];
129
		}
130
 
131
		// verifier que les coordonnees de chaque bord de la bbox sont valides
132
		$limites = array(
133
			'ouest' => floatval(Config::get('carte.limite_ouest')),
134
			'est'   => floatval(Config::get('carte.limite_est')),
135
			'sud'   => floatval(Config::get('carte.limite_sud')),
136
			'nord'  => floatval(Config::get('carte.limite_nord'))
137
		);
138
 
139
		if (floatval($bbox['ouest']) >= $limites['ouest']  && floatval($bbox['ouest']) <= $limites['est']
140
			&& floatval($bbox['est']) >= $limites['ouest'] && floatval($bbox['est']) <= $limites['est']
141
			&& floatval($bbox['nord']) >= $limites['sud']  && floatval($bbox['nord']) <= $limites['nord']
142
			&& floatval($bbox['sud']) >= $limites['sud']   && floatval($bbox['sud']) <= $limites['nord']) {
143
			return $bbox;
144
		}
145
		return null;
146
	}
147
 
148
	private function verifierExistenceReferentiel($referentiel) {
149
		if ($referentiel == '*') {
150
			return '';
151
		}
152
		$listeReferentiels = Referentiel::listeReferentielsDisponibles();
153
		foreach ($listeReferentiels as $nomReferentiel) {
154
			if (strstr($nomReferentiel, $referentiel) !== false) {
155
				$this->referentiel = $nomReferentiel;
156
				break;
157
			}
158
		}
159
		return null;
160
	}
161
 
162
	private function verifierExistenceTaxon($numTaxon) {
163
		$listeReferentiels = Referentiel::listeReferentielsDisponibles();
164
		// tester chaque referentiel jusqu'a trouver un taxon correspondant
165
		// a tous les criteres demandes dans les masques
166
		foreach ($listeReferentiels as $nomReferentiel) {
167
			$referentiel = new Referentiel($nomReferentiel);
168
			$taxon = $referentiel->recupererTaxon($numTaxon);
169
			if (!is_null($taxon)) {
170
				$this->referentiel = $nomReferentiel;
171
				return $taxon;
172
			}
173
		}
174
		return null;
175
	}
176
 
177
 
178
 
179
	//-------------------------------------------------------------------------------
180
	//    Recuperation des donnees dans la base de donnees
181
	//    et mise sous forme d'objet geographique en fonction du niveau de zoom
182
	//-------------------------------------------------------------------------------
183
 
184
	private function recupererStations() {
185
		$this->getBdd()->requeter("USE tb_cel");
186
		$selectTypeSite = "If((longitude IS NULL AND latitude IS NULL) OR (longitude=0 AND latitude=0)"
187
			. " OR (longitude=999.99999 AND latitude=999.99999), 'COMMUNE', 'STATION')";
188
		$requete = 'SELECT ce_zone_geo, zone_geo, station, longitude, latitude, nom AS "nom_commune",'
189
			. ' wgs84_longitude AS "lng_commune", wgs84_latitude AS "lat_commune", ' . $selectTypeSite
190
			. ' AS "type_site" FROM cel_obs LEFT JOIN cel_zones_geo cz ON ce_zone_geo=id_zone_geo WHERE transmission=1'
191
			. $this->construireWhereDepartement() . $this->construireWhereUtilisateur()
192
			. $this->construireWhereTaxon() . $this->construireWhereCoordonnees()
193
			. " GROUP BY longitude, latitude, wgs84_longitude, wgs84_latitude";
194
		return $this->getBdd()->recupererTous($requete);
195
		/*
196
 
197
		// requete n°1 : recuperer dans la base cel les informations sur les stations
198
		// dans l'espace de recherche dont la precision de la localisation est au niveau de la commune
199
		$requete = 'SELECT id_zone_geo AS "ce_zone_geo", nom AS "zone_geo", \'\' AS "nom",'
200
			. ' wgs84_latitude AS "latitude", wgs84_longitude AS "longitude", \'COMMUNE\' AS "type_site"'
201
			. ' FROM cel_zones_geo WHERE 1 ' . $this->construireWhereDepartement('id')
202
			. $this->construireWhereCoordonnees('wgs84_') . ' AND id_zone_geo IN('
203
			. 'SELECT ce_zone_geo FROM cel_obs WHERE transmission=1' . $this->construireWhereTaxon()  . ')';
204
		$stations = $this->getBdd()->recupererTous($requete);
205
		// requete 2 : recuperer la liste des stations ayant des observations publiques
206
		// dans l'espace de recherche dont la precision de la localisation est au lieu-dit
207
		$requete = 'SELECT ce_zone_geo, zone_geo, station AS "nom", latitude, longitude, \'STATION\''
208
			. ' AS "type_site" FROM cel_obs WHERE transmission=1'
209
			. $this->construireWhereTaxon() . $this->construireWhereCoordonnees()
210
			. $this->construireWhereDepartement('ce') . ' GROUP BY longitude, latitude';
211
		return array_merge($stations, $this->getBdd()->recupererTous($requete));*/
212
	}
213
 
214
	private function getBdd() {
215
		if (!isset($this->bdd)) {
216
			$this->bdd = new Bdd();
217
		}
218
		return $this->bdd;
219
	}
220
 
221
	private function construireWhereTaxon() {
222
		$sql = '';
223
		if (!is_null($this->taxon)) {
224
			$criteres = "nom_ret LIKE '" . addslashes($this->taxon['nom']) . "%'";
225
			foreach ($this->sousTaxons as $sousTaxon) {
226
				$criteres .= " OR nom_ret LIKE '" . addslashes($sousTaxon['nom']) . "%'";
227
			}
228
			$sql = " AND ($criteres)";
229
		}
230
		return $sql;
231
	}
232
 
233
	private function construireWhereDepartement() {
234
		$sql = '';
235
		if (strlen(trim($this->departement)) > 0) {
236
			$valeurs_a_proteger = explode(',',trim($this->departement));
237
			foreach ($valeurs_a_proteger as $valeur) {
238
				$valeurs_protegees[] = "ce_zone_geo LIKE " . $this->getBdd()->proteger('INSEE-C:' . $valeur . '%');
239
			}
240
			$valeurs = implode(' OR ', $valeurs_protegees);
241
			$sql = " AND ($valeurs)";
242
		}
243
		return $sql;
244
	}
245
 
246
	private function construireWhereUtilisateur() {
247
		$sql = '';
248
		if ($this->utilisateur != '') {
249
			$utilisateur = $this->getBdd()->proteger($this->utilisateur);
250
			$sql = " AND courriel_utilisateur = $utilisateur ";
251
		}
252
		return $sql;
253
	}
254
 
255
	private function construireWhereCoordonnees() {
256
		$sql = " AND ((longitude BETWEEN " . $this->bbox['ouest'] . " AND " . $this->bbox['est']
257
			. " AND latitude BETWEEN " . $this->bbox['sud'] . " AND " . $this->bbox['nord']
258
			. " AND wgs84_longitude IS NULL AND wgs84_latitude IS NULL) OR (wgs84_longitude BETWEEN "
259
			. $this->bbox['ouest'] . " AND " . $this->bbox['est'] . " AND wgs84_latitude BETWEEN "
260
			. $this->bbox['sud'] . " AND " . $this->bbox['nord'] . "))";
261
		/*$sql = " AND {$suffixeChamp}latitude BETWEEN " . $this->bbox['sud'] . " AND "
262
			. $this->bbox['nord'] . " AND ";
263
		if ($this->bbox['ouest'] > $this->bbox['est']) {
264
			$sql .= "({$suffixeChamp}longitude >= " . $this->bbox['ouest']
265
				. " OR {$suffixeChamp}longitude <= " . $this->bbox['est'] . ")";
266
		} else {
267
			$sql .= "{$suffixeChamp}longitude BETWEEN " . $this->bbox['ouest'] . " AND "
268
				. $this->bbox['est'];
269
		}*/
270
		return $sql;
271
	}
272
 
273
}
274
 
275
?>