Subversion Repositories eFlore/Projets.eflore-projets

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1056 jpm 1
<?php
2
/**
3
 * Création de la table osm_communes.
4
 * /opt/lampp/bin/php cli.php osm -a communes -m manuel
5
 *
6
 */
7
class ZonesAdmin {
8
 
9
	private $conteneur;
10
	private $bdd;
11
	private $messages;
12
	private $mode;
13
 
14
	private $bddOsmNom;
15
 
16
	public function __construct($conteneur) {
17
		$this->conteneur = $conteneur;
18
		$this->bdd = $this->conteneur->getBdd();
19
		$this->bddOsmNom = $this->conteneur->getParametre('bdd_osm_nom');
20
		$this->messages = $this->conteneur->getMessages();
21
		$this->mode = $this->conteneur->getParametre('m');
22
	}
23
 
24
	public function executer() {
25
		// Lancement de l'action demandée
26
		$cmd = $this->conteneur->getParametre('a');
27
		switch ($cmd) {
28
			case 'za' :
1060 jpm 29
				$this->supprimerTableOsmZonesAdmin();
30
				$this->conteneur->getEfloreCommun()->chargerStructureSql();
1056 jpm 31
				$this->creerTableOsmZonesAdmin();
1060 jpm 32
				$this->corrigerTableOsmZonesAdmin();
33
				$this->mettreAJourIdZoneGeo();
1056 jpm 34
				break;
1060 jpm 35
			case 'zaCreer' :
36
				$this->creerTableOsmZonesAdmin();
37
				break;
1056 jpm 38
			case 'zaCorriger' :
39
				$this->corrigerTableOsmZonesAdmin();
40
				break;
41
			case 'zaMajIdZG' :
42
				$this->mettreAJourIdZoneGeo();
43
				break;
44
			case 'zaSupprimer' :
45
				$this->supprimerTableOsmZonesAdmin();
46
				break;
47
			default :
48
				$this->messages->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
49
		}
50
 
51
	}
52
 
53
	private function supprimerTableOsmZonesAdmin() {
54
		$requete = 'DROP TABLE IF EXISTS osm_zones_admin '.
55
			' -- '.__FILE__.' : '.__LINE__;
56
		$this->bdd->requeter($requete);
57
	}
58
 
59
	private function creerTableOsmZonesAdmin() {
60
		$requete = 'INSERT INTO osm_zones_admin '.
61
			"SELECT if(ref_insee != '' AND admin_level = 8, CONCAT('INSEE-C:',ref_insee), ".
62
			"if(ref_insee != '' AND admin_level = 6, CONCAT('INSEE-D:',ref_insee), ".
63
			"if(ref_insee != '' AND admin_level = 4, CONCAT('INSEE-R:',ref_insee), ".
64
			"if(iso3166_2 != '', CONCAT('ISO-3166-2:',iso3166_2), ".
65
			"if(iso3166_1 != '', CONCAT('ISO-3166-1:',iso3166_1), ".
66
			"CONCAT('OSM-ID:',osm_id)))))), ".
67
			'osm_id, osm_version, osm_timestamp, '.
68
			"if(name_fr != '', name_fr, name), ".
1072 jpm 69
			'shape, shape_centroid, Y(shape_centroid), X(shape_centroid), zone, '.
1056 jpm 70
			'admin_level, iso3166_1, iso3166_2, ref_insee, ref_nuts, '.
71
			'name, name_fr, name_en, name_es, wikipedia '.
72
			"FROM {$this->bddOsmNom}.multipolygons_ref ".
73
			' -- '.__FILE__.' : '.__LINE__;
74
		$this->bdd->requeter($requete);
75
	}
76
 
77
	private function corrigerTableOsmZonesAdmin() {
78
		$requete = 'SELECT osm_id, other_tags '.
79
			"FROM {$this->bddOsmNom}.multipolygons_ref ".
80
			"WHERE iso3166_2 IS NULL AND other_tags LIKE '%\"iso3166-2\"%' ".
81
			' -- '.__FILE__.' : '.__LINE__;
82
		$resultat = $this->bdd->recupererTous($requete);
83
		if ($resultat) {
84
			foreach ($resultat as $infos) {
85
				$tags = $this->getTags($infos['other_tags']);
86
				$iso_3166_2 = $this->bdd->proteger($tags['iso3166-2']);
87
				$osm_id = $this->bdd->proteger($infos['osm_id']);
88
 
89
				$requete = 'UPDATE osm_zones_admin '.
90
					"SET code_iso_3166_2 = $iso_3166_2 ".
91
					"WHERE osm_id = $osm_id ".
92
					' -- '.__FILE__.' : '.__LINE__;
93
				$this->bdd->requeter($requete);
94
			}
95
		}
96
	}
97
 
98
	private function getTags($tagsInfos) {
99
		$tags = array();
100
		$clesValeurs = explode(',', $tagsInfos);
101
		foreach ($clesValeurs as $cleValeur) {
102
			list($cle, $val) = explode('=>', $cleValeur);
103
			$tags[trim($cle, '"')] = trim($val, '"');
104
		}
105
		return $tags;
106
	}
107
 
108
	private function mettreAJourIdZoneGeo() {
109
		$requete = 'UPDATE osm_zones_admin '.
110
			"SET id_zone_geo = if(code_insee != '' AND niveau = 8, CONCAT('INSEE-C:',code_insee), ".
111
			"if(code_insee != '' AND niveau = 6, CONCAT('INSEE-D:',code_insee), ".
112
			"if(code_insee != '' AND niveau = 4, CONCAT('INSEE-R:',code_insee), ".
113
			"if(code_iso_3166_2 != '', CONCAT('ISO-3166-2:',code_iso_3166_2), ".
114
			"if(code_iso_3166_1 != '', CONCAT('ISO-3166-1:',code_iso_3166_1), ".
115
			"CONCAT('OSM-ID:',osm_id)))))) ".
116
			' -- '.__FILE__.' : '.__LINE__;
117
		$this->bdd->requeter($requete);
118
	}
119
}