Subversion Repositories eFlore/Applications.moissonnage

Rev

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

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