/trunk/services/modules/0.1/sources/BaznatFormateur.php |
---|
File deleted |
\ No newline at end of file |
/trunk/services/modules/0.1/sources/SophyFormateur.php |
---|
File deleted |
\ No newline at end of file |
/trunk/services/modules/0.1/sources/Formateur.php |
---|
3,81 → 3,73 |
abstract class Formateur { |
protected $criteresRecherche; |
protected $bdd; |
protected $bdd = null; |
protected $nomSource = ''; |
public function __construct($criteresRecherche) { |
public function __construct($criteresRecherche, $source) { |
$this->criteresRecherche = $criteresRecherche; |
$this->nomSource = Config::get('nom_source'); |
$this->nomSource = $source; |
} |
public function recupererStations() { |
$stations = null; |
if ($this->estTraitementRecuperationAutorise()) { |
$stations = $this->traiterRecupererStations(); |
protected function getBdd() { |
if (is_null($this->bdd)) { |
$this->bdd = new Bdd(); |
$nomBdd = $this->nomSource == 'floradata' ? Config::get('bdd_nom_floradata') : Config::get('bdd_nom_eflore'); |
$this->bdd->requeter("USE {$nomBdd}"); |
} |
return $stations; |
return $this->bdd; |
} |
public function recupererObservations() { |
$stations = null; |
if ($this->estTraitementRecuperationAutorise()) { |
$stations = $this->traiterRecupererObservations(); |
public function recupererStations() { |
$stations = array(); |
if ($this->nomSource == 'floradata' || $this->calculerNombreCriteresNonSpatiaux() > 0) { |
$requeteSql = $this->construireRequeteStations(); |
$stations = $this->getBdd()->recupererTous($requeteSql); |
if ($this->determinerFormatRetour(count($stations)) == 'maille') { |
$maillage = new Maillage($this->criteresRecherche->bbox, |
$this->criteresRecherche->zoom, $this->nomSource); |
$maillage->genererMaillesVides(); |
$maillage->ajouterStations($stations); |
$stations = $maillage->formaterSortie(); |
} |
} else { |
$nombreStations = $this->obtenirNombreStationsDansBbox(); |
if ($this->determinerFormatRetour($nombreStations) == 'maille') { |
$stations = $this->recupererMaillesDansBbox(); |
} else { |
$requeteSql = $this->construireRequeteStations(); |
$stations = $this->getBdd()->recupererTous($requeteSql); |
} |
} |
return $stations; |
} |
protected function estTraitementRecuperationAutorise() { |
return (!( |
isset($this->criteresRecherche->nbJours) || |
(isset($this->criteresRecherche->referentiel) && |
$this->criteresRecherche->referentiel != Config::get('referentiel_source')) |
)); |
public function recupererWfs() { |
$requeteSql = $this->construireRequeteWfs(); |
return $this->getBdd()->recupererTous($requeteSql); |
} |
protected function traiterRecupererStations() { |
$stations = array(); |
$nombreStations = $this->obtenirNombreStationsDansBbox(); |
$seuilMaillage = Config::get('seuil_maillage'); |
$nombreParametresNonSpatiaux = $this->calculerNombreCriteresNonSpatiaux(); |
if ($nombreStations >= $seuilMaillage && $nombreParametresNonSpatiaux == 0) { |
// pas besoin de rechercher les stations correspondant a ces criteres |
// recuperer les mailles se trouvant dans l'espace de recherche demande |
$stations = $this->recupererMaillesDansBbox(); |
protected function determinerFormatRetour($nombreStations) { |
$formatRetour = 'point'; |
$zoomMaxMaillage = Config::get('zoom_maximal_maillage'); |
if (isset($this->criteresRecherche->format) && $this->criteresRecherche->format == 'maille' |
&& $this->criteresRecherche->zoom <= $zoomMaxMaillage) { |
$formatRetour = 'maille'; |
} else { |
$requeteSql = $this->construireRequeteStations(); |
$stations = $this->getBdd()->recupererTous($requeteSql); |
$zoom = $this->criteresRecherche->zoom; |
$zoomMaxMaillage = Config::get('zoom_maximal_maillage'); |
if ($zoom <= $zoomMaxMaillage && count($stations) >= $seuilMaillage) { |
$maillage = new Maillage($this->criteresRecherche->bbox, $zoom, $this->nomSource); |
$maillage->genererMaillesVides(); |
$maillage->ajouterPoints($stations); |
$stations = $maillage->formaterSortie(); |
$seuilMaillage = Config::get('seuil_maillage'); |
if ($this->criteresRecherche->zoom <= $zoomMaxMaillage && $nombreStations > $seuilMaillage) { |
$formatRetour = 'maille'; |
} |
} |
$formateurJSON = new FormateurJson($this->nomSource); |
$donneesFormatees = $formateurJSON->formaterStations($stations); |
return $donneesFormatees; |
return $formatRetour; |
} |
protected function traiterRecupererObservations() { |
$requeteSql = $this->construireRequeteObservations(); |
$observations = $this->getBdd()->recupererTous($requeteSql); |
$this->recupererNumeroNomenclaturauxTaxons($observations); |
$nomStation = $this->obtenirNomsStationsSurPoint(); |
$formateurJSON = new FormateurJson($this->nomSource); |
$donneesFormatees = $formateurJSON->formaterObservations($observations, $nomStation); |
return $donneesFormatees; |
} |
protected function calculerNombreCriteresNonSpatiaux() { |
$nombreParametresNonSpatiaux = 0; |
$criteresAIgnorer = array('zoom', 'bbox', 'longitude', 'latitude', 'referentiel'); |
$criteresAIgnorer = array('zoom', 'bbox', 'stations', 'referentiel', 'format'); |
foreach ($this->criteresRecherche as $nomCritere => $valeur) { |
if (!in_array($nomCritere, $criteresAIgnorer)) { |
echo $nomCritere.chr(13); |
$nombreParametresNonSpatiaux ++; |
} |
} |
84,66 → 76,56 |
return $nombreParametresNonSpatiaux; |
} |
protected function getBdd() { |
if (!isset($this->bdd)) { |
$this->bdd = new Bdd(); |
abstract protected function construireRequeteStations(); |
protected function obtenirNombreStationsDansBbox() {} |
protected function recupererMaillesDansBbox() {} |
public function recupererObservations() { |
$requeteSql = $this->construireRequeteObservations(); |
$observations = $this->getBdd()->recupererTous($requeteSql); |
if ($this->nomSource != 'floradata') { |
$this->recupererNumeroNomenclaturauxTaxons($observations); |
} |
$this->bdd->requeter("USE ".Config::get('bdd_nom')); |
return $this->bdd; |
} |
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 recupererNumeroNomenclaturauxTaxons(& $observations) { |
$nomStation = $this->obtenirNomsStationsSurPoint(); |
if (strlen($nomStation) == 0) { |
$nomStation = 'station sans nom'; |
} |
for ($index = 0; $index < count($observations); $index ++) { |
$taxons = isset($this->criteresRecherche->taxon) ? $this->criteresRecherche->taxon : null; |
if (!is_null($taxons)) { |
foreach ($taxons as $taxon) { |
if ($observations[$index]['nomSci'] == 0) { |
continue; |
} |
if (isset($this->criteresRecherche->taxon)) { |
$taxon = $this->rechercherTaxonDansReferentiel($observations[$index]['nomSci'], |
$this->criteresRecherche->referentiel); |
} else { |
$taxon = $this->obtenirNumeroTaxon($observations[$index]['nomSci']); |
} |
if (!is_null($taxon)) { |
$observations[$index]['nn'] = $taxon['nn']; |
$observations[$index]['num_referentiel'] = $taxon['referentiel']; |
} else { |
$observations[$index]['nn'] = ''; |
} |
} |
} |
$observations[$index]['nom_station'] = $nomStation; |
} |
return $observations; |
} |
protected function rechercherTaxonDansReferentiel($nomScientifique, $nomReferentiel) { |
$referentiel = new Referentiel($nomReferentiel); |
$taxon = $referentiel->obtenirNumeroNomenclatural($nomScientifique); |
return $taxon; |
} |
abstract protected function construireRequeteObservations(); |
protected function obtenirNumeroTaxon($nomScientifique) { |
$taxonTrouve = null; |
$listeReferentiels = Referentiel::recupererListeReferentielsDisponibles(); |
foreach ($listeReferentiels as $nomReferentiel) { |
$taxon = $this->rechercherTaxonDansReferentiel($nomScientifique, $nomReferentiel); |
protected function recupererNumeroNomenclaturauxTaxons(& $observations) { |
for ($index = 0; $index < count($observations); $index ++) { |
if (strlen (trim($observations[$index]['nomSci'])) == 0) { |
continue; |
} |
$numeroNomenclatural = isset($observations[$index]['nn']) ? $observations[$index]['nn'] : null; |
$referentiels = Referentiel::recupererListeReferentielsDisponibles(); |
$taxon = null; |
$indexRef = 0; |
while ($indexRef < count($referentiels) && is_null($taxon)) { |
$referentiel = new Referentiel($referentiels[$indexRef]); |
$taxon = $referentiel->obtenirNumeroNomenclatural($observations[$index]['nomSci'], |
$numeroNomenclatural); |
$indexRef ++; |
} |
if (!is_null($taxon)) { |
$taxonTrouve = $taxon; |
break; |
$observations[$index]['nn'] = $taxon['nn']; |
$observations[$index]['nom_referentiel'] = $taxon['referentiel']; |
} else { |
$observations[$index]['nn'] = ''; |
} |
} |
return $taxonTrouve; |
} |
abstract protected function obtenirNomsStationsSurPoint(); |
} |
?> |
/trunk/services/modules/0.1/sources/MoissonnageFormateur.php |
---|
New file |
0,0 → 1,265 |
<?php |
/** |
* |
* Classe en charge de recuperer les donnees d'observation ou liees a leur localisation |
* Le jeu de donnees a interroger est partage en commun avec l'application EFlore |
* |
* On passera en parametre lors de la creation d'une instance un objet contenant la liste des criteres |
* qui vont restreindre le nombre de resultats a renvoyer |
* |
* Les deux operations suivantes peuvent etre utilisees dans les services : |
* - recupererStations : va rechercher dans la base de donnees tous les points d'observations |
* correspondant aux criteres de recherche demandes. La precision des lieux d'observation est |
* soit un point precis, soit ramenee au niveau de la commune dans laquelle l'observation a ete faite |
* En fonction du niveau de zoom et du nombre de resultats trouves, la presentation se fera |
* soit par les points localisant les stations pour des niveaux de zoom eleves ou pour un nombre |
* de stations inferieur a un seuil, ou par des mailles de 64*64 pixels dans le cas contraire |
* |
* - recupererObservations : va rechercher dans la base de donnees les donnees sur des observations |
* a partir des coordonnees longitude et latitude d'une station (+ des parametres additionnels) |
* |
* Les donnees seront renvoyees au format JSON |
* |
* @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> |
* @version $Id$ |
* @copyright 2013 Tela Botanica (accueil@tela-botanica.org) |
* |
*/ |
class MoissonnageFormateur extends Formateur { |
final protected function construireRequeteStations() { |
$requete = |
"SELECT COUNT(guid) AS observations, lieu_station_nom AS nom, lieu_station_latitude AS latitude, ". |
"lieu_station_longitude AS longitude, 'STATION' AS type_site, lieu_commune_code_insee AS code_insee, ". |
"'{$this->nomSource}' AS source FROM {$this->nomSource}_tapir WHERE 1 ". |
$this->construireWhereDepartement().' '. |
$this->construireWhereAuteur().' '. |
$this->construireWhereDate().' '. |
$this->construireWhereReferentiel().' '. |
$this->construireWhereTaxon().' '. |
$this->construireWhereCoordonneesBbox().' '. |
"GROUP BY lieu_station_longitude, lieu_station_latitude"; |
return $requete; |
} |
final protected function construireRequeteObservations() { |
$requete = |
"SELECT observation_id AS id_obs, nom_scientifique_complet AS nomSci, ". |
"observation_date AS date, lieu_station_nom AS lieu, observateur_nom_complet AS observateur, ". |
"'{$this->nomSource}' AS projet FROM {$this->nomSource}_tapir WHERE 1 ". |
$this->construireWhereAuteur().' '. |
$this->construireWhereReferentiel().' '. |
$this->construireWhereDate().' '. |
$this->construireWhereTaxon().' '. |
$this->construireWhereCoordonneesPoint().' '. |
"ORDER BY nom_scientifique_complet, date, observateur"; |
return $requete; |
} |
final protected function construireRequeteWfs() { |
$requete = |
"SELECT nom_scientifique_complet AS taxon, lieu_station_nom AS nom, lieu_station_latitude AS latitude, ". |
"lieu_station_longitude AS longitude, 'STATION' AS type_site, lieu_commune_code_insee AS code_insee, ". |
"'{$this->nomSource}' AS source FROM {$this->nomSource}_tapir WHERE 1 ". |
$this->construireWhereCoordonneesBbox().' '. |
$this->construireWhereNomScientifique().' '. |
"ORDER BY lieu_station_longitude, lieu_station_latitude"; |
return $requete; |
} |
private function construireWhereTaxon() { |
$sql = ''; |
if (isset($this->criteresRecherche->taxon)) { |
$taxons = $this->criteresRecherche->taxon; |
$criteres = array(); |
foreach ($taxons as $taxon) { |
$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%"); |
if ($taxon['rang'] >= Config::get('rang.espece')) { |
$criteres = array_merge($criteres, $this->concatenerSynonymesEtSousEspeces($taxon)); |
} elseif ($taxon['rang'] == Config::get('rang.famille')) { |
$criteres = array_merge($criteres, $this->concatenerTaxonsGenres($taxon)); |
} |
} |
$sql = "AND (".implode(' OR ',array_unique($criteres)).")"; |
} |
return $sql; |
} |
private function concatenerSynonymesEtSousEspeces($taxon) { |
$referentiel = new Referentiel($this->criteresRecherche->referentiel, $taxon); |
$sousTaxons = $referentiel->recupererSynonymesEtSousEspeces(); |
$criteres = array(); |
foreach ($sousTaxons as $sousTaxon) { |
$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%"); |
} |
return $criteres; |
} |
private function concatenerTaxonsGenres($taxon) { |
$referentiel = new Referentiel($this->criteresRecherche->referentiel, $taxon); |
$sousTaxons = $referentiel->recupererGenres(); |
$criteres = array(); |
foreach ($sousTaxons as $sousTaxon) { |
$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%"); |
} |
return $criteres; |
} |
private function construireWhereReferentiel() { |
$sql = ''; |
if (isset($this->criteresRecherche->referentiel)) { |
$referentielSource = Config::get('referentiel_source'); |
if (strstr($this->criteresRecherche->referentiel, $referentielSource) === false) { |
$sql = "AND 0"; |
} |
} |
return $sql; |
} |
private function construireWhereDepartement() { |
$sql = ''; |
if (isset($this->criteresRecherche->departement)) { |
$valeurs_a_proteger = $this->criteresRecherche->departement; |
foreach ($valeurs_a_proteger as $valeur) { |
$aProteger = $this->getBdd()->proteger($valeur . '%'); |
$valeurs_protegees[] = "lieu_commune_code_insee LIKE {$aProteger}"; |
} |
$valeurs = implode(' OR ', $valeurs_protegees); |
$sql = "AND ($valeurs)"; |
} |
return $sql; |
} |
private function construireWhereAuteur() { |
$sql = ''; |
if (isset($this->criteresRecherche->auteur)) { |
$auteur = $this->getBdd()->proteger($this->criteresRecherche->auteur); |
$sql = "AND observateur_nom_complet = $auteur"; |
} |
return $sql; |
} |
private function construireWhereDate() { |
$sql = ''; |
$dateDebut = isset($this->criteresRecherche->dateDebut) ? $this->criteresRecherche->dateDebut : null; |
$dateFin = isset($this->criteresRecherche->dateFin) ? $this->criteresRecherche->dateFin : null; |
if (!is_null($dateDebut) || !is_null($dateFin)) { |
$dateDebut = !is_null($dateDebut) ? substr($dateDebut, 0, 4) : null; |
$dateFin = !is_null($dateFin) ? substr($dateFin, 0, 4) : date('Y'); |
$condition = ''; |
if ($dateDebut == $dateFin) { |
$condition = "observation_date=".$dateDebut; |
} elseif (is_null($dateFin)) { |
$condition = "observation_date>=".$dateDebut; |
} elseif (is_null($dateDebut)) { |
$condition = "observation_date<=".$dateFin; |
} else { |
$condition = "observation_date BETWEEN ".$dateDebut." AND ".$dateFin; |
} |
$sql = "AND ($condition)"; |
} |
return $sql; |
} |
private function construireWhereCoordonneesBbox() { |
$sql = ''; |
if (isset($this->criteresRecherche->bbox)) { |
$bboxRecherche = $this->criteresRecherche->bbox; |
$conditions = array(); |
foreach ($bboxRecherche as $bbox) { |
$conditions[] = "(lieu_station_longitude BETWEEN ".$bbox['ouest']." AND ".$bbox['est']." ". |
"AND lieu_station_latitude BETWEEN ".$bbox['sud']." AND ".$bbox['nord'].")"; |
} |
$sql = 'AND ('.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_scientifique_complet {$filtre['operateur']} {$valeur}"; break; |
} |
} |
return $sql; |
} |
private function construireWhereCoordonneesPoint() { |
$sql = ''; |
$conditions = array(); |
foreach ($this->criteresRecherche->stations as $station) { |
if ($station[0] == $this->nomSource) { |
$longitude = str_replace(",", ".", strval($station[2])); |
$latitude = str_replace(",", ".", strval($station[3])); |
$conditions[] = "(lieu_station_latitude={$longitude} AND lieu_station_longitude={$latitude})"; |
} |
} |
if (count($conditions) > 0) { |
$sql = "AND (".implode(" OR ", $conditions).")"; |
} |
return $sql; |
} |
final protected function obtenirNomsStationsSurPoint() { |
$requete = "SELECT DISTINCTROW lieu_station_nom FROM {$this->nomSource}_tapir WHERE 1 ". |
$this->construireWhereTaxon().' '. |
$this->construireWhereCoordonneesPoint(); |
$stations = $this->getBdd()->recupererTous($requete); |
$nomsStations = array(); |
foreach ($stations as $station) { |
$nomsStations[] = $station['lieu_station_nom']; |
} |
return implode(', ', $nomsStations); |
} |
final protected function obtenirNombreStationsDansBbox() { |
$bbox = $this->criteresRecherche->bbox; |
$zoom = $this->criteresRecherche->zoom; |
$requete = |
"SELECT zoom, Sum(nombre_sites) AS total_points FROM mailles_{$this->nomSource} ". |
"WHERE zoom=".$zoom." ".$this->construireWhereMaillesBbox()." GROUP BY zoom"; |
$resultat = $this->getBdd()->recuperer($requete); |
return $resultat['total_points']; |
} |
final protected function recupererMaillesDansBbox() { |
$bbox = $this->criteresRecherche->bbox; |
$zoom = $this->criteresRecherche->zoom; |
$requete = |
"SELECT limite_sud AS sud, limite_ouest AS ouest, limite_est AS est, limite_nord AS nord, ". |
"nombre_sites AS stations, nombre_observations AS observations FROM mailles_{$this->nomSource} ". |
"WHERE zoom=".$zoom." ".$this->construireWhereMaillesBbox(); |
$mailles = $this->getBdd()->recupererTous($requete); |
// placer les totaux des nombres de stations dans des mailles vides |
$maillage = new Maillage($this->criteresRecherche->bbox, $zoom, $this->nomSource); |
$maillage->genererMaillesVides(); |
$maillage->ajouterMailles($mailles); |
return $maillage->formaterSortie(); |
} |
private function construireWhereMaillesBbox() { |
$bboxRecherche = $this->criteresRecherche->bbox; |
$conditions = array(); |
$sql = ''; |
foreach ($bboxRecherche as $bbox) { |
$conditions[] = "(limite_sud<=".$bbox['nord']." AND limite_nord>=".$bbox['sud']." ". |
"AND limite_ouest<=". $bbox['est']." AND limite_est>=".$bbox['ouest'].")"; |
} |
if (count($conditions) > 0) { |
$sql = 'AND ('.implode(' OR ', $conditions).')'; |
} |
return $sql; |
} |
} |
?> |
/trunk/services/modules/0.1/sources/FloradataFormateur.php |
---|
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; |
} |
} |
?> |