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();
|
|
|
54 |
}
|
|
|
55 |
$niveauxZoom = range(Config::get('zoom_minimal'), Config::get('zoom_maximal'), 1);
|
|
|
56 |
foreach ($niveauxZoom as $zoom) {
|
|
|
57 |
print("Génération des mailles au niveau de zoom {$zoom}...\n");
|
|
|
58 |
$this->resolution = $this->resolutionInitiale / pow(2, $zoom);
|
|
|
59 |
$coordonneesLng = $this->calculerCoordonneesLimitesTuiles($zoom, 'lng');
|
|
|
60 |
$coordonneesLat = $this->calculerCoordonneesLimitesTuiles($zoom, 'lat');
|
|
|
61 |
$this->ajouterMaillesDansBdd($zoom, $coordonneesLng, $coordonneesLat);
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
private function estTableCreee() {
|
|
|
66 |
$tables = $this->getBdd()->recupererTous("SHOW TABLES FROM ".Config::get('bdd_nom'));
|
|
|
67 |
$table = Config::get('eflore_table_mailles');
|
|
|
68 |
for ($index = 0; $index < count($tables) && $tables[$index]['Tables_in_tb_eflore'] != $table; $index ++);
|
|
|
69 |
return $index < count($tables);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
private function creerStructureTable() {
|
|
|
73 |
$table = Config::get('eflore_table_mailles');
|
|
|
74 |
print("Table {$table} non trouvée dans la base de données, création de sa structure...\n");
|
|
|
75 |
$requete =
|
|
|
76 |
"CREATE TABLE {$table} (".
|
|
|
77 |
"zoom TINYINT UNSIGNED NOT NULL,".
|
|
|
78 |
"axe ENUM('lat','lng') NOT NULL,".
|
|
|
79 |
"position SMALLINT UNSIGNED NOT NULL,".
|
|
|
80 |
"debut DECIMAL(9,6) NOT NULL,".
|
|
|
81 |
"fin DECIMAL(9,6) NOT NULL,".
|
|
|
82 |
"PRIMARY KEY (zoom, axe, position)".
|
|
|
83 |
") ENGINE=MyISAM, CHARACTER SET utf8";
|
|
|
84 |
$this->getBdd()->requeter($requete);
|
|
|
85 |
$requete = "ALTER TABLE {$table} ADD INDEX (debut), ADD INDEX(fin)";
|
|
|
86 |
$this->getBdd()->requeter($requete);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
private function calculerCoordonneesLimitesTuiles($zoom, $axe) {
|
|
|
90 |
$variableConfigLimite = $axe == 'lng' ? 'carte.limite_est' : 'carte.limite_nord';
|
|
|
91 |
$limiteCalcul = Config::get($variableConfigLimite);
|
|
|
92 |
$nombrePixels = pow(2, $zoom+8);
|
|
|
93 |
$tailleTableau = $nombrePixels / $this->longueurMaille;
|
|
|
94 |
$valeurs = array();
|
|
|
95 |
for ($index = 0; $index <= $tailleTableau; $index ++) {
|
|
|
96 |
$valeur = $this->convertirPixelEnLatLng($index * $this->longueurMaille, $axe);
|
|
|
97 |
$valeurs[] = $this->convertirFloatToString($valeur);
|
|
|
98 |
}
|
|
|
99 |
return $valeurs;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
private function convertirPixelEnLatLng($pixel, $axe) {
|
|
|
103 |
$coord_metrique = $pixel * $this->resolution - $this->origine;
|
|
|
104 |
$coord_lnglat = ($coord_metrique / $this->origine) * 180.0;
|
|
|
105 |
if ($axe == 'lat') {
|
|
|
106 |
$coord_lnglat = 180 / M_PI * (2 * atan(exp($coord_lnglat * M_PI / 180.0)) - M_PI / 2.0);
|
|
|
107 |
}
|
|
|
108 |
return $coord_lnglat;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
private function ajouterMaillesDansBdd($zoom, $coordonneesLng, $coordonneesLat) {
|
|
|
112 |
$table = Config::get('eflore_table_mailles');
|
|
|
113 |
$nomsChamps = Config::get('eflore_champs_table');
|
|
|
114 |
$this->getBdd()->requeter("DELETE FROM {$table} WHERE zoom=$zoom");
|
|
|
115 |
$insertions = array();
|
|
|
116 |
for ($index = 0; $index < count($coordonneesLng)-1; $index ++) {
|
|
|
117 |
$insertions[] = "({$zoom},'lng',{$index},".$coordonneesLng[$index].",".$coordonneesLng[$index+1].")";
|
|
|
118 |
}
|
|
|
119 |
for ($index = 0; $index < count($coordonneesLat)-1; $index ++) {
|
|
|
120 |
$insertions[] = "({$zoom},'lat',{$index},".$coordonneesLat[$index].",".$coordonneesLat[$index+1].")";
|
|
|
121 |
}
|
|
|
122 |
$requete = "INSERT INTO {$table}({$nomsChamps}) VALUES ".implode(',', $insertions);
|
|
|
123 |
$this->getBdd()->requeter($requete);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
private function getBdd() {
|
|
|
127 |
if (is_null($this->bdd)) {
|
|
|
128 |
$this->bdd = new Bdd();
|
|
|
129 |
}
|
|
|
130 |
return $this->bdd;
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
private function convertirFloatToString($float) {
|
|
|
134 |
return str_replace(',', '.', strval($float));
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
?>
|