Subversion Repositories eFlore/Projets.communes

Rev

Rev 5 | Go to most recent revision | Details | 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 {
15
 
16
	private $fichier_id = '';
17
 
18
	private $coordonnees_simples = array();
19
 
20
	protected $parametres_autorises = array(
21
		'-f' => array(true, true, 'Fichier KML à nettoyer.'));
22
 
23
	public function executer() {
24
		// Récupération de paramêtres
25
 
26
		// Lancement de l'action demandée
27
		$cmd = $this->getParametre('a');
28
	    switch ($cmd) {
29
			case 'nettoyer' :
30
				$this->executerNettoyage();
31
				break;
32
			default :
33
				$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
34
		}
35
    }
36
 
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})/', $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
 
52
	    	$num = 1;
53
	    	$pas = 2750000;
54
	    	$taille = 0;
55
	    	$nbre_placemarks = count($placemarks);
56
	    	foreach ($placemarks as $id => $placemark) {
57
	    		$placemarks_portion[] = $placemark;
58
	    		$taille += mb_strlen($placemark);
59
	    		if ($taille > $pas || $id == ($nbre_placemarks - 1)) {
60
			    	$taille = 0;
61
			    	$donnees = array('nom' => $nom_kml, 'placemarks' => $placemarks_portion);
62
 
63
			    	$sortie = SquelettePhp::analyser($squelette, $donnees);
64
			    	$suffixe_fichier = ($id == ($nbre_placemarks - 1) && $num == 1) ? '' : '_'.$num++;
65
			    	if (file_put_contents(sprintf($fichier_sortie, $suffixe_fichier), $sortie)) {
66
			    		@chmod($fichier_sortie, octdec(Config::get('fichierSortieDroits')));
67
			    	} else {
68
			    		trigger_error("Impossible d'écrire dans le fichier : $fichier_sortie", E_USER_WARNING);
69
			    	}
70
			    	$placemarks_portion = array();
71
	    		}
72
	    	}
73
    	}
74
    }
75
 
76
    private function analyserFichierKml($fichier_kml) {
77
    	$placemarks = array();
78
    	$lignesKML = file($fichier_kml, FILE_SKIP_EMPTY_LINES);
79
    	foreach ($lignesKML as $id => $ligne) {
80
    		if (preg_match('/^\s*<Placemark>/', $ligne)) {
81
    			if (isset($placemark)) {
82
    				$placemarks[] = $placemark;
83
    			}
84
    			$placemark = '';
85
    		}
86
 
87
    		$this->trouverFichierId($ligne);
88
    		if (isset($placemark)) {
89
	    		$ligne = $this->remplacerChaines($ligne);
90
    			$placemark .= $this->traiterLigne($ligne);
91
 
92
    		}
93
    	}
94
    	return $placemarks;
95
    }
96
    private function trouverFichierId($ligne) {
97
        if (preg_match('/<Document><Folder><name>([^<]+)<\/name>/', $ligne, $match)) {
98
    		$this->fichier_id = $match[1];
99
    	}
100
    }
101
 
102
    private function remplacerChaines($ligne) {
103
		$ligne = preg_replace("/^\s+(<[\/]?Placemark>)/", "\t\t$1", $ligne);
104
		$ligne = preg_replace("/^\s+(<name>)/", "\t\t\t$1", $ligne);
105
		$ligne = preg_replace("/(<ExtendedData>)(<SchemaData)/", "\t\t$1\n\t\t\t\t$2", $ligne);
106
		$ligne = preg_replace("/(<\/SchemaData>)(<\/ExtendedData>)/", "\t\t\t$1\n\t\t\t$2", $ligne);
107
	    $ligne = preg_replace('/^\t+(<SimpleData name=")CODE_INSEE(">)/', "\t\t\t\t\t$1code_insee$2", $ligne);
108
	    $ligne = preg_replace('/(schemaUrl="#)'.$this->fichier_id.'(")/', "$1commune$2", $ligne);
109
	    return $ligne;
110
    }
111
 
112
    private function traiterLigne($ligne) {
113
    	$ligne_traitee = $ligne;
114
    	if (preg_match('/^\s*(<Polygon><outerBoundaryIs><LinearRing><coordinates>)([^<]+)([^$]+)/', $ligne, $match)) {
115
    		$debut_chaine_polygone = $match[1];
116
    		$coordonnees = $this->traiterCoordonnees($match[2]);
117
    		$fin_chaine_polygone = $match[3];
118
    		$ligne_traitee = "\t\t\t".$debut_chaine_polygone.$coordonnees.$fin_chaine_polygone;
119
    	} else if (preg_match('/^\s*<Style>.*<\/Style>$/u', $ligne)) {
120
    		$url_style = Config::get('urlStyle');
121
    		$ligne_traitee = "\t\t\t<styleUrl>$url_style#commune-limite</styleUrl>\n";
122
    	} else if (preg_match('/^\t+<SimpleData name="Name">/', $ligne)) {
123
    		 // Ne pas prendre en compte cette ligne
124
    	} else {
125
			$ligne_traitee = $ligne;
126
		}
127
		return $ligne_traitee;
128
	}
129
 
130
	private function traiterCoordonnees($chaine_coordonnees) {
131
		$coordonnees = explode(' ', $chaine_coordonnees);
132
		$geo_points = array();
133
		foreach ($coordonnees as $coord) {
134
			list($lon, $lat) = explode(',', $coord);
135
			$geo_points[] = new GeoPoint($lat, $lon);
136
		}
137
		$reducteur = new PolylineReducer($geo_points);
138
		$coordonnees_simplifiees = $reducteur->SimplerLine(0.0001);
139
 
140
		$polygone_points = array();
141
		foreach ($coordonnees_simplifiees as $geoPoint) {
142
			$lat = str_replace(',', '.', $geoPoint->latitude);
143
			$lon = str_replace(',', '.', $geoPoint->longitude);
144
			$polygone_points[] = $lon.','.$lat;
145
		}
146
 
147
		$this->traiterInfo("Traitement coordonnées (avt/aprs) : %s/%s", array(count($coordonnees), count($coordonnees_simplifiees)));
148
		return implode(' ', $polygone_points);
149
	}
150
}
151
?>