Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 1520 Rev 1523
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
 */
19
// +-------------------------------------------------------------------------------------------------------------------+
20
// +-------------------------------------------------------------------------------------------------------------------+
20
class Codeinseecommune extends Script {
21
class Codeinseecommune extends Script {
21
	
22
	
22
	protected $tableauTaxon;
23
	protected $tableauTaxon;
23
	protected $dao;
24
	protected $dao;
24
	protected $observations;
25
	protected $observations;
-
 
26
	
-
 
27
	protected $mode_verbeux = false;
-
 
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(
27
		'-n' => array(true, true, 'Nom du fichier ou du dossier à traiter'));
31
		'-n' => array(true, true, 'Nom du fichier ou du dossier à traiter'));
28
 
32
 
29
// +-------------------------------------------------------------------------------------------------------------------+
33
// +-------------------------------------------------------------------------------------------------------------------+
30
	public function executer() {
34
	public function executer() {
31
		include_once dirname(__FILE__).'/bibliotheque/Dao.php';
35
		include_once dirname(__FILE__).'/bibliotheque/Dao.php';
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
36
		$cmd = $this->getParametre('a');
40
		$cmd = $this->getParametre('a');
-
 
41
		$this->mode_verbeux = $this->getParametre('v');
-
 
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();
47
				$this->calculerCodesInsee();
42
				$this->modifierCodeInsee();
-
 
43
				break;
48
				break;
44
			case 'sanscorrespondance' :
-
 
45
				// devrait être être l'option utilisée dans le cron quotidien
-
 
46
				$this->calculerCodeInseeSansCorrespondance();
-
 
47
				$this->modifierCodeInsee();
-
 
48
			break;
-
 
49
			case 'colonne' :
49
			case 'colonne' :
50
				$this->creerColonne();
50
				$this->creerColonne();
51
				break;
51
				break;
52
			case 'calculer' :
52
			case 'sanscorrespondance' :
-
 
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
		}
61
	}
62
	}
62
 
63
 
63
// +-------------------------------------------------------------------------------------------------------------------+
64
// +-------------------------------------------------------------------------------------------------------------------+
64
 
65
 
65
	private function creerColonne() {
66
	private function creerColonne() {
66
		$this->dao->creerColonneCodeInseeCalculee();
67
		$this->dao->creerColonneCodeInseeCalcule();
67
	}
68
	}
68
	
69
	
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
	}
73
	
74
	
74
	private function calculerCodeInsee() {
75
	private function calculerCodesInsee() {
75
		$liste_coordonnees = $this->dao->rechercherCoordonnees();
76
		$liste_coordonnees = $this->dao->rechercherCoordonnees();
76
		$this->traiterCoordonnees($liste_coordonnees);
77
		$this->traiterCoordonnees($liste_coordonnees);
77
	}
78
	}
78
	
79
	
79
	private function traiterCoordonnees($liste_coordonnees) {
80
	private function traiterCoordonnees($liste_coordonnees) {
80
		echo "-------------------------------------------------------------------\n";
-
 
81
		echo "  Traitement et calcul des codes INSEE (".count($liste_coordonnees)." coordonnées en tout) ... \n";
-
 
82
		echo "-------------------------------------------------------------------\n";
-
 
-
 
81
		
83
		$debut = microtime(true);
82
		$debut = microtime(true);
-
 
83
		$nb_coordonnees_modifiees = 0;
84
		$i = 0;
84
		$nb_coordonnees_ignorees = 0;
85
		$total = count($liste_coordonnees);
85
		$total = count($liste_coordonnees);
-
 
86
		
-
 
87
		if($this->mode_verbeux) {
-
 
88
			echo "-------------------------------------------------------------------\n";
-
 
89
			echo "  Calcul des codes INSEE et modification (".count($liste_coordonnees)." coordonnées en tout) ... \n";
-
 
90
			echo "  Enrichissement des champs zone geo et ce_zone_geo vides ... \n";
-
 
91
			echo "-------------------------------------------------------------------\n";
-
 
92
		}
-
 
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);
88
				$code_insee = $this->chercherCodeCommune('osm', $coordonnees['latitude'], $coordonnees['longitude']);
96
			if ($infos_coord != null) {
89
				if ($code_insee != "") {
97
				$coordonnee_a_traiter = array(
90
					$this->dao->ajouterCodeInseeCalculee($coordonnees['latitude'], $coordonnees['longitude'], $code_insee);
98
					'latitude' => $coordonnees['latitude'], 
91
				} else {
99
					'longitude' => $coordonnees['longitude'],
92
					$code_insee = $this->chercherCodeCommune('lion1906', $coordonnees['latitude'], $coordonnees['longitude']);
100
					'code_insee' => $infos_coord['code_insee'],
93
					if ($code_insee != "") {
101
					'nom' => $infos_coord['nom']	
-
 
102
				);
94
						$this->dao->ajouterCodeInseeCalculee($coordonnees['latitude'], $coordonnees['longitude'], $code_insee);
103
				$this->modifierInformationsGeo($coordonnee_a_traiter);
-
 
104
				$nb_coordonnees_modifiees++;
95
					}
105
			} else {
-
 
106
				$nb_coordonnees_ignorees++;
96
				}
107
			}
-
 
108
			if($this->mode_verbeux) {
-
 
109
				$this->afficherAvancement('  Coordonnées traitées ', $nb_coordonnees_modifiees);
97
			}
110
			}
98
			$i++;
-
 
99
			$this->afficherAvancement('  Lignes traitées ', $i);
-
 
100
			$this->dao->testerActiviteConnection();
-
 
101
		}
111
		}
102
		echo "\n";
112
		
103
		$fin = microtime(true);
113
		$fin = microtime(true);
-
 
114
		if($this->mode_verbeux) {
-
 
115
			echo "\n";
104
		echo "-------------------------------------------------------------------\n";
116
			echo "-------------------------------------------------------------------\n";
-
 
117
			echo "  Fin de la mise à jour des codes INSEE calculés et de l'enrichissement, \n";
105
		echo "  Fin du traitement, ".($fin - $debut)." secondes écoulées \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";
106
		echo "-------------------------------------------------------------------\n";
121
			echo "-------------------------------------------------------------------\n";
107
		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);
-
 
139
		$this->modifierCodeInseeEtZoneGeo($coordonnee_a_traiter);
-
 
140
	}
-
 
141
	
-
 
142
	private function modifierCodeInseeCalculePourCoordonnees($coordonnee_a_traiter) {
-
 
143
		$this->dao->testerActiviteConnection();
-
 
144
		$this->dao->ajouterCodeInseeCalcule($coordonnee_a_traiter['latitude'], $coordonnee_a_traiter['longitude'], $coordonnee_a_traiter['code_insee']);
108
	}
145
	}
109
		
146
		
110
	private function modifierCodeInsee() {
147
	private function modifierCodeInseeEtZoneGeo($coordonnee_a_traiter) {
-
 
148
			$this->dao->testerActiviteConnection();
111
		$this->dao->modifierCodeInsee();
149
			$this->dao->modifierCodeInseeEtZoneGeo($coordonnee_a_traiter);
112
	}
150
	}
113
	
151
	
114
	private function chercherCodeCommune($projet, $latitude, $longitude) {
152
	private function chercherInfosCommune($projet, $latitude, $longitude) {
115
		$url_service = "www.tela-botanica.org/service:eflore:0.1/{$projet}/nom-commune".
153
		$url_service = "www.tela-botanica.org/service:eflore:0.1/{$projet}/nom-commune".
116
					"?lat={$latitude}&lon={$longitude}";
154
					"?lat={$latitude}&lon={$longitude}";
117
		
155
		
118
		$url_service = str_replace(',', '.', $url_service);
156
		$url_service = str_replace(',', '.', $url_service);
119
		$ch = curl_init($url_service);
157
		$ch = curl_init($url_service);
120
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
158
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
121
		$reponse = curl_exec($ch);
159
		$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;
126
	}
165
	}
127
 
166
 
128
	private function traiterReponseServiceCommune($response) {
167
	private function traiterReponseServiceCommune($reponse) {
129
		$code_insee = '';
168
		$retour = null;
130
		$reponse = json_decode($reponse);
169
		$reponse = json_decode($reponse);
131
		// cas du service lion 1906 qui peut renvoyer plusieurs communes (on prend la première)
170
		// cas du service lion 1906 qui peut renvoyer plusieurs communes (on prend la première)
132
		if(is_array($reponse)) {
171
		if(is_array($reponse)) {
133
			$reponse = $reponse[0];
172
			$reponse = $reponse[0];
134
		}
173
		}
135
		if (isset($reponse->codeINSEE)) {
174
		if (isset($reponse->codeINSEE)) {
136
			$code_insee = $reponse->codeINSEE;
175
			$retour = array('code_insee' => $reponse->codeINSEE,
-
 
176
							'nom' => $reponse->nom);
137
		} 
177
		} 
138
		return $code_insee;
178
		return $retour;
139
	}
179
	}
140
	
180
	
141
	private function testerCoordonneesWgsFrance($latitude, $longitude) {
181
	private function testerCoordonneesWgsFrance($latitude, $longitude) {
142
		$coord_france = false;
182
		$coord_france = false;
143
		if ($latitude != '' && $longitude != '') {
183
		if ($latitude != '' && $longitude != '') {
144
			if ($latitude < 51.071667 && $latitude > 41.316667) {
184
			if ($latitude < 51.071667 && $latitude > 41.316667) {
145
				if ($longitude < 9.513333 && $longitude > -5.140278) {
185
				if ($longitude < 9.513333 && $longitude > -5.140278) {
146
					$coord_france = true;
186
					$coord_france = true;
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
?>