Subversion Repositories eFlore/Applications.cel

Rev

Rev 1520 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1520 Rev 1523
Line 1... Line 1...
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 codes INSEE de la banque de données cel
6
 *
6
 *
7
 * Description : classe permettant d'insérer les tableaux phytosociologiques de la banque de données SOPHY
7
 * Description : classe permettant d'affecter des codes INSEE aux obs possédant des coordonnées mais pas de commune
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
 * @author		Aurélien PERONNET <aurelien@tela-botanica.org>
14
 * @copyright	Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
15
 * @copyright	Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
15
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
16
 * @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
17
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
17
 * @version		$Id$
18
 * @version		$Id$
18
 */
19
 */
Line 20... Line 21...
20
class Codeinseecommune extends Script {
21
class Codeinseecommune extends Script {
Line 21... Line 22...
21
	
22
	
22
	protected $tableauTaxon;
23
	protected $tableauTaxon;
23
	protected $dao;
24
	protected $dao;
-
 
25
	protected $observations;
-
 
26
	
-
 
27
	protected $mode_verbeux = false;
24
	protected $observations;
28
	
25
	// Paramêtres autorisées lors de l'appel au script en ligne de commande
29
	// Paramêtres autorisées lors de l'appel au script en ligne de commande
26
	protected $parametres_autorises = array(
30
	protected $parametres_autorises = array(
Line 27... Line 31...
27
		'-n' => array(true, true, 'Nom du fichier ou du dossier à traiter'));
31
		'-n' => array(true, true, 'Nom du fichier ou du dossier à traiter'));
Line 32... Line 36...
32
	
36
	
33
		$this->dao = new Dao();
37
		$this->dao = new Dao();
34
		// Récupération de paramétres
38
		// Récupération de paramétres
35
		// Lancement de l'action demandée
39
		// Lancement de l'action demandée
-
 
40
		$cmd = $this->getParametre('a');
-
 
41
		$this->mode_verbeux = $this->getParametre('v');
36
		$cmd = $this->getParametre('a');
42
		
37
		switch ($cmd) {
43
		switch ($cmd) {
38
			case 'tout' :
44
			case 'tout' :
39
				// à effectuer manuellement au premier lancement du script
45
				// à effectuer manuellement au premier lancement du script
40
				$this->creerColonne();
46
				$this->creerColonne();
41
				$this->calculerCodeInsee();
-
 
42
				$this->modifierCodeInsee();
47
				$this->calculerCodesInsee();
43
				break;
-
 
44
			case 'sanscorrespondance' :
-
 
45
				// devrait être être l'option utilisée dans le cron quotidien
-
 
46
				$this->calculerCodeInseeSansCorrespondance();
-
 
47
				$this->modifierCodeInsee();
-
 
48
			break;
48
				break;
49
			case 'colonne' :
49
			case 'colonne' :
50
				$this->creerColonne();
50
				$this->creerColonne();
51
				break;
51
				break;
-
 
52
			case 'sanscorrespondance' :
52
			case 'calculer' :
53
				// devrait être être l'option utilisée dans le cron quotidien (avec l'option -v 0)
53
				$this->calculerCodeInsee();
54
				$this->calculerCodesInseeSansCorrespondance();
54
				break;
55
				break;
55
			case 'modifier' :
56
			case 'calculer' :
56
				$this->modifierCodeInsee();
57
				$this->calculerCodesInsee();
57
				break;
58
				break;
58
			default :
59
			default :
59
				$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
60
				$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
60
		}
61
		}
Line 61... Line 62...
61
	}
62
	}
Line 62... Line 63...
62
 
63
 
63
// +-------------------------------------------------------------------------------------------------------------------+
64
// +-------------------------------------------------------------------------------------------------------------------+
64
 
65
 
Line 65... Line 66...
65
	private function creerColonne() {
66
	private function creerColonne() {
66
		$this->dao->creerColonneCodeInseeCalculee();
67
		$this->dao->creerColonneCodeInseeCalcule();
67
	}
68
	}
68
	
69
	
Line 69... Line 70...
69
	private function calculerCodeInseeSansCorrespondance() {
70
	private function calculerCodesInseeSansCorrespondance() {
70
		$liste_coordonnees = $this->dao->rechercherCoordonneesSansCorrespondances();
71
		$liste_coordonnees = $this->dao->rechercherCoordonneesSansCorrespondances();
71
		$this->traiterCoordonnees($liste_coordonnees);
72
		$this->traiterCoordonnees($liste_coordonnees);
72
	}
73
	}
Line 73... Line 74...
73
	
74
	
74
	private function calculerCodeInsee() {
-
 
75
		$liste_coordonnees = $this->dao->rechercherCoordonnees();
-
 
76
		$this->traiterCoordonnees($liste_coordonnees);
-
 
-
 
75
	private function calculerCodesInsee() {
77
	}
76
		$liste_coordonnees = $this->dao->rechercherCoordonnees();
-
 
77
		$this->traiterCoordonnees($liste_coordonnees);
78
	
78
	}
79
	private function traiterCoordonnees($liste_coordonnees) {
79
	
-
 
80
	private function traiterCoordonnees($liste_coordonnees) {
-
 
81
		
-
 
82
		$debut = microtime(true);
-
 
83
		$nb_coordonnees_modifiees = 0;
-
 
84
		$nb_coordonnees_ignorees = 0;
-
 
85
		$total = count($liste_coordonnees);
-
 
86
		
-
 
87
		if($this->mode_verbeux) {
80
		echo "-------------------------------------------------------------------\n";
88
			echo "-------------------------------------------------------------------\n";
81
		echo "  Traitement et calcul des codes INSEE (".count($liste_coordonnees)." coordonnées en tout) ... \n";
89
			echo "  Calcul des codes INSEE et modification (".count($liste_coordonnees)." coordonnées en tout) ... \n";
82
		echo "-------------------------------------------------------------------\n";
90
			echo "  Enrichissement des champs zone geo et ce_zone_geo vides ... \n";
83
		$debut = microtime(true);
91
			echo "-------------------------------------------------------------------\n";
84
		$i = 0;
92
		}
85
		$total = count($liste_coordonnees);
93
				
86
		foreach ($liste_coordonnees as $coordonnees) {
94
		foreach ($liste_coordonnees as $coordonnees) {
87
			if ($this->testerCoordonneesWgsFrance($coordonnees['latitude'], $coordonnees['longitude'])) {
95
			$infos_coord = $this->obtenirInfosPourCoordonnees($coordonnees);
-
 
96
			if ($infos_coord != null) {
88
				$code_insee = $this->chercherCodeCommune('osm', $coordonnees['latitude'], $coordonnees['longitude']);
97
				$coordonnee_a_traiter = array(
-
 
98
					'latitude' => $coordonnees['latitude'], 
89
				if ($code_insee != "") {
99
					'longitude' => $coordonnees['longitude'],
-
 
100
					'code_insee' => $infos_coord['code_insee'],
90
					$this->dao->ajouterCodeInseeCalculee($coordonnees['latitude'], $coordonnees['longitude'], $code_insee);
101
					'nom' => $infos_coord['nom']	
-
 
102
				);
-
 
103
				$this->modifierInformationsGeo($coordonnee_a_traiter);
91
				} else {
104
				$nb_coordonnees_modifiees++;
92
					$code_insee = $this->chercherCodeCommune('lion1906', $coordonnees['latitude'], $coordonnees['longitude']);
-
 
93
					if ($code_insee != "") {
-
 
94
						$this->dao->ajouterCodeInseeCalculee($coordonnees['latitude'], $coordonnees['longitude'], $code_insee);
-
 
95
					}
105
			} else {
96
				}
106
				$nb_coordonnees_ignorees++;
97
			}
107
			}
-
 
108
			if($this->mode_verbeux) {
-
 
109
				$this->afficherAvancement('  Coordonnées traitées ', $nb_coordonnees_modifiees);
98
			$i++;
110
			}
-
 
111
		}
99
			$this->afficherAvancement('  Lignes traitées ', $i);
112
		
-
 
113
		$fin = microtime(true);
-
 
114
		if($this->mode_verbeux) {
100
			$this->dao->testerActiviteConnection();
115
			echo "\n";
101
		}
116
			echo "-------------------------------------------------------------------\n";
-
 
117
			echo "  Fin de la mise à jour des codes INSEE calculés et de l'enrichissement, \n";
-
 
118
			echo "  ".($fin - $debut)." secondes écoulées \n";
-
 
119
			echo "  ".$nb_coordonnees_modifiees." code insee calculés et modifiés \n";
-
 
120
			echo "  ".$nb_coordonnees_ignorees." coordonnées ignorées \n";
-
 
121
			echo "-------------------------------------------------------------------\n";
-
 
122
			echo "\n";
-
 
123
		}
-
 
124
	}
-
 
125
	
-
 
126
	private function obtenirInfosPourCoordonnees($coordonnees) {
-
 
127
		$infos_coord = null;
-
 
128
		if ($this->testerCoordonneesWgsFrance($coordonnees['latitude'], $coordonnees['longitude'])) {
-
 
129
			$infos_coord = $this->chercherInfosCommune('osm', $coordonnees['latitude'], $coordonnees['longitude']);
-
 
130
			if ($infos_coord == null) {
-
 
131
				$infos_coord = $this->chercherInfosCommune('lion1906', $coordonnees['latitude'], $coordonnees['longitude']);
-
 
132
			}
-
 
133
		}
-
 
134
		return $infos_coord;
-
 
135
	}
-
 
136
	
-
 
137
	private function modifierInformationsGeo($coordonnee_a_traiter) {
-
 
138
		$this->modifierCodeInseeCalculePourCoordonnees($coordonnee_a_traiter);
102
		echo "\n";
139
		$this->modifierCodeInseeEtZoneGeo($coordonnee_a_traiter);
Line 103... Line 140...
103
		$fin = microtime(true);
140
	}
104
		echo "-------------------------------------------------------------------\n";
141
	
-
 
142
	private function modifierCodeInseeCalculePourCoordonnees($coordonnee_a_traiter) {
105
		echo "  Fin du traitement, ".($fin - $debut)." secondes écoulées \n";
143
		$this->dao->testerActiviteConnection();
Line 106... Line 144...
106
		echo "-------------------------------------------------------------------\n";
144
		$this->dao->ajouterCodeInseeCalcule($coordonnee_a_traiter['latitude'], $coordonnee_a_traiter['longitude'], $coordonnee_a_traiter['code_insee']);
107
		echo "\n";
145
	}
108
	}
146
		
Line 109... Line 147...
109
		
147
	private function modifierCodeInseeEtZoneGeo($coordonnee_a_traiter) {
110
	private function modifierCodeInsee() {
148
			$this->dao->testerActiviteConnection();
111
		$this->dao->modifierCodeInsee();
149
			$this->dao->modifierCodeInseeEtZoneGeo($coordonnee_a_traiter);
112
	}
150
	}
-
 
151
	
113
	
152
	private function chercherInfosCommune($projet, $latitude, $longitude) {
114
	private function chercherCodeCommune($projet, $latitude, $longitude) {
153
		$url_service = "www.tela-botanica.org/service:eflore:0.1/{$projet}/nom-commune".
Line 115... Line 154...
115
		$url_service = "www.tela-botanica.org/service:eflore:0.1/{$projet}/nom-commune".
154
					"?lat={$latitude}&lon={$longitude}";
116
					"?lat={$latitude}&lon={$longitude}";
155
		
Line 117... Line 156...
117
		
156
		$url_service = str_replace(',', '.', $url_service);
118
		$url_service = str_replace(',', '.', $url_service);
157
		$ch = curl_init($url_service);
119
		$ch = curl_init($url_service);
158
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
120
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
159
		$reponse = curl_exec($ch);
121
		$reponse = curl_exec($ch);
160
		
122
		$reponse = $this->traiterReponseServiceCommune($response);
161
		$infos_coord = $this->traiterReponseServiceCommune($reponse);
123
		curl_close($ch);
162
		curl_close($ch);
124
		
163
		
125
		return $code_insee;
164
		return $infos_coord;
-
 
165
	}
126
	}
166
 
127
 
167
	private function traiterReponseServiceCommune($reponse) {
128
	private function traiterReponseServiceCommune($response) {
168
		$retour = null;
Line 129... Line 169...
129
		$code_insee = '';
169
		$reponse = json_decode($reponse);
130
		$reponse = json_decode($reponse);
170
		// 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)
171
		if(is_array($reponse)) {
Line 147... Line 187...
147
				}
187
				}
148
			}
188
			}
149
		}
189
		}
150
		return $coord_france;
190
		return $coord_france;
151
	}
191
	}
152
	private function analyserCodeDeptComm($ligneDecoupe) {
-
 
153
		if (preg_match('/^\d{2}$/', $ligneDecoupe['codeDept'])) {
-
 
154
		} elseif (preg_match('/^\s{0,1}\d{1,2}\s{0,1}$/',$ligneDecoupe['codeDept'])) {
-
 
155
			$ligneDecoupe['codeDept'] = str_replace(' ', '0', $ligneDecoupe['codeDept']);
-
 
156
		} else {
-
 
157
			$ligneDecoupe['codeDept'] = null;
-
 
158
		}
-
 
159
		if (preg_match('/^\d{3}$/', $ligneDecoupe['codeCommune'])) {
-
 
160
		} elseif (preg_match('/^\s{0,2}\d{1,2,3}\s{0,2}$/', $ligneDecoupe['codeCommune'])) {
-
 
161
			$ligneDecoupe['codeCommune'] = str_replace(' ', '0', $ligneDecoupe['codeCommune']);
-
 
162
		} elseif ($ligneDecoupe['codeDept'] == null) {
-
 
163
			$ligneDecoupe['codeCommune'] = null;
-
 
164
		} else {
-
 
165
			$ligneDecoupe['codeCommune'] = '000';
-
 
166
		}
-
 
167
		return $ligneDecoupe;
-
 
168
	}
-
 
169
	private function analyserCoordGrdParis($ligneDecoupe, $ligne) {
-
 
170
		if (preg_match('/[\s0]0\.000/', $ligneDecoupe['latitude'], $match)) {
-
 
171
			$ligneDecoupe['latitude'] = null;
-
 
172
			$ligneDecoupe['longitude'] = null;
-
 
173
		} elseif (preg_match('/\d{1,2}\.\d{2,3}/', $ligneDecoupe['latitude'], $match)) {// format souhaité
-
 
174
			$ligneDecoupe['latitude_wgs'] = round($ligneDecoupe['latitude']*0.9, 7);
-
 
175
			$ligneDecoupe['longitude_wgs'] = round($ligneDecoupe['longitude']*0.9+2.3372291, 7);
-
 
176
		} elseif (preg_match('/(\d{2})[\d\s,](\d{3})/', $ligneDecoupe['latitude'], $match)) {//erreur de saisie
-
 
177
			$ligneDecoupe['latitude'] = $match[1].'.'.$match[2];
-
 
178
			$ligneDecoupe['latitude_wgs'] = round($ligneDecoupe['latitude']*0.9, 7);
-
 
179
			$ligneDecoupe['longitude_wgs'] = round($ligneDecoupe['longitude']*0.9 + 2.3372291, 7);
-
 
180
		} elseif (preg_match('/^[a-zA-Z\s]*[a-zA-Z][a-zA-Z\s]*$/', $ligneDecoupe['latitude'])) {// lat absente + nom long
-
 
181
			$ligneDecoupe['nomStation'] = rtrim(substr($ligne, 10, 48));
-
 
182
			$ligneDecoupe['latitude'] = null;
-
 
183
			$ligneDecoupe['longitude'] = null;
-
 
184
		} elseif (preg_match('/.[AO].123/', $ligneDecoupe['latitude'], $match)) {
-
 
185
			$ligneDecoupe['latitude'] = null;
-
 
186
			$ligneDecoupe['longitude'] = null;
-
 
187
		}  elseif ($ligneDecoupe['latitude'] != null) {
-
 
188
			$ligneDecoupe['latitude'] = null;
-
 
189
			$ligneDecoupe['longitude'] = null;
-
 
190
		}
-
 
191
		return $ligneDecoupe;
-
 
192
	}
-
 
193
}
192
}
194
?>
193
?>
195
194