Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 34 | Details | Compare with Previous | 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) {
43 alex 48
					if ($cle != 'taxon' && $cle != 'auteur') {
34 alex 49
						$station[$cle] = $valeur;
50
					}
51
				}
52
				$station['taxons'] = array(trim($stationBdd['taxon']));
43 alex 53
				$station['auteurs'] = array(trim($stationBdd['auteur']));
34 alex 54
				$this->mettreAJourBbox($station);
55
			} else {
56
				$station['taxons'][] = trim($stationBdd['taxon']);
43 alex 57
				$station['auteurs'][] = trim($stationBdd['auteur']);
34 alex 58
			}
59
		}
60
		return $stationsRetour;
61
	}
62
 
63
	private function estNonNul($valeur) {
64
		return (!is_null($valeur) && strlen(trim($valeur)) > 0);
65
	}
66
 
67
	private function mettreAJourBbox($station) {
68
		if (is_null($this->bbox['sud']) || floatval($station['latitude']) < floatval($this->bbox['sud'])) {
69
			$this->bbox['sud'] = $station['latitude'];
70
		} elseif (is_null($this->bbox['nord']) || floatval($station['latitude']) > floatval($this->bbox['nord'])) {
71
			$this->bbox['nord'] = $station['latitude'];
72
		}
73
		if (is_null($this->bbox['ouest']) || floatval($station['longitude']) < floatval($this->bbox['ouest'])) {
74
			$this->bbox['ouest'] = $station['longitude'];
75
		} elseif (is_null($this->bbox['est']) || floatval($station['longitude']) > floatval($this->bbox['est'])) {
76
			$this->bbox['est'] = $station['longitude'];
77
		}
78
	}
79
 
80
	private function mettreEnPageStationFloradata(& $station) {
81
		$station['nom_station'] = trim($station['station']);
82
		if ($this->estNonNul($station['zone_geo'])) {
83
			$station['nom_station'] .= ", ".$station['zone_geo'];
84
		}
85
		$station['nom_station'] = str_replace("&", "&amp;", trim($station['nom_station']));
86
		$station['departement'] = '';
87
		$station['code_insee'] = '';
88
		if ($this->estNonNul($station['ce_zone_geo'])) {
89
			$station['code_insee'] = substr($station['ce_zone_geo'], 8);
90
			$station['departement'] = substr($station['code_insee'],0, 2);
91
			if (intval($station['departement']) > 95) {
92
				$station['departement'] = substr($station['code_insee'],0, 2);
93
			}
94
		}
95
		unset($station['station']);
96
		unset($station['zone_geo']);
97
		unset($station['ce_zone_geo']);
43 alex 98
		$station['taxons'] = str_replace("&", "&amp;", implode(', ', $station['taxons']));
99
		$station['auteurs'] = str_replace("&", "&amp;", implode(', ', array_unique($station['auteurs'])));
34 alex 100
	}
101
 
102
	private function mettreEnPageStationMoissonnage(& $station) {
103
		$station['nom_station'] = str_replace("&", "&amp;", trim($station['nom']));
104
		$station['departement'] = '';
105
		if ($this->estNonNul($station['code_insee'])) {
106
			$station['departement'] = substr($station['code_insee'],0, 2);
107
			if (intval($station['departement']) > 95) {
108
				$station['departement'] = substr($station['code_insee'],0, 2);
109
			}
110
		}
111
		unset($station['nom']);
43 alex 112
		$station['taxons'] = str_replace("&", "&amp;", implode(', ', $station['taxons']));
113
		$station['auteurs'] = str_replace("&", "&amp;", implode(', ', array_unique($station['auteurs'])));
34 alex 114
	}
115
 
116
	public function formaterException(Exception $erreur) {
117
		$nomFichierWfs = dirname(__FILE__).DS."squelettes".DS."Exception.tpl.xml";
118
		$item = array('message' => $erreur->getMessage());
119
		return SquelettePhp::analyser($nomFichierWfs, $item);
120
	}
121
 
122
}
123
 
124
?>