Subversion Repositories eFlore/Applications.moissonnage

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
26 alex 1
<?php
2
 
3
/**
4
 *
5
 * Classe en charge de recuperer les donnees d'observation ou liees a leur localisation
6
 * Le jeu de donnees a interroger est partage en commun avec l'application EFlore
7
 *
8
 * On passera en parametre lors de la creation d'une instance un objet contenant la liste des criteres
9
 * qui vont restreindre le nombre de resultats a renvoyer
10
 *
11
 * Les deux operations suivantes peuvent etre utilisees dans les services :
12
 *   - recupererStations : va rechercher dans la base de donnees tous les points d'observations
13
 *     correspondant aux criteres de recherche demandes. La precision des lieux d'observation est
14
 *     soit un point precis, soit ramenee au niveau de la commune dans laquelle l'observation a ete faite
15
 *     En fonction du niveau de zoom et du nombre de resultats trouves, la presentation se fera
16
 *     soit par les points localisant les stations pour des niveaux de zoom eleves ou pour un nombre
17
 *     de stations inferieur a un seuil, ou par des mailles de 64*64 pixels dans le cas contraire
18
 *
19
 *   - recupererObservations : va rechercher dans la base de donnees les donnees sur des observations
20
 *     a partir des coordonnees longitude et latitude d'une station (+ des parametres additionnels)
21
 *
22
 * Les donnees seront renvoyees au format JSON
23
 *
24
 * @package framework-0.3
25
 * @author Alexandre GALIBERT <alexandre.galibert@tela-botanica.org>
26
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
27
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
28
 * @version $Id$
29
 * @copyright 2013 Tela Botanica (accueil@tela-botanica.org)
30
 *
31
 */
32
 
33
class SophyFormateur {
34
 
35
	private $criteresRecherche;
36
	private $bdd;
37
 
38
 
39
	public function __construct($criteresRecherche) {
40
		$this->criteresRecherche = $criteresRecherche;
41
	}
42
 
43
	public function recupererStations() {
44
		$stations = array();
45
		$nombreStations = $this->obtenirNombreStationsDansBbox();
46
		$seuilMaillage   = Config::get('seuil_maillage');
47
		$nombreParametresNonSpatiaux = $this->calculerNombreCriteresNonSpatiaux();
48
 
49
		if ($nombreStations >= $seuilMaillage && $nombreParametresNonSpatiaux == 0) {
50
			// pas besoin de rechercher les stations correspondant a ces criteres
51
			// recuperer les mailles se trouvant dans l'espace de recherche demande
52
			$stations = $this->recupererMaillesDansBbox();
53
		} else {
54
			$requeteSql = $this->construireRequeteStations();
55
			$stations = $this->getBdd()->recupererTous($requeteSql);
56
			$zoom = $this->criteresRecherche->zoom;
57
			$zoomMaxMaillage = Config::get('zoom_maximal_maillage');
58
			if ($zoom <= $zoomMaxMaillage && count($stations) >= $seuilMaillage) {
59
				// generer un maillage a partir des stations recuperees dans la base de donnees
60
				$maillage = new Maillage($this->criteresRecherche->bbox, $zoom, 'sophy');
61
				$maillage->genererMaillesVides();
62
				$maillage->ajouterPoints($stations);
63
				$stations = $maillage->formaterSortie();
64
			}
65
		}
66
 
67
		// mettre en forme les informations au format JSON
68
		$formateurJSON = new FormateurJson('sophy');
69
		$donneesFormatees = $formateurJSON->formaterStations($stations);
70
		return $donneesFormatees;
71
	}
72
 
73
	public function recupererObservations() {
74
		$requeteSql = $this->construireRequeteObservations();
75
		$observations = $this->getBdd()->recupererTous($requeteSql);
76
		$this->recupererNumeroNomenclaturauxTaxons($observations);
77
		$nomStation = $this->obtenirNomsStationsSurPoint();
78
		$formateurJSON = new FormateurJson('sophy');
79
		$donneesFormatees = $formateurJSON->formaterObservations($observations, $nomStation);
80
		return $donneesFormatees;
81
	}
82
 
83
	private function calculerNombreCriteresNonSpatiaux() {
84
		$nombreParametresNonSpatiaux = 0;
85
		$criteresSpatiaux = array('zoom', 'bbox', 'longitude', 'latitude');
86
		foreach ($this->criteresRecherche as $nomCritere => $valeur) {
87
			if (!in_array($nomCritere, $criteresSpatiaux)) {
88
				$nombreParametresNonSpatiaux ++;
89
			}
90
		}
91
		return $nombreParametresNonSpatiaux;
92
	}
93
 
94
 
95
 
96
	// ------------------------------------------------------------------------ //
97
	// Fonctions de construction des criteres de recherches pour la requete SQL //
98
	// ------------------------------------------------------------------------ //
99
 
100
	private function getBdd() {
101
		if (!isset($this->bdd)) {
102
			$this->bdd = new Bdd();
103
		}
104
		$this->bdd->requeter("USE ".Config::get('bdd_nom'));
105
		return $this->bdd;
106
	}
107
 
108
	private function construireRequeteStations() {
109
		$requete =
110
			"SELECT DISTINCT lieu_station_nom AS nom, lieu_station_latitude AS latitude, ".
111
			"lieu_station_longitude AS longitude, 'STATION' AS type_site ".
112
			"FROM sophy_tapir WHERE 1 ".
113
			$this->construireWhereDepartement().' '.
114
			$this->construireWhereAuteur().' '.
115
			$this->construireWhereReferentiel().' '.
116
			$this->construireWhereTaxon().' '.
117
			$this->construireWhereCoordonneesBbox().' '.
118
			"GROUP BY lieu_station_latitude, lieu_station_longitude";
119
		return $requete;
120
	}
121
 
122
	private function construireRequeteObservations() {
123
		$requete =
124
			"SELECT observation_id AS id_obs, nom_scientifique_complet AS nomSci, ".
125
			"observation_date AS date, lieu_station_nom AS lieu, observateur_nom_complet AS observateur ".
126
			"FROM sophy_tapir WHERE 1 ".
127
			$this->construireWhereAuteur().' '.
128
			$this->construireWhereReferentiel().' '.
129
			$this->construireWhereTaxon().' '.
130
			$this->construireWhereCoordonneesPoint().' '.
131
			"ORDER BY nom_scientifique_complet, date, observateur";
132
		return $requete;
133
	}
134
 
135
	private function construireWhereTaxon() {
136
		$sql = '';
137
		if (isset($this->criteresRecherche->taxon)) {
138
			$taxon = $this->criteresRecherche->taxon;
139
			$criteres = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%");
140
 
141
			$referentiel = new Referentiel($this->criteresRecherche->referentiel, $this->criteresRecherche->taxon);
142
			$sousTaxons = $referentiel->recupererSousTaxons();
143
			foreach ($sousTaxons as $sousTaxon) {
144
				$criteres .= " OR nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%");
145
			}
146
			$sql = "AND ($criteres)";
147
		}
148
		return $sql;
149
	}
150
 
151
	// TODO : completer le corps des methodes construire where pour referentiel et departement
152
	private function construireWhereReferentiel() {
153
		$sql = '';
154
		return $sql;
155
	}
156
 
157
	private function construireWhereDepartement() {
158
		$sql = '';
159
		return $sql;
160
	}
161
 
162
	private function construireWhereAuteur() {
163
		$sql = '';
164
		if (isset($this->criteresRecherche->auteur)) {
165
			$auteur = $this->getBdd()->proteger($this->criteresRecherche->auteur);
166
			$sql = "AND observateur_nom_complet = $auteur";
167
		}
168
		return $sql;
169
	}
170
 
171
	private function construireWhereCoordonneesBbox() {
172
		$bbox = $this->criteresRecherche->bbox;
173
		$sql = "AND lieu_station_longitude BETWEEN ".$bbox['ouest']." AND ".$bbox['est']." ".
174
			"AND lieu_station_latitude BETWEEN ".$bbox['sud']." AND ".$bbox['nord'];
175
		return $sql;
176
	}
177
 
178
	private function construireWhereCoordonneesPoint() {
179
		$sql = "AND lieu_station_latitude=".$this->criteresRecherche->latitude." ".
180
			"AND lieu_station_longitude=".$this->criteresRecherche->longitude;
181
		return $sql;
182
	}
183
 
184
	private function obtenirNomsStationsSurPoint() {
185
		$requete = "SELECT DISTINCTROW lieu_station_nom FROM sophy_tapir WHERE 1 ".
186
			$this->construireWhereTaxon().' '.
187
			$this->construireWhereCoordonneesPoint();
188
		$stations = $this->getBdd()->recupererTous($requete);
189
		$nomsStations = array();
190
		foreach ($stations as $station) {
191
			$nomsStations[] = $station['lieu_station_nom'];
192
		}
193
		return implode(', ', $nomsStations);
194
	}
195
 
196
	private function obtenirNombreStationsDansBbox() {
197
		$bbox = $this->criteresRecherche->bbox;
198
		$zoom = $this->criteresRecherche->zoom;
199
		$requete =
200
			"SELECT zoom, Sum(nombre_sites) AS total_points FROM mailles_sophy ".
201
			"WHERE zoom=".$zoom." AND limite_sud<=".$bbox['nord']." AND limite_nord>=".$bbox['sud']." ".
202
			"AND limite_ouest<=".$bbox['est']." AND limite_est>=".$bbox['ouest']." GROUP BY zoom";
203
		$resultat = $this->getBdd()->recuperer($requete);
204
		return $resultat['total_points'];
205
	}
206
 
207
	private function recupererMaillesDansBbox() {
208
		$bbox = $this->criteresRecherche->bbox;
209
		$zoom = $this->criteresRecherche->zoom;
210
		$requete =
211
			"SELECT zoom, position_latitude, position_longitude, limite_sud AS sud, limite_ouest AS ouest, ".
212
			"limite_nord AS nord, limite_est AS est, nombre_sites AS points, 'MAILLE' AS type_site ".
213
			"FROM mailles_sophy WHERE zoom=".$zoom." AND limite_sud<=".$bbox['nord']." ".
214
			"AND limite_nord>=".$bbox['sud']." AND limite_ouest<=". $bbox['est']." ".
215
			"AND limite_est>=".$bbox['ouest'];
216
		return $this->getBdd()->recupererTous($requete);
217
	}
218
 
219
	private function recupererNumeroNomenclaturauxTaxons(& $observations) {
220
		for ($index = 0; $index < count($observations);  $index ++) {
221
			$taxon = isset($this->criteresRecherche->taxon) ? $this->criteresRecherche->taxon : null;
222
 
223
			if (is_null($taxon) && strlen(trim($observations[$index]['nomSci'])) > 0) {
224
				if (isset($this->criteresRecherche->taxon)) {
225
					$taxon = $this->rechercherTaxonDansReferentiel($observations[$index]['nomSci'],
226
						$this->criteresRecherche->referentiel);
227
				} else {
228
					$taxon = $this->obtenirNumeroTaxon($observations[$index]['nomSci']);
229
				}
230
			}
231
 
232
			if (!is_null($taxon)) {
233
				$observations[$index]['nn'] = $taxon['nn'];
234
				$observations[$index]['num_referentiel'] = $taxon['referentiel'];
235
			} else {
236
				$observations[$index]['nn'] = '';
237
			}
238
		}
239
	}
240
 
241
	private function rechercherTaxonDansReferentiel($nomScientifique, $nomReferentiel) {
242
		$referentiel = new Referentiel($nomReferentiel);
243
		$taxon = $referentiel->obtenirNumeroNomenclatural($nomScientifique);
244
		return $taxon;
245
	}
246
 
247
	private function obtenirNumeroTaxon($nomScientifique) {
248
		$taxonTrouve = null;
249
		$listeReferentiels = Referentiel::recupererListeReferentielsDisponibles();
250
		foreach ($listeReferentiels as $nomReferentiel) {
251
			$taxon = $this->rechercherTaxonDansReferentiel($nomScientifique, $nomReferentiel);
252
			if (!is_null($taxon)) {
253
				$taxonTrouve = $taxon;
254
				break;
255
			}
256
		}
257
		return $taxonTrouve;
258
	}
259
 
260
 
261
}
262
 
263
?>