29 |
alex |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Exemple lancement:
|
|
|
5 |
* /opt/lampp/bin/php -d memory_limit=3500M ~/web/moissonnage/scripts/cli.php maillage_projet
|
|
|
6 |
* -a source=nom_source
|
|
|
7 |
*
|
|
|
8 |
* @author alex
|
|
|
9 |
*
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
class MaillageProjet extends Script {
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
private $nomProjet = 'maillage_projet';
|
|
|
16 |
private $nomSource = '';
|
|
|
17 |
private $bdd = null;
|
|
|
18 |
private $tableRequetage = '';
|
|
|
19 |
|
|
|
20 |
private $mailles = array();
|
|
|
21 |
private $points = array();
|
|
|
22 |
|
|
|
23 |
|
|
|
24 |
public function executer() {
|
|
|
25 |
try {
|
|
|
26 |
$this->initialiserScript();
|
|
|
27 |
$cmd = $this->getParametre('a');
|
|
|
28 |
$parametre = explode('=', $cmd);
|
|
|
29 |
if (count($parametre) != 2 && $parametre[0] != 'source') {
|
|
|
30 |
throw new Exception("Parametre passé incorrect : attendu source=nom_de_la_source\n");
|
|
|
31 |
} else {
|
|
|
32 |
if (!$this->estSourceDisponible($parametre[1])) {
|
|
|
33 |
$sourcesDispo = Config::get('sources_disponibles');
|
|
|
34 |
$message =
|
|
|
35 |
"Erreur lors de l'éxécution du script : la soruce demandée n'est pas disponible.\n".
|
|
|
36 |
"Les sources dont le maillage est réalisable sont les suivantes : {$sourcesDispo}.\n";
|
|
|
37 |
throw new Exception($message);
|
|
|
38 |
} else {
|
|
|
39 |
$this->nomSource = $parametre[1];
|
|
|
40 |
$this->chargerConfigurationSource($parametre[1]);
|
|
|
41 |
$this->tableRequetage = $parametre[1].Config::get('suffixe_table_interrogation');
|
|
|
42 |
$this->genererMaillage();
|
|
|
43 |
}
|
|
|
44 |
}
|
|
|
45 |
} catch(Exception $e) {
|
|
|
46 |
$this->traiterErreur($e->getMessage());
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
private function initialiserScript() {
|
|
|
51 |
$fichierIni = $this->getScriptChemin().'maillage_projet.ini';
|
|
|
52 |
if (file_exists($fichierIni)) {
|
|
|
53 |
Config::charger($fichierIni);
|
|
|
54 |
} else {
|
|
|
55 |
$m = "Veuillez configurer le projet en créant le fichier '{$this->nomProjet}.ini' ".
|
|
|
56 |
"dans le dossier du module de script du projet à partir du fichier '{$this->nomProjet}.defaut.ini'.";
|
|
|
57 |
throw new Exception($m);
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
private function estSourceDisponible($nomSource) {
|
|
|
62 |
$sourcesDispo = explode(',', Config::get('sources_disponibles'));
|
|
|
63 |
return (in_array($nomSource, $sourcesDispo));
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
private function chargerConfigurationSource($nomSource) {
|
|
|
67 |
$fichierIni = $this->getScriptChemin()."sources".DIRECTORY_SEPARATOR."{$nomSource}.ini";
|
|
|
68 |
if (file_exists($fichierIni)) {
|
|
|
69 |
Config::charger($fichierIni);
|
|
|
70 |
} else {
|
|
|
71 |
$m = "Veuillez configurer les champs specifiques à la source en créant le fichier ".
|
|
|
72 |
"'{$nomSource}.ini' dans le dossier sources de script du projet {$this->nomProjet}.";
|
|
|
73 |
throw new Exception($m);
|
|
|
74 |
}
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
private function genererMaillage() {
|
|
|
79 |
if (!$this->estTableCreee()) {
|
|
|
80 |
$this->creerStructureTable();
|
|
|
81 |
}
|
|
|
82 |
$zoomMinimal = Config::get('zoom_minimal');
|
|
|
83 |
$niveauxZoom = range($zoomMinimal, Config::get('zoom_maximal'), 1);
|
|
|
84 |
foreach ($niveauxZoom as $zoom) {
|
|
|
85 |
print("Génération des mailles au niveau de zoom {$zoom}...\n");
|
|
|
86 |
if ($zoom == $zoomMinimal) {
|
|
|
87 |
$this->initialiserMailles();
|
|
|
88 |
$this->genererMaillesPourZoomMinimal();
|
|
|
89 |
} else {
|
|
|
90 |
$this->mailles->redecouperMailles();
|
|
|
91 |
$this->ajouterMaillesDansBdd();
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
private function estTableCreee() {
|
|
|
97 |
$tables = $this->getBdd()->recupererTous("SHOW TABLES FROM ".Config::get('bdd_nom'));
|
|
|
98 |
$tableInsertion = 'mailles_'.$this->nomSource;
|
|
|
99 |
for ($index = 0; $index < count($tables) && $tables[$index]['Tables_in_tb_eflore'] != $tableInsertion;
|
|
|
100 |
$index ++);
|
|
|
101 |
return $index < count($tables);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
private function creerStructureTable() {
|
|
|
105 |
$tableInsertion = 'mailles_'.$this->nomSource;
|
|
|
106 |
print("Table {$tableInsertion} non trouvée dans la base de données, création de sa structure...\n");
|
|
|
107 |
$requete =
|
|
|
108 |
"CREATE TABLE {$tableInsertion} (".
|
|
|
109 |
"code_maille VARCHAR(25) NOT NULL PRIMARY KEY,".
|
|
|
110 |
"zoom TINYINT UNSIGNED NOT NULL,".
|
|
|
111 |
"position_latitude SMALLINT UNSIGNED NOT NULL,".
|
|
|
112 |
"position_longitude SMALLINT UNSIGNED NOT NULL,".
|
|
|
113 |
"limite_sud DECIMAL(9,6) NOT NULL,".
|
|
|
114 |
"limite_est DECIMAL(9,6) NOT NULL,".
|
|
|
115 |
"limite_nord DECIMAL(9,6) NOT NULL,".
|
|
|
116 |
"limite_ouest DECIMAL(9,6) NOT NULL,".
|
|
|
117 |
"nombre_sites MEDIUMINT UNSIGNED NOT NULL,".
|
|
|
118 |
"nombre_observations MEDIUMINT UNSIGNED NOT NULL".
|
|
|
119 |
") ENGINE=MyISAM, CHARACTER SET utf8";
|
|
|
120 |
$this->getBdd()->requeter($requete);
|
|
|
121 |
$requete = "ALTER TABLE {$tableInsertion} ADD INDEX bbox(zoom, limite_sud, ".
|
|
|
122 |
"limite_nord, limite_ouest, limite_est)";
|
|
|
123 |
$this->getBdd()->requeter($requete);
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
private function initialiserMailles() {
|
|
|
127 |
$limitesEspaceMaillage = $this->recupererExtremesCoordonnees();
|
|
|
128 |
$this->recupererIndexMailles($limitesEspaceMaillage, Config::get('zoom_minimal'));
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
private function recupererExtremesCoordonnees() {
|
|
|
132 |
$champLongitude = Config::get('tbl.champ_longitude');
|
|
|
133 |
$champLatitude = Config::get('tbl.champ_latitude');
|
|
|
134 |
$requete = "SELECT Min({$champLongitude}) AS lng_min, Max({$champLongitude}) AS lng_max, ".
|
|
|
135 |
"Min({$champLatitude}) AS lat_min, Max({$champLatitude}) AS lat_max FROM {$this->tableRequetage}";
|
|
|
136 |
$limites = $this->getBdd()->recuperer($requete);
|
|
|
137 |
return $limites;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
private function getBdd() {
|
|
|
141 |
if (is_null($this->bdd)) {
|
|
|
142 |
$this->bdd = new Bdd();
|
|
|
143 |
}
|
|
|
144 |
return $this->bdd;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
private function recupererIndexMailles($limites, $zoom) {
|
|
|
148 |
$bbox = array(
|
|
|
149 |
'sud' => $limites['lat_min'],
|
|
|
150 |
'ouest' => $limites['lng_min'],
|
|
|
151 |
'est' => $limites['lng_max'],
|
|
|
152 |
'nord' => $limites['lat_max']
|
|
|
153 |
);
|
|
|
154 |
$this->mailles = new Maillage($bbox, $zoom);
|
|
|
155 |
$this->mailles->genererMaillesVides();
|
|
|
156 |
}
|
|
|
157 |
|
|
|
158 |
private function genererMaillesPourZoomMinimal() {
|
|
|
159 |
$this->points = $this->recupererPoints();
|
|
|
160 |
$this->mailles->ajouterPoints($this->points);
|
|
|
161 |
$this->ajouterMaillesDansBdd();
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
private function recupererPoints() {
|
|
|
165 |
print("Récupération des points dans la base de données...\n");
|
|
|
166 |
$champLongitude = Config::get('tbl.champ_longitude');
|
|
|
167 |
$champLatitude = Config::get('tbl.champ_latitude');
|
|
|
168 |
$id = Config::get('tbl.champ_id');
|
|
|
169 |
$requete = "SELECT COUNT({$id}) AS observations, {$champLatitude} AS latitude, {$champLongitude} ".
|
|
|
170 |
"AS longitude FROM {$this->tableRequetage} GROUP BY {$champLongitude},{$champLatitude}";
|
|
|
171 |
$points = $this->getBdd()->recupererTous($requete);
|
|
|
172 |
return $points;
|
|
|
173 |
print("Points chargés\n");
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
private function ajouterMaillesDansBdd() {
|
|
|
177 |
$aInserer = $this->mailles->formaterPourInsertionBdd();
|
|
|
178 |
$champsTable = Config::get('champs_table_mailles');
|
|
|
179 |
$tableInsertion = 'mailles_'.$this->nomSource;
|
|
|
180 |
$ordresInsertion = [];
|
|
|
181 |
$this->getBdd()->requeter("DELETE FROM {$tableInsertion} WHERE zoom={$aInserer[0]['zoom']}");
|
|
|
182 |
foreach ($aInserer as $ligne) {
|
|
|
183 |
$codeMaille = $this->nomSource.'.'.$ligne['zoom'].'-'.$ligne['indexLat'].'-'.$ligne['indexLng'];
|
|
|
184 |
$sql = "('{$codeMaille}',{$ligne['zoom']},{$ligne['indexLng']},{$ligne['indexLat']},".
|
|
|
185 |
"{$ligne['ouest']},{$ligne['sud']},{$ligne['est']},{$ligne['nord']},".
|
|
|
186 |
"{$ligne['points']},{$ligne['observations']})";
|
|
|
187 |
$ordresInsertion[] = $sql;
|
|
|
188 |
}
|
|
|
189 |
$requete = "INSERT INTO {$tableInsertion} ({$champsTable}) VALUES ".implode(',', $ordresInsertion);
|
|
|
190 |
$this->getBdd()->requeter($requete);
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
?>
|