Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 34 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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