29 |
alex |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Exemple lancement:
|
|
|
5 |
* /opt/lampp/bin/php -d memory_limit=3500M ~/web/moissonnage/scripts/cli.php tuilage -a genererMaillage
|
|
|
6 |
*
|
|
|
7 |
* @author alex
|
|
|
8 |
*
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
class Tuilage extends Script {
|
|
|
12 |
|
|
|
13 |
|
|
|
14 |
private $bdd = null;
|
|
|
15 |
private $resolution = 0;
|
|
|
16 |
private $resolutionInitiale = 0;
|
|
|
17 |
private $origine = 0;
|
|
|
18 |
private $longueurMaille;
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
public function executer() {
|
|
|
22 |
try {
|
|
|
23 |
$this->initialiserScript();
|
|
|
24 |
$cmd = $this->getParametre('a');
|
|
|
25 |
switch ($cmd) {
|
|
|
26 |
case 'genererMaillage' :
|
|
|
27 |
$this->genererTuilage();
|
|
|
28 |
break;
|
|
|
29 |
default :
|
|
|
30 |
throw new Exception("Erreur : la commande '{$cmd}' n'existe pas!");
|
|
|
31 |
}
|
|
|
32 |
} catch(Exception $e) {
|
|
|
33 |
$this->traiterErreur($e->getMessage());
|
|
|
34 |
}
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
private function initialiserScript() {
|
|
|
38 |
$fichierIni = $this->getScriptChemin().'tuilage.ini';
|
|
|
39 |
if (file_exists($fichierIni)) {
|
|
|
40 |
Config::charger($fichierIni);
|
|
|
41 |
} else {
|
|
|
42 |
$m = "Veuillez configurer le projet en créant le fichier '{$this->projetNom}.ini' ".
|
|
|
43 |
"dans le dossier du module de script du projet à partir du fichier '{$this->projetNom}.defaut.ini'.";
|
|
|
44 |
throw new Exception($m);
|
|
|
45 |
}
|
|
|
46 |
$this->longueurMaille = Config::get('taille_mailles_px');
|
|
|
47 |
$this->resolutionInitiale = 2 * M_PI * Config::get('rayon_terre') / 256;
|
|
|
48 |
$this->origine = 2 * M_PI * Config::get('rayon_terre') / 2.0;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
private function genererTuilage() {
|
|
|
52 |
if (!$this->estTableCreee()) {
|
|
|
53 |
$this->creerStructureTable();
|
30 |
delphine |
54 |
print("Table créée");
|
29 |
alex |
55 |
}
|
|
|
56 |
$niveauxZoom = range(Config::get('zoom_minimal'), Config::get('zoom_maximal'), 1);
|
|
|
57 |
foreach ($niveauxZoom as $zoom) {
|
|
|
58 |
print("Génération des mailles au niveau de zoom {$zoom}...\n");
|
|
|
59 |
$this->resolution = $this->resolutionInitiale / pow(2, $zoom);
|
|
|
60 |
$coordonneesLng = $this->calculerCoordonneesLimitesTuiles($zoom, 'lng');
|
|
|
61 |
$coordonneesLat = $this->calculerCoordonneesLimitesTuiles($zoom, 'lat');
|
30 |
delphine |
62 |
print("mailles calculées\n");
|
29 |
alex |
63 |
$this->ajouterMaillesDansBdd($zoom, $coordonneesLng, $coordonneesLat);
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
private function estTableCreee() {
|
30 |
delphine |
68 |
print("test table");
|
29 |
alex |
69 |
$tables = $this->getBdd()->recupererTous("SHOW TABLES FROM ".Config::get('bdd_nom'));
|
|
|
70 |
$table = Config::get('eflore_table_mailles');
|
30 |
delphine |
71 |
for ($index = 0; $index < count($tables) && $tables[$index]['Tables_in_tb_eflore_test'] != $table; $index ++);
|
29 |
alex |
72 |
return $index < count($tables);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
private function creerStructureTable() {
|
|
|
76 |
$table = Config::get('eflore_table_mailles');
|
|
|
77 |
print("Table {$table} non trouvée dans la base de données, création de sa structure...\n");
|
|
|
78 |
$requete =
|
|
|
79 |
"CREATE TABLE {$table} (".
|
|
|
80 |
"zoom TINYINT UNSIGNED NOT NULL,".
|
|
|
81 |
"axe ENUM('lat','lng') NOT NULL,".
|
|
|
82 |
"position SMALLINT UNSIGNED NOT NULL,".
|
|
|
83 |
"debut DECIMAL(9,6) NOT NULL,".
|
|
|
84 |
"fin DECIMAL(9,6) NOT NULL,".
|
|
|
85 |
"PRIMARY KEY (zoom, axe, position)".
|
|
|
86 |
") ENGINE=MyISAM, CHARACTER SET utf8";
|
|
|
87 |
$this->getBdd()->requeter($requete);
|
|
|
88 |
$requete = "ALTER TABLE {$table} ADD INDEX (debut), ADD INDEX(fin)";
|
|
|
89 |
$this->getBdd()->requeter($requete);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
private function calculerCoordonneesLimitesTuiles($zoom, $axe) {
|
|
|
93 |
$variableConfigLimite = $axe == 'lng' ? 'carte.limite_est' : 'carte.limite_nord';
|
|
|
94 |
$limiteCalcul = Config::get($variableConfigLimite);
|
|
|
95 |
$nombrePixels = pow(2, $zoom+8);
|
|
|
96 |
$tailleTableau = $nombrePixels / $this->longueurMaille;
|
|
|
97 |
$valeurs = array();
|
|
|
98 |
for ($index = 0; $index <= $tailleTableau; $index ++) {
|
|
|
99 |
$valeur = $this->convertirPixelEnLatLng($index * $this->longueurMaille, $axe);
|
|
|
100 |
$valeurs[] = $this->convertirFloatToString($valeur);
|
|
|
101 |
}
|
|
|
102 |
return $valeurs;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
private function convertirPixelEnLatLng($pixel, $axe) {
|
|
|
106 |
$coord_metrique = $pixel * $this->resolution - $this->origine;
|
|
|
107 |
$coord_lnglat = ($coord_metrique / $this->origine) * 180.0;
|
|
|
108 |
if ($axe == 'lat') {
|
|
|
109 |
$coord_lnglat = 180 / M_PI * (2 * atan(exp($coord_lnglat * M_PI / 180.0)) - M_PI / 2.0);
|
|
|
110 |
}
|
|
|
111 |
return $coord_lnglat;
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
private function ajouterMaillesDansBdd($zoom, $coordonneesLng, $coordonneesLat) {
|
|
|
115 |
$table = Config::get('eflore_table_mailles');
|
|
|
116 |
$nomsChamps = Config::get('eflore_champs_table');
|
|
|
117 |
$this->getBdd()->requeter("DELETE FROM {$table} WHERE zoom=$zoom");
|
|
|
118 |
$insertions = array();
|
30 |
delphine |
119 |
$j = 10000;
|
|
|
120 |
if (count($coordonneesLng) > $j) {
|
|
|
121 |
for ($i=0; $i < count($coordonneesLng); $i=$i+$j) {
|
|
|
122 |
$insertions = array();print($i." ".$j." ");
|
|
|
123 |
if ($i+$j > count($coordonneesLng)) {
|
|
|
124 |
$k =count($coordonneesLng) - 1;
|
|
|
125 |
} else {
|
|
|
126 |
$k = $i+$j;
|
|
|
127 |
} print_r($k."\n");
|
|
|
128 |
for ($index = $i; $index < $k; $index ++) {
|
|
|
129 |
$insertions[] = "({$zoom},'lng',{$index},".$coordonneesLng[$index].",".$coordonneesLng[$index+1].")";
|
|
|
130 |
}
|
|
|
131 |
for ($index = $i; $index < $k; $index ++) {
|
|
|
132 |
$insertions[] = "({$zoom},'lat',{$index},".$coordonneesLat[$index].",".$coordonneesLat[$index+1].")";
|
|
|
133 |
}
|
|
|
134 |
$requete = "INSERT INTO {$table}({$nomsChamps}) VALUES ".implode(',', $insertions);
|
|
|
135 |
$this->getBdd()->requeter($requete);
|
|
|
136 |
}
|
|
|
137 |
} else {
|
|
|
138 |
for ($index = 0; $index < count($coordonneesLng)-1; $index ++) {
|
|
|
139 |
$insertions[] = "({$zoom},'lng',{$index},".$coordonneesLng[$index].",".$coordonneesLng[$index+1].")";
|
|
|
140 |
}
|
|
|
141 |
for ($index = 0; $index < count($coordonneesLat)-1; $index ++) {
|
|
|
142 |
$insertions[] = "({$zoom},'lat',{$index},".$coordonneesLat[$index].",".$coordonneesLat[$index+1].")";
|
|
|
143 |
}
|
|
|
144 |
$requete = "INSERT INTO {$table}({$nomsChamps}) VALUES ".implode(',', $insertions);
|
|
|
145 |
$this->getBdd()->requeter($requete);
|
29 |
alex |
146 |
}
|
30 |
delphine |
147 |
|
29 |
alex |
148 |
}
|
|
|
149 |
|
|
|
150 |
private function getBdd() {
|
|
|
151 |
if (is_null($this->bdd)) {
|
|
|
152 |
$this->bdd = new Bdd();
|
|
|
153 |
}
|
|
|
154 |
return $this->bdd;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
private function convertirFloatToString($float) {
|
|
|
158 |
return str_replace(',', '.', strval($float));
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
}
|
|
|
162 |
|
|
|
163 |
?>
|