Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 970 Rev 1296
1
<?php
1
<?php
2
/**
2
/**
3
 * Service recherche de commune par coordonnées et vice versa
3
 * Service recherche de commune par coordonnées et vice versa
4
 * Encodage en entrée : utf8
4
 * Encodage en entrée : utf8
5
 * Encodage en sortie : utf8
5
 * Encodage en sortie : utf8
6
 *
6
 *
7
 * @author Aurélien PERONNET <aurelien@tela-botanica.org>
7
 * @author Aurélien PERONNET <aurelien@tela-botanica.org>
8
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
8
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
9
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
9
 * @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
10
 * @version $Id$
10
 * @version $Id$
11
 */
11
 */
12
class CoordSearch extends Cel {
12
class CoordSearch extends Cel {
13
	
13
	
14
	private $adresse_service_geonames = null;
14
	private $adresse_service_geonames = null;
15
	private $adresse_service_local = null;
15
	private $adresse_service_local = null;
16
	
16
	
17
	private $nom_service_geocoding = null;
17
	private $nom_service_geocoding = null;
18
	private $nom_service_reverse_geocoding = null;
18
	private $nom_service_reverse_geocoding = null;
19
	
19
	
20
	function CoordSearch($config) {
20
	function CoordSearch($config) {
21
		
21
		
22
		parent::__construct($config);
22
		parent::__construct($config);
23
		
23
		
24
		$this->adresse_service_geonames = $this->config['cel']['url_service_geo_geonames'];
24
		$this->adresse_service_geonames = $this->config['cel']['url_service_geo_geonames'];
25
		$this->adresse_service_local = $this->config['cel']['url_service_geo_local'];
25
		$this->adresse_service_local = $this->config['cel']['url_service_geo_local'];
26
		
26
		
27
		$this->nom_service_geocoding = $this->config['cel']['nom_service_geocoding_geonames'];
27
		$this->nom_service_geocoding = $this->config['cel']['nom_service_geocoding_geonames'];
28
		$this->nom_service_reverse_geocoding = $this->config['cel']['nom_service_reverse_geocoding_geonames'];
28
		$this->nom_service_reverse_geocoding = $this->config['cel']['nom_service_reverse_geocoding_geonames'];
29
	}
29
	}
30
 
30
 
31
	/**
31
	/**
32
	 * Recherche de coordonnées suivant ce qui est fourni
32
	 * Recherche de coordonnées suivant ce qui est fourni
33
	 * 
33
	 * 
34
	 * $uid[0] = latitude (ou * si recherche coordonnées d'une commune)
34
	 * $uid[0] = latitude (ou * si recherche coordonnées d'une commune)
35
	 * $uid[1] = longitude (ou * si recherche coordonnées d'une commune)
35
	 * $uid[1] = longitude (ou * si recherche coordonnées d'une commune)
36
	 * $uid[2] = commune (ou * si recherche d'une commune correspondant à des coordonnées)
36
	 * $uid[2] = commune (ou * si recherche d'une commune correspondant à des coordonnées)
37
	 * $uid[3] = code_postal (ou * si recherche d'une commune correspondant à des coordonnées)
37
	 * $uid[3] = code_postal (ou * si recherche d'une commune correspondant à des coordonnées)
38
	 * $uid[4] = code_pays (ou * si recherche d'une commune correspondant à des coordonnées, par défaut vaut FR)
38
	 * $uid[4] = code_pays (ou * si recherche d'une commune correspondant à des coordonnées, par défaut vaut FR)
39
	 */
39
	 */
40
	function getElement($uid){
40
	function getElement($uid){
41
 
41
 
42
		$header = '';
42
		$header = '';
43
		$retour = array();
43
		$retour = array();
44
 
44
 
45
		$params = $this->traiterParametres($uid);
45
		$params = $this->traiterParametres($uid);
46
		
46
		
47
		if ($this->estUneRequeteReverseGeocoding($params)) {
47
		if ($this->estUneRequeteReverseGeocoding($params)) {
48
 
48
 
49
			$informations_communes = $this->effectuerRequeteReverseGeocodingCartoOsm($params['lat'], $params['lon']);
49
			$informations_communes = $this->effectuerRequeteReverseGeocodingCartoOsm($params['lat'], $params['lon']);
50
			
50
			
51
			if (!$informations_communes) {
51
			if (!$informations_communes) {
52
				$informations_communes = $this->effectuerRequeteReverseGeocodingGeonames($params['lat'], $params['lon']);
52
				$informations_communes = $this->effectuerRequeteReverseGeocodingGeonames($params['lat'], $params['lon']);
53
			}
53
			}
54
			
54
			
55
			$header = 'Content-Type: application/json; charset=UTF-8';
55
			$header = 'Content-Type: application/json; charset=UTF-8';
56
			$retour = json_encode($informations_communes) ;
56
			$retour = json_encode($informations_communes) ;
57
 
57
 
58
	    } elseif ($this->estUneRequeteGeocoding($params)) {
58
	    } elseif ($this->estUneRequeteGeocoding($params)) {
59
	    	    	
59
	    	
-
 
60
			$informations_coord = $this->chercherCentroideCommuneBdd($params['commune'],$params['code_postal']);
-
 
61
			if(!$informations_coord) {
60
	    	$informations_coord = $this->effectuerRequeteGeocodingGeonames($params['commune'],$params['code_postal'],$params['code_pays']);
62
				$informations_coord = $this->effectuerRequeteGeocodingGeonames($params['commune'],$params['code_postal'],$params['code_pays']);
-
 
63
			}
61
 
64
 
62
			$header = 'Content-Type: application/json; charset=UTF-8';
65
			$header = 'Content-Type: application/json; charset=UTF-8';
63
			$retour = json_encode($informations_coord);
66
			$retour = json_encode($informations_coord);
64
 
67
 
65
	    } else {
68
	    } else {
66
 
69
 
67
	      $header = 'HTTP/1.0 400 Bad Request';
70
	      $header = 'HTTP/1.0 400 Bad Request';
68
	      $retour = 'Commune ou Coordonnées non spécifiées' ;
71
	      $retour = 'Commune ou Coordonnées non spécifiées' ;
69
	    }
72
	    }
70
 
73
 
71
		header($header);
74
		header($header);
72
		echo $retour;
75
		echo $retour;
73
	}
76
	}
74
	
77
	
75
	protected function traiterParametres($params) {
78
	protected function traiterParametres($params) {
76
		
79
		
77
		$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
80
		$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
78
		$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*'); 
81
		$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*'); 
79
 
82
 
80
		$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*'); 
83
		$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*'); 
81
		$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
84
		$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
82
		
-
 
83
		if(strlen($code_postal) > 2) {
-
 
84
			$code_postal = substr($code_postal,0,2);	
-
 
85
		}
-
 
86
 
85
 
87
		$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
86
		$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
88
		
87
		
89
		return array('lat' => $lat, 'lon' => $lng, 'commune' => $commune,
88
		return array('lat' => $lat, 'lon' => $lng, 'commune' => $commune,
90
					'code_postal' => $code_postal, 'code_pays' => $code_pays);
89
					'code_postal' => $code_postal, 'code_pays' => $code_pays);
91
	}
90
	}
92
	
91
	
93
	private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
92
	private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
94
		return isset($params[$indice]) ? str_replace('"','',urldecode($params[$indice])) : $valeur_si_non_present; 
93
		return isset($params[$indice]) ? str_replace('"','',urldecode($params[$indice])) : $valeur_si_non_present; 
95
	}
94
	}
96
	
95
	
97
	private function estUneRequeteReverseGeocoding($params) {
96
	private function estUneRequeteReverseGeocoding($params) {
98
		
97
		
99
		return ($params['lat'] != '*' && $params['lon'] != '*');
98
		return ($params['lat'] != '*' && $params['lon'] != '*');
100
	}
99
	}
101
	
100
	
102
	private function estUneRequeteGeocoding($params) {
101
	private function estUneRequeteGeocoding($params) {
103
		return ($params['commune'] != '*');
102
		return ($params['commune'] != '*');
104
	}
103
	}
105
	
104
	
106
	private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
105
	private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
107
		
106
		
108
		$infos_commune_json = @file_get_contents($this->url_service_geo_local."?lat=".$lat."&lon=".$lon);
107
		$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=".$lat."&lon=".$lon);
109
		$infos_commune = json_decode($infos_commune_json);
-
 
110
		
-
 
111
		print_r($infos_commune);
108
		$infos_commune = json_decode($infos_commune_json);
112
		
109
		
113
		$retour = false;
110
		$retour = false;
114
		
111
		
115
		if ($this->estUnRetourOsmValide($infos_commune)) {
112
		if ($this->estUnRetourOsmValide($infos_commune)) {
116
			$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
113
			$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
117
		}
114
		}
118
		
115
		
119
		return $retour;
116
		return $retour;
120
	}
117
	}
121
	
118
	
122
	private function estUnretourOsmValide($retour) {
119
	private function estUnretourOsmValide($retour) {
123
		return (is_a($retour,'stdClass') && property_exists($retour,'nom') && property_exists($retour,'codeINSEE'));
120
		return (is_a($retour,'stdClass') && property_exists($retour,'nom') && property_exists($retour,'codeINSEE'));
124
	}
121
	}
125
	
122
	
126
	private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) {
123
	private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) {
127
		
124
		
128
		$infos_commune_json = @file_get_contents($this->adresse_service_geonames.
125
		$infos_commune_json = @file_get_contents($this->adresse_service_geonames.
129
												$this->nom_service_reverse_geocoding.
126
			$this->nom_service_reverse_geocoding.
130
												"?featureClass=ADM4&lat=".urlencode($lat)."&lng=".urlencode($lon).
127
			"?featureClass=ADM4&lat=".urlencode($lat)."&lng=".urlencode($lon).
131
												"&style=full") ;
128
			"&style=full") ;
132
											
129
											
133
		$objet_retour = json_decode($infos_commune_json);
130
		$objet_retour = json_decode($infos_commune_json);
134
		
131
		
135
		$retour = false;
132
		$retour = false;
136
		if($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) { 
133
		if($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) { 
137
			$retour = array('nom' => $objet_retour->geonames[0]->name, 'code_insee' => $objet_retour->geonames[0]->adminCode4);
134
			$retour = array('nom' => $objet_retour->geonames[0]->name, 'code_insee' => $objet_retour->geonames[0]->adminCode4);
138
		}
135
		}
139
		
136
		
140
		return $retour;
137
		return $retour;
141
	}
138
	}
142
	
139
	
143
	private function estUnRetourReverseGeocodingGeonamesValide($retour) {
140
	private function estUnRetourReverseGeocodingGeonamesValide($retour) {
144
		
141
		
145
		$valide = false;
142
		$valide = false;
146
		
143
		
147
		if (is_a($retour,'stdClass') && property_exists($retour,'geonames') 
144
		if (is_a($retour,'stdClass') && property_exists($retour,'geonames') 
148
			&& is_array($retour->geonames) && count($retour->geonames) > 0) {
145
			&& is_array($retour->geonames) && count($retour->geonames) > 0) {
149
			$objet_resultats = $retour->geonames[0];
146
			$objet_resultats = $retour->geonames[0];
150
			if (property_exists($objet_resultats,'adminName4') && property_exists($objet_resultats,'adminCode2')) {
147
			if (property_exists($objet_resultats,'adminName4') && property_exists($objet_resultats,'adminCode2')) {
151
				$valide = true;
148
				$valide = true;
152
			}
149
			}
153
		} 
150
		} 
154
		
151
		
155
		return $valide;
152
		return $valide;
156
	}
153
	}
-
 
154
 
-
 
155
	private function chercherCentroideCommuneBdd($commune, $departement) {
-
 
156
 
-
 
157
		$commune = str_replace(' ','_',$commune);
-
 
158
		$commune = str_replace('-','_',$commune);
-
 
159
 
-
 
160
		if(strlen($departement) > 2) {
-
 
161
			$departement = substr($departement,0,2);
-
 
162
		}
-
 
163
		$requete_selection_commune = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
-
 
164
					      'WHERE nom LIKE '.$this->proteger($commune).' AND code LIKE '.$this->proteger($departement.'%');
-
 
165
 
-
 
166
		$commune_coordonnees = $this->executerRequete($requete_selection_commune);
-
 
167
 
-
 
168
		$retour = false;
-
 
169
 
-
 
170
		if($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
-
 
171
 
-
 
172
			  $lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
-
 
173
			  
-
 
174
			  $retour = array('lat' => (float)$lat_lon['lat'],
-
 
175
			  'lng' => (float)$lat_lon['lng'],
-
 
176
			  'nom' => $commune,
-
 
177
			  'code_insee' => $commune_coordonnees[0]['code']
-
 
178
			  );
-
 
179
 
-
 
180
		}
-
 
181
 
-
 
182
		return $retour;
-
 
183
	}
-
 
184
 
-
 
185
	private function convertirUtmVersLatLong($x, $y, $sector) {
-
 
186
    	
-
 
187
		$lat_long = array();
-
 
188
		
-
 
189
		$convertisseur = new gPoint();
-
 
190
		$convertisseur->setUTM($x, $y, $sector);
-
 
191
		$convertisseur->convertTMtoLL();
-
 
192
		$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
-
 
193
		$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
-
 
194
			
-
 
195
		return $lat_long;
-
 
196
	}
157
	
197
	
158
	private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
198
	private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
159
		
199
		
160
		$requete = $this->adresse_service_geonames.
200
		$requete = $this->adresse_service_geonames.
161
				$this->nom_service_geocoding.
201
				$this->nom_service_geocoding.
162
				"?placename_startsWith=".urlencode($commune);
202
				"?placename_startsWith=".urlencode($commune);
163
 
203
 
164
		if($code_postal != '*') {
204
		if($code_postal != '*') {
165
			$requete .= "&postalcode_startsWith=".urlencode($code_postal);
205
			$requete .= "&postalcode_startsWith=".urlencode($code_postal);
166
	    }
206
		}
167
		$requete .= "&country=".urlencode($code_pays)."&maxRows=10" ;
207
		$requete .= "&country=".urlencode($code_pays)."&maxRows=10" ;
168
 
208
 
169
		$coord_json = @file_get_contents($requete);
209
		$coord_json = @file_get_contents($requete);
170
		$objet_retour = json_decode($coord_json);
210
		$objet_retour = json_decode($coord_json);
171
		
211
		
172
		$retour = false;
212
		$retour = false;
173
		
213
		
174
		if($this->estUnRetourGeocodingGeonamesValide($objet_retour)) { 
214
		if($this->estUnRetourGeocodingGeonamesValide($objet_retour)) { 
175
			$retour = array('lat' => $objet_retour->postalCodes[0]->lat, 
215
			$retour = array('lat' => $objet_retour->postalCodes[0]->lat, 
176
							'lng' => $objet_retour->postalCodes[0]->lng,
216
							'lng' => $objet_retour->postalCodes[0]->lng,
177
							'nom' => $objet_retour->postalCodes[0]->placeName,
217
							'nom' => $objet_retour->postalCodes[0]->placeName,
178
							'code_insee' => $objet_retour->postalCodes[0]->postalCode
218
							'code_insee' => $objet_retour->postalCodes[0]->postalCode
179
							);
219
							);
180
		}
220
		}
181
 
221
 
182
		return $retour;
222
		return $retour;
183
	}
223
	}
184
	
224
	
185
	private function estUnRetourGeocodingGeonamesValide($retour) {
225
	private function estUnRetourGeocodingGeonamesValide($retour) {
186
		$valide = false;
226
		$valide = false;
187
		
227
		
188
		if (is_a($retour,'stdClass') && property_exists($retour,'postalCodes') 
228
		if (is_a($retour,'stdClass') && property_exists($retour,'postalCodes') 
189
			&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
229
			&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
190
			$objet_resultats = $retour->postalCodes[0];
230
			$objet_resultats = $retour->postalCodes[0];
191
			if (property_exists($objet_resultats,'lat') && property_exists($objet_resultats,'lng')) {
231
			if (property_exists($objet_resultats,'lat') && property_exists($objet_resultats,'lng')) {
192
				$valide = true;
232
				$valide = true;
193
			}
233
			}
194
		} 
234
		} 
195
		
235
		
196
		return $valide;
236
		return $valide;
197
	}
237
	}
198
}
238
}
199
?>
239
?>