Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 1970 Rev 2143
Line 8... Line 8...
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
	}
Line 30... Line 30...
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)
Line 41... Line 41...
41
 
41
 
42
		$header = '';
42
		$header = '';
Line 43... Line 43...
43
		$retour = array();
43
		$retour = array();
44
 
44
 
45
		$params = $this->traiterParametres($uid);
45
		$params = $this->traiterParametres($uid);
Line 46... Line 46...
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
			}
Line 54... Line 54...
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
 
Line 72... Line 72...
72
	    }
72
	    }
Line 73... Line 73...
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, '*');
Line 81... Line 81...
81
		$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*'); 
81
		$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*');
82
 
82
 
Line 83... Line 83...
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
			"?lat=".urlencode($lat)."&lng=".urlencode($lon).
127
			"?lat=".urlencode($lat)."&lng=".urlencode($lon).
128
			"&style=full");
128
			"&style=full");
129
		$objet_retour = json_decode($infos_commune_json);
129
		$objet_retour = json_decode($infos_commune_json);
130
		
130
 
131
		$retour = false;
131
		$retour = false;
132
		if($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) { 
132
		if($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) {
133
			$retour = array('nom' => $objet_retour->geonames[0]->adminName4, 'code_insee' => $objet_retour->geonames[0]->adminCode4);
133
			$retour = array('nom' => $objet_retour->geonames[0]->adminName4, 'code_insee' => $objet_retour->geonames[0]->adminCode4);
134
		}
134
		}
135
		
135
 
136
		return $retour;
136
		return $retour;
137
	}
137
	}
138
	
138
 
139
	private function estUnRetourReverseGeocodingGeonamesValide($retour) {
139
	private function estUnRetourReverseGeocodingGeonamesValide($retour) {
140
		
140
 
141
		$valide = false;
141
		$valide = false;
142
		
142
 
143
		if (is_a($retour,'stdClass') && property_exists($retour,'geonames') 
143
		if (is_a($retour,'stdClass') && property_exists($retour,'geonames')
144
			&& is_array($retour->geonames) && count($retour->geonames) > 0) {
144
			&& is_array($retour->geonames) && count($retour->geonames) > 0) {
145
			$objet_resultats = $retour->geonames[0];
145
			$objet_resultats = $retour->geonames[0];
146
			if (property_exists($objet_resultats,'adminName4') && property_exists($objet_resultats,'adminCode2')) {
146
			if (property_exists($objet_resultats,'adminName4') && property_exists($objet_resultats,'adminCode2')) {
147
				$valide = true;
147
				$valide = true;
148
			}
148
			}
149
		} 
149
		}
Line 150... Line 150...
150
		
150
 
Line 160... Line 160...
160
			$departement = substr($departement,0,2);
160
			$departement = substr($departement,0,2);
161
		}
161
		}
162
		$requete_selection_commune = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
162
		$requete_selection_commune = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
163
					      'WHERE nom LIKE '.Cel::db()->proteger($commune_formatee).' AND code LIKE '.Cel::db()->proteger($departement.'%');
163
					      'WHERE nom LIKE '.Cel::db()->proteger($commune_formatee).' AND code LIKE '.Cel::db()->proteger($departement.'%');
Line 164... Line 164...
164
 
164
 
Line 165... Line 165...
165
		$commune_coordonnees = Cel::db()->executerRequete($requete_selection_commune);
165
		$commune_coordonnees = Cel::db()->requeter($requete_selection_commune);
Line 166... Line 166...
166
 
166
 
Line 167... Line 167...
167
		$retour = false;
167
		$retour = false;
168
 
168
 
169
		if($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
169
		if($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
170
 
170
 
171
			  $lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
171
			  $lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
172
			  
172
 
173
			  $retour = array('lat' => (float)$lat_lon['lat'],
173
			  $retour = array('lat' => (float)$lat_lon['lat'],
Line 180... Line 180...
180
 
180
 
181
		return $retour;
181
		return $retour;
Line 182... Line 182...
182
	}
182
	}
183
 
183
 
184
	private function convertirUtmVersLatLong($x, $y, $sector) {
184
	private function convertirUtmVersLatLong($x, $y, $sector) {
185
    	
185
 
186
		$lat_long = array();
186
		$lat_long = array();
187
		
187
 
188
		$convertisseur = new gPoint();
188
		$convertisseur = new gPoint();
189
		$convertisseur->setUTM($x, $y, $sector);
189
		$convertisseur->setUTM($x, $y, $sector);
190
		$convertisseur->convertTMtoLL();
190
		$convertisseur->convertTMtoLL();
191
		$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
191
		$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
192
		$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
192
		$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
193
			
193
 
194
		return $lat_long;
194
		return $lat_long;
195
	}
195
	}
196
	
196
 
197
	private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
197
	private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
198
		
198
 
199
		$requete = $this->adresse_service_geonames.
199
		$requete = $this->adresse_service_geonames.
Line 200... Line 200...
200
				$this->nom_service_geocoding.
200
				$this->nom_service_geocoding.
Line 205... Line 205...
205
		}
205
		}
206
		$requete .= "&country=".urlencode($code_pays)."&maxRows=10" ;
206
		$requete .= "&country=".urlencode($code_pays)."&maxRows=10" ;
Line 207... Line 207...
207
 
207
 
208
		$coord_json = @file_get_contents($requete);
208
		$coord_json = @file_get_contents($requete);
209
		$objet_retour = json_decode($coord_json);
209
		$objet_retour = json_decode($coord_json);
210
		
210
 
211
		$retour = false;
211
		$retour = false;
212
		
212
 
213
		if($this->estUnRetourGeocodingGeonamesValide($objet_retour)) { 
213
		if($this->estUnRetourGeocodingGeonamesValide($objet_retour)) {
214
			$retour = array('lat' => $objet_retour->postalCodes[0]->lat, 
214
			$retour = array('lat' => $objet_retour->postalCodes[0]->lat,
215
							'lng' => $objet_retour->postalCodes[0]->lng,
215
							'lng' => $objet_retour->postalCodes[0]->lng,
216
							'nom' => $objet_retour->postalCodes[0]->placeName,
216
							'nom' => $objet_retour->postalCodes[0]->placeName,
217
							'code_insee' => $objet_retour->postalCodes[0]->postalCode
217
							'code_insee' => $objet_retour->postalCodes[0]->postalCode
218
							);
218
							);
Line 219... Line 219...
219
		}
219
		}
220
 
220
 
221
		return $retour;
221
		return $retour;
222
	}
222
	}
223
	
223
 
224
	private function estUnRetourGeocodingGeonamesValide($retour) {
224
	private function estUnRetourGeocodingGeonamesValide($retour) {
225
		$valide = false;
225
		$valide = false;
226
		
226
 
227
		if (is_a($retour,'stdClass') && property_exists($retour,'postalCodes') 
227
		if (is_a($retour,'stdClass') && property_exists($retour,'postalCodes')
228
			&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
228
			&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
229
			$objet_resultats = $retour->postalCodes[0];
229
			$objet_resultats = $retour->postalCodes[0];
230
			if (property_exists($objet_resultats,'lat') && property_exists($objet_resultats,'lng')) {
230
			if (property_exists($objet_resultats,'lat') && property_exists($objet_resultats,'lng')) {
231
				$valide = true;
231
				$valide = true;
232
			}
232
			}
233
		} 
233
		}
234
		
234
 
235
		return $valide;
235
		return $valide;
236
	}
236
	}
237
}
237
}