Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 809 Rev 970
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_db']['url_service_geo_geonames'];
24
		$this->adresse_service_geonames = $this->config['cel']['url_service_geo_geonames'];
25
		$this->adresse_service_local = $this->config['cel_db']['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_db']['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_db']['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->effectuerRequeteGeocodingGeonames($params['commune'],$params['code_postal'],$params['code_pays']);
60
	    	$informations_coord = $this->effectuerRequeteGeocodingGeonames($params['commune'],$params['code_postal'],$params['code_pays']);
61
 
61
 
62
			$header = 'Content-Type: application/json; charset=UTF-8';
62
			$header = 'Content-Type: application/json; charset=UTF-8';
63
			$retour = json_encode($informations_coord);
63
			$retour = json_encode($informations_coord);
64
 
64
 
65
	    } else {
65
	    } else {
66
 
66
 
67
	      $header = 'HTTP/1.0 400 Bad Request';
67
	      $header = 'HTTP/1.0 400 Bad Request';
68
	      $retour = 'Commune ou Coordonnées non spécifiées' ;
68
	      $retour = 'Commune ou Coordonnées non spécifiées' ;
69
	    }
69
	    }
70
 
70
 
71
		header($header);
71
		header($header);
72
		echo $retour;
72
		echo $retour;
73
	}
73
	}
74
	
74
	
75
	protected function traiterParametres($params) {
75
	protected function traiterParametres($params) {
76
		
76
		
77
		$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
77
		$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
78
		$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*'); 
78
		$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*'); 
79
 
79
 
80
		$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*'); 
80
		$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*'); 
81
		$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
81
		$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
-
 
82
		
-
 
83
		if(strlen($code_postal) > 2) {
-
 
84
			$code_postal = substr($code_postal,0,2);	
-
 
85
		}
82
 
86
 
83
		$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
87
		$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
84
		
88
		
85
		return array('lat' => $lat, 'lon' => $lng, 'commune' => $commune,
89
		return array('lat' => $lat, 'lon' => $lng, 'commune' => $commune,
86
					'code_postal' => $code_postal, 'code_pays' => $code_pays);
90
					'code_postal' => $code_postal, 'code_pays' => $code_pays);
87
	}
91
	}
88
	
92
	
89
	private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
93
	private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
90
		return isset($params[$indice]) ? str_replace('"','',urldecode($params[$indice])) : $valeur_si_non_present; 
94
		return isset($params[$indice]) ? str_replace('"','',urldecode($params[$indice])) : $valeur_si_non_present; 
91
	}
95
	}
92
	
96
	
93
	private function estUneRequeteReverseGeocoding($params) {
97
	private function estUneRequeteReverseGeocoding($params) {
94
		
98
		
95
		return ($params['lat'] != '*' && $params['lon'] != '*');
99
		return ($params['lat'] != '*' && $params['lon'] != '*');
96
	}
100
	}
97
	
101
	
98
	private function estUneRequeteGeocoding($params) {
102
	private function estUneRequeteGeocoding($params) {
99
		return ($params['commune'] != '*');
103
		return ($params['commune'] != '*');
100
	}
104
	}
101
	
105
	
102
	private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
106
	private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
103
		
107
		
104
		$infos_commune_json = @file_get_contents($this->url_service_geo_local."?lat=".$lat."&lon=".$lon);
108
		$infos_commune_json = @file_get_contents($this->url_service_geo_local."?lat=".$lat."&lon=".$lon);
105
		$infos_commune = json_decode($infos_commune_json);
109
		$infos_commune = json_decode($infos_commune_json);
-
 
110
		
-
 
111
		print_r($infos_commune);
106
		
112
		
107
		$retour = false;
113
		$retour = false;
108
		
114
		
109
		if ($this->estUnRetourOsmValide($infos_commune)) {
115
		if ($this->estUnRetourOsmValide($infos_commune)) {
110
			$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
116
			$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
111
		}
117
		}
112
		
118
		
113
		return $retour;
119
		return $retour;
114
	}
120
	}
115
	
121
	
116
	private function estUnretourOsmValide($retour) {
122
	private function estUnretourOsmValide($retour) {
117
		return (is_a($retour,'stdClass') && property_exists($retour,'nom') && property_exists($retour,'codeINSEE'));
123
		return (is_a($retour,'stdClass') && property_exists($retour,'nom') && property_exists($retour,'codeINSEE'));
118
	}
124
	}
119
	
125
	
120
	private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) {
126
	private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) {
121
		
127
		
122
		$infos_commune_json = @file_get_contents($this->adresse_service_geonames.
128
		$infos_commune_json = @file_get_contents($this->adresse_service_geonames.
123
												$this->nom_service_reverse_geocoding.
129
												$this->nom_service_reverse_geocoding.
124
												"?featureClass=ADM4&lat=".urlencode($lat)."&lng=".urlencode($lon).
130
												"?featureClass=ADM4&lat=".urlencode($lat)."&lng=".urlencode($lon).
125
												"&style=full") ;
131
												"&style=full") ;
126
											
132
											
127
		$objet_retour = json_decode($infos_commune_json);
133
		$objet_retour = json_decode($infos_commune_json);
128
		
134
		
129
		$retour = false;
135
		$retour = false;
130
		if($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) { 
136
		if($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) { 
131
			$retour = array('nom' => $objet_retour->geonames[0]->name, 'code_insee' => $objet_retour->geonames[0]->adminCode4);
137
			$retour = array('nom' => $objet_retour->geonames[0]->name, 'code_insee' => $objet_retour->geonames[0]->adminCode4);
132
		}
138
		}
133
		
139
		
134
		return $retour;
140
		return $retour;
135
	}
141
	}
136
	
142
	
137
	private function estUnRetourReverseGeocodingGeonamesValide($retour) {
143
	private function estUnRetourReverseGeocodingGeonamesValide($retour) {
138
		
144
		
139
		$valide = false;
145
		$valide = false;
140
		
146
		
141
		if (is_a($retour,'stdClass') && property_exists($retour,'geonames') 
147
		if (is_a($retour,'stdClass') && property_exists($retour,'geonames') 
142
			&& is_array($retour->geonames) && count($retour->geonames) > 0) {
148
			&& is_array($retour->geonames) && count($retour->geonames) > 0) {
143
			$objet_resultats = $retour->geonames[0];
149
			$objet_resultats = $retour->geonames[0];
144
			if (property_exists($objet_resultats,'adminName4') && property_exists($objet_resultats,'adminCode2')) {
150
			if (property_exists($objet_resultats,'adminName4') && property_exists($objet_resultats,'adminCode2')) {
145
				$valide = true;
151
				$valide = true;
146
			}
152
			}
147
		} 
153
		} 
148
		
154
		
149
		return $valide;
155
		return $valide;
150
	}
156
	}
151
	
157
	
152
	private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
158
	private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
153
		
159
		
154
		$requete = $this->adresse_service_geonames.
160
		$requete = $this->adresse_service_geonames.
155
				$this->nom_service_geocoding.
161
				$this->nom_service_geocoding.
156
				"?placename_startsWith=".urlencode($commune);
162
				"?placename_startsWith=".urlencode($commune);
157
 
163
 
158
		if($code_postal != '*') {
164
		if($code_postal != '*') {
159
			$requete .= "&postalcode_startsWith=".urlencode($code_postal);
165
			$requete .= "&postalcode_startsWith=".urlencode($code_postal);
160
	    }
166
	    }
161
		$requete .= "&country=".urlencode($code_pays)."&maxRows=10" ;
167
		$requete .= "&country=".urlencode($code_pays)."&maxRows=10" ;
162
 
168
 
163
		$coord_json = @file_get_contents($requete);
169
		$coord_json = @file_get_contents($requete);
164
		$objet_retour = json_decode($coord_json);
170
		$objet_retour = json_decode($coord_json);
165
		
171
		
166
		$retour = false;
172
		$retour = false;
167
		
173
		
168
		if($this->estUnRetourGeocodingGeonamesValide($objet_retour)) { 
174
		if($this->estUnRetourGeocodingGeonamesValide($objet_retour)) { 
169
			$retour = array('lat' => $objet_retour->postalCodes[0]->lat, 
175
			$retour = array('lat' => $objet_retour->postalCodes[0]->lat, 
170
							'lng' => $objet_retour->postalCodes[0]->lng,
176
							'lng' => $objet_retour->postalCodes[0]->lng,
171
							'nom' => $objet_retour->postalCodes[0]->placeName,
177
							'nom' => $objet_retour->postalCodes[0]->placeName,
172
							'code_insee' => $objet_retour->postalCodes[0]->postalCode
178
							'code_insee' => $objet_retour->postalCodes[0]->postalCode
173
							);
179
							);
174
		}
180
		}
175
 
181
 
176
		return $retour;
182
		return $retour;
177
	}
183
	}
178
	
184
	
179
	private function estUnRetourGeocodingGeonamesValide($retour) {
185
	private function estUnRetourGeocodingGeonamesValide($retour) {
180
		$valide = false;
186
		$valide = false;
181
		
187
		
182
		if (is_a($retour,'stdClass') && property_exists($retour,'postalCodes') 
188
		if (is_a($retour,'stdClass') && property_exists($retour,'postalCodes') 
183
			&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
189
			&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
184
			$objet_resultats = $retour->postalCodes[0];
190
			$objet_resultats = $retour->postalCodes[0];
185
			if (property_exists($objet_resultats,'lat') && property_exists($objet_resultats,'lng')) {
191
			if (property_exists($objet_resultats,'lat') && property_exists($objet_resultats,'lng')) {
186
				$valide = true;
192
				$valide = true;
187
			}
193
			}
188
		} 
194
		} 
189
		
195
		
190
		return $valide;
196
		return $valide;
191
	}
197
	}
192
}
198
}
193
?>
199
?>