Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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