Rev 29 | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php
/**
* Exemple lancement:
* /opt/lampp/bin/php -d memory_limit=3500M ~/web/moissonnage/scripts/cli.php tuilage -a genererMaillage
*
* @author alex
*
*/
class Tuilage extends Script {
private $bdd = null;
private $resolution = 0;
private $resolutionInitiale = 0;
private $origine = 0;
private $longueurMaille;
public function executer() {
try {
$this->initialiserScript();
$cmd = $this->getParametre('a');
switch ($cmd) {
case 'genererMaillage' :
$this->genererTuilage();
break;
default :
throw new Exception("Erreur : la commande '{$cmd}' n'existe pas!");
}
} catch(Exception $e) {
$this->traiterErreur($e->getMessage());
}
}
private function initialiserScript() {
$fichierIni = $this->getScriptChemin().'tuilage.ini';
if (file_exists($fichierIni)) {
Config::charger($fichierIni);
} else {
$m = "Veuillez configurer le projet en créant le fichier '{$this->projetNom}.ini' ".
"dans le dossier du module de script du projet à partir du fichier '{$this->projetNom}.defaut.ini'.";
throw new Exception($m);
}
$this->longueurMaille = Config::get('taille_mailles_px');
$this->resolutionInitiale = 2 * M_PI * Config::get('rayon_terre') / 256;
$this->origine = 2 * M_PI * Config::get('rayon_terre') / 2.0;
}
private function genererTuilage() {
if (!$this->estTableCreee()) {
$this->creerStructureTable();
print("Table créée");
}
$niveauxZoom = range(Config::get('zoom_minimal'), Config::get('zoom_maximal'), 1);
foreach ($niveauxZoom as $zoom) {
print("Génération des mailles au niveau de zoom {$zoom}...\n");
$this->resolution = $this->resolutionInitiale / pow(2, $zoom);
$coordonneesLng = $this->calculerCoordonneesLimitesTuiles($zoom, 'lng');
$coordonneesLat = $this->calculerCoordonneesLimitesTuiles($zoom, 'lat');
print("mailles calculées\n");
$this->ajouterMaillesDansBdd($zoom, $coordonneesLng, $coordonneesLat);
}
}
private function estTableCreee() {
print("test table");
$tables = $this->getBdd()->recupererTous("SHOW TABLES FROM ".Config::get('bdd_nom'));
$table = Config::get('eflore_table_mailles');
for ($index = 0; $index < count($tables) && $tables[$index]['Tables_in_tb_eflore_test'] != $table; $index ++);
return $index < count($tables);
}
private function creerStructureTable() {
$table = Config::get('eflore_table_mailles');
print("Table {$table} non trouvée dans la base de données, création de sa structure...\n");
$requete =
"CREATE TABLE {$table} (".
"zoom TINYINT UNSIGNED NOT NULL,".
"axe ENUM('lat','lng') NOT NULL,".
"position SMALLINT UNSIGNED NOT NULL,".
"debut DECIMAL(9,6) NOT NULL,".
"fin DECIMAL(9,6) NOT NULL,".
"PRIMARY KEY (zoom, axe, position)".
") ENGINE=MyISAM, CHARACTER SET utf8";
$this->getBdd()->requeter($requete);
$requete = "ALTER TABLE {$table} ADD INDEX (debut), ADD INDEX(fin)";
$this->getBdd()->requeter($requete);
}
private function calculerCoordonneesLimitesTuiles($zoom, $axe) {
$variableConfigLimite = $axe == 'lng' ? 'carte.limite_est' : 'carte.limite_nord';
$limiteCalcul = Config::get($variableConfigLimite);
$nombrePixels = pow(2, $zoom+8);
$tailleTableau = $nombrePixels / $this->longueurMaille;
$valeurs = array();
for ($index = 0; $index <= $tailleTableau; $index ++) {
$valeur = $this->convertirPixelEnLatLng($index * $this->longueurMaille, $axe);
$valeurs[] = $this->convertirFloatToString($valeur);
}
return $valeurs;
}
private function convertirPixelEnLatLng($pixel, $axe) {
$coord_metrique = $pixel * $this->resolution - $this->origine;
$coord_lnglat = ($coord_metrique / $this->origine) * 180.0;
if ($axe == 'lat') {
$coord_lnglat = 180 / M_PI * (2 * atan(exp($coord_lnglat * M_PI / 180.0)) - M_PI / 2.0);
}
return $coord_lnglat;
}
private function ajouterMaillesDansBdd($zoom, $coordonneesLng, $coordonneesLat) {
$table = Config::get('eflore_table_mailles');
$nomsChamps = Config::get('eflore_champs_table');
$this->getBdd()->requeter("DELETE FROM {$table} WHERE zoom=$zoom");
$insertions = array();
$j = 10000;
if (count($coordonneesLng) > $j) {
for ($i=0; $i < count($coordonneesLng); $i=$i+$j) {
$insertions = array();print($i." ".$j." ");
if ($i+$j > count($coordonneesLng)) {
$k =count($coordonneesLng) - 1;
} else {
$k = $i+$j;
} print_r($k."\n");
for ($index = $i; $index < $k; $index ++) {
$insertions[] = "({$zoom},'lng',{$index},".$coordonneesLng[$index].",".$coordonneesLng[$index+1].")";
}
for ($index = $i; $index < $k; $index ++) {
$insertions[] = "({$zoom},'lat',{$index},".$coordonneesLat[$index].",".$coordonneesLat[$index+1].")";
}
$requete = "INSERT INTO {$table}({$nomsChamps}) VALUES ".implode(',', $insertions);
$this->getBdd()->requeter($requete);
}
} else {
for ($index = 0; $index < count($coordonneesLng)-1; $index ++) {
$insertions[] = "({$zoom},'lng',{$index},".$coordonneesLng[$index].",".$coordonneesLng[$index+1].")";
}
for ($index = 0; $index < count($coordonneesLat)-1; $index ++) {
$insertions[] = "({$zoom},'lat',{$index},".$coordonneesLat[$index].",".$coordonneesLat[$index+1].")";
}
$requete = "INSERT INTO {$table}({$nomsChamps}) VALUES ".implode(',', $insertions);
$this->getBdd()->requeter($requete);
}
}
private function getBdd() {
if (is_null($this->bdd)) {
$this->bdd = new Bdd();
}
return $this->bdd;
}
private function convertirFloatToString($float) {
return str_replace(',', '.', strval($float));
}
}
?>