1046 |
jpm |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Traitement de l'ordre :
|
|
|
4 |
*
|
|
|
5 |
* Fonction qui rajoute l'ordre et le sens de chaque way d'une relation dans la table `osm_relation_a_chemins`
|
|
|
6 |
*
|
|
|
7 |
* Exemple de lancement du script :
|
|
|
8 |
* /opt/lampp/bin/php -d memory_limit=8000M cli.php osm -a ordre -m manuel
|
|
|
9 |
*
|
|
|
10 |
*/
|
|
|
11 |
class PolygoneCreateur {
|
|
|
12 |
private $conteneur;
|
|
|
13 |
private $bdd;
|
|
|
14 |
private $messages;
|
|
|
15 |
private $mode;
|
|
|
16 |
|
|
|
17 |
public function __construct($conteneur) {
|
|
|
18 |
$this->conteneur = $conteneur;
|
|
|
19 |
$this->bdd = $this->conteneur->getBdd();
|
|
|
20 |
$this->messages = $this->conteneur->getMessages();
|
|
|
21 |
$this->mode = $this->conteneur->getParametre('m');
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Fonction qui parcourt tous les ways les noeuds de chaque relation en prenant en considération l'ordre et
|
|
|
26 |
* le sens de chaque ways concaténés ensemble (séparés par des virgules). Update du champ polygone de chaque
|
|
|
27 |
* relation dans la table `osm_relations`
|
|
|
28 |
*/
|
|
|
29 |
public function executer() {
|
|
|
30 |
// Lancement de l'action demandée
|
|
|
31 |
$cmd = $this->conteneur->getParametre('a');
|
|
|
32 |
switch ($cmd) {
|
|
|
33 |
case 'polygone' :
|
|
|
34 |
$this->genererPolygones();
|
|
|
35 |
break;
|
|
|
36 |
case 'centre' :
|
|
|
37 |
$this->mettreAJourCentroide();
|
|
|
38 |
break;
|
|
|
39 |
case 'zero' :
|
|
|
40 |
$this->remettreOrdreAZero();
|
|
|
41 |
break;
|
|
|
42 |
default :
|
|
|
43 |
$msgTpl = "Erreur : la commande '%s' n'est pas prise en compte par la classe %s !";
|
|
|
44 |
$msg = sprintf($msgTpl, $cmd, get_class($this));
|
|
|
45 |
throw new Exception($msg);
|
|
|
46 |
}
|
|
|
47 |
print "\n";// Pour ramener à la ligne en fin de script
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
private function genererPolygones() {
|
|
|
51 |
$relations = $this->getRelations();
|
|
|
52 |
foreach ($relations as $relation) {
|
|
|
53 |
$chemins = $this->getChemins($relation['id_relation']);
|
|
|
54 |
$noeuds = array();
|
|
|
55 |
foreach ($chemins as $chemin) {
|
|
|
56 |
$noeuds = array_merge($noeuds, $this->getNoeuds($chemin['id_chemin'], $chemin['sens']));
|
|
|
57 |
}
|
|
|
58 |
$this->creerPolygone($relation, $noeuds);
|
|
|
59 |
|
|
|
60 |
if ($this->mode == 'manuel') {
|
|
|
61 |
$this->messages->afficherAvancement("Création du polygone pour la relation : ", 1);
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
private function getRelations() {
|
|
|
67 |
$requete = 'SELECT id_relation, nom, code_insee '.
|
|
|
68 |
'FROM osm_relations '.
|
|
|
69 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
70 |
$relations = $this->bdd->recupererTous($requete);
|
|
|
71 |
return $relations;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
private function getChemins($idRelation) {
|
|
|
75 |
$requete = 'SELECT id_chemin, sens '.
|
|
|
76 |
'FROM osm_relation_a_chemins '.
|
|
|
77 |
"WHERE id_relation = $idRelation ".
|
|
|
78 |
'ORDER BY ordre '.
|
|
|
79 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
80 |
$chemins = $this->bdd->recupererTous($requete);
|
|
|
81 |
return $chemins;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
private function getNoeuds($idChemin, $sens) {
|
|
|
85 |
$tri = ($sens == 'directe') ? 'ASC' : 'DESC';
|
|
|
86 |
$requete = 'SELECT NLL.id_noeud, NLL.lat, NLL.`long` '.
|
|
|
87 |
'FROM osm_chemin_a_noeuds AS WN '.
|
|
|
88 |
' INNER JOIN osm_noeuds AS NLL ON (WN.id_noeud = NLL.id_noeud) '.
|
|
|
89 |
"WHERE WN.id_chemin = $idChemin ".
|
|
|
90 |
"ORDER BY WN.ordre $tri ".
|
|
|
91 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
92 |
$noeuds = $this->bdd->recupererTous($requete);
|
|
|
93 |
$latLng = array();
|
|
|
94 |
foreach ($noeuds as $noeud) {
|
|
|
95 |
$latLng[] = $noeud['lat'].' '.$noeud['long'];
|
|
|
96 |
}
|
|
|
97 |
return $latLng;
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
private function creerPolygone($relation, $noeuds) {
|
|
|
101 |
$polygone = implode(', ', $noeuds);
|
|
|
102 |
$idRelation = $relation['id_relation'];
|
|
|
103 |
$nomCommuneP = $this->bdd->proteger($relation['nom']);
|
|
|
104 |
$InseeP = $this->bdd->proteger($relation['code_insee']);
|
|
|
105 |
$note = ($noeuds[0] == $noeuds[count($noeuds) - 1]) ? 'Polygone complet' : 'Polygone incomplet';
|
|
|
106 |
|
|
|
107 |
$requete = 'REPLACE INTO osm_communes (id_relation, nom, code_insee, polygone, notes ) '.
|
|
|
108 |
"VALUES ($idRelation, $nomCommuneP, $InseeP, ".
|
|
|
109 |
"POLYFROMTEXT('MULTIPOLYGON ((($polygone)))'), '$note') ".
|
|
|
110 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
111 |
unset($polygone);
|
|
|
112 |
|
|
|
113 |
$this->bdd->requeter($requete);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* Pour chaque commune, renseigne le champe "centre" avec un point centroïde du polygone (si non null).
|
|
|
118 |
*/
|
|
|
119 |
private function mettreAJourCentroide() {
|
|
|
120 |
$requete = 'UPDATE osm_communes '.
|
|
|
121 |
'SET centre = CENTROID(polygone) '.
|
|
|
122 |
"WHERE polygone IS NOT NULL ".
|
|
|
123 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
124 |
$retour = $this->bdd->requeter($requete);
|
|
|
125 |
$this->messages->traiterInfo("Nombre de centroïde mis à jour : ".$retour->rowCount());
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
/**
|
|
|
129 |
* Pour chaque commune, remet à zéro l'ordre des chemins si le polygone est incomplet.
|
|
|
130 |
*/
|
|
|
131 |
private function remettreOrdreAZero() {
|
|
|
132 |
$sousRequeteRelations = 'SELECT DISTINCT id_relation '.
|
|
|
133 |
'FROM osm_communes '.
|
|
|
134 |
"WHERE notes = 'Polygone incomplet' ";
|
|
|
135 |
|
|
|
136 |
$requete = 'UPDATE osm_relation_a_chemins '.
|
|
|
137 |
'SET ordre = NULL '.
|
|
|
138 |
"WHERE id_relation IN ($sousRequeteRelations) ".
|
|
|
139 |
' -- '.__FILE__.' : '.__LINE__;
|
|
|
140 |
$retour = $this->bdd->requeter($requete);
|
|
|
141 |
echo $requete;
|
|
|
142 |
$this->messages->traiterInfo("Nombre de chemins remis à zéro : ".$retour->rowCount());
|
|
|
143 |
}
|
|
|
144 |
}
|