Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 970 | Rev 998 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 970 Rev 971
1
<?php
1
<?php
2
 
2
 
3
class SourceDonnees {
3
class SourceDonnees {
4
	
4
	
5
	private $bdd = null;
5
	private $bdd = null;
6
	private $limitesCarte = '';
6
	private $limitesCarte = '';
7
	private $taxon = array();
7
	private $taxon = array();
8
	
8
	
9
	private $nomRang = '';
9
	private $nomRang = '';
10
	private $taxons = array();
10
	private $taxons = array();
11
	private $genres = array();
11
	private $genres = array();
12
	private $utilisateurs = null;
12
	private $utilisateurs = null;
13
	
13
	
14
	
14
	
15
	public function __construct($limitesCarte, $taxon) {
15
	public function __construct($limitesCarte, $taxon) {
16
		$this->limitesCarte = $limitesCarte;
16
		$this->limitesCarte = $limitesCarte;
17
		foreach ($this->limitesCarte as $bord => $valeur) {
17
		foreach ($this->limitesCarte as $bord => $valeur) {
18
			$this->limitesCarte[$bord] = str_replace(",", ".", round($valeur, 6));
18
			$this->limitesCarte[$bord] = str_replace(",", ".", round($valeur, 6));
19
		}
19
		}
20
		$this->bdd = new Bdd();
20
		$this->bdd = new Bdd();
21
		$this->taxon = $taxon;
21
		$this->taxon = $taxon;
22
		$this->nomRang = $this->obtenirNomRang();
22
		$this->nomRang = $this->obtenirNomRang();
23
		if ($this->nomRang == 'espece' || $this->nomRang == 'sous_espece') {
23
		if ($this->nomRang == 'espece' || $this->nomRang == 'sous_espece') {
24
			$this->taxons = $this->recupererSynonymesEtSousEspeces();
24
			$this->taxons = $this->recupererSynonymesEtSousEspeces();
25
		} elseif ($this->nomRang == 'famille') {
25
		} elseif ($this->nomRang == 'famille') {
26
			$this->genres = $this->recupererGenres();
26
			$this->genres = $this->recupererGenres();
27
		}
27
		}
28
		$this->utilisateurs = new Utilisateurs();
28
		$this->utilisateurs = new Utilisateurs();
29
	}
29
	}
30
	
30
	
31
	private function obtenirNomRang() {
31
	private function obtenirNomRang() {
32
		$nomsRangs = array('famille', 'genre', 'espece', 'sous_espece');
32
		$nomsRangs = array('famille', 'genre', 'espece', 'sous_espece');
33
		$rangs = explode(',', Config::get('rangs'));
33
		$rangs = explode(',', Config::get('rangs'));
34
		for ($index = 0; $index < count($nomsRangs) && $rangs[$index] != $this->taxon['rang']; $index ++);
34
		for ($index = 0; $index < count($nomsRangs) && $rangs[$index] != $this->taxon['rang']; $index ++);
35
		$position = $index == count($nomsRangs) ? count($nomsRangs)-1 : $index;
35
		$position = $index == count($nomsRangs) ? count($nomsRangs)-1 : $index;
36
		return $nomsRangs[$position];
36
		return $nomsRangs[$position];
37
	}
37
	}
38
	
38
	
39
	protected function recupererSynonymesEtSousEspeces() {
39
	protected function recupererSynonymesEtSousEspeces() {
40
		$requete =
40
		$requete =
41
		"SELECT num_nom, nom_sci, nom_complet, num_taxonomique FROM bdtfx_v1_01 WHERE hierarchie LIKE '%-{$this->taxon['num_nom']}-%' ".
41
		"SELECT num_nom, nom_sci, nom_complet, num_taxonomique FROM ".Config::get('bdd_table_referentiel').
-
 
42
		" WHERE hierarchie LIKE '%-{$this->taxon['num_nom']}-%' ".
42
		"OR num_taxonomique = {$this->taxon['num_taxonomique']}";
43
		"OR num_nom_retenu = {$this->taxon['num_nom_retenu']}";
43
		return $this->bdd->recupererTous($requete);
44
		return $this->bdd->recupererTous($requete);
44
	}
45
	}
45
	
46
	
46
	protected function recupererGenres() {
47
	protected function recupererGenres() {
47
		$this->bdd->requeter("USE ".Config::get('bdd_nom'));
48
		$this->bdd->requeter("USE ".Config::get('bdd_nom'));
48
		$requete =
49
		$requete =
49
		"SELECT num_nom, nom_sci, num_taxonomique FROM bdtfx_v1_01 WHERE rang=220 AND num_tax_sup={$this->taxon['num_nom']}";
50
		"SELECT num_nom, nom_sci, num_taxonomique FROM ".Config::get('bdd_table_referentiel').
-
 
51
		" WHERE rang=220 AND num_tax_sup={$this->taxon['num_nom']}";
50
		return $this->bdd->recupererTous($requete);
52
		return $this->bdd->recupererTous($requete);
51
	}
53
	}
52
	
54
	
53
	public function recupererStationsFloradata() {
55
	public function recupererStationsFloradata() {
54
		$this->bdd->requeter("USE ".Config::get('bdd_nom_floradata'));
56
		$this->bdd->requeter("USE ".Config::get('bdd_nom_floradata'));
55
		$requete =
57
		$requete =
56
		"SELECT DISTINCTROW zone_geo AS commune, Date(date_observation) AS date, Floor(wgs84_latitude*10)/10 AS lat, ".
58
		"SELECT DISTINCTROW zone_geo AS commune, Date(date_observation) AS date, Floor(wgs84_latitude*10)/10 AS lat, ".
57
		"Floor(wgs84_longitude*10)/10 AS lng, courriel_utilisateur AS auteur ".
59
		"Floor(wgs84_longitude*10)/10 AS lng, courriel_utilisateur AS auteur ".
58
		"FROM cel_obs LEFT JOIN cel_zones_geo cz ON ce_zone_geo=id_zone_geo ".
60
		"FROM cel_obs LEFT JOIN cel_zones_geo cz ON ce_zone_geo=id_zone_geo ".
59
		"WHERE ".$this->construireWhereTaxonFloradata()." AND transmission=1 AND ".
61
		"WHERE ".$this->construireWhereTaxonFloradata()." AND transmission=1 AND nom_referentiel = '".Config::get('referentielsDispo')."' AND ".
60
		"wgs84_longitude BETWEEN ".$this->limitesCarte['ouest']." AND ".$this->limitesCarte['est']." ".
62
		"wgs84_longitude BETWEEN ".$this->limitesCarte['ouest']." AND ".$this->limitesCarte['est']." ".
61
		"AND wgs84_latitude BETWEEN ".$this->limitesCarte['sud']." AND ".$this->limitesCarte['nord']." ".
63
		"AND wgs84_latitude BETWEEN ".$this->limitesCarte['sud']." AND ".$this->limitesCarte['nord']." ".
62
		"AND date_observation<>'0000-00-00 00-00-00' ORDER BY lat DESC, lng ASC, commune, date";
64
		"AND date_observation<>'0000-00-00 00-00-00' ORDER BY lat DESC, lng ASC, commune, date";
63
		$stations = $this->bdd->recupererTous($requete);
65
		$stations = $this->bdd->recupererTous($requete);
64
		
66
		
65
		$this->extraireIdentitesAuteurs($stations);
67
		$this->extraireIdentitesAuteurs($stations);
66
		foreach($stations as &$station) {
68
		foreach($stations as &$station) {
67
			$station['auteur'] = $this->utilisateurs->getIntitule($station['auteur']);
69
			$station['auteur'] = $this->utilisateurs->getIntitule($station['auteur']);
68
		}	
70
		}	
69
		return $stations;
71
		return $stations;
70
	}
72
	}
71
	
73
	
72
	private function extraireIdentitesAuteurs($stations) {
74
	private function extraireIdentitesAuteurs($stations) {
73
		$courriels = array();
75
		$courriels = array();
74
		foreach ($stations as &$station) {
76
		foreach ($stations as &$station) {
75
			$courriels[] = $station['auteur'];
77
			$courriels[] = $station['auteur'];
76
		}
78
		}
77
		$this->utilisateurs->setCourriels($courriels);
79
		$this->utilisateurs->setCourriels($courriels);
78
		$this->utilisateurs->chargerIdentites();
80
		$this->utilisateurs->chargerIdentites();
79
	}
81
	}
80
	
82
	
81
	private function construireWhereTaxonFloradata() {
83
	private function construireWhereTaxonFloradata() {
82
		$criteres = array();
84
		$criteres = array();
83
		$nomRang = $this->obtenirNomRang($this->taxon);
85
		$nomRang = $this->obtenirNomRang($this->taxon);
84
		if ($this->nomRang == 'famille') {
86
		if ($this->nomRang == 'famille') {
85
			$criteres[] = "famille=".$this->bdd->proteger($this->taxon['nom_sci']);
87
			$criteres[] = "famille=".$this->bdd->proteger($this->taxon['nom_sci']);
86
		} elseif ($this->nomRang == 'genre') {
88
		} elseif ($this->nomRang == 'genre') {
87
			$criteres[] = "nom_sel LIKE ".$this->bdd->proteger($this->taxon['nom_sci'].'%');
89
			$criteres[] = "nom_sel LIKE ".$this->bdd->proteger($this->taxon['nom_sci'].'%');
88
		} else {
90
		} else {
89
			$taxons = array($this->taxon['num_taxonomique']);
91
			$taxons = array($this->taxon['num_nom']);
90
			foreach ($this->taxons as $sousTaxon) {
92
			foreach ($this->taxons as $sousTaxon) {
91
				$taxons[] = $sousTaxon['num_taxonomique'];
93
				$taxons[] = $sousTaxon['num_nom'];
92
			}
94
			}
93
			$criteres[] = "nt IN (".implode(',', array_unique($taxons))	.")";
95
			$criteres[] = "nom_sel_nn IN (".implode(',', array_unique($taxons))	.")";
94
		}
96
		}
95
		return "(".implode(' OR ',array_unique($criteres)).")";
97
		return "(".implode(' OR ',array_unique($criteres)).")";
96
	}
98
	}
97
	
99
	
98
	public function recupererStationsMoissonnage($source) {
100
	public function recupererStationsMoissonnage($source) {
99
		$this->bdd->requeter("USE ".Config::get('bdd_nom'));
101
		$this->bdd->requeter("USE ".Config::get('bdd_nom'));
100
		$requete =
102
		$requete =
101
		"SELECT DISTINCTROW lieu_commune_code_insee, observation_date AS date, observateur_nom_complet AS auteur ".
103
		"SELECT DISTINCTROW lieu_commune_code_insee, observation_date AS date, observateur_nom_complet AS auteur ".
102
		"FROM {$source}_tapir WHERE ".$this->construireWhereTaxonMoissonnage($source)." ".
104
		"FROM {$source}_tapir WHERE ".$this->construireWhereTaxonMoissonnage($source)." ".
103
		"AND lieu_station_longitude BETWEEN ".$this->limitesCarte['ouest']." AND ".$this->limitesCarte['est']." ".
105
		"AND lieu_station_longitude BETWEEN ".$this->limitesCarte['ouest']." AND ".$this->limitesCarte['est']." ".
104
		"AND lieu_station_latitude BETWEEN ".$this->limitesCarte['sud']." AND ".$this->limitesCarte['nord']." ".
106
		"AND lieu_station_latitude BETWEEN ".$this->limitesCarte['sud']." AND ".$this->limitesCarte['nord']." ".
105
		"AND Length(lieu_commune_code_insee)=5 ORDER BY lieu_commune_code_insee, date"." -- " . __FILE__ . ":" . __LINE__." ". @$_SERVER['REQUEST_URI'];
107
		"AND Length(lieu_commune_code_insee)=5 ORDER BY lieu_commune_code_insee, date"." -- " . __FILE__ . ":" . __LINE__." ". @$_SERVER['REQUEST_URI'];
106
		$stations = $this->bdd->recupererTous($requete);
108
		$stations = $this->bdd->recupererTous($requete);
107
		$this->rechercherInfosCommune($stations);
109
		$this->rechercherInfosCommune($stations);
108
		return $stations;
110
		return $stations;
109
	}
111
	}
110
	
112
	
111
	private function construireWhereTaxonMoissonnage($source) {
113
	private function construireWhereTaxonMoissonnage($source) {
112
		$nomRang = $this->obtenirNomRang();
114
		$nomRang = $this->obtenirNomRang();
113
		$criteres = array();
115
		$criteres = array();
114
 
116
 
115
		$nom = ($source == "baznat") ? implode('%',explode(' ',$this->taxon['nom_complet'] )) : $this->taxon['nom_sci'] ;
117
		$nom = ($source == "baznat") ? implode('%',explode(' ',$this->taxon['nom_complet'] )) : $this->taxon['nom_sci'] ;
116
		$criteres[] = "nom_scientifique_complet LIKE ".$this->bdd->proteger($nom);
118
		$criteres[] = "nom_scientifique_complet LIKE ".$this->bdd->proteger($nom);
117
		if ($this->nomRang == 'espece' || $this->nomRang == 'sous_espece') {
119
		if ($this->nomRang == 'espece' || $this->nomRang == 'sous_espece') {
118
			foreach ($this->taxons as $sousTaxon) {
120
			foreach ($this->taxons as $sousTaxon) {
119
				$nom = ($source == "baznat") ? implode('%',explode(' ',$sousTaxon['nom_complet'] )) : $sousTaxon['nom_sci'];
121
				$nom = ($source == "baznat") ? implode('%',explode(' ',$sousTaxon['nom_complet'] )) : $sousTaxon['nom_sci'];
120
				$criteres[] = "nom_scientifique_complet LIKE ".$this->bdd->proteger($nom);
122
				$criteres[] = "nom_scientifique_complet LIKE ".$this->bdd->proteger($nom);
121
			}
123
			}
122
		} elseif ($this->nomRang == 'famille') {
124
		} elseif ($this->nomRang == 'famille') {
123
			foreach ($this->genres as $genre) {
125
			foreach ($this->genres as $genre) {
124
				$criteres[] = "nom_scientifique_complet LIKE ".$this->bdd->proteger($genre['nom_sci']."%");
126
				$criteres[] = "nom_scientifique_complet LIKE ".$this->bdd->proteger($genre['nom_sci']."%");
125
			}
127
			}
126
		}
128
		}
127
		return "(".implode(' OR ',array_unique($criteres)).")";
129
		return "(".implode(' OR ',array_unique($criteres)).")";
128
	}
130
	}
129
	
131
	
130
	private function rechercherInfosCommune(& $stations) {
132
	private function rechercherInfosCommune(& $stations) {
131
		$codesInsee = array();
133
		$codesInsee = array();
132
		foreach ($stations as $station) {
134
		foreach ($stations as $station) {
133
			$codeInsee = $station['lieu_commune_code_insee'];
135
			$codeInsee = $station['lieu_commune_code_insee'];
134
			if (substr($codeInsee, 0, 2) == '20') {
136
			if (substr($codeInsee, 0, 2) == '20') {
135
				$codeInsee  = '2A'.substr($codeInsee, 2);
137
				$codeInsee  = '2A'.substr($codeInsee, 2);
136
				$codeInsee2 = '2B'.substr($codeInsee, 2);
138
				$codeInsee2 = '2B'.substr($codeInsee, 2);
137
			}
139
			}
138
			if (!in_array($codeInsee, $codesInsee)) {
140
			if (!in_array($codeInsee, $codesInsee)) {
139
				if (substr($codeInsee, 0, 2) == '20') {
141
				if (substr($codeInsee, 0, 2) == '20') {
140
					$codesInsee[] = "'$codeInsee2'";
142
					$codesInsee[] = "'$codeInsee2'";
141
				}
143
				}
142
				$codesInsee[] = "'$codeInsee'";
144
				$codesInsee[] = "'$codeInsee'";
143
			}
145
			}
144
		}
146
		}
145
		$nomTableCommunes = Config::get('bdd_table_communes');
147
		$nomTableCommunes = Config::get('bdd_table_communes');
146
		$communes = array();
148
		$communes = array();
147
		if(count($codesInsee) > 0) {
149
		if(count($codesInsee) > 0) {
148
			$requete =
150
			$requete =
149
			"SELECT insee, nom AS commune, Floor(latitude_degre*10)/10 AS lat, Floor(longitude_degre*10)/10 AS lng ".
151
			"SELECT insee, nom AS commune, Floor(latitude_degre*10)/10 AS lat, Floor(longitude_degre*10)/10 AS lng ".
150
			"FROM $nomTableCommunes WHERE insee IN (".implode(',', array_unique($codesInsee)).") ORDER BY insee";
152
			"FROM $nomTableCommunes WHERE insee IN (".implode(',', array_unique($codesInsee)).") ORDER BY insee";
151
			$communes = $this->bdd->recupererTous($requete);
153
			$communes = $this->bdd->recupererTous($requete);
152
		}
154
		}
153
		$indexStation = 0;
155
		$indexStation = 0;
154
		foreach ($communes as $commune) {
156
		foreach ($communes as $commune) {
155
			$codeInsee = $commune['insee'];
157
			$codeInsee = $commune['insee'];
156
			if (substr($codeInsee, 0, 2) == '2A' || substr($codeInsee, 0, 2) == '2B') {
158
			if (substr($codeInsee, 0, 2) == '2A' || substr($codeInsee, 0, 2) == '2B') {
157
				$codeInsee = '20'.substr($codeInsee, 2);
159
				$codeInsee = '20'.substr($codeInsee, 2);
158
			}
160
			}
159
			while ($stations[$indexStation]['lieu_commune_code_insee'] < $codeInsee) {
161
			while ($stations[$indexStation]['lieu_commune_code_insee'] < $codeInsee) {
160
				$indexStation ++;
162
				$indexStation ++;
161
			}
163
			}
162
			if ($stations[$indexStation]['lieu_commune_code_insee'] == $codeInsee) {
164
			if ($stations[$indexStation]['lieu_commune_code_insee'] == $codeInsee) {
163
				$stations[$indexStation]['lat'] = $commune['lat'];
165
				$stations[$indexStation]['lat'] = $commune['lat'];
164
				$stations[$indexStation]['lng'] = $commune['lng'];
166
				$stations[$indexStation]['lng'] = $commune['lng'];
165
				$stations[$indexStation]['commune'] = $commune['commune'];
167
				$stations[$indexStation]['commune'] = $commune['commune'];
166
			}
168
			}
167
		}
169
		}
168
		
170
		
169
		$lat = array();
171
		$lat = array();
170
		$lng = array();
172
		$lng = array();
171
		foreach ($stations as $index => $station) {
173
		foreach ($stations as $index => $station) {
172
			if (!isset($station['lat'])) {
174
			if (!isset($station['lat'])) {
173
				$station['lat'] = -100;
175
				$station['lat'] = -100;
174
				$station['lng'] = -100;
176
				$station['lng'] = -100;
175
			}
177
			}
176
			$lat[$index] = $station['lat'];
178
			$lat[$index] = $station['lat'];
177
			$lng[$index] = $station['lng'];
179
			$lng[$index] = $station['lng'];
178
		}
180
		}
179
		array_multisort($lat, SORT_DESC, $lng, SORT_ASC, $stations);
181
		array_multisort($lat, SORT_DESC, $lng, SORT_ASC, $stations);
180
	}
182
	}
181
	
183
	
182
}
184
}
183
 
185
 
184
?>
186
?>