Subversion Repositories eFlore/Applications.moissonnage

Rev

Rev 29 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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