Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 26 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 26 Rev 31
Line 28... Line 28...
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
 */
Line 32... Line 32...
32
 
32
 
Line 33... Line -...
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;
-
 
Line 92... Line -...
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 ".
-
 
40
			"FROM sophy_tapir WHERE 1 ".
112
			"FROM sophy_tapir WHERE 1 ".
41
			$this->construireWhereDepartement().' '.
113
			$this->construireWhereDepartement().' '.
42
			$this->construireWhereAuteur().' '.
114
			$this->construireWhereAuteur().' '.
43
			$this->construireWhereDate().' '.
115
			$this->construireWhereReferentiel().' '.
44
			$this->construireWhereReferentiel().' '.
116
			$this->construireWhereTaxon().' '.
45
			$this->construireWhereTaxon().' '.
117
			$this->construireWhereCoordonneesBbox().' '.
46
			$this->construireWhereCoordonneesBbox().' '.
Line 118... Line 47...
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, ".
-
 
54
			"observation_date AS date, lieu_station_nom AS lieu, observateur_nom_complet AS observateur ".
125
			"observation_date AS date, lieu_station_nom AS lieu, observateur_nom_complet AS observateur ".
55
			"FROM sophy_tapir WHERE 1 ".
126
			"FROM sophy_tapir WHERE 1 ".
56
			$this->construireWhereAuteur().' '.
127
			$this->construireWhereAuteur().' '.
57
			$this->construireWhereReferentiel().' '.
128
			$this->construireWhereReferentiel().' '.
58
			$this->construireWhereDate().' '.
129
			$this->construireWhereTaxon().' '.
59
			$this->construireWhereTaxon().' '.
Line 130... Line 60...
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
	}
-
 
64
	
-
 
65
	protected function construireWhereTaxon() {
-
 
66
		$sql = '';
134
	
67
		if (isset($this->criteresRecherche->taxon)) {
135
	private function construireWhereTaxon() {
-
 
136
		$sql = '';
68
			$taxons = $this->criteresRecherche->taxon;
137
		if (isset($this->criteresRecherche->taxon)) {
69
			$criteres = array();
138
			$taxon = $this->criteresRecherche->taxon;
70
			foreach ($taxons as $taxon) {
139
			$criteres = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%");
71
				$nomRang = $this->getNomRang($taxon);
-
 
72
				$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%");
140
				
73
				if ($nomRang == 'espece') {
141
			$referentiel = new Referentiel($this->criteresRecherche->referentiel, $this->criteresRecherche->taxon);
74
					$criteres = array_merge($criteres, $this->concatenerTaxonsSousEspeces($taxon));
142
			$sousTaxons = $referentiel->recupererSousTaxons();
75
				} elseif ($nomRang == 'genre') {
143
			foreach ($sousTaxons as $sousTaxon) {
76
					$criteres = array_merge($criteres, $this->concatenerTaxonsFamilles($taxon));
144
				$criteres .= " OR nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%");
77
				}
Line -... Line 78...
-
 
78
			}
-
 
79
			$sql = "AND (".implode(' OR ',array_unique($criteres)).")";
-
 
80
		}
-
 
81
		return $sql;
-
 
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();
145
			}
98
		foreach ($sousTaxons as $sousTaxon) {
146
			$sql = "AND ($criteres)";
99
			$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%");
147
		}
100
		}
148
		return $sql;
101
		return $criteres;
149
	}
102
	}
Line 150... Line 103...
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
-
 
105
	protected function construireWhereReferentiel() {
-
 
106
		$sql = '';
-
 
107
		return $sql;
-
 
108
	}
-
 
109
	
-
 
110
	protected function construireWhereDepartement() {
-
 
111
		$sql = '';
-
 
112
		if (isset($this->criteresRecherche->departement)) {
-
 
113
			$valeurs_a_proteger = $this->criteresRecherche->departement;
152
	private function construireWhereReferentiel() {
114
			foreach ($valeurs_a_proteger as $valeur) {
153
		$sql = '';
115
				$aProteger = $this->getBdd()->proteger($valeur . '%');
Line 154... Line 116...
154
		return $sql;
116
				$valeurs_protegees[] = "lieu_commune_code_insee LIKE {$aProteger}";
155
	}
117
			}
156
	
118
			$valeurs = implode(' OR ', $valeurs_protegees);
157
	private function construireWhereDepartement() {
119
			$sql = "AND ($valeurs)";
158
		$sql = '';
120
		}
159
		return $sql;
121
		return $sql;
160
	}
122
	}
161
	
123
	
-
 
124
	protected function construireWhereAuteur() {
-
 
125
		$sql = '';
-
 
126
		if (isset($this->criteresRecherche->auteur)) {
-
 
127
			$auteur = $this->getBdd()->proteger($this->criteresRecherche->auteur);
-
 
128
			$sql = "AND observateur_nom_complet = $auteur";
-
 
129
		}
-
 
130
		return $sql;
-
 
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)) {
Line 162... Line 146...
162
	private function construireWhereAuteur() {
146
				$condition = "observation_date<=".$dateFin;
163
		$sql = '';
147
			} else {
164
		if (isset($this->criteresRecherche->auteur)) {
148
				$condition = "observation_date BETWEEN ".$dateDebut." AND ".$dateFin;
165
			$auteur = $this->getBdd()->proteger($this->criteresRecherche->auteur);
149
			}
166
			$sql = "AND observateur_nom_complet = $auteur";
150
			$sql = "AND ($condition)";
167
		}
151
		}
Line 168... Line 152...
168
		return $sql;
152
		return $sql;
169
	}
153
	}
170
	
154
	
171
	private function construireWhereCoordonneesBbox() {
155
	protected function construireWhereCoordonneesBbox() {
172
		$bbox = $this->criteresRecherche->bbox;
156
		$bbox = $this->criteresRecherche->bbox;
Line 173... Line 157...
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() {
Line 191... Line 175...
191
			$nomsStations[] = $station['lieu_station_nom'];
175
			$nomsStations[] = $station['lieu_station_nom'];
192
		}
176
		}
193
		return implode(', ', $nomsStations);
177
		return implode(', ', $nomsStations);
194
	}
178
	}
Line 195... Line 179...
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'];
Line 205... Line 189...
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'];
-
 
216
		return $this->getBdd()->recupererTous($requete);
-
 
217
	}
-
 
218
	
199
			"AND limite_est>=".$bbox['ouest'];
219
	private function recupererNumeroNomenclaturauxTaxons(& $observations) {
200
		$mailles = $this->getBdd()->recupererTous($requete, Bdd::MODE_OBJET);
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)) {
201
		// placer les totaux des nombres de stations dans des mailles vides
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'];
202
		$maillage = new Maillage($this->criteresRecherche->bbox, $zoom, $this->nomSource);
235
			} else {
-
 
236
				$observations[$index]['nn'] = '';
-
 
237
			}
-
 
238
		}
-
 
239
	}
-
 
240
	
203
		$maillage->genererMaillesVides();
241
	private function rechercherTaxonDansReferentiel($nomScientifique, $nomReferentiel) {
-
 
242
		$referentiel = new Referentiel($nomReferentiel);
-
 
243
		$taxon = $referentiel->obtenirNumeroNomenclatural($nomScientifique);
204
		$maillage->ajouterMailles($mailles);
Line 244... Line -...
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
	}
205
		return $maillage->formaterSortie();
Line 259... Line 206...
259
	
206
	}
260
	
207