Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 31 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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