Subversion Repositories eFlore/Applications.moissonnage

Rev

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

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