Subversion Repositories eFlore/Applications.moissonnage

Rev

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

Rev Author Line No. Line
26 alex 1
<?php
2
 
3
 
4
class FormateurJson {
5
 
6
	private $sourceDonnees;
7
 
8
	public function __construct($source) {
9
		$this->sourceDonnees = $source;
10
	}
11
 
12
 
13
	public function formaterStations($stations) {
14
		$objetJSON = new StdClass();
15
		$objetJSON->type = "FeatureCollection";
16
		$objetJSON->stats = new StdClass();
17
		$objetJSON->stats->communes = 0;
18
		$objetJSON->stats->stations = 0;
19
		$objetJSON->features = array();
20
 
21
		foreach ($stations as $station) {
22
			$stationJSON = NULL;
23
			// construction d'un objet feature adapte a la structure des donnees spatiales
24
			if ($station['type_site'] == 'MAILLE') {
25
				$stationJSON = $this->formaterMaille($station);
26
			} else {
27
				if ($station['type_site'] == 'STATION') {
28
					$objetJSON->stats->communes ++;
29
				} else {
30
					$objetJSON->stats->stations ++;
31
				}
32
				$stationJSON = $this->formaterPoint($station);
33
			}
34
			if (!is_null($stationJSON)) {
35
				$objetJSON->features[] = $stationJSON;
36
			}
37
		}
38
		return $objetJSON;
39
	}
40
 
41
	private function formaterPoint($station) {
42
		$json = new StdClass();
43
		$json->type = "Feature";
44
		$json->geometry = new StdClass();
45
		$json->properties = new StdClass();
46
		$json->geometry->type = "Point";
47
		$json->properties->typeSite = $station['type_site'];
48
		if ($this->sourceDonnees == 'floradata' && $station['type_site'] == 'COMMUNE') {
49
			$json->geometry->coordinates = array($station['lat_commune'], $station['lng_commune']);
50
		} else {
51
			$json->geometry->coordinates = array($station['latitude'], $station['longitude']);
52
		}
53
 
54
		if ($this->sourceDonnees == 'floradata') {
55
			$json->properties->nom = $this->construireNomStationFloradata($station);
56
		} else {
57
			$station['code_insee'] = '';
58
			$codeDepartement = $this->extraireCodeDepartement($station['code_insee']);
59
			$json->properties->nom = trim($station['nom'])." ({$codeDepartement})";
60
		}
61
 
62
		return $json;
63
	}
64
 
65
	private function construireNomStationFloradata($station) {
66
		$nom = ($station['type_site'] == 'COMMUNE') ? trim($station['nom_commune']) : trim($station['station']);
67
		$codeDepartement = $this->extraireCodeDepartement($station['ce_zone_geo']);
68
		if ($station['type_site'] == 'COMMUNE') {
69
			$nom = $station['zone_geo'] . " ({$codeDepartement})";
70
		} else {
71
			if (strlen($nom) == 0) {
72
				$nom = 'station sans nom, '.trim($station['zone_geo']);
73
			}
74
			$nom .= " ({$codeDepartement})";;
75
		}
76
		return $nom;
77
	}
78
 
79
	private function extraireCodeDepartement($codeInsee) {
80
		$codeInsee = substr($codeInsee,-5);
81
		$codeDepartement = substr($codeInsee, 0 ,2);
82
		if (intval($codeDepartement) > 95) {
83
			substr($codeInsee, 0 ,3);
84
		}
85
		return $codeDepartement;
86
	}
87
 
88
 
89
	private function formaterMaille($maille) {
90
		if ($maille['points'] == 0) {
91
			return null;
92
		}
93
		$json = new StdClass();
94
		$json->type = "Feature";
95
		$json->geometry = new StdClass();
96
		$json->geometry->type = "Polygon";
97
		$json->geometry->coordinates = array(
98
			array(floatval($maille['sud']),  floatval($maille['ouest'])),
99
			array(floatval($maille['sud']),  floatval($maille['est'])),
100
			array(floatval($maille['nord']), floatval($maille['est'])),
101
			array(floatval($maille['nord']), floatval($maille['ouest'])),
102
			array(floatval($maille['sud']),  floatval($maille['ouest']))
103
		);
104
		$json->properties = new StdClass();
105
		$json->properties->typeSite = $maille['type_site'];
106
		$json->properties->nombrePoints = $maille['points'];
107
		return $json;
108
	}
109
 
110
 
111
	public function formaterObservations($observations, $nomSite) {
112
		$objetJSON = new StdClass();
113
		$objetJSON->site = trim($nomSite);
114
		$objetJSON->total = count($observations);
115
 
116
		$objetJSON->observations = array();
117
		foreach ($observations as $observation) {
118
			$observationJson = new stdClass();
119
			foreach ($observation as $colonne => $valeur) {
120
				if ($colonne == 'nom_referentiel') {
121
					$observationJson->urlEflore = $this->genererUrlFicheEflore($observation);
122
				} else {
123
					$observationJson->$colonne = is_string($valeur) ? trim($valeur) : $valeur;
124
				}
125
			}
126
			$objetJSON->observations[] = $observationJson;
127
		}
128
		return $objetJSON;
129
	}
130
 
131
	private function genererUrlFicheEflore($observation) {
132
		$url = null;
133
		if (strstr('bdtfx', $observation['nom_referentiel']) !== false) {
134
			$url = 'http://www.tela-botanica.org/bdtfx-nn-'.$observation['nn'];
135
		}
136
		return $url;
137
	}
138
 
139
}
140
 
141
?>