| 727 | alex | 1 | <?php
 | 
        
           |  |  | 2 |   | 
        
           | 747 | alex | 3 | class SourceDonnees {
 | 
        
           | 727 | alex | 4 |   | 
        
           | 747 | alex | 5 | 	private $bdd = null;
 | 
        
           |  |  | 6 | 	private $limitesCarte = '';
 | 
        
           |  |  | 7 | 	private $taxon = array();
 | 
        
           | 727 | alex | 8 |   | 
        
           | 747 | alex | 9 | 	private $nomRang = '';
 | 
        
           |  |  | 10 | 	private $taxons = array();
 | 
        
           |  |  | 11 | 	private $genres = array();
 | 
        
           | 727 | alex | 12 |   | 
        
           | 747 | alex | 13 |   | 
        
           |  |  | 14 | 	public function __construct($limitesCarte, $taxon) {
 | 
        
           | 727 | alex | 15 | 		$this->limitesCarte = $limitesCarte;
 | 
        
           |  |  | 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;
 | 
        
           | 747 | alex | 21 | 		$this->nomRang = $this->obtenirNomRang();
 | 
        
           |  |  | 22 | 		if ($this->nomRang == 'espece' || $this->nomRang == 'sous_espece') {
 | 
        
           |  |  | 23 | 			$this->taxons = $this->recupererSynonymesEtSousEspeces();
 | 
        
           |  |  | 24 | 		} elseif ($this->nomRang == 'famille') {
 | 
        
           |  |  | 25 | 			$this->genres = $this->recupererGenres();
 | 
        
           |  |  | 26 | 		}
 | 
        
           | 727 | alex | 27 | 	}
 | 
        
           |  |  | 28 |   | 
        
           | 747 | alex | 29 | 	private function obtenirNomRang() {
 | 
        
           | 727 | alex | 30 | 		$nomsRangs = array('famille', 'genre', 'espece', 'sous_espece');
 | 
        
           |  |  | 31 | 		$rangs = explode(',', Config::get('rangs'));
 | 
        
           |  |  | 32 | 		for ($index = 0; $index < count($nomsRangs) && $rangs[$index] != $this->taxon['rang']; $index ++);
 | 
        
           |  |  | 33 | 		$position = $index == count($nomsRangs) ? count($nomsRangs)-1 : $index;
 | 
        
           |  |  | 34 | 		return $nomsRangs[$position];
 | 
        
           |  |  | 35 | 	}
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 | 	protected function recupererSynonymesEtSousEspeces() {
 | 
        
           |  |  | 38 | 		$requete =
 | 
        
           |  |  | 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']}";
 | 
        
           |  |  | 41 | 		return $this->bdd->recupererTous($requete);
 | 
        
           |  |  | 42 | 	}
 | 
        
           |  |  | 43 |   | 
        
           |  |  | 44 | 	protected function recupererGenres() {
 | 
        
           |  |  | 45 | 		$this->bdd->requeter("USE ".Config::get('bdd_nom'));
 | 
        
           |  |  | 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']}";
 | 
        
           |  |  | 48 | 		return $this->bdd->recupererTous($requete);
 | 
        
           |  |  | 49 | 	}
 | 
        
           |  |  | 50 |   | 
        
           | 747 | alex | 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 | 		}
 | 
        
           | 750 | alex | 125 | 		$nomTableCommunes = Config::get('bdd_nom_communes');
 | 
        
           | 747 | alex | 126 | 		$requete =
 | 
        
           |  |  | 127 | 		"SELECT insee, nom AS commune, Floor(latitude_degre*10)/10 AS lat, Floor(longitude_degre*10)/10 AS lng ".
 | 
        
           | 750 | alex | 128 | 		"FROM $nomTableCommunes WHERE insee IN (".implode(',', array_unique($codesInsee)).") ORDER BY insee";
 | 
        
           | 747 | alex | 129 | 		$communes = $this->bdd->recupererTous($requete);
 | 
        
           |  |  | 130 |   | 
        
           |  |  | 131 | 		$indexStation = 0;
 | 
        
           |  |  | 132 | 		foreach ($communes as $commune) {
 | 
        
           |  |  | 133 | 			$codeInsee = $commune['insee'];
 | 
        
           |  |  | 134 | 			if (substr($codeInsee, 0, 2) == '2A' || substr($codeInsee, 0, 2) == '2B') {
 | 
        
           |  |  | 135 | 				$codeInsee = '20'.substr($codeInsee, 2);
 | 
        
           |  |  | 136 | 			}
 | 
        
           |  |  | 137 | 			while ($stations[$indexStation]['lieu_commune_code_insee'] < $codeInsee) {
 | 
        
           |  |  | 138 | 				$indexStation ++;
 | 
        
           |  |  | 139 | 			}
 | 
        
           |  |  | 140 | 			if ($stations[$indexStation]['lieu_commune_code_insee'] == $codeInsee) {
 | 
        
           |  |  | 141 | 				$stations[$indexStation]['lat'] = $commune['lat'];
 | 
        
           |  |  | 142 | 				$stations[$indexStation]['lng'] = $commune['lng'];
 | 
        
           |  |  | 143 | 				$stations[$indexStation]['commune'] = $commune['commune'];
 | 
        
           |  |  | 144 | 			}
 | 
        
           |  |  | 145 | 		}
 | 
        
           |  |  | 146 |   | 
        
           |  |  | 147 | 		$lat = array();
 | 
        
           |  |  | 148 | 		$lng = array();
 | 
        
           |  |  | 149 | 		foreach ($stations as $index => $station) {
 | 
        
           |  |  | 150 | 			if (!isset($station['lat'])) {
 | 
        
           |  |  | 151 | 				$station['lat'] = -100;
 | 
        
           |  |  | 152 | 				$station['lng'] = -100;
 | 
        
           |  |  | 153 | 			}
 | 
        
           |  |  | 154 | 			$lat[$index] = $station['lat'];
 | 
        
           |  |  | 155 | 			$lng[$index] = $station['lng'];
 | 
        
           |  |  | 156 | 		}
 | 
        
           |  |  | 157 | 		array_multisort($lat, SORT_DESC, $lng, SORT_ASC, $stations);
 | 
        
           |  |  | 158 | 	}
 | 
        
           |  |  | 159 |   | 
        
           | 727 | alex | 160 | }
 | 
        
           |  |  | 161 |   | 
        
           |  |  | 162 | ?>
 |