Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 31 | Rev 40 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 31 Rev 34
Line 1... Line 1...
1
<?php
1
<?php
Line 2... Line 2...
2
 
2
 
Line 3... Line -...
3
 
-
 
Line 4... Line 3...
4
class FormateurJson {
3
 
5
	
-
 
6
	private $sourceDonnees;
-
 
Line 7... Line 4...
7
	
4
class FormateurJson {
8
	public function __construct($source = '') {
5
	
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();
-
 
15
		$objetJSON->stats->formeDonnees = '';
18
		$objetJSON->stats->formeDonnees = '';
16
		if (count($stations) > 0) {
19
		if (count($stations) > 0) {
17
			$objetJSON->stats->formeDonnees = ($stations[0]['type_site'] == 'MAILLE') ? 'maille' : 'point';
20
			$objetJSON->stats->formeDonnees = ($stations[0]['type_site'] == 'MAILLE') ? 'maille' : 'point';
18
		}
21
		}
19
		$objetJSON->stats->stations = 0;
22
		$objetJSON->stats->sites = 0;
20
		$objetJSON->stats->observations = 0;
23
		$objetJSON->stats->observations = 0;
21
		
-
 
22
		$objetJSON->features = array();
24
		$objetJSON->features = array();
23
		foreach ($stations as $station) {
25
		foreach ($stations as $station) {
-
 
26
			$stationJSON = NULL;
24
			$stationJSON = null;
-
 
25
			if ($station['type_site'] == 'MAILLE') {
-
 
26
				$stationJSON = $this->formaterMaille($station);
27
			if ($station['type_site'] == 'MAILLE') {
27
				$objetJSON->stats->stations += array_sum($station['stations']);
28
				$stationJSON = $this->formaterMaille($station);
28
				$objetJSON->stats->observations += array_sum($station['observations']);
29
				$objetJSON->stats->sites += $station['points'];
-
 
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'];
Line -... Line 33...
-
 
33
			}
-
 
34
			$objetJSON->features[] = $stationJSON;
-
 
35
			$this->ajouterSourcesAuxStats($station, $objetJSON->stats);
-
 
36
		}
-
 
37
		return $objetJSON;
-
 
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
			}
35
			if (!is_null($stationJSON)) {
47
		} else {
36
				$objetJSON->features[] = $stationJSON;
48
			if (!in_array($station['source'], $stats->source)) {
37
			}
49
				$stats->source[] = $station['source'];
38
		}
50
			}
39
		return $objetJSON;
51
		}
40
	}
52
	}
41
	
53
	
42
	private function formaterPoint($station) {
54
	private function formaterPoint(& $station) {
43
		$json = new StdClass();
-
 
44
		$json->type = "Feature";
55
		$json = new StdClass();
45
		$json->geometry = new StdClass();
-
 
46
		$json->properties = new StdClass();
56
		$json->type = "Feature";
47
		$json->geometry->type = "Point";
-
 
48
		$json->properties->source = $this->sourceDonnees;
-
 
49
		$json->properties->typeSite = $station['type_site'];
-
 
50
		if ($this->sourceDonnees == 'floradata' && $station['type_site'] == 'COMMUNE') {
57
		$json->geometry = new StdClass();
51
			$json->geometry->coordinates = array($station['lat_commune'], $station['lng_commune']);
58
		$json->properties = new StdClass();
52
		} else {
59
		$json->geometry->type = "Point";
53
			$json->geometry->coordinates = array($station['latitude'], $station['longitude']);
60
		$json->properties->source = $station['source'];
-
 
61
		$json->properties->typeSite = $station['type_site'];
-
 
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 = '';
54
		}
66
		if ($station['source'] != 'floradata') {
55
		
-
 
56
		if ($this->sourceDonnees == 'floradata') {
67
			$json->properties->nom = trim($station['nom'])." ({$codeDepartement})";
57
			$json->properties->nom = $this->construireNomStationFloradata($station);
68
		} else {
Line 58... Line 69...
58
		} else {
69
			$station['station'] = (is_null($station['station']) || strlen(trim($station['station'])) == 0)
59
			$codeDepartement = $this->extraireCodeDepartement($station['code_insee']);
-
 
60
			$json->properties->nom = trim($station['nom'])." ({$codeDepartement})";
-
 
61
		}
-
 
62
		
-
 
63
		return $json;
-
 
64
	}
-
 
65
	
-
 
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})";
70
				? $station['zone_geo'] : $station['station'];
Line 71... Line 71...
71
		} else {
71
			$nom = $station['type_site'] == 'COMMUNE' ? $station['zone_geo'] : $station['station'];
72
			if (strlen($nom) == 0) {
-
 
73
				$nom = 'station sans nom, '.trim($station['zone_geo']);
72
			$json->properties->nom = trim($nom)." ({$codeDepartement})";
74
			}
73
		}
75
			$nom .= " ({$codeDepartement})";
74
		return $json;
76
		}
75
	}
77
		return $nom;
76
	
78
	}
77
	private function construireNomStation(& $station) {
Line 98... Line 97...
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
	}
Line 108... Line 111...
108
	
111
	
-
 
112
	
109
	
113
	public function formaterObservations($observations) {
110
	public function formaterObservations($observations, $nomSite) {
114
		//print_r($observations); exit;
111
		$objetJSON = new StdClass();
115
		$objetJSON = new StdClass();
Line 112... Line 116...
112
		$objetJSON->site = trim($nomSite);
116
		$objetJSON->site = trim($observations[0]['nom_station']);
113
		$objetJSON->total = count($observations);
117
		$objetJSON->total = count($observations);
-
 
118
		
114
		
119
		$objetJSON->observations = array();
115
		$objetJSON->observations = array();
120
		foreach ($observations as $observation) {
116
		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 {
-
 
127
					$observationJson->$colonne = is_string($valeur) ? trim($valeur) : $valeur;
122
					$observationJson->$colonne = is_string($valeur) ? trim($valeur) : $valeur;
128
				}
123
				}
129
			}
124
			}
130
			$this->formaterDateObservation($observationJson);
125
			$objetJSON->observations[] = $observationJson;
131
			$objetJSON->observations[] = $observationJson;
Line -... Line 132...
-
 
132
		}
-
 
133
		return $objetJSON;
-
 
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);
126
		}
139
			$observation->date = $dateFormatee;
127
		return $objetJSON;
140
		}
128
	}
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) {
Line 133... Line 146...
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
	}
-
 
150
	
137
	
151
	private function concatenerLieuObservation(& $observation) {
138
	public function formaterMaillesVides($mailles) {
152
		$lieux = [];
139
		$objetJSON = new StdClass();
153
		if (!is_null($observation['lieudit'])) {
140
		$objetJSON->type = "FeatureCollection";
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';
-
 
144
		$objetJSON->stats->sites = 0;
-
 
145
		$objetJSON->features = array();
157
			$lieux[] = $observation['milieu'];
Line 146... Line 158...
146
		foreach ($mailles as $maille) {
158
		}
Line 147... Line 159...
147
			$objetJSON->features[] = $this->formaterMaille($maille);
159
		unset($observation['lieudit']);
148
		}
160
		unset($observation['milieu']);