Line 19... |
Line 19... |
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.4
|
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 |
*/
|
Line 32... |
Line 32... |
32 |
|
32 |
|
Line 33... |
Line 33... |
33 |
class SophyFormateur extends Formateur {
|
33 |
class MoissonnageFormateur extends Formateur {
|
34 |
|
34 |
|
35 |
|
35 |
|
36 |
protected function construireRequeteStations() {
|
36 |
final protected function construireRequeteStations() {
|
37 |
$requete =
|
37 |
$requete =
|
38 |
"SELECT COUNT(guid) AS observations, 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, ".
|
39 |
"lieu_station_longitude AS longitude, 'STATION' AS type_site, lieu_commune_code_insee AS code_insee ".
|
39 |
"lieu_station_longitude AS longitude, 'STATION' AS type_site, lieu_commune_code_insee AS code_insee, ".
|
40 |
"FROM sophy_tapir WHERE 1 ".
|
40 |
"'{$this->nomSource}' AS source FROM {$this->nomSource}_tapir WHERE 1 ".
|
41 |
$this->construireWhereDepartement().' '.
|
41 |
$this->construireWhereDepartement().' '.
|
42 |
$this->construireWhereAuteur().' '.
|
42 |
$this->construireWhereAuteur().' '.
|
43 |
$this->construireWhereDate().' '.
|
43 |
$this->construireWhereDate().' '.
|
44 |
$this->construireWhereReferentiel().' '.
|
44 |
$this->construireWhereReferentiel().' '.
|
45 |
$this->construireWhereTaxon().' '.
|
45 |
$this->construireWhereTaxon().' '.
|
46 |
$this->construireWhereCoordonneesBbox().' '.
|
46 |
$this->construireWhereCoordonneesBbox().' '.
|
Line 47... |
Line 47... |
47 |
"GROUP BY lieu_station_longitude, lieu_station_latitude";
|
47 |
"GROUP BY lieu_station_longitude, lieu_station_latitude";
|
48 |
return $requete;
|
48 |
return $requete;
|
49 |
}
|
49 |
}
|
50 |
|
50 |
|
51 |
protected function construireRequeteObservations() {
|
51 |
final protected function construireRequeteObservations() {
|
52 |
$requete =
|
52 |
$requete =
|
53 |
"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 ".
|
54 |
"observation_date AS date, lieu_station_nom AS lieu, observateur_nom_complet AS observateur, ".
|
55 |
"FROM sophy_tapir WHERE 1 ".
|
55 |
"'{$this->nomSource}' AS projet FROM {$this->nomSource}_tapir WHERE 1 ".
|
56 |
$this->construireWhereAuteur().' '.
|
56 |
$this->construireWhereAuteur().' '.
|
57 |
$this->construireWhereReferentiel().' '.
|
57 |
$this->construireWhereReferentiel().' '.
|
58 |
$this->construireWhereDate().' '.
|
58 |
$this->construireWhereDate().' '.
|
59 |
$this->construireWhereTaxon().' '.
|
59 |
$this->construireWhereTaxon().' '.
|
Line -... |
Line 60... |
- |
|
60 |
$this->construireWhereCoordonneesPoint().' '.
|
- |
|
61 |
"ORDER BY nom_scientifique_complet, date, observateur";
|
- |
|
62 |
return $requete;
|
- |
|
63 |
}
|
- |
|
64 |
|
- |
|
65 |
final protected function construireRequeteWfs() {
|
- |
|
66 |
$requete =
|
- |
|
67 |
"SELECT nom_scientifique_complet AS taxon, lieu_station_nom AS nom, lieu_station_latitude AS latitude, ".
|
- |
|
68 |
"lieu_station_longitude AS longitude, 'STATION' AS type_site, lieu_commune_code_insee AS code_insee, ".
|
- |
|
69 |
"'{$this->nomSource}' AS source FROM {$this->nomSource}_tapir WHERE 1 ".
|
- |
|
70 |
$this->construireWhereCoordonneesBbox().' '.
|
60 |
$this->construireWhereCoordonneesPoint().' '.
|
71 |
$this->construireWhereNomScientifique().' '.
|
61 |
"ORDER BY nom_scientifique_complet, date, observateur";
|
72 |
"ORDER BY lieu_station_longitude, lieu_station_latitude";
|
62 |
return $requete;
|
73 |
return $requete;
|
63 |
}
|
74 |
}
|
64 |
|
75 |
|
65 |
protected function construireWhereTaxon() {
|
76 |
private function construireWhereTaxon() {
|
66 |
$sql = '';
|
- |
|
67 |
if (isset($this->criteresRecherche->taxon)) {
|
77 |
$sql = '';
|
68 |
$taxons = $this->criteresRecherche->taxon;
|
78 |
if (isset($this->criteresRecherche->taxon)) {
|
69 |
$criteres = array();
|
79 |
$taxons = $this->criteresRecherche->taxon;
|
70 |
foreach ($taxons as $taxon) {
|
80 |
$criteres = array();
|
71 |
$nomRang = $this->getNomRang($taxon);
|
81 |
foreach ($taxons as $taxon) {
|
72 |
$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%");
|
82 |
$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%");
|
73 |
if ($nomRang == 'espece') {
|
83 |
if ($taxon['rang'] >= Config::get('rang.espece')) {
|
74 |
$criteres = array_merge($criteres, $this->concatenerTaxonsSousEspeces($taxon));
|
84 |
$criteres = array_merge($criteres, $this->concatenerSynonymesEtSousEspeces($taxon));
|
75 |
} elseif ($nomRang == 'genre') {
|
85 |
} elseif ($taxon['rang'] == Config::get('rang.famille')) {
|
76 |
$criteres = array_merge($criteres, $this->concatenerTaxonsFamilles($taxon));
|
86 |
$criteres = array_merge($criteres, $this->concatenerTaxonsGenres($taxon));
|
77 |
}
|
87 |
}
|
Line 78... |
Line 88... |
78 |
}
|
88 |
}
|
79 |
$sql = "AND (".implode(' OR ',array_unique($criteres)).")";
|
89 |
$sql = "AND (".implode(' OR ',array_unique($criteres)).")";
|
80 |
}
|
90 |
}
|
81 |
return $sql;
|
91 |
return $sql;
|
82 |
}
|
92 |
}
|
83 |
|
93 |
|
84 |
protected function concatenerTaxonsSousEspeces($taxon) {
|
94 |
private function concatenerSynonymesEtSousEspeces($taxon) {
|
85 |
$referentiel = new Referentiel($this->criteresRecherche->referentiel, $taxon);
|
95 |
$referentiel = new Referentiel($this->criteresRecherche->referentiel, $taxon);
|
86 |
$sousTaxons = $referentiel->recupererTaxonsSousEspeces();
|
96 |
$sousTaxons = $referentiel->recupererSynonymesEtSousEspeces();
|
Line 87... |
Line 97... |
87 |
$criteres = array();
|
97 |
$criteres = array();
|
88 |
foreach ($sousTaxons as $sousTaxon) {
|
98 |
foreach ($sousTaxons as $sousTaxon) {
|
89 |
$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%");
|
99 |
$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%");
|
90 |
}
|
100 |
}
|
91 |
return $criteres;
|
101 |
return $criteres;
|
92 |
}
|
102 |
}
|
93 |
|
103 |
|
94 |
protected function concatenerTaxonsFamilles($taxon) {
|
104 |
private function concatenerTaxonsGenres($taxon) {
|
95 |
$referentiel = new Referentiel($this->criteresRecherche->referentiel, $taxon);
|
105 |
$referentiel = new Referentiel($this->criteresRecherche->referentiel, $taxon);
|
Line 96... |
Line -... |
96 |
$sousTaxons = $referentiel->recupererTaxonsFamilles();
|
- |
|
97 |
$criteres = array();
|
106 |
$sousTaxons = $referentiel->recupererGenres();
|
98 |
foreach ($sousTaxons as $sousTaxon) {
|
107 |
$criteres = array();
|
- |
|
108 |
foreach ($sousTaxons as $sousTaxon) {
|
- |
|
109 |
$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%");
|
- |
|
110 |
}
|
- |
|
111 |
return $criteres;
|
- |
|
112 |
}
|
- |
|
113 |
|
99 |
$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%");
|
114 |
private function construireWhereReferentiel() {
|
100 |
}
|
115 |
$sql = '';
|
Line 101... |
Line 116... |
101 |
return $criteres;
|
116 |
if (isset($this->criteresRecherche->referentiel)) {
|
102 |
}
|
117 |
$referentielSource = Config::get('referentiel_source');
|
103 |
|
118 |
if (strstr($this->criteresRecherche->referentiel, $referentielSource) === false) {
|
104 |
// TODO : completer le corps des methodes construire where pour referentiel et departement
|
119 |
$sql = "AND 0";
|
105 |
protected function construireWhereReferentiel() {
|
120 |
}
|
106 |
$sql = '';
|
121 |
}
|
Line 119... |
Line 134... |
119 |
$sql = "AND ($valeurs)";
|
134 |
$sql = "AND ($valeurs)";
|
120 |
}
|
135 |
}
|
121 |
return $sql;
|
136 |
return $sql;
|
122 |
}
|
137 |
}
|
Line 123... |
Line 138... |
123 |
|
138 |
|
124 |
protected function construireWhereAuteur() {
|
139 |
private function construireWhereAuteur() {
|
125 |
$sql = '';
|
140 |
$sql = '';
|
126 |
if (isset($this->criteresRecherche->auteur)) {
|
141 |
if (isset($this->criteresRecherche->auteur)) {
|
127 |
$auteur = $this->getBdd()->proteger($this->criteresRecherche->auteur);
|
142 |
$auteur = $this->getBdd()->proteger($this->criteresRecherche->auteur);
|
128 |
$sql = "AND observateur_nom_complet = $auteur";
|
143 |
$sql = "AND observateur_nom_complet = $auteur";
|
129 |
}
|
144 |
}
|
130 |
return $sql;
|
145 |
return $sql;
|
Line 131... |
Line 146... |
131 |
}
|
146 |
}
|
132 |
|
147 |
|
133 |
protected function construireWhereDate() {
|
148 |
private function construireWhereDate() {
|
134 |
$sql = '';
|
149 |
$sql = '';
|
135 |
$dateDebut = isset($this->criteresRecherche->dateDebut) ? $this->criteresRecherche->dateDebut : null;
|
150 |
$dateDebut = isset($this->criteresRecherche->dateDebut) ? $this->criteresRecherche->dateDebut : null;
|
136 |
$dateFin = isset($this->criteresRecherche->dateFin) ? $this->criteresRecherche->dateFin : null;
|
151 |
$dateFin = isset($this->criteresRecherche->dateFin) ? $this->criteresRecherche->dateFin : null;
|
Line 150... |
Line 165... |
150 |
$sql = "AND ($condition)";
|
165 |
$sql = "AND ($condition)";
|
151 |
}
|
166 |
}
|
152 |
return $sql;
|
167 |
return $sql;
|
153 |
}
|
168 |
}
|
Line 154... |
Line 169... |
154 |
|
169 |
|
- |
|
170 |
private function construireWhereCoordonneesBbox() {
|
- |
|
171 |
$sql = '';
|
155 |
protected function construireWhereCoordonneesBbox() {
|
172 |
if (isset($this->criteresRecherche->bbox)) {
|
- |
|
173 |
$bboxRecherche = $this->criteresRecherche->bbox;
|
- |
|
174 |
$conditions = array();
|
156 |
$bbox = $this->criteresRecherche->bbox;
|
175 |
foreach ($bboxRecherche as $bbox) {
|
157 |
$sql = "AND lieu_station_longitude BETWEEN ".$bbox['ouest']." AND ".$bbox['est']." ".
|
176 |
$conditions[] = "(lieu_station_longitude BETWEEN ".$bbox['ouest']." AND ".$bbox['est']." ".
|
- |
|
177 |
"AND lieu_station_latitude BETWEEN ".$bbox['sud']." AND ".$bbox['nord'].")";
|
- |
|
178 |
}
|
- |
|
179 |
$sql = 'AND ('.implode(' OR ', $conditions).')';
|
- |
|
180 |
}
|
- |
|
181 |
return $sql;
|
- |
|
182 |
}
|
- |
|
183 |
|
- |
|
184 |
private function construireWhereNomScientifique() {
|
- |
|
185 |
$sql = '';
|
- |
|
186 |
if (isset($this->criteresRecherche->filtre)) {
|
- |
|
187 |
$filtre = $this->criteresRecherche->filtre;
|
- |
|
188 |
$valeur = "'{$filtre['valeur']}".($filtre['operateur'] == 'LIKE' ? "%" : "")."'";
|
- |
|
189 |
switch ($filtre['champ']) {
|
- |
|
190 |
case "taxon" : $sql = "AND nom_scientifique_complet {$filtre['operateur']} {$valeur}"; break;
|
- |
|
191 |
}
|
158 |
"AND lieu_station_latitude BETWEEN ".$bbox['sud']." AND ".$bbox['nord'];
|
192 |
}
|
159 |
return $sql;
|
193 |
return $sql;
|
Line 160... |
Line 194... |
160 |
}
|
194 |
}
|
- |
|
195 |
|
- |
|
196 |
private function construireWhereCoordonneesPoint() {
|
161 |
|
197 |
$sql = '';
|
- |
|
198 |
$conditions = array();
|
- |
|
199 |
foreach ($this->criteresRecherche->stations as $station) {
|
- |
|
200 |
if ($station[0] == $this->nomSource) {
|
162 |
protected function construireWhereCoordonneesPoint() {
|
201 |
$longitude = str_replace(",", ".", strval($station[2]));
|
- |
|
202 |
$latitude = str_replace(",", ".", strval($station[3]));
|
- |
|
203 |
$conditions[] = "(lieu_station_latitude={$longitude} AND lieu_station_longitude={$latitude})";
|
- |
|
204 |
}
|
- |
|
205 |
}
|
- |
|
206 |
if (count($conditions) > 0) {
|
163 |
$sql = "AND lieu_station_latitude=".$this->criteresRecherche->latitude." ".
|
207 |
$sql = "AND (".implode(" OR ", $conditions).")";
|
164 |
"AND lieu_station_longitude=".$this->criteresRecherche->longitude;
|
208 |
}
|
Line 165... |
Line 209... |
165 |
return $sql;
|
209 |
return $sql;
|
166 |
}
|
210 |
}
|
167 |
|
211 |
|
168 |
protected function obtenirNomsStationsSurPoint() {
|
212 |
final protected function obtenirNomsStationsSurPoint() {
|
169 |
$requete = "SELECT DISTINCTROW lieu_station_nom FROM sophy_tapir WHERE 1 ".
|
213 |
$requete = "SELECT DISTINCTROW lieu_station_nom FROM {$this->nomSource}_tapir WHERE 1 ".
|
170 |
$this->construireWhereTaxon().' '.
|
214 |
$this->construireWhereTaxon().' '.
|
171 |
$this->construireWhereCoordonneesPoint();
|
215 |
$this->construireWhereCoordonneesPoint();
|
172 |
$stations = $this->getBdd()->recupererTous($requete);
|
216 |
$stations = $this->getBdd()->recupererTous($requete);
|
173 |
$nomsStations = array();
|
217 |
$nomsStations = array();
|
174 |
foreach ($stations as $station) {
|
218 |
foreach ($stations as $station) {
|
175 |
$nomsStations[] = $station['lieu_station_nom'];
|
219 |
$nomsStations[] = $station['lieu_station_nom'];
|
Line 176... |
Line 220... |
176 |
}
|
220 |
}
|
177 |
return implode(', ', $nomsStations);
|
221 |
return implode(', ', $nomsStations);
|
178 |
}
|
222 |
}
|
179 |
|
223 |
|
180 |
protected function obtenirNombreStationsDansBbox() {
|
224 |
final protected function obtenirNombreStationsDansBbox() {
|
181 |
$bbox = $this->criteresRecherche->bbox;
|
- |
|
182 |
$zoom = $this->criteresRecherche->zoom;
|
225 |
$bbox = $this->criteresRecherche->bbox;
|
183 |
$requete =
|
226 |
$zoom = $this->criteresRecherche->zoom;
|
184 |
"SELECT zoom, Sum(nombre_sites) AS total_points FROM mailles_sophy ".
|
227 |
$requete =
|
185 |
"WHERE zoom=".$zoom." AND limite_sud<=".$bbox['nord']." AND limite_nord>=".$bbox['sud']." ".
|
228 |
"SELECT zoom, Sum(nombre_sites) AS total_points FROM mailles_{$this->nomSource} ".
|
Line 186... |
Line 229... |
186 |
"AND limite_ouest<=".$bbox['est']." AND limite_est>=".$bbox['ouest']." GROUP BY zoom";
|
229 |
"WHERE zoom=".$zoom." ".$this->construireWhereMaillesBbox()." GROUP BY zoom";
|
187 |
$resultat = $this->getBdd()->recuperer($requete);
|
230 |
$resultat = $this->getBdd()->recuperer($requete);
|
188 |
return $resultat['total_points'];
|
231 |
return $resultat['total_points'];
|
189 |
}
|
232 |
}
|
190 |
|
233 |
|
191 |
protected function recupererMaillesDansBbox() {
|
234 |
final protected function recupererMaillesDansBbox() {
|
192 |
$bbox = $this->criteresRecherche->bbox;
|
235 |
$bbox = $this->criteresRecherche->bbox;
|
193 |
$zoom = $this->criteresRecherche->zoom;
|
- |
|
194 |
$requete =
|
- |
|
195 |
"SELECT limite_sud AS latitudeSud, limite_ouest AS longitudeOuest, limite_est AS longitudeEst, ".
|
236 |
$zoom = $this->criteresRecherche->zoom;
|
196 |
"limite_nord AS latitudeNord, nombre_sites, nombre_observations FROM mailles_sophy ".
|
237 |
$requete =
|
197 |
"WHERE zoom=".$zoom." AND limite_sud<=".$bbox['nord']." ".
|
238 |
"SELECT limite_sud AS sud, limite_ouest AS ouest, limite_est AS est, limite_nord AS nord, ".
|
198 |
"AND limite_nord>=".$bbox['sud']." AND limite_ouest<=". $bbox['est']." ".
|
239 |
"nombre_sites AS stations, nombre_observations AS observations FROM mailles_{$this->nomSource} ".
|
199 |
"AND limite_est>=".$bbox['ouest'];
|
240 |
"WHERE zoom=".$zoom." ".$this->construireWhereMaillesBbox();
|
200 |
$mailles = $this->getBdd()->recupererTous($requete, Bdd::MODE_OBJET);
|
241 |
$mailles = $this->getBdd()->recupererTous($requete);
|
201 |
// placer les totaux des nombres de stations dans des mailles vides
|
242 |
// placer les totaux des nombres de stations dans des mailles vides
|
Line -... |
Line 243... |
- |
|
243 |
$maillage = new Maillage($this->criteresRecherche->bbox, $zoom, $this->nomSource);
|
- |
|
244 |
$maillage->genererMaillesVides();
|
- |
|
245 |
$maillage->ajouterMailles($mailles);
|
- |
|
246 |
return $maillage->formaterSortie();
|
- |
|
247 |
}
|
- |
|
248 |
|
- |
|
249 |
private function construireWhereMaillesBbox() {
|
- |
|
250 |
$bboxRecherche = $this->criteresRecherche->bbox;
|
- |
|
251 |
$conditions = array();
|
- |
|
252 |
$sql = '';
|
- |
|
253 |
foreach ($bboxRecherche as $bbox) {
|
- |
|
254 |
$conditions[] = "(limite_sud<=".$bbox['nord']." AND limite_nord>=".$bbox['sud']." ".
|
- |
|
255 |
"AND limite_ouest<=". $bbox['est']." AND limite_est>=".$bbox['ouest'].")";
|
- |
|
256 |
}
|
202 |
$maillage = new Maillage($this->criteresRecherche->bbox, $zoom, $this->nomSource);
|
257 |
if (count($conditions) > 0) {
|
Line 203... |
Line 258... |
203 |
$maillage->genererMaillesVides();
|
258 |
$sql = 'AND ('.implode(' OR ', $conditions).')';
|
204 |
$maillage->ajouterMailles($mailles);
|
259 |
}
|