Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 31 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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