1058 |
jpm |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Exemple de lancement du script :
|
|
|
4 |
*
|
|
|
5 |
* Analyse du fichir OSM :
|
|
|
6 |
* /opt/lampp/bin/php cli.php osm -a analyser -f "../donnees/osm/france_communes_new.osm" -m manuel
|
|
|
7 |
*
|
|
|
8 |
*/
|
|
|
9 |
class ParseurOsm {
|
|
|
10 |
|
|
|
11 |
private $conteneur;
|
|
|
12 |
private $bdd;
|
|
|
13 |
private $messages;
|
|
|
14 |
private $mode;
|
|
|
15 |
|
|
|
16 |
private $communes = array();
|
|
|
17 |
private $relations_communes = array();
|
|
|
18 |
private $relation_a_chemins = array();
|
|
|
19 |
private $chemin_a_noeuds = array();
|
|
|
20 |
private $noeuds = array();
|
|
|
21 |
private $pas = 10000;
|
|
|
22 |
private $pas_commune = 1000;
|
|
|
23 |
|
|
|
24 |
public function __construct($conteneur) {
|
|
|
25 |
$this->conteneur = $conteneur;
|
|
|
26 |
$this->bdd = $this->conteneur->getBdd();
|
|
|
27 |
$this->messages = $this->conteneur->getMessages();
|
|
|
28 |
$this->mode = $this->conteneur->getParametre('m');
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
public function executer() {
|
|
|
32 |
// Lancement de l'action demandée
|
|
|
33 |
$cmd = $this->conteneur->getParametre('a');
|
|
|
34 |
switch ($cmd) {
|
|
|
35 |
case 'analyser' :
|
|
|
36 |
$this->lireFichierOsm();
|
|
|
37 |
break;
|
|
|
38 |
case 'polygone' :
|
|
|
39 |
$this->remplirPolygone();
|
|
|
40 |
break;
|
|
|
41 |
case 'zero' :
|
|
|
42 |
$this->remettreOrdreAZero();
|
|
|
43 |
break;
|
|
|
44 |
default :
|
|
|
45 |
$this->messages->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
|
|
|
46 |
}
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Lit le fichier OSM et lance l'analyse des noeuds xml en fonction de leur type.
|
|
|
51 |
*/
|
|
|
52 |
private function lireFichierOsm() {
|
|
|
53 |
$lecteur = $this->getLecteurXml();
|
|
|
54 |
while ($lecteur->read()) {
|
|
|
55 |
if ($lecteur->nodeType == XMLREADER::ELEMENT) {
|
|
|
56 |
$this->analyserElementXml($lecteur->localName, $lecteur->expand());
|
|
|
57 |
}
|
|
|
58 |
if ($this->mode == 'manuel') {
|
|
|
59 |
$this->messages->afficherAvancement("Analyse de la ligne du fichier OSM : ", 1);
|
|
|
60 |
}
|
|
|
61 |
}
|
|
|
62 |
$this->insererElementsOrphelins();
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
private function analyserElementXml($elementNom, $noeudDom) {
|
|
|
66 |
switch ($elementNom) {
|
|
|
67 |
case 'relation' :
|
|
|
68 |
$this->analyserRelation($noeudDom);
|
|
|
69 |
break;
|
|
|
70 |
case 'way' :
|
|
|
71 |
$this->analyserWay($noeudDom);
|
|
|
72 |
break;
|
|
|
73 |
case 'node' :
|
|
|
74 |
$this->analyserNode($noeudDom);
|
|
|
75 |
break;
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
private function insererElementsOrphelins() {
|
|
|
80 |
$this->insererRelationsCommunes();
|
|
|
81 |
$this->insererRelationAChemins();
|
|
|
82 |
$this->insererCheminANoeuds();
|
|
|
83 |
$this->insererNoeuds();
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
private function getLecteurXml() {
|
|
|
87 |
$fichierOsm = $this->conteneur->getParametre('f');
|
|
|
88 |
$lecteur = new XMLReader();
|
|
|
89 |
$ouvertureXmlOk = $lecteur->open($fichierOsm);
|
|
|
90 |
if ($ouvertureXmlOk === false) {
|
|
|
91 |
$msgTpl = "Impossible d'ouvrir le fichier osm : %s";
|
|
|
92 |
$msg = sprintf($msgTpl, $this->conteneur->getParametre('f'));
|
|
|
93 |
new Exception($msg);
|
|
|
94 |
}
|
|
|
95 |
return $lecteur;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
/**
|
|
|
99 |
* Récupère l'id commune, nom commune et le code INSEE et remplie la table `CommuneOSM`
|
|
|
100 |
*/
|
|
|
101 |
private function analyserRelation($relation) {
|
|
|
102 |
$idRelation = $relation->getAttribute('id');
|
|
|
103 |
|
|
|
104 |
$chemins = $relation->getElementsByTagName('member');
|
|
|
105 |
$this->analyserChemins($idRelation, $chemins);
|
|
|
106 |
if (count($this->relation_a_chemins) > $this->pas) {
|
|
|
107 |
$this->insererRelationAChemins();
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
$tags = $relation->getElementsByTagName('tag');
|
|
|
111 |
$this->analyserTags($idRelation, $tags);
|
|
|
112 |
if (count($this->relations_communes) > $this->pas_commune) {
|
|
|
113 |
$this->insererRelationsCommunes();
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
private function analyserChemins($relation_id, $chemins) {
|
|
|
118 |
$ordreChemin = 1;
|
|
|
119 |
foreach ($chemins as $chemin) {
|
|
|
120 |
if ($chemin->getAttribute('type') == 'way') { //écarter le noeud centrale
|
|
|
121 |
$chemin_id = $chemin->getAttribute('ref');
|
|
|
122 |
$role = $chemin->getAttribute('role');//role: null, inner, outer, exclave et enclave.
|
|
|
123 |
$this->relation_a_chemins[] = array($relation_id, $chemin_id, $role, $ordreChemin++);
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
private function analyserTags($relation_id, $tags) {
|
|
|
129 |
$commune_nom = null;
|
|
|
130 |
$commune_insee = null;
|
|
|
131 |
foreach ($tags as $tag) {
|
|
|
132 |
$tag_cle = $tag->getAttribute('k');
|
|
|
133 |
$tag_val = $tag->getAttribute('v');
|
|
|
134 |
|
|
|
135 |
switch ($tag_cle) {
|
|
|
136 |
case 'name' :
|
|
|
137 |
$commune_nom = $tag_val;
|
|
|
138 |
break;
|
|
|
139 |
case 'ref:INSEE' :
|
|
|
140 |
$commune_insee = $tag_val;
|
|
|
141 |
break;
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
if (!is_null($commune_nom) && !is_null($commune_insee)) {
|
|
|
145 |
|
|
|
146 |
if (!isset($this->relations_communes[$relation_id])) {
|
|
|
147 |
$this->relations_communes[$relation_id] = array($relation_id, $commune_nom, $commune_insee);
|
|
|
148 |
} else {
|
|
|
149 |
$e = "La relation #%s possédant le tag ref:INSEE «%s» est déjà prise en compte.";
|
|
|
150 |
$this->messages->traiterErreur($e, array($relation_id, $commune_insee));
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
if (!isset($this->communes[$commune_insee])) {
|
|
|
154 |
$this->communes[$commune_insee] = $relation_id;
|
|
|
155 |
} else {
|
|
|
156 |
$e = "La relation #%s contient déjà le tag ref:INSEE avec la valeur %s.".
|
|
|
157 |
"Veuillez corriger la carte OSM.";
|
|
|
158 |
$this->messages->traiterErreur($e, array($this->communes[$commune_insee], $commune_insee, $relation_id));
|
|
|
159 |
}
|
|
|
160 |
break;// Stoppe le foreach car nous avons les infos nécessaires
|
|
|
161 |
}
|
|
|
162 |
}
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Récupère l'id_way et tous les id_node de chaque way et remplie la table `osm_chemin_a_noeuds`
|
|
|
167 |
*/
|
|
|
168 |
private function analyserWay($way) {
|
|
|
169 |
$chemin_id = $way->getAttribute('id');
|
|
|
170 |
$ordre = 1;
|
|
|
171 |
$noeuds = $way->getElementsByTagName('nd');
|
|
|
172 |
foreach ($noeuds as $noeud) {
|
|
|
173 |
$noeud_id = $noeud->getAttribute('ref');
|
|
|
174 |
$this->chemin_a_noeuds[] = array($chemin_id, $noeud_id, $ordre++);
|
|
|
175 |
}
|
|
|
176 |
|
|
|
177 |
if (count($this->chemin_a_noeuds) > $this->pas) {
|
|
|
178 |
$this->insererCheminANoeuds();
|
|
|
179 |
}
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* Fonction qui récupère tous les l'id_node et les valeurs(Lat/Lon) correspondantes et remplie la table `osm_noeuds`
|
|
|
184 |
*/
|
|
|
185 |
private function analyserNode($node) {
|
|
|
186 |
$noeud_id = $node->getAttribute('id');
|
|
|
187 |
$lat = $node->getAttribute('lat');
|
|
|
188 |
$lon = $node->getAttribute('lon');
|
|
|
189 |
$this->noeuds[] = array($noeud_id, $lat, $lon);
|
|
|
190 |
|
|
|
191 |
if (count($this->noeuds) > $this->pas) {
|
|
|
192 |
$this->insererNoeuds();
|
|
|
193 |
}
|
|
|
194 |
}
|
|
|
195 |
|
|
|
196 |
private function insererRelationsCommunes() {
|
|
|
197 |
if (count($this->relations_communes) > 0) {
|
|
|
198 |
$requete = 'INSERT INTO osm_relations (id_relation, nom, code_insee) '.
|
|
|
199 |
'VALUES '.$this->creerValuesMultiple($this->relations_communes).
|
|
|
200 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
201 |
$this->bdd->requeter($requete);
|
|
|
202 |
}
|
|
|
203 |
$this->relations_communes = array();
|
|
|
204 |
}
|
|
|
205 |
|
|
|
206 |
private function insererRelationAChemins() {
|
|
|
207 |
if (count($this->relation_a_chemins) > 0) {
|
|
|
208 |
$requete = 'INSERT INTO osm_relation_a_chemins (id_relation, id_chemin, role, ordre) '.
|
|
|
209 |
'VALUES '.$this->creerValuesMultiple($this->relation_a_chemins).
|
|
|
210 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
211 |
$this->bdd->requeter($requete);
|
|
|
212 |
}
|
|
|
213 |
$this->relation_a_chemins = array();
|
|
|
214 |
}
|
|
|
215 |
|
|
|
216 |
private function insererCheminANoeuds() {
|
|
|
217 |
if (count($this->chemin_a_noeuds) > 0) {
|
|
|
218 |
$requete = 'INSERT INTO osm_chemin_a_noeuds (id_chemin, id_noeud, ordre) '.
|
|
|
219 |
'VALUES '.$this->creerValuesMultiple($this->chemin_a_noeuds).
|
|
|
220 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
221 |
$this->bdd->requeter($requete);
|
|
|
222 |
}
|
|
|
223 |
$this->chemin_a_noeuds = array();
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
private function insererNoeuds() {
|
|
|
227 |
if (count($this->noeuds) > 0) {
|
|
|
228 |
$requete = 'INSERT INTO osm_noeuds (id_noeud, lat, `long`) '.
|
|
|
229 |
'VALUES '.$this->creerValuesMultiple($this->noeuds).
|
|
|
230 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
231 |
$this->bdd->requeter($requete);
|
|
|
232 |
}
|
|
|
233 |
$this->noeuds = array();
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
private function creerValuesMultiple($donnees) {
|
|
|
237 |
$values = array();
|
|
|
238 |
foreach ($donnees as $infos) {
|
|
|
239 |
$infosP = $this->bdd->proteger($infos);
|
|
|
240 |
$values[] = implode(',', $infosP);
|
|
|
241 |
}
|
|
|
242 |
$valuesClause = '('.implode('),(', $values).')';
|
|
|
243 |
return $valuesClause;
|
|
|
244 |
}
|
|
|
245 |
}
|