Subversion Repositories eFlore/Applications.moissonnage

Compare Revisions

Ignore whitespace Rev 30 → Rev 31

/trunk/services/modules/0.1/sources/Formateur.php
New file
0,0 → 1,149
<?php
 
abstract class Formateur {
protected $criteresRecherche;
protected $bdd;
protected $nomSource = '';
public function __construct($criteresRecherche) {
$this->criteresRecherche = $criteresRecherche;
$this->nomSource = Config::get('nom_source');
}
public function recupererStations() {
$stations = null;
if ($this->estTraitementRecuperationAutorise()) {
$stations = $this->traiterRecupererStations();
}
return $stations;
}
public function recupererObservations() {
$stations = null;
if ($this->estTraitementRecuperationAutorise()) {
$stations = $this->traiterRecupererObservations();
}
return $stations;
}
protected function estTraitementRecuperationAutorise() {
return (!(
isset($this->criteresRecherche->nbJours) ||
(isset($this->criteresRecherche->referentiel) &&
$this->criteresRecherche->referentiel != Config::get('referentiel_source'))
));
}
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();
} 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();
}
}
$formateurJSON = new FormateurJson($this->nomSource);
$donneesFormatees = $formateurJSON->formaterStations($stations);
return $donneesFormatees;
}
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');
foreach ($this->criteresRecherche as $nomCritere => $valeur) {
if (!in_array($nomCritere, $criteresAIgnorer)) {
echo $nomCritere.chr(13);
$nombreParametresNonSpatiaux ++;
}
}
return $nombreParametresNonSpatiaux;
}
protected function getBdd() {
if (!isset($this->bdd)) {
$this->bdd = new Bdd();
}
$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) {
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'] = '';
}
}
}
}
}
protected function rechercherTaxonDansReferentiel($nomScientifique, $nomReferentiel) {
$referentiel = new Referentiel($nomReferentiel);
$taxon = $referentiel->obtenirNumeroNomenclatural($nomScientifique);
return $taxon;
}
protected 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;
}
}
 
?>