Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 2462 Rev 2538
Line 15... Line 15...
15
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
15
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
16
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
16
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
17
 */
17
 */
18
class CoordSearch extends Cel {
18
class CoordSearch extends Cel {
Line 19... Line 19...
19
 
19
 
20
	private $adresse_service_geonames = null;
20
	private $adresse_service_mondial = null;
Line 21... Line -...
21
	private $adresse_service_local = null;
-
 
22
 
-
 
23
	private $nom_service_geocoding = null;
-
 
24
	private $nom_service_reverse_geocoding = null;
21
	private $adresse_service_local = null;
25
 
22
 
Line 26... Line 23...
26
	public function __construct($config) {
23
	public function __construct($config) {
27
		parent::__construct($config);
24
		parent::__construct($config);
28
 
-
 
29
		$this->adresse_service_geonames = $this->config['cel']['url_service_geo_geonames'];
-
 
30
		$this->adresse_service_local = $this->config['cel']['url_service_geo_local'];
-
 
31
 
25
 
Line 32... Line 26...
32
		$this->nom_service_geocoding = $this->config['cel']['nom_service_geocoding_geonames'];
26
		$this->adresse_service_mondial = $this->config['cel']['url_service_geo_mondial'];
33
		$this->nom_service_reverse_geocoding = $this->config['cel']['nom_service_reverse_geocoding_geonames'];
27
		$this->adresse_service_local = $this->config['cel']['url_service_geo_local'];
34
	}
28
	}
Line 50... Line 44...
50
 
44
 
51
		if ($this->estUneRequeteReverseGeocoding($params)) {
45
		if ($this->estUneRequeteReverseGeocoding($params)) {
Line 52... Line 46...
52
			$informations_communes = $this->effectuerRequeteReverseGeocodingCartoOsm($params['lat'], $params['lon']);
46
			$informations_communes = $this->effectuerRequeteReverseGeocodingCartoOsm($params['lat'], $params['lon']);
53
 
47
 
54
			if (!$informations_communes) {
48
			if (!$informations_communes) {
Line 55... Line 49...
55
				$informations_communes = $this->effectuerRequeteReverseGeocodingGeonames($params['lat'], $params['lon']);
49
				$informations_communes = $this->effectuerRequeteReverseGeocodingMondiale($params['lat'], $params['lon']);
56
			}
50
			}
57
 
51
 
58
			$header = 'Content-Type: application/json; charset=UTF-8';
52
			$header = 'Content-Type: application/json; charset=UTF-8';
59
			$retour = json_encode($informations_communes);
53
			$retour = json_encode($informations_communes);
60
		} elseif ($this->estUneRequeteGeocoding($params)) {
54
		} elseif ($this->estUneRequeteGeocoding($params)) {
61
			$informations_coord = $this->chercherCentroideCommuneBdd($params['commune'],$params['code_postal']);
55
			$informations_coord = $this->chercherCentroideCommuneBdd($params['commune'],$params['code_postal']);
Line 62... Line 56...
62
			if (!$informations_coord) {
56
			if (!$informations_coord) {
63
				$informations_coord = $this->effectuerRequeteGeocodingGeonames($params['commune'],$params['code_postal'],$params['code_pays']);
57
				$informations_coord = $this->effectuerRequeteGeocodingMondiale($params['commune'],$params['code_postal'],$params['code_pays']);
64
			}
58
			}
Line 111... Line 105...
111
 
105
 
112
	private function estUnretourOsmValide($retour) {
106
	private function estUnretourOsmValide($retour) {
113
		return (is_a($retour, 'stdClass') && property_exists($retour, 'nom') && property_exists($retour, 'codeINSEE'));
107
		return (is_a($retour, 'stdClass') && property_exists($retour, 'nom') && property_exists($retour, 'codeINSEE'));
Line 114... Line -...
114
	}
-
 
115
 
-
 
116
	private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) {
-
 
117
		$url = $this->adresse_service_geonames.
-
 
118
			$this->nom_service_reverse_geocoding.
-
 
119
			'?lat='.urlencode($lat).'&lng='.urlencode($lon).
-
 
120
			'&style=full';
-
 
121
		$infos_commune_json = @file_get_contents($url);
-
 
122
		$objet_retour = json_decode($infos_commune_json);
-
 
123
 
-
 
124
		$retour = false;
-
 
125
		if ($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) {
-
 
126
			$retour = array(
-
 
127
				'nom' => $objet_retour->geonames[0]->adminName4,
-
 
128
				'code_insee' => $objet_retour->geonames[0]->adminCode4);
-
 
129
		}
-
 
130
		return $retour;
-
 
131
	}
-
 
132
 
-
 
133
	private function estUnRetourReverseGeocodingGeonamesValide($retour) {
-
 
134
		$valide = false;
-
 
135
		if (is_a($retour, 'stdClass') && property_exists($retour, 'geonames')
-
 
136
				&& is_array($retour->geonames) && count($retour->geonames) > 0) {
-
 
137
			$objet_resultats = $retour->geonames[0];
-
 
138
			if (property_exists($objet_resultats, 'adminName4') && property_exists($objet_resultats, 'adminCode2')) {
-
 
139
				$valide = true;
-
 
140
			}
-
 
141
		}
-
 
142
		return $valide;
-
 
143
	}
108
	}
144
 
109
 
145
	private function chercherCentroideCommuneBdd($commune, $departement) {
110
	private function chercherCentroideCommuneBdd($commune, $departement) {
146
		$commune_formatee = str_replace(array(' ', '-'), '_', $commune);
111
		$commune_formatee = str_replace(array(' ', '-'), '_', $commune);
147
		if (strlen($departement) > 2) {
112
		if (strlen($departement) > 2) {
Line 175... Line 140...
175
		$lat_long = array();
140
		$lat_long = array();
176
		$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
141
		$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
177
		$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
142
		$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
178
		return $lat_long;
143
		return $lat_long;
179
	}
144
	}
180
 
145
	
181
	private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
146
	private function effectuerRequeteReverseGeocodingMondiale($lat, $lon) {
182
		$url = $this->adresse_service_geonames.
147
		$url = $this->adresse_service_mondial.'?lat='.$lat.'&lon='.$lon;
183
			$this->nom_service_geocoding.
-
 
184
			'?placename_startsWith='.urlencode($commune).
-
 
185
			(($code_postal != '*') ? '&postalcode_startsWith='.urlencode($code_postal) : '').
148
		// Pour limiter par niveau ajouter ce qui suit à la requête : 
186
			'&country='.urlencode($code_pays).'&maxRows=10';
149
		//.'&niveau=2,3,4';
187
		$coord_json = @file_get_contents($url);
150
		$res = json_decode(file_get_contents($url), true);
188
		$objet_retour = json_decode($coord_json);
-
 
189
 
151
		
190
		$retour = false;
152
		$code = "";
191
		if ($this->estUnRetourGeocodingGeonamesValide($objet_retour)) {
-
 
192
			$retour = array(
153
		$localite = "";
-
 
154
		
193
				'lat' => $objet_retour->postalCodes[0]->lat,
155
		if(!empty($res)) {
194
				'lng' => $objet_retour->postalCodes[0]->lng,
156
			$infos_pays = $res[min(array_keys($res))];
195
				'nom' => $objet_retour->postalCodes[0]->placeName,
157
			$infos_localite = $res[max(array_keys($res))];
196
				'code_insee' => $objet_retour->postalCodes[0]->postalCode
-
 
197
			);
-
 
198
		}
-
 
199
		return $retour;
-
 
200
	}
-
 
201
 
158
			
202
	private function estUnRetourGeocodingGeonamesValide($retour) {
-
 
203
		$valide = false;
159
			$localite = $infos_localite['intitule'];
204
		if (is_a($retour, 'stdClass') && property_exists($retour, 'postalCodes')
-
 
205
				&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
160
			if(!empty($infos_pays['codeIso31661'])) {
206
			$objet_resultats = $retour->postalCodes[0];
161
				$code = $infos_pays['codeIso31661'];
207
			if (property_exists($objet_resultats, 'lat') && property_exists($objet_resultats, 'lng')) {
162
			} elseif(!empty($infos_pays['codeIso31662'])) {
208
				$valide = true;
163
				$code = substr($infos_pays['codeIso31662'], 0, 2);
209
			}
164
			}
210
		}
165
		}
-
 
166
 
211
		return $valide;
167
		return array('nom' => $localite, 'code_insee' => $code);
212
	}
168
	}
213
}
169
}
214
170