/opt/lampp/bin/php cli.php mon_script -a test * * @author Jean-Pascal MILCENT * @copyright Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org) * @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3 * @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2 * @version $Id$ */ class NettoyageKml extends Script { private $fichier_id = ''; private $coordonnees_simples = array(); protected $parametres_autorises = array( '-f' => array(true, true, 'Fichier KML à nettoyer.')); public function executer() { // Récupération de paramêtres // Lancement de l'action demandée $cmd = $this->getParametre('a'); switch ($cmd) { case 'nettoyer' : $this->executerNettoyage(); break; default : $this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd)); } } private function executerNettoyage() { $kml = $this->getParametre('f'); if (file_exists($kml)) { $placemarks = $this->analyserFichierKml($kml); $nom_kml = basename($kml, '.kml'); $dossier_kml = dirname($kml); $dpt = '00'; if (preg_match('/^([0-9]{2}|2[AB])/', $nom_kml, $match)) { $dpt = $match[1]; } $fichier_sortie = $dossier_kml.DS.$dpt.'%s.kml'; $squelette = dirname(__FILE__).DS.'squelettes'.DS.'defaut.tpl.kml'; $num = 1; $pas = Config::get('fichierSortie.taille'); $taille = 0; $nbre_placemarks = count($placemarks); foreach ($placemarks as $id => $placemark) { $placemarks_portion[] = $placemark; $taille += mb_strlen($placemark); if ($taille > $pas || $id == ($nbre_placemarks - 1)) { $taille = 0; $donnees = array('nom' => $nom_kml, 'placemarks' => $placemarks_portion); $sortie = SquelettePhp::analyser($squelette, $donnees); $suffixe_fichier = ($id == ($nbre_placemarks - 1) && $num == 1) ? '' : '_'.$num++; if (file_put_contents(sprintf($fichier_sortie, $suffixe_fichier), $sortie)) { @chmod($fichier_sortie, octdec(Config::get('fichierSortie.droits'))); } else { trigger_error("Impossible d'écrire dans le fichier : $fichier_sortie", E_USER_WARNING); } $placemarks_portion = array(); } } } } private function analyserFichierKml($fichier_kml) { $placemarks = array(); $lignesKML = file($fichier_kml, FILE_SKIP_EMPTY_LINES); foreach ($lignesKML as $id => $ligne) { if (preg_match('/^\s*/', $ligne)) { if (isset($placemark)) { $placemarks[] = $placemark; } $placemark = ''; } $this->trouverFichierId($ligne); if (isset($placemark)) { $ligne = $this->remplacerChaines($ligne); $placemark .= $this->traiterLigne($ligne); } } return $placemarks; } private function trouverFichierId($ligne) { if (preg_match('/([^<]+)<\/name>/', $ligne, $match)) { $this->fichier_id = $match[1]; } } private function remplacerChaines($ligne) { $ligne = preg_replace("/^\s+(<[\/]?Placemark>)/", "\t\t$1", $ligne); $ligne = preg_replace("/^\s+()/", "\t\t\t$1", $ligne); $ligne = preg_replace("/()()(<\/ExtendedData>)/", "\t\t\t$1\n\t\t\t$2", $ligne); $ligne = preg_replace('/^\t+()/', "\t\t\t\t\t$1code_insee$2", $ligne); $ligne = preg_replace('/(schemaUrl="#)'.$this->fichier_id.'(")/', "$1commune$2", $ligne); return $ligne; } private function traiterLigne($ligne) { $ligne_traitee = $ligne; if (preg_match('/^\s*()([^<]+)([^$]+)/', $ligne, $match)) { $debut_chaine_polygone = $match[1]; $coordonnees = $this->traiterCoordonnees($match[2]); $fin_chaine_polygone = $match[3]; $ligne_traitee = "\t\t\t".$debut_chaine_polygone.$coordonnees.$fin_chaine_polygone; } else if (preg_match('/^\s*