Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 727 | Rev 750 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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