Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 40 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
26 alex 1
<?php
2
 
3
 
4
class FormateurJson {
5
 
6
 
34 alex 7
	public function __construct() {}
26 alex 8
 
9
 
10
	public function formaterStations($stations) {
11
		$objetJSON = new StdClass();
12
		$objetJSON->type = "FeatureCollection";
13
		$objetJSON->stats = new StdClass();
34 alex 14
		$objetJSON->stats->source = array();
31 alex 15
		$objetJSON->stats->formeDonnees = '';
16
		if (count($stations) > 0) {
17
			$objetJSON->stats->formeDonnees = ($stations[0]['type_site'] == 'MAILLE') ? 'maille' : 'point';
18
		}
34 alex 19
		$objetJSON->stats->stations = 0;
31 alex 20
		$objetJSON->stats->observations = 0;
34 alex 21
 
26 alex 22
		$objetJSON->features = array();
23
		foreach ($stations as $station) {
34 alex 24
			$stationJSON = null;
26 alex 25
			if ($station['type_site'] == 'MAILLE') {
26
				$stationJSON = $this->formaterMaille($station);
34 alex 27
				$objetJSON->stats->stations += array_sum($station['stations']);
28
				$objetJSON->stats->observations += array_sum($station['observations']);
26 alex 29
			} else {
30
				$stationJSON = $this->formaterPoint($station);
34 alex 31
				$objetJSON->stats->stations ++;
32
				$objetJSON->stats->observations += $station['observations'];
26 alex 33
			}
34 alex 34
			$objetJSON->features[] = $stationJSON;
35
			$this->ajouterSourcesAuxStats($station, $objetJSON->stats);
26 alex 36
		}
37
		return $objetJSON;
38
	}
39
 
34 alex 40
	private function ajouterSourcesAuxStats($station, & $stats) {
41
		if ($station['type_site'] == 'MAILLE') {
42
			foreach ($station['stations'] as $source => $nombreStations) {
43
				if (!in_array($source, $stats->source)) {
44
					$stats->source[] = $source;
45
				}
46
			}
47
		} else {
48
			if (!in_array($station['source'], $stats->source)) {
49
				$stats->source[] = $station['source'];
50
			}
51
		}
52
	}
53
 
54
	private function formaterPoint(& $station) {
26 alex 55
		$json = new StdClass();
56
		$json->type = "Feature";
57
		$json->geometry = new StdClass();
58
		$json->properties = new StdClass();
59
		$json->geometry->type = "Point";
34 alex 60
		$json->properties->source = $station['source'];
26 alex 61
		$json->properties->typeSite = $station['type_site'];
34 alex 62
		$json->geometry->coordinates = array($station['latitude'], $station['longitude']);
63
		$codeInsee = isset($station['code_insee']) ? $station['code_insee'] : substr($station['ce_zone_geo'],-5);
64
		$codeDepartement = $this->extraireCodeDepartement($codeInsee);
65
		$nom = '';
66
		if ($station['source'] != 'floradata') {
67
			$json->properties->nom = trim($station['nom'])." ({$codeDepartement})";
26 alex 68
		} else {
34 alex 69
			$station['station'] = (is_null($station['station']) || strlen(trim($station['station'])) == 0)
70
				? $station['zone_geo'] : $station['station'];
71
			$nom = $station['type_site'] == 'COMMUNE' ? $station['zone_geo'] : $station['station'];
72
			$json->properties->nom = trim($nom)." ({$codeDepartement})";
26 alex 73
		}
74
		return $json;
75
	}
76
 
34 alex 77
	private function construireNomStation(& $station) {
26 alex 78
	}
79
 
80
	private function extraireCodeDepartement($codeInsee) {
81
		$codeDepartement = substr($codeInsee, 0 ,2);
82
		if (intval($codeDepartement) > 95) {
34 alex 83
			$codeDepartement = substr($codeInsee, 0 ,3);
26 alex 84
		}
85
		return $codeDepartement;
86
	}
87
 
88
 
89
	private function formaterMaille($maille) {
90
		$json = new StdClass();
91
		$json->type = "Feature";
92
		$json->geometry = new StdClass();
93
		$json->geometry->type = "Polygon";
94
		$json->geometry->coordinates = array(
95
			array(floatval($maille['sud']),  floatval($maille['ouest'])),
96
			array(floatval($maille['sud']),  floatval($maille['est'])),
97
			array(floatval($maille['nord']), floatval($maille['est'])),
98
			array(floatval($maille['nord']), floatval($maille['ouest'])),
99
			array(floatval($maille['sud']),  floatval($maille['ouest']))
100
		);
101
		$json->properties = new StdClass();
34 alex 102
		$json->properties->source = array();
103
		foreach ($maille['stations'] as $source => $nombreStations) {
104
			$json->properties->source[] = $source;
105
		}
31 alex 106
		$json->properties->typeSite = 'MAILLE';
34 alex 107
		$json->properties->stations = $maille['stations'];
108
		$json->properties->observations = $maille['observations'];
26 alex 109
		return $json;
110
	}
111
 
112
 
34 alex 113
	public function formaterObservations($observations) {
114
		//print_r($observations); exit;
26 alex 115
		$objetJSON = new StdClass();
34 alex 116
		$objetJSON->site = trim($observations[0]['nom_station']);
26 alex 117
		$objetJSON->total = count($observations);
118
 
119
		$objetJSON->observations = array();
120
		foreach ($observations as $observation) {
34 alex 121
			$this->concatenerLieuObservation($observation);
26 alex 122
			$observationJson = new stdClass();
123
			foreach ($observation as $colonne => $valeur) {
124
				if ($colonne == 'nom_referentiel') {
125
					$observationJson->urlEflore = $this->genererUrlFicheEflore($observation);
126
				} else {
127
					$observationJson->$colonne = is_string($valeur) ? trim($valeur) : $valeur;
128
				}
129
			}
34 alex 130
			$this->formaterDateObservation($observationJson);
26 alex 131
			$objetJSON->observations[] = $observationJson;
132
		}
133
		return $objetJSON;
134
	}
135
 
34 alex 136
	private function formaterDateObservation(& $observation) {
137
		if (isset($observation->date) && strlen($observation->date) > 4) {
138
			$dateFormatee = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '$3/$2/$1', $observation->date);
139
			$observation->date = $dateFormatee;
140
		}
141
	}
142
 
143
	private function genererUrlFicheEflore(& $observation) {
26 alex 144
		$url = null;
34 alex 145
		if (strstr($observation['nom_referentiel'], 'bdtfx') !== false) {
26 alex 146
			$url = 'http://www.tela-botanica.org/bdtfx-nn-'.$observation['nn'];
147
		}
148
		return $url;
149
	}
150
 
34 alex 151
	private function concatenerLieuObservation(& $observation) {
40 alex 152
		$lieux = array();
44 alex 153
		if (isset($observation['lieudit']) && !is_null($observation['lieudit'])) {
34 alex 154
			$lieux[] = $observation['lieudit'];
31 alex 155
		}
44 alex 156
		if (isset($observation['milieu']) && !is_null($observation['milieu'])) {
34 alex 157
			$lieux[] = $observation['milieu'];
158
		}
159
		unset($observation['lieudit']);
160
		unset($observation['milieu']);
161
		$observation['lieu'] = implode(', ', $lieux);
31 alex 162
	}
163
 
26 alex 164
}
165
 
166
?>