Subversion Repositories eFlore/Projets.eflore-projets

Rev

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

<?php
//declare(encoding='UTF-8');
/**
 * Classe permettant de :
 *  - convertir la latitude et longitude en degre, et rajouter l'objet point centroide de la commune.
 *  - charger la bdd
 * Exemple de lancement du script : :
 * /opt/lampp/bin/php cli.php lion1906 -a chargerTous
 *
 * @category    php 5.2
 * @package             eFlore/Scripts
 * @author              Mohcen BENMOUNAH <mohcen@tela-botanica.org>
 * @author              Jean-Pascal MILCENT <jpm@tela-botanica.org>
 * @copyright   Copyright (c) 2011, Tela Botanica (accueil@tela-botanica.org)
 * @license             http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
 * @license             http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
 * @version             $Id$
 */
class Lion1906 extends EfloreScript {

        public function executer() {
                try {
                        $this->initialiserProjet('lion1906');

                        // Lancement de l'action demandée
                        $cmd = $this->getParametre('a');
                        switch ($cmd) {
                                case 'chargerTous' :
                                        $this->chargerStructureSql();
                                        $this->chargerMetaDonnees();
                                        $this->chargerLion1906();
                                        $this->preparerTable();
                                        $this->convertirRadianEnDegre();
                                        break;
                                case 'convertir' :
                                        $this->preparerTable();
                                        $this->convertirRadianEnDegre();
                                        break;
                                case 'supprimerTous' :
                                        $this->supprimerTous();
                                        break;
                                default :
                                        throw new Exception("Erreur : la commande '$cmd' n'existe pas!");
                        }
                } catch (Exception $e) {
                        $this->traiterErreur($e->getMessage());
                }
        }

        protected function chargerMetaDonnees() {
                $contenuSql = $this->recupererContenu(Config::get('chemins.lion1906Meta'));
                $this->executerScripSql($contenuSql);
        }

        private function chargerLion1906() {
                $chemin = Config::get('chemins.lion1906');
                $table = Config::get('tables.lion1906');
                $requete = "LOAD DATA INFILE '$chemin' ".
                                        "REPLACE INTO TABLE $table ".
                                        'CHARACTER SET latin1 '.
                                        'FIELDS '.
                                        "       TERMINATED BY ';' ".
                                        "       ENCLOSED BY '' ".
                                        "       ESCAPED BY '\\\' ".
                                        'IGNORE 1 LINES';
                $this->getBdd()->requeter($requete);
        }

        private function convertirRadianEnDegre() {
                $table = Config::get('tables.lion1906');
                $requete =      'SELECT insee, latitude_radian, longitude_radian '.
                                        "FROM $table ";
                $LatLons = $this->getBdd()->recupererTous($requete);

                foreach ($LatLons as $LatLon) {
                        $insee = $LatLon['insee'] ;
                        $latitude_degre = $LatLon['latitude_radian'] * 180 / pi();
                        $longitude_degre = $LatLon['longitude_radian'] * 180 / pi();
                        $latitude_degre = str_replace(',', '.', $latitude_degre);
                        $longitude_degre = str_replace(',', '.', $longitude_degre);
                        $this->formerPointCentre($latitude_degre, $longitude_degre, $insee);
                        $this->afficherAvancement('Analyse des communes Lion1906');
                }
        }

        private function preparerTable() {
                $table = Config::get('tables.lion1906');
                $requete =      "ALTER TABLE $table ".
                                        'DROP latitude_degre, '.
                                        'DROP longitude_degre, '.
                                        'DROP centroide, '.
                                        'DROP INDEX insee ';
                $this->getBdd()->requeter($requete);

                $requete =      "ALTER TABLE $table ".
                                        '       ADD latitude_degre double NOT NULL , '.
                                        '       ADD longitude_degre double NOT NULL , '.
                                        '       ADD centroide point NOT NULL ';
                $this->getBdd()->requeter($requete);

                $requete =      "ALTER TABLE $table ".
                                        'ADD INDEX insee (insee, latitude_degre, longitude_degre, centroide) ';
                $this->getBdd()->requeter($requete);
        }

        private function formerPointCentre($latitude_degre, $longitude_degre, $insee) {
                $centre = "$latitude_degre $longitude_degre" ;
                $table = Config::get('tables.lion1906');
                $requete = "UPDATE $table ".
                                        "SET latitude_degre = '$latitude_degre', longitude_degre = '$longitude_degre', ".
                                        "       centroide = POINTFROMTEXT('POINT($centre)') ".
                                        "WHERE insee = '$insee' ";
                $this->getBdd()->requeter($requete);
        }

        private function supprimerTous() {
                $requete = "DROP TABLE IF EXISTS lion1906_meta, lion1906_communes_v2008";
                $this->getBdd()->requeter($requete);
        }

}
?>