Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 1311 Rev 1765
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']);
60
			$informations_coord = $this->chercherCentroideCommuneBdd($params['commune'],$params['code_postal']);
61
			if(!$informations_coord) {
61
			if(!$informations_coord) {
62
				$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
			}
63
			}
64
 
64
 
65
			$header = 'Content-Type: application/json; charset=UTF-8';
65
			$header = 'Content-Type: application/json; charset=UTF-8';
66
			$retour = json_encode($informations_coord);
66
			$retour = json_encode($informations_coord);
67
 
67
 
68
	    } else {
68
	    } else {
69
 
69
 
70
	      $header = 'HTTP/1.0 400 Bad Request';
70
	      $header = 'HTTP/1.0 400 Bad Request';
71
	      $retour = 'Commune ou Coordonnées non spécifiées' ;
71
	      $retour = 'Commune ou Coordonnées non spécifiées' ;
72
	    }
72
	    }
73
 
73
 
74
		header($header);
74
		header($header);
75
		echo $retour;
75
		echo $retour;
76
	}
76
	}
77
	
77
	
78
	protected function traiterParametres($params) {
78
	protected function traiterParametres($params) {
79
		
79
		
80
		$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
80
		$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
81
		$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*'); 
81
		$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*'); 
82
 
82
 
83
		$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*'); 
83
		$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*'); 
84
		$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
84
		$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
85
 
85
 
86
		$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
86
		$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
87
		
87
		
88
		return array('lat' => $lat, 'lon' => $lng, 'commune' => $commune,
88
		return array('lat' => $lat, 'lon' => $lng, 'commune' => $commune,
89
					'code_postal' => $code_postal, 'code_pays' => $code_pays);
89
					'code_postal' => $code_postal, 'code_pays' => $code_pays);
90
	}
90
	}
91
	
91
	
92
	private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
92
	private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
93
		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; 
94
	}
94
	}
95
	
95
	
96
	private function estUneRequeteReverseGeocoding($params) {
96
	private function estUneRequeteReverseGeocoding($params) {
97
		
97
		
98
		return ($params['lat'] != '*' && $params['lon'] != '*');
98
		return ($params['lat'] != '*' && $params['lon'] != '*');
99
	}
99
	}
100
	
100
	
101
	private function estUneRequeteGeocoding($params) {
101
	private function estUneRequeteGeocoding($params) {
102
		return ($params['commune'] != '*');
102
		return ($params['commune'] != '*');
103
	}
103
	}
104
	
104
	
105
	private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
105
	private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
106
		
106
		
107
		$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=".$lat."&lon=".$lon);
107
		$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=".$lat."&lon=".$lon);
108
		$infos_commune = json_decode($infos_commune_json);
108
		$infos_commune = json_decode($infos_commune_json);
109
		
109
		
110
		$retour = false;
110
		$retour = false;
111
		
111
		
112
		if ($this->estUnRetourOsmValide($infos_commune)) {
112
		if ($this->estUnRetourOsmValide($infos_commune)) {
113
			$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
113
			$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
114
		}
114
		}
115
		
115
		
116
		return $retour;
116
		return $retour;
117
	}
117
	}
118
	
118
	
119
	private function estUnretourOsmValide($retour) {
119
	private function estUnretourOsmValide($retour) {
120
		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'));
121
	}
121
	}
122
	
122
	
123
	private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) {
123
	private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) {
124
		
124
		
125
		$infos_commune_json = @file_get_contents($this->adresse_service_geonames.
125
		$infos_commune_json = @file_get_contents($this->adresse_service_geonames.
126
			$this->nom_service_reverse_geocoding.
126
			$this->nom_service_reverse_geocoding.
127
			"?featureClass=ADM4&lat=".urlencode($lat)."&lng=".urlencode($lon).
127
			"?featureClass=ADM4&lat=".urlencode($lat)."&lng=".urlencode($lon).
128
			"&style=full") ;
128
			"&style=full") ;
129
											
129
											
130
		$objet_retour = json_decode($infos_commune_json);
130
		$objet_retour = json_decode($infos_commune_json);
131
		
131
		
132
		$retour = false;
132
		$retour = false;
133
		if($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) { 
133
		if($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) { 
134
			$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);
135
		}
135
		}
136
		
136
		
137
		return $retour;
137
		return $retour;
138
	}
138
	}
139
	
139
	
140
	private function estUnRetourReverseGeocodingGeonamesValide($retour) {
140
	private function estUnRetourReverseGeocodingGeonamesValide($retour) {
141
		
141
		
142
		$valide = false;
142
		$valide = false;
143
		
143
		
144
		if (is_a($retour,'stdClass') && property_exists($retour,'geonames') 
144
		if (is_a($retour,'stdClass') && property_exists($retour,'geonames') 
145
			&& is_array($retour->geonames) && count($retour->geonames) > 0) {
145
			&& is_array($retour->geonames) && count($retour->geonames) > 0) {
146
			$objet_resultats = $retour->geonames[0];
146
			$objet_resultats = $retour->geonames[0];
147
			if (property_exists($objet_resultats,'adminName4') && property_exists($objet_resultats,'adminCode2')) {
147
			if (property_exists($objet_resultats,'adminName4') && property_exists($objet_resultats,'adminCode2')) {
148
				$valide = true;
148
				$valide = true;
149
			}
149
			}
150
		} 
150
		} 
151
		
151
		
152
		return $valide;
152
		return $valide;
153
	}
153
	}
154
 
154
 
155
	private function chercherCentroideCommuneBdd($commune, $departement) {
155
	private function chercherCentroideCommuneBdd($commune, $departement) {
156
 
156
 
157
		$commune_formatee = str_replace(' ','_',$commune);
157
		$commune_formatee = str_replace(' ','_',$commune);
158
		$commune_formatee = str_replace('-','_',$commune_formatee);
158
		$commune_formatee = str_replace('-','_',$commune_formatee);
159
 
159
 
160
		if(strlen($departement) > 2) {
160
		if(strlen($departement) > 2) {
161
			$departement = substr($departement,0,2);
161
			$departement = substr($departement,0,2);
162
		}
162
		}
163
		$requete_selection_commune = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
163
		$requete_selection_commune = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
164
					      'WHERE nom LIKE '.$this->proteger($commune_formatee).' AND code LIKE '.$this->proteger($departement.'%');
164
					      'WHERE nom LIKE '.Cel::db()->proteger($commune_formatee).' AND code LIKE '.Cel::db()->proteger($departement.'%');
165
 
165
 
166
		$commune_coordonnees = $this->executerRequete($requete_selection_commune);
166
		$commune_coordonnees = Cel::db()->executerRequete($requete_selection_commune);
167
 
167
 
168
		$retour = false;
168
		$retour = false;
169
 
169
 
170
		if($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
170
		if($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
171
 
171
 
172
			  $lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
172
			  $lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
173
			  
173
			  
174
			  $retour = array('lat' => (float)$lat_lon['lat'],
174
			  $retour = array('lat' => (float)$lat_lon['lat'],
175
			  'lng' => (float)$lat_lon['lng'],
175
			  'lng' => (float)$lat_lon['lng'],
176
			  'nom' => $commune,
176
			  'nom' => $commune,
177
			  'code_insee' => $commune_coordonnees[0]['code']
177
			  'code_insee' => $commune_coordonnees[0]['code']
178
			  );
178
			  );
179
 
179
 
180
		}
180
		}
181
 
181
 
182
		return $retour;
182
		return $retour;
183
	}
183
	}
184
 
184
 
185
	private function convertirUtmVersLatLong($x, $y, $sector) {
185
	private function convertirUtmVersLatLong($x, $y, $sector) {
186
    	
186
    	
187
		$lat_long = array();
187
		$lat_long = array();
188
		
188
		
189
		$convertisseur = new gPoint();
189
		$convertisseur = new gPoint();
190
		$convertisseur->setUTM($x, $y, $sector);
190
		$convertisseur->setUTM($x, $y, $sector);
191
		$convertisseur->convertTMtoLL();
191
		$convertisseur->convertTMtoLL();
192
		$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
192
		$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
193
		$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
193
		$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
194
			
194
			
195
		return $lat_long;
195
		return $lat_long;
196
	}
196
	}
197
	
197
	
198
	private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
198
	private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
199
		
199
		
200
		$requete = $this->adresse_service_geonames.
200
		$requete = $this->adresse_service_geonames.
201
				$this->nom_service_geocoding.
201
				$this->nom_service_geocoding.
202
				"?placename_startsWith=".urlencode($commune);
202
				"?placename_startsWith=".urlencode($commune);
203
 
203
 
204
		if($code_postal != '*') {
204
		if($code_postal != '*') {
205
			$requete .= "&postalcode_startsWith=".urlencode($code_postal);
205
			$requete .= "&postalcode_startsWith=".urlencode($code_postal);
206
		}
206
		}
207
		$requete .= "&country=".urlencode($code_pays)."&maxRows=10" ;
207
		$requete .= "&country=".urlencode($code_pays)."&maxRows=10" ;
208
 
208
 
209
		$coord_json = @file_get_contents($requete);
209
		$coord_json = @file_get_contents($requete);
210
		$objet_retour = json_decode($coord_json);
210
		$objet_retour = json_decode($coord_json);
211
		
211
		
212
		$retour = false;
212
		$retour = false;
213
		
213
		
214
		if($this->estUnRetourGeocodingGeonamesValide($objet_retour)) { 
214
		if($this->estUnRetourGeocodingGeonamesValide($objet_retour)) { 
215
			$retour = array('lat' => $objet_retour->postalCodes[0]->lat, 
215
			$retour = array('lat' => $objet_retour->postalCodes[0]->lat, 
216
							'lng' => $objet_retour->postalCodes[0]->lng,
216
							'lng' => $objet_retour->postalCodes[0]->lng,
217
							'nom' => $objet_retour->postalCodes[0]->placeName,
217
							'nom' => $objet_retour->postalCodes[0]->placeName,
218
							'code_insee' => $objet_retour->postalCodes[0]->postalCode
218
							'code_insee' => $objet_retour->postalCodes[0]->postalCode
219
							);
219
							);
220
		}
220
		}
221
 
221
 
222
		return $retour;
222
		return $retour;
223
	}
223
	}
224
	
224
	
225
	private function estUnRetourGeocodingGeonamesValide($retour) {
225
	private function estUnRetourGeocodingGeonamesValide($retour) {
226
		$valide = false;
226
		$valide = false;
227
		
227
		
228
		if (is_a($retour,'stdClass') && property_exists($retour,'postalCodes') 
228
		if (is_a($retour,'stdClass') && property_exists($retour,'postalCodes') 
229
			&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
229
			&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
230
			$objet_resultats = $retour->postalCodes[0];
230
			$objet_resultats = $retour->postalCodes[0];
231
			if (property_exists($objet_resultats,'lat') && property_exists($objet_resultats,'lng')) {
231
			if (property_exists($objet_resultats,'lat') && property_exists($objet_resultats,'lng')) {
232
				$valide = true;
232
				$valide = true;
233
			}
233
			}
234
		} 
234
		} 
235
		
235
		
236
		return $valide;
236
		return $valide;
237
	}
237
	}
238
}
238
}
239
?>
239
?>