30,96 → 30,25 |
* |
*/ |
|
class SophyFormateur { |
class SophyFormateur extends Formateur { |
|
private $criteresRecherche; |
private $bdd; |
|
|
public function __construct($criteresRecherche) { |
$this->criteresRecherche = $criteresRecherche; |
} |
|
public function recupererStations() { |
$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(); |
} 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) { |
// generer un maillage a partir des stations recuperees dans la base de donnees |
$maillage = new Maillage($this->criteresRecherche->bbox, $zoom, 'sophy'); |
$maillage->genererMaillesVides(); |
$maillage->ajouterPoints($stations); |
$stations = $maillage->formaterSortie(); |
} |
} |
|
// mettre en forme les informations au format JSON |
$formateurJSON = new FormateurJson('sophy'); |
$donneesFormatees = $formateurJSON->formaterStations($stations); |
return $donneesFormatees; |
} |
|
public function recupererObservations() { |
$requeteSql = $this->construireRequeteObservations(); |
$observations = $this->getBdd()->recupererTous($requeteSql); |
$this->recupererNumeroNomenclaturauxTaxons($observations); |
$nomStation = $this->obtenirNomsStationsSurPoint(); |
$formateurJSON = new FormateurJson('sophy'); |
$donneesFormatees = $formateurJSON->formaterObservations($observations, $nomStation); |
return $donneesFormatees; |
} |
|
private function calculerNombreCriteresNonSpatiaux() { |
$nombreParametresNonSpatiaux = 0; |
$criteresSpatiaux = array('zoom', 'bbox', 'longitude', 'latitude'); |
foreach ($this->criteresRecherche as $nomCritere => $valeur) { |
if (!in_array($nomCritere, $criteresSpatiaux)) { |
$nombreParametresNonSpatiaux ++; |
} |
} |
return $nombreParametresNonSpatiaux; |
} |
|
|
|
// ------------------------------------------------------------------------ // |
// 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() { |
protected function construireRequeteStations() { |
$requete = |
"SELECT DISTINCT lieu_station_nom AS nom, lieu_station_latitude AS latitude, ". |
"lieu_station_longitude AS longitude, 'STATION' AS type_site ". |
"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 ". |
"FROM sophy_tapir WHERE 1 ". |
$this->construireWhereDepartement().' '. |
$this->construireWhereAuteur().' '. |
$this->construireWhereDate().' '. |
$this->construireWhereReferentiel().' '. |
$this->construireWhereTaxon().' '. |
$this->construireWhereCoordonneesBbox().' '. |
"GROUP BY lieu_station_latitude, lieu_station_longitude"; |
"GROUP BY lieu_station_longitude, lieu_station_latitude"; |
return $requete; |
} |
|
private function construireRequeteObservations() { |
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 ". |
126,6 → 55,7 |
"FROM sophy_tapir WHERE 1 ". |
$this->construireWhereAuteur().' '. |
$this->construireWhereReferentiel().' '. |
$this->construireWhereDate().' '. |
$this->construireWhereTaxon().' '. |
$this->construireWhereCoordonneesPoint().' '. |
"ORDER BY nom_scientifique_complet, date, observateur"; |
132,34 → 62,66 |
return $requete; |
} |
|
private function construireWhereTaxon() { |
protected function construireWhereTaxon() { |
$sql = ''; |
if (isset($this->criteresRecherche->taxon)) { |
$taxon = $this->criteresRecherche->taxon; |
$criteres = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%"); |
|
$referentiel = new Referentiel($this->criteresRecherche->referentiel, $this->criteresRecherche->taxon); |
$sousTaxons = $referentiel->recupererSousTaxons(); |
foreach ($sousTaxons as $sousTaxon) { |
$criteres .= " OR nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%"); |
$taxons = $this->criteresRecherche->taxon; |
$criteres = array(); |
foreach ($taxons as $taxon) { |
$nomRang = $this->getNomRang($taxon); |
$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($taxon['nom']."%"); |
if ($nomRang == 'espece') { |
$criteres = array_merge($criteres, $this->concatenerTaxonsSousEspeces($taxon)); |
} elseif ($nomRang == 'genre') { |
$criteres = array_merge($criteres, $this->concatenerTaxonsFamilles($taxon)); |
} |
} |
$sql = "AND ($criteres)"; |
$sql = "AND (".implode(' OR ',array_unique($criteres)).")"; |
} |
return $sql; |
} |
|
protected function concatenerTaxonsSousEspeces($taxon) { |
$referentiel = new Referentiel($this->criteresRecherche->referentiel, $taxon); |
$sousTaxons = $referentiel->recupererTaxonsSousEspeces(); |
$criteres = array(); |
foreach ($sousTaxons as $sousTaxon) { |
$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%"); |
} |
return $criteres; |
} |
|
protected function concatenerTaxonsFamilles($taxon) { |
$referentiel = new Referentiel($this->criteresRecherche->referentiel, $taxon); |
$sousTaxons = $referentiel->recupererTaxonsFamilles(); |
$criteres = array(); |
foreach ($sousTaxons as $sousTaxon) { |
$criteres[] = "nom_scientifique_complet LIKE ".$this->getBdd()->proteger($sousTaxon['nom']."%"); |
} |
return $criteres; |
} |
|
// TODO : completer le corps des methodes construire where pour referentiel et departement |
private function construireWhereReferentiel() { |
protected function construireWhereReferentiel() { |
$sql = ''; |
return $sql; |
} |
|
private function construireWhereDepartement() { |
protected 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() { |
protected function construireWhereAuteur() { |
$sql = ''; |
if (isset($this->criteresRecherche->auteur)) { |
$auteur = $this->getBdd()->proteger($this->criteresRecherche->auteur); |
167,8 → 129,30 |
} |
return $sql; |
} |
|
protected 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() { |
protected function construireWhereCoordonneesBbox() { |
$bbox = $this->criteresRecherche->bbox; |
$sql = "AND lieu_station_longitude BETWEEN ".$bbox['ouest']." AND ".$bbox['est']." ". |
"AND lieu_station_latitude BETWEEN ".$bbox['sud']." AND ".$bbox['nord']; |
175,13 → 159,13 |
return $sql; |
} |
|
private function construireWhereCoordonneesPoint() { |
protected function construireWhereCoordonneesPoint() { |
$sql = "AND lieu_station_latitude=".$this->criteresRecherche->latitude." ". |
"AND lieu_station_longitude=".$this->criteresRecherche->longitude; |
return $sql; |
} |
|
private function obtenirNomsStationsSurPoint() { |
protected function obtenirNomsStationsSurPoint() { |
$requete = "SELECT DISTINCTROW lieu_station_nom FROM sophy_tapir WHERE 1 ". |
$this->construireWhereTaxon().' '. |
$this->construireWhereCoordonneesPoint(); |
193,7 → 177,7 |
return implode(', ', $nomsStations); |
} |
|
private function obtenirNombreStationsDansBbox() { |
protected function obtenirNombreStationsDansBbox() { |
$bbox = $this->criteresRecherche->bbox; |
$zoom = $this->criteresRecherche->zoom; |
$requete = |
204,60 → 188,23 |
return $resultat['total_points']; |
} |
|
private function recupererMaillesDansBbox() { |
protected function recupererMaillesDansBbox() { |
$bbox = $this->criteresRecherche->bbox; |
$zoom = $this->criteresRecherche->zoom; |
$requete = |
"SELECT zoom, position_latitude, position_longitude, limite_sud AS sud, limite_ouest AS ouest, ". |
"limite_nord AS nord, limite_est AS est, nombre_sites AS points, 'MAILLE' AS type_site ". |
"FROM mailles_sophy WHERE zoom=".$zoom." AND limite_sud<=".$bbox['nord']." ". |
"SELECT limite_sud AS latitudeSud, limite_ouest AS longitudeOuest, limite_est AS longitudeEst, ". |
"limite_nord AS latitudeNord, nombre_sites, nombre_observations FROM mailles_sophy ". |
"WHERE zoom=".$zoom." AND limite_sud<=".$bbox['nord']." ". |
"AND limite_nord>=".$bbox['sud']." AND limite_ouest<=". $bbox['est']." ". |
"AND limite_est>=".$bbox['ouest']; |
return $this->getBdd()->recupererTous($requete); |
$mailles = $this->getBdd()->recupererTous($requete, Bdd::MODE_OBJET); |
// 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 recupererNumeroNomenclaturauxTaxons(& $observations) { |
for ($index = 0; $index < count($observations); $index ++) { |
$taxon = isset($this->criteresRecherche->taxon) ? $this->criteresRecherche->taxon : null; |
|
if (is_null($taxon) && strlen(trim($observations[$index]['nomSci'])) > 0) { |
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'] = ''; |
} |
} |
} |
|
private function rechercherTaxonDansReferentiel($nomScientifique, $nomReferentiel) { |
$referentiel = new Referentiel($nomReferentiel); |
$taxon = $referentiel->obtenirNumeroNomenclatural($nomScientifique); |
return $taxon; |
} |
|
private function obtenirNumeroTaxon($nomScientifique) { |
$taxonTrouve = null; |
$listeReferentiels = Referentiel::recupererListeReferentielsDisponibles(); |
foreach ($listeReferentiels as $nomReferentiel) { |
$taxon = $this->rechercherTaxonDansReferentiel($nomScientifique, $nomReferentiel); |
if (!is_null($taxon)) { |
$taxonTrouve = $taxon; |
break; |
} |
} |
return $taxonTrouve; |
} |
|
|
} |
|
?> |