Subversion Repositories eFlore/Applications.moissonnage

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
34 alex 1
<?php
2
 
3
class FormateurWfs {
4
 
5
	const TYPE_MIME = 'text/xml';
6
	private $bbox = null;
7
 
8
	public function formaterGetCapabilities() {
9
		$nomFichierWfs = dirname(__FILE__).DS."squelettes".DS."GetCapabilities.tpl.xml";
10
		return SquelettePhp::analyser($nomFichierWfs);
11
	}
12
 
13
	public function formaterDescribeFeatureType($sources) {
14
		$nomFichierWfs = dirname(__FILE__).DS."squelettes".DS."DescribeFeatureType.tpl.xml";
15
		if (is_null($sources)) {
16
			$sources = Config::get('sources_dispo');
17
		}
18
		$listeSources = is_array($sources) == 1 ? $sources : explode(',', $sources);
19
		$item = array('listeSources' => $listeSources);
20
		return SquelettePhp::analyser($nomFichierWfs, $item);
21
	}
22
 
23
	public function formaterGetFeature(& $stations, $sources) {
24
		$nomFichierWfs = dirname(__FILE__).DS."squelettes".DS."GetFeature.tpl.xml";
25
		$this->bbox = array('ouest' => null, 'est' => null, 'sud' => null, 'nord'=> null);
26
		$stationsRetour = $this->mettreEnPageStations($stations);
27
		$listeSources = implode(',', $sources);
28
		$item = array('enveloppe' => $this->bbox, 'stations' => $stationsRetour, 'listeSources' => $listeSources);
29
		return SquelettePhp::analyser($nomFichierWfs, $item);
30
	}
31
 
32
	private function mettreEnPageStations(& $stations) {
33
		$station = array('longitude' => null, 'latitude' => null);
34
		$stationsRetour = array();
35
		foreach ($stations as $stationBdd) {
36
			if ($this->estNonNul($stationBdd['longitude']) && $this->estNonNul($stationBdd['latitude'])
37
			&& ($stationBdd['longitude'] != $station['longitude'] || $stationBdd['latitude'] != $station['latitude'])) {
38
				if (isset($station['source'])) {
39
					if ($station['source'] == 'floradata') {
40
						$this->mettreEnPageStationFloradata($station);
41
					} else {
42
						$this->mettreEnPageStationMoissonnage($station);
43
					}
44
					$stationsRetour[] = $station;
45
				}
46
 
47
				foreach ($stationBdd as $cle => $valeur) {
48
					if ($cle != 'taxon') {
49
						$station[$cle] = $valeur;
50
					}
51
				}
52
				$station['taxons'] = array(trim($stationBdd['taxon']));
53
				$this->mettreAJourBbox($station);
54
			} else {
55
				$station['taxons'][] = trim($stationBdd['taxon']);
56
			}
57
		}
58
		return $stationsRetour;
59
	}
60
 
61
	private function estNonNul($valeur) {
62
		return (!is_null($valeur) && strlen(trim($valeur)) > 0);
63
	}
64
 
65
	private function mettreAJourBbox($station) {
66
		if (is_null($this->bbox['sud']) || floatval($station['latitude']) < floatval($this->bbox['sud'])) {
67
			$this->bbox['sud'] = $station['latitude'];
68
		} elseif (is_null($this->bbox['nord']) || floatval($station['latitude']) > floatval($this->bbox['nord'])) {
69
			$this->bbox['nord'] = $station['latitude'];
70
		}
71
		if (is_null($this->bbox['ouest']) || floatval($station['longitude']) < floatval($this->bbox['ouest'])) {
72
			$this->bbox['ouest'] = $station['longitude'];
73
		} elseif (is_null($this->bbox['est']) || floatval($station['longitude']) > floatval($this->bbox['est'])) {
74
			$this->bbox['est'] = $station['longitude'];
75
		}
76
	}
77
 
78
	private function mettreEnPageStationFloradata(& $station) {
79
		$station['nom_station'] = trim($station['station']);
80
		if ($this->estNonNul($station['zone_geo'])) {
81
			$station['nom_station'] .= ", ".$station['zone_geo'];
82
		}
83
		$station['nom_station'] = str_replace("&", "&amp;", trim($station['nom_station']));
84
		$station['departement'] = '';
85
		$station['code_insee'] = '';
86
		if ($this->estNonNul($station['ce_zone_geo'])) {
87
			$station['code_insee'] = substr($station['ce_zone_geo'], 8);
88
			$station['departement'] = substr($station['code_insee'],0, 2);
89
			if (intval($station['departement']) > 95) {
90
				$station['departement'] = substr($station['code_insee'],0, 2);
91
			}
92
		}
93
		unset($station['station']);
94
		unset($station['zone_geo']);
95
		unset($station['ce_zone_geo']);
96
		$station['taxons'] = str_replace("&", "&amp;", implode(',', $station['taxons']));
97
	}
98
 
99
	private function mettreEnPageStationMoissonnage(& $station) {
100
		$station['nom_station'] = str_replace("&", "&amp;", trim($station['nom']));
101
		$station['departement'] = '';
102
		if ($this->estNonNul($station['code_insee'])) {
103
			$station['departement'] = substr($station['code_insee'],0, 2);
104
			if (intval($station['departement']) > 95) {
105
				$station['departement'] = substr($station['code_insee'],0, 2);
106
			}
107
		}
108
		unset($station['nom']);
109
		$station['taxons'] = str_replace("&", "&amp;", implode(',', $station['taxons']));
110
	}
111
 
112
	public function formaterException(Exception $erreur) {
113
		$nomFichierWfs = dirname(__FILE__).DS."squelettes".DS."Exception.tpl.xml";
114
		$item = array('message' => $erreur->getMessage());
115
		return SquelettePhp::analyser($nomFichierWfs, $item);
116
	}
117
 
118
}
119
 
120
?>