Subversion Repositories eFlore/Projets.eflore-projets

Rev

Rev 1060 | 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' :
29
				$this->creerTableOsmZonesAdmin();
30
				break;
31
			case 'zaCorriger' :
32
				$this->corrigerTableOsmZonesAdmin();
33
				break;
34
			case 'zaMajIdZG' :
35
				$this->mettreAJourIdZoneGeo();
36
				break;
37
			case 'zaSupprimer' :
38
				$this->supprimerTableOsmZonesAdmin();
39
				break;
40
			default :
41
				$this->messages->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
42
		}
43
 
44
	}
45
 
46
	private function supprimerTableOsmZonesAdmin() {
47
		$requete = 'DROP TABLE IF EXISTS osm_zones_admin '.
48
			' -- '.__FILE__.' : '.__LINE__;
49
		$this->bdd->requeter($requete);
50
	}
51
 
52
	private function creerTableOsmZonesAdmin() {
53
		$requete = 'INSERT INTO osm_zones_admin '.
54
			"SELECT if(ref_insee != '' AND admin_level = 8, CONCAT('INSEE-C:',ref_insee), ".
55
			"if(ref_insee != '' AND admin_level = 6, CONCAT('INSEE-D:',ref_insee), ".
56
			"if(ref_insee != '' AND admin_level = 4, CONCAT('INSEE-R:',ref_insee), ".
57
			"if(iso3166_2 != '', CONCAT('ISO-3166-2:',iso3166_2), ".
58
			"if(iso3166_1 != '', CONCAT('ISO-3166-1:',iso3166_1), ".
59
			"CONCAT('OSM-ID:',osm_id)))))), ".
60
			'osm_id, osm_version, osm_timestamp, '.
61
			"if(name_fr != '', name_fr, name), ".
62
			'shape, shape_centroid, Y(shape_centroid), X(shape_centroid), '.
63
			'admin_level, iso3166_1, iso3166_2, ref_insee, ref_nuts, '.
64
			'name, name_fr, name_en, name_es, wikipedia '.
65
			"FROM {$this->bddOsmNom}.multipolygons_ref ".
66
			' -- '.__FILE__.' : '.__LINE__;
67
		$this->bdd->requeter($requete);
68
	}
69
 
70
	private function corrigerTableOsmZonesAdmin() {
71
		$requete = 'SELECT osm_id, other_tags '.
72
			"FROM {$this->bddOsmNom}.multipolygons_ref ".
73
			"WHERE iso3166_2 IS NULL AND other_tags LIKE '%\"iso3166-2\"%' ".
74
			' -- '.__FILE__.' : '.__LINE__;
75
		$resultat = $this->bdd->recupererTous($requete);
76
		if ($resultat) {
77
			foreach ($resultat as $infos) {
78
				$tags = $this->getTags($infos['other_tags']);
79
				$iso_3166_2 = $this->bdd->proteger($tags['iso3166-2']);
80
				$osm_id = $this->bdd->proteger($infos['osm_id']);
81
 
82
				$requete = 'UPDATE osm_zones_admin '.
83
					"SET code_iso_3166_2 = $iso_3166_2 ".
84
					"WHERE osm_id = $osm_id ".
85
					' -- '.__FILE__.' : '.__LINE__;
86
				$this->bdd->requeter($requete);
87
			}
88
		}
89
	}
90
 
91
	private function getTags($tagsInfos) {
92
		$tags = array();
93
		$clesValeurs = explode(',', $tagsInfos);
94
		foreach ($clesValeurs as $cleValeur) {
95
			list($cle, $val) = explode('=>', $cleValeur);
96
			$tags[trim($cle, '"')] = trim($val, '"');
97
		}
98
		return $tags;
99
	}
100
 
101
	private function mettreAJourIdZoneGeo() {
102
		$requete = 'UPDATE osm_zones_admin '.
103
			"SET id_zone_geo = if(code_insee != '' AND niveau = 8, CONCAT('INSEE-C:',code_insee), ".
104
			"if(code_insee != '' AND niveau = 6, CONCAT('INSEE-D:',code_insee), ".
105
			"if(code_insee != '' AND niveau = 4, CONCAT('INSEE-R:',code_insee), ".
106
			"if(code_iso_3166_2 != '', CONCAT('ISO-3166-2:',code_iso_3166_2), ".
107
			"if(code_iso_3166_1 != '', CONCAT('ISO-3166-1:',code_iso_3166_1), ".
108
			"CONCAT('OSM-ID:',osm_id)))))) ".
109
			' -- '.__FILE__.' : '.__LINE__;
110
		$this->bdd->requeter($requete);
111
	}
112
}