Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php
/**
 * Création de la table osm_communes.
 * /opt/lampp/bin/php cli.php osm -a communes -m manuel
 *
 */
class ZonesAdmin {

        private $conteneur;
        private $bdd;
        private $messages;
        private $mode;

        private $bddOsmNom;

        public function __construct($conteneur) {
                $this->conteneur = $conteneur;
                $this->bdd = $this->conteneur->getBdd();
                $this->bddOsmNom = $this->conteneur->getParametre('bdd_osm_nom');
                $this->messages = $this->conteneur->getMessages();
                $this->mode = $this->conteneur->getParametre('m');
        }

        public function executer() {
                // Lancement de l'action demandée
                $cmd = $this->conteneur->getParametre('a');
                switch ($cmd) {
                        case 'za' :
                                $this->creerTableOsmZonesAdmin();
                                break;
                        case 'zaCorriger' :
                                $this->corrigerTableOsmZonesAdmin();
                                break;
                        case 'zaMajIdZG' :
                                $this->mettreAJourIdZoneGeo();
                                break;
                        case 'zaSupprimer' :
                                $this->supprimerTableOsmZonesAdmin();
                                break;
                        default :
                                $this->messages->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
                }

        }

        private function supprimerTableOsmZonesAdmin() {
                $requete = 'DROP TABLE IF EXISTS osm_zones_admin '.
                        ' -- '.__FILE__.' : '.__LINE__;
                $this->bdd->requeter($requete);
        }

        private function creerTableOsmZonesAdmin() {
                $requete = 'INSERT INTO osm_zones_admin '.
                        "SELECT if(ref_insee != '' AND admin_level = 8, CONCAT('INSEE-C:',ref_insee), ".
                        "if(ref_insee != '' AND admin_level = 6, CONCAT('INSEE-D:',ref_insee), ".
                        "if(ref_insee != '' AND admin_level = 4, CONCAT('INSEE-R:',ref_insee), ".
                        "if(iso3166_2 != '', CONCAT('ISO-3166-2:',iso3166_2), ".
                        "if(iso3166_1 != '', CONCAT('ISO-3166-1:',iso3166_1), ".
                        "CONCAT('OSM-ID:',osm_id)))))), ".
                        'osm_id, osm_version, osm_timestamp, '.
                        "if(name_fr != '', name_fr, name), ".
                        'shape, shape_centroid, Y(shape_centroid), X(shape_centroid), '.
                        'admin_level, iso3166_1, iso3166_2, ref_insee, ref_nuts, '.
                        'name, name_fr, name_en, name_es, wikipedia '.
                        "FROM {$this->bddOsmNom}.multipolygons_ref ".
                        ' -- '.__FILE__.' : '.__LINE__;
                $this->bdd->requeter($requete);
        }

        private function corrigerTableOsmZonesAdmin() {
                $requete = 'SELECT osm_id, other_tags '.
                        "FROM {$this->bddOsmNom}.multipolygons_ref ".
                        "WHERE iso3166_2 IS NULL AND other_tags LIKE '%\"iso3166-2\"%' ".
                        ' -- '.__FILE__.' : '.__LINE__;
                $resultat = $this->bdd->recupererTous($requete);
                if ($resultat) {
                        foreach ($resultat as $infos) {
                                $tags = $this->getTags($infos['other_tags']);
                                $iso_3166_2 = $this->bdd->proteger($tags['iso3166-2']);
                                $osm_id = $this->bdd->proteger($infos['osm_id']);

                                $requete = 'UPDATE osm_zones_admin '.
                                        "SET code_iso_3166_2 = $iso_3166_2 ".
                                        "WHERE osm_id = $osm_id ".
                                        ' -- '.__FILE__.' : '.__LINE__;
                                $this->bdd->requeter($requete);
                        }
                }
        }

        private function getTags($tagsInfos) {
                $tags = array();
                $clesValeurs = explode(',', $tagsInfos);
                foreach ($clesValeurs as $cleValeur) {
                        list($cle, $val) = explode('=>', $cleValeur);
                        $tags[trim($cle, '"')] = trim($val, '"');
                }
                return $tags;
        }

        private function mettreAJourIdZoneGeo() {
                $requete = 'UPDATE osm_zones_admin '.
                        "SET id_zone_geo = if(code_insee != '' AND niveau = 8, CONCAT('INSEE-C:',code_insee), ".
                        "if(code_insee != '' AND niveau = 6, CONCAT('INSEE-D:',code_insee), ".
                        "if(code_insee != '' AND niveau = 4, CONCAT('INSEE-R:',code_insee), ".
                        "if(code_iso_3166_2 != '', CONCAT('ISO-3166-2:',code_iso_3166_2), ".
                        "if(code_iso_3166_1 != '', CONCAT('ISO-3166-1:',code_iso_3166_1), ".
                        "CONCAT('OSM-ID:',osm_id)))))) ".
                        ' -- '.__FILE__.' : '.__LINE__;
                $this->bdd->requeter($requete);
        }
}