Subversion Repositories eFlore/Projets.communes

Rev

Rev 9 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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