Subversion Repositories eFlore/Applications.moissonnage

Rev

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

Rev 26 Rev 31
1
<?php
1
<?php
2
 
2
 
3
/**
3
/**
4
 *
4
 *
5
 * Classe en charge de recuperer les donnees d'observation ou liees a leur localisation
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
6
 * Le jeu de donnees a interroger est partage en commun avec l'application EFlore
7
 *
7
 *
8
 * On passera en parametre lors de la creation d'une instance un objet contenant la liste des criteres
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
9
 * qui vont restreindre le nombre de resultats a renvoyer
10
 *
10
 *
11
 * Les deux operations suivantes peuvent etre utilisees dans les services :
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
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
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
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
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
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
17
 *     de stations inferieur a un seuil, ou par des mailles de 64*64 pixels dans le cas contraire
18
 *
18
 *
19
 *   - recupererObservations : va rechercher dans la base de donnees les donnees sur des observations
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)
20
 *     a partir des coordonnees longitude et latitude d'une station (+ des parametres additionnels)
21
 *
21
 *
22
 * Les donnees seront renvoyees au format JSON
22
 * Les donnees seront renvoyees au format JSON
23
 *
23
 *
24
 * @package framework-0.3
24
 * @package framework-0.3
25
 * @author Alexandre GALIBERT <alexandre.galibert@tela-botanica.org>
25
 * @author Alexandre GALIBERT <alexandre.galibert@tela-botanica.org>
26
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
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>
27
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
28
 * @version $Id$
28
 * @version $Id$
29
 * @copyright 2013 Tela Botanica (accueil@tela-botanica.org)
29
 * @copyright 2013 Tela Botanica (accueil@tela-botanica.org)
30
 *
30
 *
31
 */
31
 */
32
 
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;
33
class SophyFormateur extends Formateur {
106
	}
34
	
107
	
35
 
108
	private function construireRequeteStations() {
36
	protected function construireRequeteStations() {
109
		$requete =
37
		$requete =
110
			"SELECT DISTINCT lieu_station_nom AS nom, lieu_station_latitude AS latitude, ".
38
			"SELECT COUNT(guid) AS observations, lieu_station_nom AS nom, lieu_station_latitude AS latitude, ".
111
			"lieu_station_longitude AS longitude, 'STATION' AS type_site ".
39
			"lieu_station_longitude AS longitude, 'STATION' AS type_site, lieu_commune_code_insee AS code_insee ".
112
			"FROM sophy_tapir WHERE 1 ".
40
			"FROM sophy_tapir WHERE 1 ".
113
			$this->construireWhereDepartement().' '.
41
			$this->construireWhereDepartement().' '.
114
			$this->construireWhereAuteur().' '.
42
			$this->construireWhereAuteur().' '.
-
 
43
			$this->construireWhereDate().' '.
115
			$this->construireWhereReferentiel().' '.
44
			$this->construireWhereReferentiel().' '.
116
			$this->construireWhereTaxon().' '.
45
			$this->construireWhereTaxon().' '.
117
			$this->construireWhereCoordonneesBbox().' '.
46
			$this->construireWhereCoordonneesBbox().' '.
118
			"GROUP BY lieu_station_latitude, lieu_station_longitude";
47
			"GROUP BY lieu_station_longitude, lieu_station_latitude";
119
		return $requete;
48
		return $requete;
120
	}
49
	}
121
	
50
	
122
	private function construireRequeteObservations() {
51
	protected function construireRequeteObservations() {
123
		$requete =
52
		$requete =
124
			"SELECT observation_id AS id_obs, nom_scientifique_complet AS nomSci, ".
53
			"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 ".
54
			"observation_date AS date, lieu_station_nom AS lieu, observateur_nom_complet AS observateur ".
126
			"FROM sophy_tapir WHERE 1 ".
55
			"FROM sophy_tapir WHERE 1 ".
127
			$this->construireWhereAuteur().' '.
56
			$this->construireWhereAuteur().' '.
128
			$this->construireWhereReferentiel().' '.
57
			$this->construireWhereReferentiel().' '.
-
 
58
			$this->construireWhereDate().' '.
129
			$this->construireWhereTaxon().' '.
59
			$this->construireWhereTaxon().' '.
130
			$this->construireWhereCoordonneesPoint().' '.
60
			$this->construireWhereCoordonneesPoint().' '.
131
			"ORDER BY nom_scientifique_complet, date, observateur";
61
			"ORDER BY nom_scientifique_complet, date, observateur";
132
		return $requete;
62
		return $requete;
133
	}
63
	}
134
	
64
	
135
	private function construireWhereTaxon() {
65
	protected function construireWhereTaxon() {
136
		$sql = '';
66
		$sql = '';
137
		if (isset($this->criteresRecherche->taxon)) {
67
		if (isset($this->criteresRecherche->taxon)) {
-
 
68
			$taxons = $this->criteresRecherche->taxon;
-
 
69
			$criteres = array();
-
 
70
			foreach ($taxons as $taxon) {
138
			$taxon = $this->criteresRecherche->taxon;
71
				$nomRang = $this->getNomRang($taxon);
139
			$criteres = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%");
-
 
140
				
72
				$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%");
141
			$referentiel = new Referentiel($this->criteresRecherche->referentiel, $this->criteresRecherche->taxon);
73
				if ($nomRang == 'espece') {
142
			$sousTaxons = $referentiel->recupererSousTaxons();
74
					$criteres = array_merge($criteres, $this->concatenerTaxonsSousEspeces($taxon));
143
			foreach ($sousTaxons as $sousTaxon) {
75
				} elseif ($nomRang == 'genre') {
-
 
76
					$criteres = array_merge($criteres, $this->concatenerTaxonsFamilles($taxon));
144
				$criteres .= " OR nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%");
77
				}
145
			}
78
			}
146
			$sql = "AND ($criteres)";
79
			$sql = "AND (".implode(' OR ',array_unique($criteres)).")";
147
		}
80
		}
148
		return $sql;
81
		return $sql;
149
	}
82
	}
-
 
83
	
-
 
84
	protected function concatenerTaxonsSousEspeces($taxon) {
-
 
85
		$referentiel = new Referentiel($this->criteresRecherche->referentiel, $taxon);
-
 
86
		$sousTaxons = $referentiel->recupererTaxonsSousEspeces();
-
 
87
		$criteres = array();
-
 
88
		foreach ($sousTaxons as $sousTaxon) {
-
 
89
			$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%");
-
 
90
		}
-
 
91
		return $criteres;
-
 
92
	}
-
 
93
	
-
 
94
	protected function concatenerTaxonsFamilles($taxon) {
-
 
95
		$referentiel = new Referentiel($this->criteresRecherche->referentiel, $taxon);
-
 
96
		$sousTaxons = $referentiel->recupererTaxonsFamilles();
-
 
97
		$criteres = array();
-
 
98
		foreach ($sousTaxons as $sousTaxon) {
-
 
99
			$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%");
-
 
100
		}
-
 
101
		return $criteres;
-
 
102
	}
150
	
103
	
151
	// TODO : completer le corps des methodes construire where pour referentiel et departement
104
	// TODO : completer le corps des methodes construire where pour referentiel et departement
152
	private function construireWhereReferentiel() {
105
	protected function construireWhereReferentiel() {
153
		$sql = '';
106
		$sql = '';
154
		return $sql;
107
		return $sql;
155
	}
108
	}
156
	
109
	
157
	private function construireWhereDepartement() {
110
	protected function construireWhereDepartement() {
-
 
111
		$sql = '';
-
 
112
		if (isset($this->criteresRecherche->departement)) {
-
 
113
			$valeurs_a_proteger = $this->criteresRecherche->departement;
-
 
114
			foreach ($valeurs_a_proteger as $valeur) {
-
 
115
				$aProteger = $this->getBdd()->proteger($valeur . '%');
-
 
116
				$valeurs_protegees[] = "lieu_commune_code_insee LIKE {$aProteger}";
-
 
117
			}
-
 
118
			$valeurs = implode(' OR ', $valeurs_protegees);
-
 
119
			$sql = "AND ($valeurs)";
158
		$sql = '';
120
		}
159
		return $sql;
121
		return $sql;
160
	}
122
	}
161
	
123
	
162
	private function construireWhereAuteur() {
124
	protected function construireWhereAuteur() {
163
		$sql = '';
125
		$sql = '';
164
		if (isset($this->criteresRecherche->auteur)) {
126
		if (isset($this->criteresRecherche->auteur)) {
165
			$auteur = $this->getBdd()->proteger($this->criteresRecherche->auteur);
127
			$auteur = $this->getBdd()->proteger($this->criteresRecherche->auteur);
166
			$sql = "AND observateur_nom_complet = $auteur";
128
			$sql = "AND observateur_nom_complet = $auteur";
167
		}
129
		}
168
		return $sql;
130
		return $sql;
169
	}
131
	}
-
 
132
 
-
 
133
	protected function construireWhereDate() {
-
 
134
		$sql = '';
-
 
135
		$dateDebut = isset($this->criteresRecherche->dateDebut) ? $this->criteresRecherche->dateDebut : null;
-
 
136
		$dateFin   = isset($this->criteresRecherche->dateFin) ? $this->criteresRecherche->dateFin : null;
-
 
137
		if (!is_null($dateDebut) || !is_null($dateFin)) {
-
 
138
			$dateDebut = !is_null($dateDebut) ? substr($dateDebut, 0, 4) : null;
-
 
139
			$dateFin   = !is_null($dateFin)   ? substr($dateFin, 0, 4)   : date('Y');
-
 
140
			$condition = '';
-
 
141
			if ($dateDebut == $dateFin) {
-
 
142
				$condition = "observation_date=".$dateDebut;
-
 
143
			} elseif (is_null($dateFin)) {
-
 
144
				$condition = "observation_date>=".$dateDebut;
-
 
145
			} elseif (is_null($dateDebut)) {
-
 
146
				$condition = "observation_date<=".$dateFin;
-
 
147
			} else {
-
 
148
				$condition = "observation_date BETWEEN ".$dateDebut." AND ".$dateFin;
-
 
149
			}
-
 
150
			$sql = "AND ($condition)";
-
 
151
		}
-
 
152
		return $sql;
-
 
153
	}
170
	
154
	
171
	private function construireWhereCoordonneesBbox() {
155
	protected function construireWhereCoordonneesBbox() {
172
		$bbox = $this->criteresRecherche->bbox;
156
		$bbox = $this->criteresRecherche->bbox;
173
		$sql = "AND lieu_station_longitude BETWEEN ".$bbox['ouest']." AND ".$bbox['est']." ".
157
		$sql = "AND lieu_station_longitude BETWEEN ".$bbox['ouest']." AND ".$bbox['est']." ".
174
			"AND lieu_station_latitude BETWEEN ".$bbox['sud']." AND ".$bbox['nord'];
158
			"AND lieu_station_latitude BETWEEN ".$bbox['sud']." AND ".$bbox['nord'];
175
		return $sql;
159
		return $sql;
176
	}
160
	}
177
	
161
	
178
	private function construireWhereCoordonneesPoint() {
162
	protected function construireWhereCoordonneesPoint() {
179
		$sql = "AND lieu_station_latitude=".$this->criteresRecherche->latitude." ".
163
		$sql = "AND lieu_station_latitude=".$this->criteresRecherche->latitude." ".
180
			"AND lieu_station_longitude=".$this->criteresRecherche->longitude;
164
			"AND lieu_station_longitude=".$this->criteresRecherche->longitude;
181
		return $sql;
165
		return $sql;
182
	}
166
	}
183
	
167
	
184
	private function obtenirNomsStationsSurPoint() {
168
	protected function obtenirNomsStationsSurPoint() {
185
		$requete = "SELECT DISTINCTROW lieu_station_nom FROM sophy_tapir WHERE 1 ".
169
		$requete = "SELECT DISTINCTROW lieu_station_nom FROM sophy_tapir WHERE 1 ".
186
			$this->construireWhereTaxon().' '.
170
			$this->construireWhereTaxon().' '.
187
			$this->construireWhereCoordonneesPoint();
171
			$this->construireWhereCoordonneesPoint();
188
		$stations = $this->getBdd()->recupererTous($requete);
172
		$stations = $this->getBdd()->recupererTous($requete);
189
		$nomsStations = array();
173
		$nomsStations = array();
190
		foreach ($stations as $station) {
174
		foreach ($stations as $station) {
191
			$nomsStations[] = $station['lieu_station_nom'];
175
			$nomsStations[] = $station['lieu_station_nom'];
192
		}
176
		}
193
		return implode(', ', $nomsStations);
177
		return implode(', ', $nomsStations);
194
	}
178
	}
195
	
179
	
196
	private function obtenirNombreStationsDansBbox() {
180
	protected function obtenirNombreStationsDansBbox() {
197
		$bbox = $this->criteresRecherche->bbox;
181
		$bbox = $this->criteresRecherche->bbox;
198
		$zoom = $this->criteresRecherche->zoom;
182
		$zoom = $this->criteresRecherche->zoom;
199
		$requete =
183
		$requete =
200
			"SELECT zoom, Sum(nombre_sites) AS total_points FROM mailles_sophy ".
184
			"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']." ".
185
			"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";
186
			"AND limite_ouest<=".$bbox['est']." AND limite_est>=".$bbox['ouest']." GROUP BY zoom";
203
		$resultat = $this->getBdd()->recuperer($requete);
187
		$resultat = $this->getBdd()->recuperer($requete);
204
		return $resultat['total_points'];
188
		return $resultat['total_points'];
205
	}
189
	}
206
	
190
	
207
	private function recupererMaillesDansBbox() {
191
	protected function recupererMaillesDansBbox() {
208
		$bbox = $this->criteresRecherche->bbox;
192
		$bbox = $this->criteresRecherche->bbox;
209
		$zoom = $this->criteresRecherche->zoom;
193
		$zoom = $this->criteresRecherche->zoom;
210
		$requete =
194
		$requete =
211
			"SELECT zoom, position_latitude, position_longitude, limite_sud AS sud, limite_ouest AS ouest, ".
195
			"SELECT limite_sud AS latitudeSud, limite_ouest AS longitudeOuest, limite_est AS longitudeEst, ".
212
			"limite_nord AS nord, limite_est AS est, nombre_sites AS points, 'MAILLE' AS type_site ".
196
			"limite_nord AS latitudeNord, nombre_sites, nombre_observations FROM mailles_sophy ".
213
			"FROM mailles_sophy WHERE zoom=".$zoom." AND limite_sud<=".$bbox['nord']." ".
197
			"WHERE zoom=".$zoom." AND limite_sud<=".$bbox['nord']." ".
214
			"AND limite_nord>=".$bbox['sud']." AND limite_ouest<=". $bbox['est']." ".
198
			"AND limite_nord>=".$bbox['sud']." AND limite_ouest<=". $bbox['est']." ".
215
			"AND limite_est>=".$bbox['ouest'];
199
			"AND limite_est>=".$bbox['ouest'];
216
		return $this->getBdd()->recupererTous($requete);
200
		$mailles = $this->getBdd()->recupererTous($requete, Bdd::MODE_OBJET);
217
	}
-
 
218
	
-
 
219
	private function recupererNumeroNomenclaturauxTaxons(& $observations) {
-
 
220
		for ($index = 0; $index < count($observations);  $index ++) {
201
		// placer les totaux des nombres de stations dans des mailles vides
221
			$taxon = isset($this->criteresRecherche->taxon) ? $this->criteresRecherche->taxon : null;
202
		$maillage = new Maillage($this->criteresRecherche->bbox, $zoom, $this->nomSource);
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);
203
		$maillage->genererMaillesVides();
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'] = '';
204
		$maillage->ajouterMailles($mailles);
237
			}
-
 
238
		}
-
 
239
	}
-
 
240
	
-
 
241
	private function rechercherTaxonDansReferentiel($nomScientifique, $nomReferentiel) {
-
 
242
		$referentiel = new Referentiel($nomReferentiel);
205
		return $maillage->formaterSortie();
243
		$taxon = $referentiel->obtenirNumeroNomenclatural($nomScientifique);
-
 
244
		return $taxon;
-
 
245
	}
206
	}
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
	
207
	
261
}
208
}
262
 
209
 
263
?>
210
?>