New file |
0,0 → 1,141 |
<?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->communes = 0; |
$objetJSON->stats->stations = 0; |
$objetJSON->features = array(); |
|
foreach ($stations as $station) { |
$stationJSON = NULL; |
// construction d'un objet feature adapte a la structure des donnees spatiales |
if ($station['type_site'] == 'MAILLE') { |
$stationJSON = $this->formaterMaille($station); |
} else { |
if ($station['type_site'] == 'STATION') { |
$objetJSON->stats->communes ++; |
} else { |
$objetJSON->stats->stations ++; |
} |
$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"; |
$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 { |
$station['code_insee'] = ''; |
$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) { |
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 = 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; |
} |
|
} |
|
?> |