Subversion Repositories eFlore/Applications.moissonnage

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php

class FormateurWfs {
        
        const TYPE_MIME = 'text/xml';
        private $bbox = null;
        
        public function formaterGetCapabilities() {
                $nomFichierWfs = dirname(__FILE__).DS."squelettes".DS."GetCapabilities.tpl.xml";
                return SquelettePhp::analyser($nomFichierWfs);
        }
        
        public function formaterDescribeFeatureType($sources) {
                $nomFichierWfs = dirname(__FILE__).DS."squelettes".DS."DescribeFeatureType.tpl.xml";
                if (is_null($sources)) {
                        $sources = Config::get('sources_dispo');
                }
                $listeSources = is_array($sources) == 1 ? $sources : explode(',', $sources);
                $item = array('listeSources' => $listeSources);
                return SquelettePhp::analyser($nomFichierWfs, $item);
        }
        
        public function formaterGetFeature(& $stations, $sources) {
                $nomFichierWfs = dirname(__FILE__).DS."squelettes".DS."GetFeature.tpl.xml";
                $this->bbox = array('ouest' => null, 'est' => null, 'sud' => null, 'nord'=> null);
                $stationsRetour = $this->mettreEnPageStations($stations);
                $listeSources = implode(',', $sources);
                $item = array('enveloppe' => $this->bbox, 'stations' => $stationsRetour, 'listeSources' => $listeSources);
                return SquelettePhp::analyser($nomFichierWfs, $item);
        }
        
        private function mettreEnPageStations(& $stations) {
                $station = array('longitude' => null, 'latitude' => null);
                $stationsRetour = array();
                foreach ($stations as $stationBdd) {
                        if ($this->estNonNul($stationBdd['longitude']) && $this->estNonNul($stationBdd['latitude'])
                        && ($stationBdd['longitude'] != $station['longitude'] || $stationBdd['latitude'] != $station['latitude'])) {
                                if (isset($station['source'])) {
                                        if ($station['source'] == 'floradata') {
                                                $this->mettreEnPageStationFloradata($station);
                                        } else {
                                                $this->mettreEnPageStationMoissonnage($station);
                                        }
                                        $stationsRetour[] = $station;
                                }
                                
                                foreach ($stationBdd as $cle => $valeur) {
                                        if ($cle != 'taxon') {
                                                $station[$cle] = $valeur;
                                        }
                                }
                                $station['taxons'] = array(trim($stationBdd['taxon']));
                                $this->mettreAJourBbox($station);
                        } else {
                                $station['taxons'][] = trim($stationBdd['taxon']);
                        }
                }
                return $stationsRetour;
        }
        
        private function estNonNul($valeur) {
                return (!is_null($valeur) && strlen(trim($valeur)) > 0);
        }
        
        private function mettreAJourBbox($station) {
                if (is_null($this->bbox['sud']) || floatval($station['latitude']) < floatval($this->bbox['sud'])) {
                        $this->bbox['sud'] = $station['latitude'];
                } elseif (is_null($this->bbox['nord']) || floatval($station['latitude']) > floatval($this->bbox['nord'])) {
                        $this->bbox['nord'] = $station['latitude'];
                }
                if (is_null($this->bbox['ouest']) || floatval($station['longitude']) < floatval($this->bbox['ouest'])) {
                        $this->bbox['ouest'] = $station['longitude'];
                } elseif (is_null($this->bbox['est']) || floatval($station['longitude']) > floatval($this->bbox['est'])) {
                        $this->bbox['est'] = $station['longitude'];
                }
        }
        
        private function mettreEnPageStationFloradata(& $station) {
                $station['nom_station'] = trim($station['station']);
                if ($this->estNonNul($station['zone_geo'])) {
                        $station['nom_station'] .= ", ".$station['zone_geo'];
                }
                $station['nom_station'] = str_replace("&", "&amp;", trim($station['nom_station']));
                $station['departement'] = '';
                $station['code_insee'] = '';
                if ($this->estNonNul($station['ce_zone_geo'])) {
                        $station['code_insee'] = substr($station['ce_zone_geo'], 8);
                        $station['departement'] = substr($station['code_insee'],0, 2);
                        if (intval($station['departement']) > 95) {
                                $station['departement'] = substr($station['code_insee'],0, 2);
                        }
                }
                unset($station['station']);
                unset($station['zone_geo']);
                unset($station['ce_zone_geo']);
                $station['taxons'] = str_replace("&", "&amp;", implode(',', $station['taxons']));
        }
        
        private function mettreEnPageStationMoissonnage(& $station) {
                $station['nom_station'] = str_replace("&", "&amp;", trim($station['nom']));
                $station['departement'] = '';
                if ($this->estNonNul($station['code_insee'])) {
                        $station['departement'] = substr($station['code_insee'],0, 2);
                        if (intval($station['departement']) > 95) {
                                $station['departement'] = substr($station['code_insee'],0, 2);
                        }
                }
                unset($station['nom']);
                $station['taxons'] = str_replace("&", "&amp;", implode(',', $station['taxons']));
        }
        
        public function formaterException(Exception $erreur) {
                $nomFichierWfs = dirname(__FILE__).DS."squelettes".DS."Exception.tpl.xml";
                $item = array('message' => $erreur->getMessage());
                return SquelettePhp::analyser($nomFichierWfs, $item);
        }
        
}

?>