26 |
alex |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
class FormateurJson {
|
|
|
5 |
|
|
|
6 |
private $sourceDonnees;
|
|
|
7 |
|
31 |
alex |
8 |
public function __construct($source = '') {
|
26 |
alex |
9 |
$this->sourceDonnees = $source;
|
|
|
10 |
}
|
|
|
11 |
|
|
|
12 |
|
|
|
13 |
public function formaterStations($stations) {
|
|
|
14 |
$objetJSON = new StdClass();
|
|
|
15 |
$objetJSON->type = "FeatureCollection";
|
|
|
16 |
$objetJSON->stats = new StdClass();
|
31 |
alex |
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 |
}
|
|
|
22 |
$objetJSON->stats->sites = 0;
|
|
|
23 |
$objetJSON->stats->observations = 0;
|
26 |
alex |
24 |
$objetJSON->features = array();
|
|
|
25 |
foreach ($stations as $station) {
|
|
|
26 |
$stationJSON = NULL;
|
|
|
27 |
if ($station['type_site'] == 'MAILLE') {
|
|
|
28 |
$stationJSON = $this->formaterMaille($station);
|
31 |
alex |
29 |
$objetJSON->stats->sites += $station['points'];
|
26 |
alex |
30 |
} else {
|
31 |
alex |
31 |
$objetJSON->stats->sites ++;
|
26 |
alex |
32 |
$stationJSON = $this->formaterPoint($station);
|
|
|
33 |
}
|
31 |
alex |
34 |
$objetJSON->stats->observations += $station['observations'];
|
26 |
alex |
35 |
if (!is_null($stationJSON)) {
|
|
|
36 |
$objetJSON->features[] = $stationJSON;
|
|
|
37 |
}
|
|
|
38 |
}
|
|
|
39 |
return $objetJSON;
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
private function formaterPoint($station) {
|
|
|
43 |
$json = new StdClass();
|
|
|
44 |
$json->type = "Feature";
|
|
|
45 |
$json->geometry = new StdClass();
|
|
|
46 |
$json->properties = new StdClass();
|
|
|
47 |
$json->geometry->type = "Point";
|
31 |
alex |
48 |
$json->properties->source = $this->sourceDonnees;
|
26 |
alex |
49 |
$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']);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
if ($this->sourceDonnees == 'floradata') {
|
|
|
57 |
$json->properties->nom = $this->construireNomStationFloradata($station);
|
|
|
58 |
} else {
|
|
|
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})";
|
|
|
71 |
} else {
|
|
|
72 |
if (strlen($nom) == 0) {
|
|
|
73 |
$nom = 'station sans nom, '.trim($station['zone_geo']);
|
|
|
74 |
}
|
31 |
alex |
75 |
$nom .= " ({$codeDepartement})";
|
26 |
alex |
76 |
}
|
|
|
77 |
return $nom;
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
private function extraireCodeDepartement($codeInsee) {
|
|
|
81 |
$codeInsee = substr($codeInsee,-5);
|
|
|
82 |
$codeDepartement = substr($codeInsee, 0 ,2);
|
|
|
83 |
if (intval($codeDepartement) > 95) {
|
|
|
84 |
substr($codeInsee, 0 ,3);
|
|
|
85 |
}
|
|
|
86 |
return $codeDepartement;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
private function formaterMaille($maille) {
|
|
|
91 |
$json = new StdClass();
|
|
|
92 |
$json->type = "Feature";
|
|
|
93 |
$json->geometry = new StdClass();
|
|
|
94 |
$json->geometry->type = "Polygon";
|
|
|
95 |
$json->geometry->coordinates = array(
|
|
|
96 |
array(floatval($maille['sud']), floatval($maille['ouest'])),
|
|
|
97 |
array(floatval($maille['sud']), floatval($maille['est'])),
|
|
|
98 |
array(floatval($maille['nord']), floatval($maille['est'])),
|
|
|
99 |
array(floatval($maille['nord']), floatval($maille['ouest'])),
|
|
|
100 |
array(floatval($maille['sud']), floatval($maille['ouest']))
|
|
|
101 |
);
|
|
|
102 |
$json->properties = new StdClass();
|
31 |
alex |
103 |
$json->properties->source = $this->sourceDonnees;
|
|
|
104 |
$json->properties->typeSite = 'MAILLE';
|
26 |
alex |
105 |
$json->properties->nombrePoints = $maille['points'];
|
|
|
106 |
return $json;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
public function formaterObservations($observations, $nomSite) {
|
|
|
111 |
$objetJSON = new StdClass();
|
|
|
112 |
$objetJSON->site = trim($nomSite);
|
|
|
113 |
$objetJSON->total = count($observations);
|
|
|
114 |
|
|
|
115 |
$objetJSON->observations = array();
|
|
|
116 |
foreach ($observations as $observation) {
|
|
|
117 |
$observationJson = new stdClass();
|
|
|
118 |
foreach ($observation as $colonne => $valeur) {
|
|
|
119 |
if ($colonne == 'nom_referentiel') {
|
|
|
120 |
$observationJson->urlEflore = $this->genererUrlFicheEflore($observation);
|
|
|
121 |
} else {
|
|
|
122 |
$observationJson->$colonne = is_string($valeur) ? trim($valeur) : $valeur;
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
$objetJSON->observations[] = $observationJson;
|
|
|
126 |
}
|
|
|
127 |
return $objetJSON;
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
private function genererUrlFicheEflore($observation) {
|
|
|
131 |
$url = null;
|
|
|
132 |
if (strstr('bdtfx', $observation['nom_referentiel']) !== false) {
|
|
|
133 |
$url = 'http://www.tela-botanica.org/bdtfx-nn-'.$observation['nn'];
|
|
|
134 |
}
|
|
|
135 |
return $url;
|
|
|
136 |
}
|
|
|
137 |
|
31 |
alex |
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 |
}
|
|
|
151 |
|
26 |
alex |
152 |
}
|
|
|
153 |
|
|
|
154 |
?>
|