22,7 → 22,7 |
* |
* Les donnees seront renvoyees au format JSON |
* |
* @package framework-0.3 |
* @package framework-0.4 |
* @author Alexandre GALIBERT <alexandre.galibert@tela-botanica.org> |
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt> |
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt> |
32,75 → 32,15 |
*/ |
|
|
class FloradataFormateur { |
final class FloradataFormateur extends Formateur { |
|
private $criteresRecherche; |
private $bdd; |
|
|
public function __construct($criteresRecherche) { |
$this->criteresRecherche = $criteresRecherche; |
} |
|
public function recupererStations() { |
$requeteSql = $this->construireRequeteStations(); |
$stations = $this->getBdd()->recupererTous($requeteSql); |
$zoomMaxMaillage = Config::get('zoom_maximal_maillage'); |
$seuilMaillage = Config::get('seuil_maillage'); |
$zoom = $this->criteresRecherche->zoom; |
$bbox = $this->criteresRecherche->bbox; |
// TODO: gérer une notion d'échelle plutot que de zoom (pour les clients SIG) |
if (count($stations) > $seuilMaillage && intval($zoom)<= $zoomMaxMaillage) { |
$maillage = new Maillage($bbox, $zoom); |
$maillage->genererMaillesVides(); |
$maillage->ajouterPoints($stations); |
$stations = $maillage->formaterSortie(); |
} |
$formateurJSON = new FormateurJson('floradata'); |
$donneesFormatees = $formateurJSON->formaterStations($stations); |
return $donneesFormatees; |
} |
|
public function recupererObservations() { |
$requeteSql = $this->construireRequeteObservations(); |
$observations = $this->getBdd()->recupererTous($requeteSql); |
$nomSite = $this->obtenirNomStation(); |
$formateurJSON = new FormateurJson('floradata'); |
$donneesFormatees = $formateurJSON->formaterObservations($observations, $nomSite); |
return $donneesFormatees; |
} |
|
|
|
// ------------------------------------------------------------------------ // |
// Fonctions de construction des criteres de recherches pour la requete SQL // |
// ------------------------------------------------------------------------ // |
|
private function getBdd() { |
if (!isset($this->bdd)) { |
$this->bdd = new Bdd(); |
} |
$this->bdd->requeter("USE ".Config::get('bdd_nom')); |
return $this->bdd; |
} |
|
private function construireRequeteStations() { |
$bbox = $this->criteresRecherche->bbox; |
$selectTypeSite = |
"IF(". |
"(longitude IS NULL OR latitude IS NULL) ". |
"OR (longitude=0 AND latitude=0) ". |
"OR (mots_cles_texte LIKE '%sensible%') ". |
"OR NOT (". |
"longitude BETWEEN ".$bbox['ouest']." AND ".$bbox['est']." ". |
"AND latitude BETWEEN ".$bbox['sud']." AND ".$bbox['nord']. |
")". |
", 'COMMUNE', 'STATION'". |
")"; |
final protected function construireRequeteStations() { |
$condition = "(longitude IS NULL OR latitude IS NULL OR (longitude=0 AND latitude=0) ". |
"OR longitude>180 OR latitude>90 OR mots_cles_texte LIKE '%sensible%')"; |
$requete = |
"SELECT COUNT(id_observation) AS observations, ce_zone_geo, zone_geo, station, ". |
"longitude, latitude, nom AS nom_commune,wgs84_longitude AS lng_commune, ". |
"wgs84_latitude AS lat_commune, ".$selectTypeSite." AS type_site ". |
"IF({$condition}, wgs84_latitude, latitude) AS latitude, IF({$condition}, wgs84_longitude, longitude) ". |
"AS longitude, IF({$condition}, 'COMMUNE', 'STATION') AS type_site, 'floradata' AS source ". |
"FROM cel_obs LEFT JOIN cel_zones_geo cz ON ce_zone_geo=id_zone_geo ". |
"WHERE transmission=1 ". |
$this->construireWhereDepartement().' '. |
109,15 → 49,16 |
$this->construireWhereReferentiel().' '. |
$this->construireWhereTaxon().' '. |
$this->construireWhereCoordonneesBbox().' '. |
"GROUP BY longitude, latitude, wgs84_longitude, wgs84_latitude"; |
"GROUP BY IF({$condition},wgs84_longitude,longitude), IF({$condition},wgs84_latitude,latitude)"; |
return $requete; |
} |
|
private function construireRequeteObservations() { |
final protected function construireRequeteObservations() { |
$requete = |
"SELECT id_observation AS id_obs, nom_ret_nn AS nn, nom_ret AS nomSci, ". |
"Date(date_observation) AS date, milieu AS lieu, nom_referentiel, ". |
"Concat(prenom_utilisateur, ' ', nom_utilisateur) AS observateur, ce_utilisateur AS observateurId ". |
"SELECT id_observation AS id_obs, nom_sel_nn AS nn, nom_referentiel, lieudit, milieu, ". |
"(DATE(IF(date_observation != '0000-00-00 00:00:00', date_observation, date_transmission))) AS date, ". |
"CONCAT(prenom_utilisateur, ' ', nom_utilisateur) AS observateur, ce_utilisateur AS observateurId, ". |
"IF(nom_sel IS NULL, 'A identifier',nom_sel) AS nomSci, 'floradata' AS projet ". |
"FROM cel_obs WHERE transmission=1 ". |
$this->construireWhereAuteur().' '. |
$this->construireWhereReferentiel().' '. |
124,24 → 65,38 |
$this->construireWhereDate().' '. |
$this->construireWhereTaxon().' '. |
$this->construireWhereCoordonneesPoint().' '. |
"ORDER BY nom_ret, date, observateur"; |
"ORDER BY IF(nom_sel IS NULL, 'A identifier',nom_sel), date, observateur"; |
return $requete; |
} |
|
protected function construireWhereTaxon() { |
final protected function construireRequeteWfs() { |
$condition = "(longitude IS NULL OR latitude IS NULL OR (longitude=0 AND latitude=0) ". |
"OR longitude>180 OR latitude>90 OR mots_cles_texte LIKE '%sensible%')"; |
$requete = |
"SELECT nom_sel AS taxon, ce_zone_geo, zone_geo, station, 'floradata' AS source, ". |
"IF({$condition}, wgs84_latitude, latitude) AS latitude, IF({$condition}, wgs84_longitude, longitude) ". |
"AS longitude, IF({$condition}, 'COMMUNE', 'STATION') AS type_site ". |
"FROM cel_obs LEFT JOIN cel_zones_geo cz ON ce_zone_geo=id_zone_geo ". |
"WHERE transmission=1 ". |
$this->construireWhereCoordonneesBbox().' '. |
$this->construireWhereNomScientifique().' '. |
"ORDER BY IF({$condition},wgs84_longitude,longitude), IF({$condition},wgs84_latitude,latitude)"; |
return $requete; |
} |
|
private function construireWhereTaxon() { |
$sql = ''; |
if (isset($this->criteresRecherche->taxon)) { |
$taxons = $this->criteresRecherche->taxon; |
$criteres = array(); |
foreach ($taxons as $taxon) { |
$nomRang = $this->getNomRang($taxon); |
if ($nomRang == 'genre') { |
if ($taxon['rang'] == Config::get('rang.famille')) { |
$criteres[] = "famille=".$this->getBdd()->proteger($taxon['nom']); |
} elseif ($taxon['rang'] == Config::get('rang.genre')) { |
$criteres[] = "nom_sel LIKE ".$this->getBdd()->proteger($taxon['nom']." %"); |
} else { |
$criteres[] = "nom_ret LIKE ".$this->getBdd()->proteger($taxon['nom']."%"); |
if ($nomRang == 'espece') { |
$criteres = array_merge($criteres, $this->concatenerTaxonsSousEspeces($taxon)); |
} |
$sousTaxons = $this->concatenerSynonymesEtSousEspeces($taxon); |
$criteres[] = "nt IN (".implode(',', $sousTaxons).")"; |
} |
} |
$sql = "AND (".implode(' OR ',array_unique($criteres)).")"; |
148,35 → 103,17 |
} |
return $sql; |
} |
|
protected function getNomRang($taxon) { |
$nomsRangs = array('famille', 'genre', 'espece', 'sous_espece'); |
for ($index = 0; $index < count($nomsRangs) |
&& Config::get("rang.".$nomsRangs[$index]) != $taxon['rang']; $index ++); |
$position = $index == count($nomsRangs) ? count($nomsRangs)-1 : $index; |
return $nomsRangs[$position]; |
} |
|
protected function concatenerTaxonsSousEspeces($taxon) { |
|
private function concatenerSynonymesEtSousEspeces($taxon) { |
$referentiel = new Referentiel($this->criteresRecherche->referentiel, $taxon); |
$sousTaxons = $referentiel->recupererTaxonsSousEspeces(); |
$sousTaxons = $referentiel->recupererSynonymesEtSousEspeces(); |
$criteres = array(); |
foreach ($sousTaxons as $sousTaxon) { |
$criteres[] = "nom_ret LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%"); |
$criteres[] = "nt=".$sousTaxon['nt']; |
} |
return $criteres; |
return array_unique($criteres); |
} |
|
protected function concatenerTaxonsFamilles($taxon) { |
$referentiel = new Referentiel($this->criteresRecherche->referentiel, $taxon); |
$sousTaxons = $referentiel->recupererTaxonsFamilles(); |
$criteres = array(); |
foreach ($sousTaxons as $sousTaxon) { |
$criteres[] = "nom_ret LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%"); |
} |
return $criteres; |
} |
|
private function construireWhereReferentiel() { |
$sql = ''; |
if (isset($this->criteresRecherche->referentiel) && !isset($this->criteresRecherche->taxon)) { |
212,7 → 149,7 |
$sql = ''; |
if (isset($this->criteresRecherche->nbJours)) { |
$nbJours = $this->criteresRecherche->nbJours; |
$sql = "AND (Datediff(Curdate(), date_creation)<={$nbJours})"; |
$sql = "AND (Datediff(Curdate(), date_transmission)<={$nbJours})"; |
} else { |
$sql = $this->construireWhereDateDebutEtFin(); |
} |
227,13 → 164,13 |
$dateFin = !is_null($dateFin) ? $dateFin : date('Y-m-d'); |
$condition = ''; |
if ($dateDebut == $dateFin) { |
$condition = "Date(date_observation)=".$this->getBdd()->proteger($dateDebut); |
$condition = "DATE(date_observation)=".$this->getBdd()->proteger($dateDebut); |
} elseif (is_null($dateFin)) { |
$condition = "Date(date_observation)>=".$this->getBdd()->proteger($dateDebut); |
$condition = "DATE(date_observation)>=".$this->getBdd()->proteger($dateDebut); |
} elseif (is_null($dateDebut)) { |
$condition = "Date(date_observation)<=".$this->getBdd()->proteger($dateFin); |
$condition = "DATE(date_observation)<=".$this->getBdd()->proteger($dateFin); |
} else { |
$condition = "Date(date_observation) BETWEEN ".$this->getBdd()->proteger($dateDebut)." ". |
$condition = "DATE(date_observation) BETWEEN ".$this->getBdd()->proteger($dateDebut)." ". |
"AND ".$this->getBdd()->proteger($dateFin); |
} |
$sql = "AND ($condition)"; |
242,42 → 179,75 |
} |
|
private function construireWhereCoordonneesBbox() { |
$bbox = $this->criteresRecherche->bbox; |
$sql = |
"AND (". |
"(". |
"latitude BETWEEN ".$bbox['sud']." AND ".$bbox['nord']." ". |
"AND longitude BETWEEN ".$bbox['ouest']." AND ".$bbox['est']. |
") OR (". |
"((longitude IS NULL OR latitude IS NULL) OR (longitude=0 AND latitude=0) ". |
"OR (longitude>180 AND latitude>90)) AND ". |
"wgs84_longitude BETWEEN ".$bbox['ouest']." AND ". $bbox['est']." ". |
"AND wgs84_latitude BETWEEN ".$bbox['sud']." AND ".$bbox['nord']. |
$sql = ''; |
if (isset($this->criteresRecherche->bbox)) { |
$sql = "AND (". |
"(".$this->genererCritereWhereBbox('').") OR (". |
"(longitude IS NULL OR latitude IS NULL OR (longitude=0 AND latitude=0) ". |
"OR longitude>180 OR latitude>90 OR mots_cles_texte LIKE '%sensible%') AND ". |
$this->genererCritereWhereBbox('wgs84_'). |
")". |
")"; |
} |
return $sql; |
} |
|
private function genererCritereWhereBbox($suffixe) { |
$bboxRecherche = $this->criteresRecherche->bbox; |
$conditions = array(); |
$sql = ''; |
foreach ($bboxRecherche as $bbox) { |
$conditions[] = "({$suffixe}latitude BETWEEN ".$bbox['sud']." AND ".$bbox['nord']." ". |
"AND {$suffixe}longitude BETWEEN ".$bbox['ouest']." AND ".$bbox['est'].")"; |
} |
if (count($conditions) > 0) { |
$sql = '('.implode(' OR ', $conditions).')'; |
} |
return $sql; |
} |
|
private function construireWhereNomScientifique() { |
$sql = ''; |
if (isset($this->criteresRecherche->filtre)) { |
$filtre = $this->criteresRecherche->filtre; |
$valeur = "'{$filtre['valeur']}".($filtre['operateur'] == 'LIKE' ? "%" : "")."'"; |
switch ($filtre['champ']) { |
case "taxon" : $sql = "AND nom_sel {$filtre['operateur']} {$valeur}"; break; |
} |
} |
return $sql; |
} |
|
private function construireWhereCoordonneesPoint() { |
$longitude = $this->criteresRecherche->longitude; |
$latitude = $this->criteresRecherche->latitude; |
$condition = "(longitude=".$longitude." AND latitude=".$latitude." ". |
"AND (mots_cles_texte IS NULL OR mots_cles_texte NOT LIKE '%sensible%'))"; |
if ($this->criteresRecherche->typeSite == 'commune') { |
$commune = $this->obtenirCoordonneesCommune(); |
$condition .= |
" OR (". |
"((longitude IS NULL OR latitude IS NULL) OR (longitude=0 AND latitude=0) ". |
"OR (longitude>180 AND latitude>90)) ". |
"AND ce_zone_geo=".$this->getBdd()->proteger($commune['id_zone_geo']). |
")"; |
$sql = ''; |
$conditions = array(); |
foreach ($this->criteresRecherche->stations as $station) { |
if ($station[0] == $this->nomSource) { |
$longitude = str_replace(",", ".", strval($station[3])); |
$latitude = str_replace(",", ".", strval($station[2])); |
if ($station[1] == 'station') { |
$conditions[] = "(longitude=".$longitude." AND latitude=".$latitude." ". |
"AND (mots_cles_texte IS NULL OR mots_cles_texte NOT LIKE '%sensible%'))"; |
} else { |
$commune = $this->obtenirCoordonneesCommune($longitude, $latitude); |
$conditions[] = |
"(". |
"((longitude IS NULL OR latitude IS NULL) OR (longitude=0 AND latitude=0) ". |
"OR longitude>180 OR latitude>90 OR mots_cles_texte LIKE '%sensible%') ". |
"AND ce_zone_geo=".$this->getBdd()->proteger($commune['id_zone_geo']). |
")"; |
} |
} |
} |
return "AND ($condition)"; |
if (count($conditions) > 0) { |
$sql = "AND (".implode(" OR ", $conditions).")"; |
} |
return $sql; |
} |
|
private function obtenirCoordonneesCommune() { |
$requete = "SELECT id_zone_geo, nom FROM cel_zones_geo WHERE wgs84_longitude=". |
$this->criteresRecherche->longitude." AND wgs84_latitude=".$this->criteresRecherche->latitude; |
private function obtenirCoordonneesCommune($longitude, $latitude) { |
$requete = "SELECT id_zone_geo, nom FROM cel_zones_geo WHERE wgs84_longitude=$longitude ". |
"AND wgs84_latitude=$latitude"; |
$commune = $this->getBdd()->recuperer($requete); |
if ($commune === false) { |
$commune = null; |
285,12 → 255,13 |
return $commune; |
} |
|
private function obtenirNomStation() { |
final protected function obtenirNomsStationsSurPoint() { |
// verifier si les coordonnees du point de requetage correspondent a une commune |
$station = $this->obtenirCoordonneesCommune(); |
$coordonnees = $this->recupererCoordonneesPremiereStation(); |
$station = $this->obtenirCoordonneesCommune($coordonnees[0], $coordonnees[1]); |
if (is_null($station)) { |
$requete = 'SELECT DISTINCT lieudit AS nom FROM cel_obs WHERE longitude='. |
$this->criteresRecherche->longitude.' AND latitude='.$this->criteresRecherche->latitude; |
$coordonnees[0].' AND latitude='.$coordonnees[1]; |
$station = $this->getBdd()->recuperer($requete); |
} |
$nomStation = ''; |
300,6 → 271,18 |
return $nomStation; |
} |
|
private function recupererCoordonneesPremiereStation() { |
$coordonnees = null; |
foreach ($this->criteresRecherche->stations as $station) { |
if ($station[0] == $this->nomSource) { |
$longitude = str_replace(",", ".", strval($station[3])); |
$latitude = str_replace(",", ".", strval($station[2])); |
$coordonnees = array($longitude, $latitude); |
} |
} |
return $coordonnees; |
} |
|
} |
|
?> |