Subversion Repositories eFlore/Applications.cel

Rev

Rev 1518 | Rev 1523 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1518 Rev 1520
1
<?php
1
<?php
2
// Encodage : UTF-8
2
// Encodage : UTF-8
3
// +-------------------------------------------------------------------------------------------------------------------+
3
// +-------------------------------------------------------------------------------------------------------------------+
4
/**
4
/**
5
 * Traitement des fichiers de la banque de données SOPHY pour insertion
5
 * Traitement des fichiers de la banque de données SOPHY pour insertion
6
 *
6
 *
7
 * Description : classe permettant d'insérer les tableaux phytosociologiques de la banque de données SOPHY
7
 * Description : classe permettant d'insérer les tableaux phytosociologiques de la banque de données SOPHY
8
 * Utilisation : php script.php code -a codeInseeCommune
8
 * Utilisation : php script.php code -a codeInseeCommune
9
 *
9
 *
10
 * @category		PHP 5.3
10
 * @category		PHP 5.3
11
 * @package		phytosocio
11
 * @package		phytosocio
12
 //Auteur original :
12
 //Auteur original :
13
 * @author		Delphine CAUQUIL <delphine@tela-botanica.org>
13
 * @author		Delphine CAUQUIL <delphine@tela-botanica.org>
14
 * @copyright	Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
14
 * @copyright	Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
15
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
15
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
16
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
16
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
17
 * @version		$Id$
17
 * @version		$Id$
18
 */
18
 */
19
// +-------------------------------------------------------------------------------------------------------------------+
19
// +-------------------------------------------------------------------------------------------------------------------+
20
class Codeinseecommune extends Script {
20
class Codeinseecommune extends Script {
21
	
21
	
22
	protected $tableauTaxon;
22
	protected $tableauTaxon;
23
	protected $dao;
23
	protected $dao;
24
	protected $observations;
24
	protected $observations;
25
	// Paramêtres autorisées lors de l'appel au script en ligne de commande
25
	// Paramêtres autorisées lors de l'appel au script en ligne de commande
26
	protected $parametres_autorises = array(
26
	protected $parametres_autorises = array(
27
		'-n' => array(true, true, 'Nom du fichier ou du dossier à traiter'));
27
		'-n' => array(true, true, 'Nom du fichier ou du dossier à traiter'));
28
 
-
 
29
 
-
 
30
 
28
 
31
// +-------------------------------------------------------------------------------------------------------------------+
29
// +-------------------------------------------------------------------------------------------------------------------+
32
	public function executer() {
30
	public function executer() {
33
		include_once dirname(__FILE__).'/bibliotheque/Dao.php';
31
		include_once dirname(__FILE__).'/bibliotheque/Dao.php';
34
 
-
 
35
		
32
	
36
		$this->dao = new Dao();
33
		$this->dao = new Dao();
37
		// Récupération de paramétres
34
		// Récupération de paramétres
38
		// Lancement de l'action demandée
35
		// Lancement de l'action demandée
39
		$cmd = $this->getParametre('a');
36
		$cmd = $this->getParametre('a');
40
		switch ($cmd) {
37
		switch ($cmd) {
41
			case 'tout' :
38
			case 'tout' :
42
				// à effectuer manuellement au premier lancement du script
39
				// à effectuer manuellement au premier lancement du script
43
				$this->creerColonne();
40
				$this->creerColonne();
44
				$this->calculerCodeInsee();
41
				$this->calculerCodeInsee();
45
				//$this->modifierCodeInsee();
42
				$this->modifierCodeInsee();
46
				break;
43
				break;
47
			case 'sanscorrespondance' :
44
			case 'sanscorrespondance' :
48
				// devrait être être l'option utilisée dans le cron quotidien
45
				// devrait être être l'option utilisée dans le cron quotidien
49
				$this->calculerCodeInseeSansCorrespondance();
46
				$this->calculerCodeInseeSansCorrespondance();
50
				//$this->modifierCodeInsee();
47
				$this->modifierCodeInsee();
51
			break;
48
			break;
52
			case 'colonne' :
49
			case 'colonne' :
53
				$this->creerColonne();
50
				$this->creerColonne();
54
				break;
51
				break;
55
			case 'calculer' :
52
			case 'calculer' :
56
				$this->calculerCodeInsee();
53
				$this->calculerCodeInsee();
57
				break;
54
				break;
58
			case 'modifier' :
55
			case 'modifier' :
59
				$this->modifierCodeInsee();
56
				$this->modifierCodeInsee();
60
				break;
57
				break;
61
			default :
58
			default :
62
				$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
59
				$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
63
		}
60
		}
64
	}
61
	}
65
 
62
 
66
// +-------------------------------------------------------------------------------------------------------------------+
63
// +-------------------------------------------------------------------------------------------------------------------+
67
 
64
 
68
	private function creerColonne() {
65
	private function creerColonne() {
69
		$this->dao->creerColonneCodeInseeCalculee();
66
		$this->dao->creerColonneCodeInseeCalculee();
70
	}
67
	}
71
	
68
	
72
	private function calculerCodeInseeSansCorrespondance() {
69
	private function calculerCodeInseeSansCorrespondance() {
73
		$liste_coordonnees = $this->dao->rechercherCoordonneesSansCorrespondances();
70
		$liste_coordonnees = $this->dao->rechercherCoordonneesSansCorrespondances();
74
		$this->traiterCoordonnees($liste_coordonnees);
71
		$this->traiterCoordonnees($liste_coordonnees);
75
	}
72
	}
76
	
73
	
77
	private function calculerCodeInsee() {
74
	private function calculerCodeInsee() {
78
		$liste_coordonnees = $this->dao->rechercherCoordonnees();
75
		$liste_coordonnees = $this->dao->rechercherCoordonnees();
79
		$this->traiterCoordonnees($liste_coordonnees);
76
		$this->traiterCoordonnees($liste_coordonnees);
80
	}
77
	}
81
	
78
	
82
	private function traiterCoordonnees($liste_coordonnees) {
79
	private function traiterCoordonnees($liste_coordonnees) {
83
		echo "-------------------------------------------------------------------\n";
80
		echo "-------------------------------------------------------------------\n";
84
		echo "  Traitement et calcul des codes INSEE (".count($liste_coordonnees)." coordonnées en tout) ... \n";
81
		echo "  Traitement et calcul des codes INSEE (".count($liste_coordonnees)." coordonnées en tout) ... \n";
85
		echo "-------------------------------------------------------------------\n";
82
		echo "-------------------------------------------------------------------\n";
86
		$debut = microtime(true);
83
		$debut = microtime(true);
87
		$i = 0;
84
		$i = 0;
88
		$total = count($liste_coordonnees);
85
		$total = count($liste_coordonnees);
89
		foreach ($liste_coordonnees as $coordonnees) {
86
		foreach ($liste_coordonnees as $coordonnees) {
90
			if ($this->testerCoordonneesWgsFrance($coordonnees['latitude'], $coordonnees['longitude'])) {
87
			if ($this->testerCoordonneesWgsFrance($coordonnees['latitude'], $coordonnees['longitude'])) {
91
				$code_insee = $this->chercherCodeCommune('osm', $coordonnees['latitude'], $coordonnees['longitude']);
88
				$code_insee = $this->chercherCodeCommune('osm', $coordonnees['latitude'], $coordonnees['longitude']);
92
				if ($code_insee != "") {
89
				if ($code_insee != "") {
93
					$this->dao->ajouterCodeInseeCalculee($coordonnees['latitude'], $coordonnees['longitude'], $code_insee);
90
					$this->dao->ajouterCodeInseeCalculee($coordonnees['latitude'], $coordonnees['longitude'], $code_insee);
94
				} else {
91
				} else {
95
					$code_insee = $this->chercherCodeCommune('lion1906', $coordonnees['latitude'], $coordonnees['longitude']);
92
					$code_insee = $this->chercherCodeCommune('lion1906', $coordonnees['latitude'], $coordonnees['longitude']);
96
					if ($code_insee != "") {
93
					if ($code_insee != "") {
97
						$this->dao->ajouterCodeInseeCalculee($coordonnees['latitude'], $coordonnees['longitude'], $code_insee);
94
						$this->dao->ajouterCodeInseeCalculee($coordonnees['latitude'], $coordonnees['longitude'], $code_insee);
98
					}
95
					}
99
				}
96
				}
100
			}
97
			}
101
			$i++;
98
			$i++;
102
			$this->afficherAvancement('  Lignes traitées ', $i);
99
			$this->afficherAvancement('  Lignes traitées ', $i);
103
			
-
 
104
			// pause toutes les 1000 requêtes
100
			$this->dao->testerActiviteConnection();
105
			if($i %1000 == 0) {
-
 
106
				sleep(2);
-
 
107
			}
-
 
108
		}
101
		}
109
		echo "\n";
102
		echo "\n";
110
		$fin = microtime(true);
103
		$fin = microtime(true);
111
		echo "-------------------------------------------------------------------\n";
104
		echo "-------------------------------------------------------------------\n";
112
		echo "  Fin du traitement, ".($fin - $debut)." secondes écoulées \n";
105
		echo "  Fin du traitement, ".($fin - $debut)." secondes écoulées \n";
113
		echo "-------------------------------------------------------------------\n";
106
		echo "-------------------------------------------------------------------\n";
114
		echo "\n";
107
		echo "\n";
115
	}
108
	}
116
	
109
		
117
	private function modifierCodeInsee() {
110
	private function modifierCodeInsee() {
118
		$this->dao->modifierCodeInsee();
111
		$this->dao->modifierCodeInsee();
119
	}
112
	}
120
	
113
	
121
	private function chercherCodeCommune($projet, $latitude, $longitude) {
114
	private function chercherCodeCommune($projet, $latitude, $longitude) {
122
		$url_service = "www.tela-botanica.org/service:eflore:0.1/{$projet}/nom-commune".
115
		$url_service = "www.tela-botanica.org/service:eflore:0.1/{$projet}/nom-commune".
123
					"?lat={$latitude}&lon={$longitude}";
116
					"?lat={$latitude}&lon={$longitude}";
124
		
117
		
125
		$url_service = str_replace(',', '.', $url_service);
118
		$url_service = str_replace(',', '.', $url_service);
126
		$ch = curl_init($url_service);
119
		$ch = curl_init($url_service);
127
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
120
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
128
		$reponse = curl_exec($ch);
121
		$reponse = curl_exec($ch);
129
		$reponse = $this->traiterReponseServiceCommune($response);
122
		$reponse = $this->traiterReponseServiceCommune($response);
130
		curl_close($ch);
123
		curl_close($ch);
131
		
124
		
132
		return $code_insee;
125
		return $code_insee;
133
	}
126
	}
134
 
127
 
135
	private function traiterReponseServiceCommune($response) {
128
	private function traiterReponseServiceCommune($response) {
136
		$code_insee = '';
129
		$code_insee = '';
137
		$reponse = json_decode($reponse);
130
		$reponse = json_decode($reponse);
138
		// cas du service lion 1906 qui peut renvoyer plusieurs communes (on prend la première)
131
		// cas du service lion 1906 qui peut renvoyer plusieurs communes (on prend la première)
139
		if(is_array($reponse)) {
132
		if(is_array($reponse)) {
140
			$reponse = $reponse[0];
133
			$reponse = $reponse[0];
141
		}
134
		}
142
		if (isset($reponse->codeINSEE)) {
135
		if (isset($reponse->codeINSEE)) {
143
			$code_insee = $reponse->codeINSEE;
136
			$code_insee = $reponse->codeINSEE;
144
		} 
137
		} 
145
		return $code_insee;
138
		return $code_insee;
146
	}
139
	}
147
	
140
	
148
	private function testerCoordonneesWgsFrance($latitude, $longitude) {
141
	private function testerCoordonneesWgsFrance($latitude, $longitude) {
149
		$coord_france = false;
142
		$coord_france = false;
150
		if ($latitude != '' && $longitude != '') {
143
		if ($latitude != '' && $longitude != '') {
151
			if ($latitude < 51.071667 && $latitude > 41.316667) {
144
			if ($latitude < 51.071667 && $latitude > 41.316667) {
152
				if ($longitude < 9.513333 && $longitude > -5.140278) {
145
				if ($longitude < 9.513333 && $longitude > -5.140278) {
153
					$coord_france = true;
146
					$coord_france = true;
154
				}
147
				}
155
			}
148
			}
156
		}
149
		}
157
		return $coord_france;
150
		return $coord_france;
158
	}
151
	}
159
	private function analyserCodeDeptComm($ligneDecoupe) {
152
	private function analyserCodeDeptComm($ligneDecoupe) {
160
		if (preg_match('/^\d{2}$/', $ligneDecoupe['codeDept'])) {
153
		if (preg_match('/^\d{2}$/', $ligneDecoupe['codeDept'])) {
161
		} elseif (preg_match('/^\s{0,1}\d{1,2}\s{0,1}$/',$ligneDecoupe['codeDept'])) {
154
		} elseif (preg_match('/^\s{0,1}\d{1,2}\s{0,1}$/',$ligneDecoupe['codeDept'])) {
162
			$ligneDecoupe['codeDept'] = str_replace(' ', '0', $ligneDecoupe['codeDept']);
155
			$ligneDecoupe['codeDept'] = str_replace(' ', '0', $ligneDecoupe['codeDept']);
163
		} else {
156
		} else {
164
			$ligneDecoupe['codeDept'] = null;
157
			$ligneDecoupe['codeDept'] = null;
165
		}
158
		}
166
		if (preg_match('/^\d{3}$/', $ligneDecoupe['codeCommune'])) {
159
		if (preg_match('/^\d{3}$/', $ligneDecoupe['codeCommune'])) {
167
		} elseif (preg_match('/^\s{0,2}\d{1,2,3}\s{0,2}$/', $ligneDecoupe['codeCommune'])) {
160
		} elseif (preg_match('/^\s{0,2}\d{1,2,3}\s{0,2}$/', $ligneDecoupe['codeCommune'])) {
168
			$ligneDecoupe['codeCommune'] = str_replace(' ', '0', $ligneDecoupe['codeCommune']);
161
			$ligneDecoupe['codeCommune'] = str_replace(' ', '0', $ligneDecoupe['codeCommune']);
169
		} elseif ($ligneDecoupe['codeDept'] == null) {
162
		} elseif ($ligneDecoupe['codeDept'] == null) {
170
			$ligneDecoupe['codeCommune'] = null;
163
			$ligneDecoupe['codeCommune'] = null;
171
		} else {
164
		} else {
172
			$ligneDecoupe['codeCommune'] = '000';
165
			$ligneDecoupe['codeCommune'] = '000';
173
		}
166
		}
174
		return $ligneDecoupe;
167
		return $ligneDecoupe;
175
	}
168
	}
176
	private function analyserCoordGrdParis($ligneDecoupe, $ligne) {
169
	private function analyserCoordGrdParis($ligneDecoupe, $ligne) {
177
		if (preg_match('/[\s0]0\.000/', $ligneDecoupe['latitude'], $match)) {
170
		if (preg_match('/[\s0]0\.000/', $ligneDecoupe['latitude'], $match)) {
178
			$ligneDecoupe['latitude'] = null;
171
			$ligneDecoupe['latitude'] = null;
179
			$ligneDecoupe['longitude'] = null;
172
			$ligneDecoupe['longitude'] = null;
180
		} elseif (preg_match('/\d{1,2}\.\d{2,3}/', $ligneDecoupe['latitude'], $match)) {// format souhaité
173
		} elseif (preg_match('/\d{1,2}\.\d{2,3}/', $ligneDecoupe['latitude'], $match)) {// format souhaité
181
			$ligneDecoupe['latitude_wgs'] = round($ligneDecoupe['latitude']*0.9, 7);
174
			$ligneDecoupe['latitude_wgs'] = round($ligneDecoupe['latitude']*0.9, 7);
182
			$ligneDecoupe['longitude_wgs'] = round($ligneDecoupe['longitude']*0.9+2.3372291, 7);
175
			$ligneDecoupe['longitude_wgs'] = round($ligneDecoupe['longitude']*0.9+2.3372291, 7);
183
		} elseif (preg_match('/(\d{2})[\d\s,](\d{3})/', $ligneDecoupe['latitude'], $match)) {//erreur de saisie
176
		} elseif (preg_match('/(\d{2})[\d\s,](\d{3})/', $ligneDecoupe['latitude'], $match)) {//erreur de saisie
184
			$ligneDecoupe['latitude'] = $match[1].'.'.$match[2];
177
			$ligneDecoupe['latitude'] = $match[1].'.'.$match[2];
185
			$ligneDecoupe['latitude_wgs'] = round($ligneDecoupe['latitude']*0.9, 7);
178
			$ligneDecoupe['latitude_wgs'] = round($ligneDecoupe['latitude']*0.9, 7);
186
			$ligneDecoupe['longitude_wgs'] = round($ligneDecoupe['longitude']*0.9 + 2.3372291, 7);
179
			$ligneDecoupe['longitude_wgs'] = round($ligneDecoupe['longitude']*0.9 + 2.3372291, 7);
187
		} elseif (preg_match('/^[a-zA-Z\s]*[a-zA-Z][a-zA-Z\s]*$/', $ligneDecoupe['latitude'])) {// lat absente + nom long
180
		} elseif (preg_match('/^[a-zA-Z\s]*[a-zA-Z][a-zA-Z\s]*$/', $ligneDecoupe['latitude'])) {// lat absente + nom long
188
			$ligneDecoupe['nomStation'] = rtrim(substr($ligne, 10, 48));
181
			$ligneDecoupe['nomStation'] = rtrim(substr($ligne, 10, 48));
189
			$ligneDecoupe['latitude'] = null;
182
			$ligneDecoupe['latitude'] = null;
190
			$ligneDecoupe['longitude'] = null;
183
			$ligneDecoupe['longitude'] = null;
191
		} elseif (preg_match('/.[AO].123/', $ligneDecoupe['latitude'], $match)) {
184
		} elseif (preg_match('/.[AO].123/', $ligneDecoupe['latitude'], $match)) {
192
			$ligneDecoupe['latitude'] = null;
185
			$ligneDecoupe['latitude'] = null;
193
			$ligneDecoupe['longitude'] = null;
186
			$ligneDecoupe['longitude'] = null;
194
		}  elseif ($ligneDecoupe['latitude'] != null) {
187
		}  elseif ($ligneDecoupe['latitude'] != null) {
195
			$ligneDecoupe['latitude'] = null;
188
			$ligneDecoupe['latitude'] = null;
196
			$ligneDecoupe['longitude'] = null;
189
			$ligneDecoupe['longitude'] = null;
197
		}
190
		}
198
		return $ligneDecoupe;
191
		return $ligneDecoupe;
199
	}
192
	}
200
}
193
}
201
?>
194
?>