Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

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