Subversion Repositories eFlore/Applications.moissonnage

Compare Revisions

Ignore whitespace Rev 18 → Rev 19

/trunk/services/modules/0.1/commun/FormateurJson.php
New file
0,0 → 1,108
<?php
 
 
class FormateurJson {
public function __construct() {}
public function formaterStations($stations) {
$objetJSON = new StdClass();
$objetJSON->type = "FeatureCollection";
$objetJSON->features = array();
if (count($stations) == 0) {
return $objetJSON;
}
foreach ($stations as $station) {
$stationJSON = NULL;
// construction d'un objet feature adapte a la structure des donnees spatiales
if (isset($station['sud'])) {
$stationJSON = $this->formaterMaille($station);
} else {
$stationJSON = $this->formaterPoint($station);
}
if (!is_null($stationJSON)) {
$objetJSON->features[] = $stationJSON;
}
}
return $objetJSON;
}
private function formaterPoint($station) {
$json = new StdClass();
$json->type = "Feature";
$json->geometry = new StdClass();
$json->properties = new StdClass();
$json->geometry->type = "Point";
if (is_null($station['latitude']) || is_null($station['longitude'])) {
$json->properties->typeSite = 'COMMUNE';
$json->geometry->coordinates = array($station['lat_commune'], $station['lng_commune']);
} else {
$json->properties->typeSite = 'STATION';
$json->geometry->coordinates = array($station['latitude'], $station['longitude']);
}
$nom = $json->properties->typeSite == 'COMMUNE' ? $station['nom_commune'] : $station['station'];
$nomDefaut = 'station sans nom';
if ($json->properties->typeSite == 'COMMUNE') {
$ce = substr($station['ce_zone_geo'],-5);
$codeDept = ((int)substr($ce, 0, 2) > 95 ? substr($ce, 0, 3) : substr($ce, 0, 2));
$nom = $station['zone_geo'] . " ($codeDept)";
} else {
if (strlen(trim($station['zone_geo']))==0) {
$nom = $nomDefaut;
} else {
$nom = $station['zone_geo'];
if (strlen(trim($station['ce_zone_geo']))!=0) {
$ce = substr($station['ce_zone_geo'],-5);
$nom .= ' ('.((int)substr($ce,0,2)>95 ? substr($ce,0,3) : substr($ce,0,2)).')';
}
}
}
$json->properties->nom = $nom;
return $json;
}
private function formaterMaille($maille) {
if ($maille['points'] == 0) {
return null;
}
$json = new StdClass();
$json->type = "Feature";
$json->geometry = new StdClass();
$json->geometry->type = "Polygon";
$json->geometry->coordinates = array(
array(floatval($maille['sud']), floatval($maille['ouest'])),
array(floatval($maille['sud']), floatval($maille['est'])),
array(floatval($maille['nord']), floatval($maille['est'])),
array(floatval($maille['nord']), floatval($maille['ouest'])),
array(floatval($maille['sud']), floatval($maille['ouest']))
);
$json->properties = new StdClass();
$json->properties->typeSite = $maille['type_site'];
$json->properties->nombrePoints = $maille['points'];
return $json;
}
public function formaterObservations($observations, $nomSite) {
$objetJSON = new StdClass();
$objetJSON->site = $nomSite;
$objetJSON->total = count($observations);
$objetJSON->observations = array();
foreach ($observations as $observation) {
$observationJson = new stdClass();
foreach ($observation as $colonne => $valeur) {
$observationJson->$colonne = $valeur;
}
$objetJSON->observations[] = $observationJson;
}
return $objetJSON;
}
}
 
?>