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