Rev 26 | Rev 34 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php
class FormateurJson {
private $sourceDonnees;
public function __construct($source = '') {
$this->sourceDonnees = $source;
}
public function formaterStations($stations) {
$objetJSON = new StdClass();
$objetJSON->type = "FeatureCollection";
$objetJSON->stats = new StdClass();
$objetJSON->stats->source = $this->sourceDonnees;
$objetJSON->stats->formeDonnees = '';
if (count($stations) > 0) {
$objetJSON->stats->formeDonnees = ($stations[0]['type_site'] == 'MAILLE') ? 'maille' : 'point';
}
$objetJSON->stats->sites = 0;
$objetJSON->stats->observations = 0;
$objetJSON->features = array();
foreach ($stations as $station) {
$stationJSON = NULL;
if ($station['type_site'] == 'MAILLE') {
$stationJSON = $this->formaterMaille($station);
$objetJSON->stats->sites += $station['points'];
} else {
$objetJSON->stats->sites ++;
$stationJSON = $this->formaterPoint($station);
}
$objetJSON->stats->observations += $station['observations'];
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";
$json->properties->source = $this->sourceDonnees;
$json->properties->typeSite = $station['type_site'];
if ($this->sourceDonnees == 'floradata' && $station['type_site'] == 'COMMUNE') {
$json->geometry->coordinates = array($station['lat_commune'], $station['lng_commune']);
} else {
$json->geometry->coordinates = array($station['latitude'], $station['longitude']);
}
if ($this->sourceDonnees == 'floradata') {
$json->properties->nom = $this->construireNomStationFloradata($station);
} else {
$codeDepartement = $this->extraireCodeDepartement($station['code_insee']);
$json->properties->nom = trim($station['nom'])." ({$codeDepartement})";
}
return $json;
}
private function construireNomStationFloradata($station) {
$nom = ($station['type_site'] == 'COMMUNE') ? trim($station['nom_commune']) : trim($station['station']);
$codeDepartement = $this->extraireCodeDepartement($station['ce_zone_geo']);
if ($station['type_site'] == 'COMMUNE') {
$nom = $station['zone_geo'] . " ({$codeDepartement})";
} else {
if (strlen($nom) == 0) {
$nom = 'station sans nom, '.trim($station['zone_geo']);
}
$nom .= " ({$codeDepartement})";
}
return $nom;
}
private function extraireCodeDepartement($codeInsee) {
$codeInsee = substr($codeInsee,-5);
$codeDepartement = substr($codeInsee, 0 ,2);
if (intval($codeDepartement) > 95) {
substr($codeInsee, 0 ,3);
}
return $codeDepartement;
}
private function formaterMaille($maille) {
$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->source = $this->sourceDonnees;
$json->properties->typeSite = 'MAILLE';
$json->properties->nombrePoints = $maille['points'];
return $json;
}
public function formaterObservations($observations, $nomSite) {
$objetJSON = new StdClass();
$objetJSON->site = trim($nomSite);
$objetJSON->total = count($observations);
$objetJSON->observations = array();
foreach ($observations as $observation) {
$observationJson = new stdClass();
foreach ($observation as $colonne => $valeur) {
if ($colonne == 'nom_referentiel') {
$observationJson->urlEflore = $this->genererUrlFicheEflore($observation);
} else {
$observationJson->$colonne = is_string($valeur) ? trim($valeur) : $valeur;
}
}
$objetJSON->observations[] = $observationJson;
}
return $objetJSON;
}
private function genererUrlFicheEflore($observation) {
$url = null;
if (strstr('bdtfx', $observation['nom_referentiel']) !== false) {
$url = 'http://www.tela-botanica.org/bdtfx-nn-'.$observation['nn'];
}
return $url;
}
public function formaterMaillesVides($mailles) {
$objetJSON = new StdClass();
$objetJSON->type = "FeatureCollection";
$objetJSON->stats = new StdClass();
$objetJSON->stats->source = '';
$objetJSON->stats->formeDonnees = 'maille';
$objetJSON->stats->sites = 0;
$objetJSON->features = array();
foreach ($mailles as $maille) {
$objetJSON->features[] = $this->formaterMaille($maille);
}
return $objetJSON;
}
}
?>