19 |
delphine |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
|
|
|
4 |
class FormateurJson {
|
|
|
5 |
|
|
|
6 |
public function __construct() {}
|
|
|
7 |
|
|
|
8 |
|
|
|
9 |
public function formaterStations($stations) {
|
|
|
10 |
$objetJSON = new StdClass();
|
|
|
11 |
$objetJSON->type = "FeatureCollection";
|
|
|
12 |
$objetJSON->features = array();
|
|
|
13 |
if (count($stations) == 0) {
|
|
|
14 |
return $objetJSON;
|
|
|
15 |
}
|
|
|
16 |
|
|
|
17 |
foreach ($stations as $station) {
|
|
|
18 |
$stationJSON = NULL;
|
|
|
19 |
// construction d'un objet feature adapte a la structure des donnees spatiales
|
|
|
20 |
if (isset($station['sud'])) {
|
|
|
21 |
$stationJSON = $this->formaterMaille($station);
|
|
|
22 |
} else {
|
|
|
23 |
$stationJSON = $this->formaterPoint($station);
|
|
|
24 |
}
|
|
|
25 |
if (!is_null($stationJSON)) {
|
|
|
26 |
$objetJSON->features[] = $stationJSON;
|
|
|
27 |
}
|
|
|
28 |
}
|
|
|
29 |
return $objetJSON;
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
private function formaterPoint($station) {
|
|
|
33 |
$json = new StdClass();
|
|
|
34 |
$json->type = "Feature";
|
|
|
35 |
$json->geometry = new StdClass();
|
|
|
36 |
$json->properties = new StdClass();
|
|
|
37 |
$json->geometry->type = "Point";
|
|
|
38 |
if (is_null($station['latitude']) || is_null($station['longitude'])) {
|
|
|
39 |
$json->properties->typeSite = 'COMMUNE';
|
|
|
40 |
$json->geometry->coordinates = array($station['lat_commune'], $station['lng_commune']);
|
|
|
41 |
} else {
|
|
|
42 |
$json->properties->typeSite = 'STATION';
|
|
|
43 |
$json->geometry->coordinates = array($station['latitude'], $station['longitude']);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
$nom = $json->properties->typeSite == 'COMMUNE' ? $station['nom_commune'] : $station['station'];
|
|
|
47 |
$nomDefaut = 'station sans nom';
|
|
|
48 |
if ($json->properties->typeSite == 'COMMUNE') {
|
|
|
49 |
$ce = substr($station['ce_zone_geo'],-5);
|
|
|
50 |
$codeDept = ((int)substr($ce, 0, 2) > 95 ? substr($ce, 0, 3) : substr($ce, 0, 2));
|
|
|
51 |
$nom = $station['zone_geo'] . " ($codeDept)";
|
|
|
52 |
} else {
|
|
|
53 |
if (strlen(trim($station['zone_geo']))==0) {
|
|
|
54 |
$nom = $nomDefaut;
|
|
|
55 |
} else {
|
|
|
56 |
$nom = $station['zone_geo'];
|
|
|
57 |
if (strlen(trim($station['ce_zone_geo']))!=0) {
|
|
|
58 |
$ce = substr($station['ce_zone_geo'],-5);
|
|
|
59 |
$nom .= ' ('.((int)substr($ce,0,2)>95 ? substr($ce,0,3) : substr($ce,0,2)).')';
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
$json->properties->nom = $nom;
|
|
|
65 |
return $json;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
private function formaterMaille($maille) {
|
|
|
69 |
if ($maille['points'] == 0) {
|
|
|
70 |
return null;
|
|
|
71 |
}
|
|
|
72 |
$json = new StdClass();
|
|
|
73 |
$json->type = "Feature";
|
|
|
74 |
$json->geometry = new StdClass();
|
|
|
75 |
$json->geometry->type = "Polygon";
|
|
|
76 |
$json->geometry->coordinates = array(
|
|
|
77 |
array(floatval($maille['sud']), floatval($maille['ouest'])),
|
|
|
78 |
array(floatval($maille['sud']), floatval($maille['est'])),
|
|
|
79 |
array(floatval($maille['nord']), floatval($maille['est'])),
|
|
|
80 |
array(floatval($maille['nord']), floatval($maille['ouest'])),
|
|
|
81 |
array(floatval($maille['sud']), floatval($maille['ouest']))
|
|
|
82 |
);
|
|
|
83 |
$json->properties = new StdClass();
|
|
|
84 |
$json->properties->typeSite = $maille['type_site'];
|
|
|
85 |
$json->properties->nombrePoints = $maille['points'];
|
|
|
86 |
return $json;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
public function formaterObservations($observations, $nomSite) {
|
|
|
91 |
$objetJSON = new StdClass();
|
|
|
92 |
$objetJSON->site = $nomSite;
|
|
|
93 |
$objetJSON->total = count($observations);
|
|
|
94 |
|
|
|
95 |
$objetJSON->observations = array();
|
|
|
96 |
foreach ($observations as $observation) {
|
|
|
97 |
$observationJson = new stdClass();
|
|
|
98 |
foreach ($observation as $colonne => $valeur) {
|
|
|
99 |
$observationJson->$colonne = $valeur;
|
|
|
100 |
}
|
|
|
101 |
$objetJSON->observations[] = $observationJson;
|
|
|
102 |
}
|
|
|
103 |
return $objetJSON;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
?>
|