31 |
alex |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
abstract class Formateur {
|
|
|
4 |
|
|
|
5 |
protected $criteresRecherche;
|
|
|
6 |
protected $bdd;
|
|
|
7 |
protected $nomSource = '';
|
|
|
8 |
|
|
|
9 |
|
|
|
10 |
public function __construct($criteresRecherche) {
|
|
|
11 |
$this->criteresRecherche = $criteresRecherche;
|
|
|
12 |
$this->nomSource = Config::get('nom_source');
|
|
|
13 |
}
|
|
|
14 |
|
|
|
15 |
public function recupererStations() {
|
|
|
16 |
$stations = null;
|
|
|
17 |
if ($this->estTraitementRecuperationAutorise()) {
|
|
|
18 |
$stations = $this->traiterRecupererStations();
|
|
|
19 |
}
|
|
|
20 |
return $stations;
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
public function recupererObservations() {
|
|
|
24 |
$stations = null;
|
|
|
25 |
if ($this->estTraitementRecuperationAutorise()) {
|
|
|
26 |
$stations = $this->traiterRecupererObservations();
|
|
|
27 |
}
|
|
|
28 |
return $stations;
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
protected function estTraitementRecuperationAutorise() {
|
|
|
32 |
return (!(
|
|
|
33 |
isset($this->criteresRecherche->nbJours) ||
|
|
|
34 |
(isset($this->criteresRecherche->referentiel) &&
|
|
|
35 |
$this->criteresRecherche->referentiel != Config::get('referentiel_source'))
|
|
|
36 |
));
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
protected function traiterRecupererStations() {
|
|
|
40 |
$stations = array();
|
|
|
41 |
$nombreStations = $this->obtenirNombreStationsDansBbox();
|
|
|
42 |
$seuilMaillage = Config::get('seuil_maillage');
|
|
|
43 |
$nombreParametresNonSpatiaux = $this->calculerNombreCriteresNonSpatiaux();
|
|
|
44 |
if ($nombreStations >= $seuilMaillage && $nombreParametresNonSpatiaux == 0) {
|
|
|
45 |
// pas besoin de rechercher les stations correspondant a ces criteres
|
|
|
46 |
// recuperer les mailles se trouvant dans l'espace de recherche demande
|
|
|
47 |
$stations = $this->recupererMaillesDansBbox();
|
|
|
48 |
} else {
|
|
|
49 |
$requeteSql = $this->construireRequeteStations();
|
|
|
50 |
$stations = $this->getBdd()->recupererTous($requeteSql);
|
|
|
51 |
$zoom = $this->criteresRecherche->zoom;
|
|
|
52 |
$zoomMaxMaillage = Config::get('zoom_maximal_maillage');
|
|
|
53 |
if ($zoom <= $zoomMaxMaillage && count($stations) >= $seuilMaillage) {
|
|
|
54 |
$maillage = new Maillage($this->criteresRecherche->bbox, $zoom, $this->nomSource);
|
|
|
55 |
$maillage->genererMaillesVides();
|
|
|
56 |
$maillage->ajouterPoints($stations);
|
|
|
57 |
$stations = $maillage->formaterSortie();
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
$formateurJSON = new FormateurJson($this->nomSource);
|
|
|
61 |
$donneesFormatees = $formateurJSON->formaterStations($stations);
|
|
|
62 |
return $donneesFormatees;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
protected function traiterRecupererObservations() {
|
|
|
66 |
$requeteSql = $this->construireRequeteObservations();
|
|
|
67 |
$observations = $this->getBdd()->recupererTous($requeteSql);
|
|
|
68 |
$this->recupererNumeroNomenclaturauxTaxons($observations);
|
|
|
69 |
$nomStation = $this->obtenirNomsStationsSurPoint();
|
|
|
70 |
$formateurJSON = new FormateurJson($this->nomSource);
|
|
|
71 |
$donneesFormatees = $formateurJSON->formaterObservations($observations, $nomStation);
|
|
|
72 |
return $donneesFormatees;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
protected function calculerNombreCriteresNonSpatiaux() {
|
|
|
76 |
$nombreParametresNonSpatiaux = 0;
|
|
|
77 |
$criteresAIgnorer = array('zoom', 'bbox', 'longitude', 'latitude', 'referentiel');
|
|
|
78 |
foreach ($this->criteresRecherche as $nomCritere => $valeur) {
|
|
|
79 |
if (!in_array($nomCritere, $criteresAIgnorer)) {
|
|
|
80 |
echo $nomCritere.chr(13);
|
|
|
81 |
$nombreParametresNonSpatiaux ++;
|
|
|
82 |
}
|
|
|
83 |
}
|
|
|
84 |
return $nombreParametresNonSpatiaux;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
protected function getBdd() {
|
|
|
88 |
if (!isset($this->bdd)) {
|
|
|
89 |
$this->bdd = new Bdd();
|
|
|
90 |
}
|
|
|
91 |
$this->bdd->requeter("USE ".Config::get('bdd_nom'));
|
|
|
92 |
return $this->bdd;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
protected function getNomRang($taxon) {
|
|
|
96 |
$nomsRangs = array('famille', 'genre', 'espece', 'sous_espece');
|
|
|
97 |
for ($index = 0; $index < count($nomsRangs)
|
|
|
98 |
&& Config::get("rang.".$nomsRangs[$index]) != $taxon['rang']; $index ++);
|
|
|
99 |
$position = $index == count($nomsRangs) ? count($nomsRangs)-1 : $index;
|
|
|
100 |
return $nomsRangs[$position];
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
protected function recupererNumeroNomenclaturauxTaxons(& $observations) {
|
|
|
104 |
for ($index = 0; $index < count($observations); $index ++) {
|
|
|
105 |
$taxons = isset($this->criteresRecherche->taxon) ? $this->criteresRecherche->taxon : null;
|
|
|
106 |
if (!is_null($taxons)) {
|
|
|
107 |
foreach ($taxons as $taxon) {
|
|
|
108 |
if ($observations[$index]['nomSci'] == 0) {
|
|
|
109 |
continue;
|
|
|
110 |
}
|
|
|
111 |
if (isset($this->criteresRecherche->taxon)) {
|
|
|
112 |
$taxon = $this->rechercherTaxonDansReferentiel($observations[$index]['nomSci'],
|
|
|
113 |
$this->criteresRecherche->referentiel);
|
|
|
114 |
} else {
|
|
|
115 |
$taxon = $this->obtenirNumeroTaxon($observations[$index]['nomSci']);
|
|
|
116 |
}
|
|
|
117 |
if (!is_null($taxon)) {
|
|
|
118 |
$observations[$index]['nn'] = $taxon['nn'];
|
|
|
119 |
$observations[$index]['num_referentiel'] = $taxon['referentiel'];
|
|
|
120 |
} else {
|
|
|
121 |
$observations[$index]['nn'] = '';
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
protected function rechercherTaxonDansReferentiel($nomScientifique, $nomReferentiel) {
|
|
|
129 |
$referentiel = new Referentiel($nomReferentiel);
|
|
|
130 |
$taxon = $referentiel->obtenirNumeroNomenclatural($nomScientifique);
|
|
|
131 |
return $taxon;
|
|
|
132 |
}
|
|
|
133 |
|
|
|
134 |
protected function obtenirNumeroTaxon($nomScientifique) {
|
|
|
135 |
$taxonTrouve = null;
|
|
|
136 |
$listeReferentiels = Referentiel::recupererListeReferentielsDisponibles();
|
|
|
137 |
foreach ($listeReferentiels as $nomReferentiel) {
|
|
|
138 |
$taxon = $this->rechercherTaxonDansReferentiel($nomScientifique, $nomReferentiel);
|
|
|
139 |
if (!is_null($taxon)) {
|
|
|
140 |
$taxonTrouve = $taxon;
|
|
|
141 |
break;
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
return $taxonTrouve;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
?>
|