Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 40 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php


class FormateurJson {
        
        
        public function __construct() {}
        
        
        public function formaterStations($stations) {
                $objetJSON = new StdClass();
                $objetJSON->type = "FeatureCollection";
                $objetJSON->stats = new StdClass();
                $objetJSON->stats->source = array();
                $objetJSON->stats->formeDonnees = '';
                if (count($stations) > 0) {
                        $objetJSON->stats->formeDonnees = ($stations[0]['type_site'] == 'MAILLE') ? 'maille' : 'point';
                }
                $objetJSON->stats->stations = 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->stations += array_sum($station['stations']);
                                $objetJSON->stats->observations += array_sum($station['observations']);
                        } else {
                                $stationJSON = $this->formaterPoint($station);
                                $objetJSON->stats->stations ++;
                                $objetJSON->stats->observations += $station['observations'];
                        }
                        $objetJSON->features[] = $stationJSON;
                        $this->ajouterSourcesAuxStats($station, $objetJSON->stats);
                }
                return $objetJSON;
        }
        
        private function ajouterSourcesAuxStats($station, & $stats) {
                if ($station['type_site'] == 'MAILLE') {
                        foreach ($station['stations'] as $source => $nombreStations) {
                                if (!in_array($source, $stats->source)) {
                                        $stats->source[] = $source;
                                }
                        }
                } else {
                        if (!in_array($station['source'], $stats->source)) {
                                $stats->source[] = $station['source'];
                        }
                }
        }
        
        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 = $station['source'];
                $json->properties->typeSite = $station['type_site'];
                $json->geometry->coordinates = array($station['latitude'], $station['longitude']);
                $codeInsee = isset($station['code_insee']) ? $station['code_insee'] : substr($station['ce_zone_geo'],-5);
                $codeDepartement = $this->extraireCodeDepartement($codeInsee);
                $nom = '';
                if ($station['source'] != 'floradata') {
                        $json->properties->nom = trim($station['nom'])." ({$codeDepartement})";
                } else {
                        $station['station'] = (is_null($station['station']) || strlen(trim($station['station'])) == 0)
                                ? $station['zone_geo'] : $station['station'];
                        $nom = $station['type_site'] == 'COMMUNE' ? $station['zone_geo'] : $station['station'];
                        $json->properties->nom = trim($nom)." ({$codeDepartement})";
                }
                return $json;
        }
        
        private function construireNomStation(& $station) {
        }
        
        private function extraireCodeDepartement($codeInsee) {
                $codeDepartement = substr($codeInsee, 0 ,2);
                if (intval($codeDepartement) > 95) {
                        $codeDepartement = 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 = array();
                foreach ($maille['stations'] as $source => $nombreStations) {
                        $json->properties->source[] = $source;
                }
                $json->properties->typeSite = 'MAILLE';
                $json->properties->stations = $maille['stations'];
                $json->properties->observations = $maille['observations'];
                return $json;
        }
        
        
        public function formaterObservations($observations) {
                //print_r($observations); exit;
                $objetJSON = new StdClass();
                $objetJSON->site = trim($observations[0]['nom_station']);
                $objetJSON->total = count($observations);
                
                $objetJSON->observations = array();
                foreach ($observations as $observation) {
                        $this->concatenerLieuObservation($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;
                                }
                        }
                        $this->formaterDateObservation($observationJson);
                        $objetJSON->observations[] = $observationJson;
                }
                return $objetJSON;
        }
        
        private function formaterDateObservation(& $observation) {
                if (isset($observation->date) && strlen($observation->date) > 4) {
                        $dateFormatee = preg_replace('/(\d{4})-(\d{2})-(\d{2})/', '$3/$2/$1', $observation->date);
                        $observation->date = $dateFormatee;
                }
        }
        
        private function genererUrlFicheEflore(& $observation) {
                $url = null;
                if (strstr($observation['nom_referentiel'], 'bdtfx') !== false) {
                        $url = 'http://www.tela-botanica.org/bdtfx-nn-'.$observation['nn'];
                }
                return $url;
        }
        
        private function concatenerLieuObservation(& $observation) {
                $lieux = array();
                if (isset($observation['lieudit']) && !is_null($observation['lieudit'])) {
                        $lieux[] = $observation['lieudit'];
                }
                if (isset($observation['milieu']) && !is_null($observation['milieu'])) {
                        $lieux[] = $observation['milieu'];
                }
                unset($observation['lieudit']);
                unset($observation['milieu']);
                $observation['lieu'] = implode(', ', $lieux);
        }
        
}

?>