Subversion Repositories eFlore/Projets.communes

Rev

Rev 14 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 jpm 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * Exemple de script utilisable avec le TBFramework.
5
 * Pour le lancer, taper en ligne de commande, en vous plaçant dans le dossier /framework/exemple/scripts/ :
6
 * <code>/opt/lampp/bin/php cli.php mon_script -a test</code>
7
 *
8
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
9
 * @copyright	Copyright (c) 2010, Tela Botanica (accueil@tela-botanica.org)
10
 * @license	http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
11
 * @license	http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
12
 * @version	$Id$
13
 */
14
class NettoyageKml extends Script {
18 jpm 15
 
2 jpm 16
	private $fichier_id = '';
18 jpm 17
 
2 jpm 18
	private $coordonnees_simples = array();
18 jpm 19
 
2 jpm 20
	protected $parametres_autorises = array(
21
		'-f' => array(true, true, 'Fichier KML à nettoyer.'));
18 jpm 22
 
2 jpm 23
	public function executer() {
24
		// Récupération de paramêtres
25
 
26
		// Lancement de l'action demandée
27
		$cmd = $this->getParametre('a');
18 jpm 28
		switch ($cmd) {
2 jpm 29
			case 'nettoyer' :
30
				$this->executerNettoyage();
31
				break;
32
			default :
33
				$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
34
		}
18 jpm 35
	}
2 jpm 36
 
18 jpm 37
	private function executerNettoyage() {
38
		$kml = $this->getParametre('f');
39
		if (file_exists($kml)) {
40
			$placemarks = $this->analyserFichierKml($kml);
41
 
42
			$nom_kml = basename($kml, '.kml');
43
			$dossier_kml = dirname($kml);
44
			$dpt = '00';
45
			if (preg_match('/^([0-9]{2}|2[AB])/', $nom_kml, $match)) {
46
				$dpt = $match[1];
47
			}
48
			$fichier_sortie = $dossier_kml.DS.$dpt.'%s.kml';
49
			$squelette = dirname(__FILE__).DS.'squelettes'.DS.'defaut.tpl.kml';
50
 
51
			$num = 1;
52
			$pas = Config::get('fichierSortie.taille');
53
			$taille = 0;
54
			$nbre_placemarks = count($placemarks);
55
			foreach ($placemarks as $id => $placemark) {
56
				$placemarks_portion[] = $placemark;
57
				$taille += mb_strlen($placemark);
58
				if ($taille > $pas || $id == ($nbre_placemarks - 1)) {
59
					$taille = 0;
60
					$donnees = array('nom' => $nom_kml, 'placemarks' => $placemarks_portion);
61
 
62
					$sortie = SquelettePhp::analyser($squelette, $donnees);
63
					$suffixe_fichier = ($id == ($nbre_placemarks - 1) && $num == 1) ? '' : '_'.$num++;
64
					if (file_put_contents(sprintf($fichier_sortie, $suffixe_fichier), $sortie)) {
65
						@chmod($fichier_sortie, octdec(Config::get('fichierSortie.droits')));
66
					} else {
67
						trigger_error("Impossible d'écrire dans le fichier : $fichier_sortie", E_USER_WARNING);
68
					}
69
					$placemarks_portion = array();
70
				}
71
			}
72
		}
73
	}
74
 
75
	private function analyserFichierKml($fichier_kml) {
76
		$placemarks = array();
77
		$lignesKML = file($fichier_kml, FILE_SKIP_EMPTY_LINES);
78
		foreach ($lignesKML as $id => $ligne) {
79
			if (preg_match('/^\s*<Placemark>/', $ligne)) {
80
				if (isset($placemark)) {
81
					$placemarks[] = $placemark;
82
				}
83
				$placemark = '';
84
			}
85
 
86
			$this->trouverFichierId($ligne);
87
			if (isset($placemark)) {
88
				$ligne = $this->remplacerChaines($ligne);
89
				$placemark .= $this->traiterLigne($ligne);
90
 
91
			}
92
		}
93
		return $placemarks;
94
	}
95
	private function trouverFichierId($ligne) {
96
		if (preg_match('/<Document><Folder><name>([^<]+)<\/name>/', $ligne, $match)) {
97
			$this->fichier_id = $match[1];
98
		}
99
	}
100
 
101
	private function remplacerChaines($ligne) {
2 jpm 102
		$ligne = preg_replace("/^\s+(<[\/]?Placemark>)/", "\t\t$1", $ligne);
103
		$ligne = preg_replace("/^\s+(<name>)/", "\t\t\t$1", $ligne);
104
		$ligne = preg_replace("/(<ExtendedData>)(<SchemaData)/", "\t\t$1\n\t\t\t\t$2", $ligne);
105
		$ligne = preg_replace("/(<\/SchemaData>)(<\/ExtendedData>)/", "\t\t\t$1\n\t\t\t$2", $ligne);
18 jpm 106
		$ligne = preg_replace('/^\t+(<SimpleData name=")CODE_INSEE(">)/', "\t\t\t\t\t$1code_insee$2", $ligne);
107
		$ligne = preg_replace('/(schemaUrl="#)'.$this->fichier_id.'(")/', "$1commune$2", $ligne);
108
		return $ligne;
109
	}
110
 
111
	private function traiterLigne($ligne) {
112
		$ligne_traitee = $ligne;
113
		if (preg_match('/^\s*(<Polygon><outerBoundaryIs><LinearRing><coordinates>)([^<]+)([^$]+)/', $ligne, $match)) {
114
			$debut_chaine_polygone = $match[1];
115
			$coordonnees = $this->traiterCoordonnees($match[2]);
116
			$fin_chaine_polygone = $match[3];
117
			$ligne_traitee = "\t\t\t".$debut_chaine_polygone.$coordonnees.$fin_chaine_polygone;
118
		} else if (preg_match('/^\s*<Style>.*<\/Style>$/u', $ligne)) {
119
			$url_style = Config::get('urlStyle');
120
			$ligne_traitee = "\t\t\t<styleUrl>$url_style#commune-limite</styleUrl>\n";
121
		} else if (preg_match('/^\t+<SimpleData name="Name">/', $ligne)) {
122
			 // Ne pas prendre en compte cette ligne
123
		} else {
2 jpm 124
			$ligne_traitee = $ligne;
125
		}
126
		return $ligne_traitee;
127
	}
18 jpm 128
 
2 jpm 129
	private function traiterCoordonnees($chaine_coordonnees) {
130
		$coordonnees = explode(' ', $chaine_coordonnees);
131
		$geo_points = array();
132
		foreach ($coordonnees as $coord) {
133
			list($lon, $lat) = explode(',', $coord);
134
			$geo_points[] = new GeoPoint($lat, $lon);
135
		}
136
		$reducteur = new PolylineReducer($geo_points);
5 jpm 137
		$coordonnees_simplifiees = $reducteur->SimplerLine(Config::get('simplification'));
18 jpm 138
 
2 jpm 139
		$polygone_points = array();
140
		foreach ($coordonnees_simplifiees as $geoPoint) {
141
			$lat = str_replace(',', '.', $geoPoint->latitude);
142
			$lon = str_replace(',', '.', $geoPoint->longitude);
143
			$polygone_points[] = $lon.','.$lat;
144
		}
18 jpm 145
 
2 jpm 146
		$this->traiterInfo("Traitement coordonnées (avt/aprs) : %s/%s", array(count($coordonnees), count($coordonnees_simplifiees)));
147
		return implode(' ', $polygone_points);
148
	}
149
}
150
?>