Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 2551 Rev 2557
Line 14... Line 14...
14
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
14
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
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 {
19
 
19
	
20
	private $adresse_service_mondial = null;
-
 
21
	private $adresse_service_local = null;
-
 
22
 
-
 
23
	public function __construct($config) {
-
 
24
		parent::__construct($config);
-
 
25
 
-
 
26
		$this->adresse_service_mondial = $this->config['cel']['url_service_geo_mondial'];
-
 
27
		$this->adresse_service_local = $this->config['cel']['url_service_geo_local'];
-
 
28
	}
-
 
29
 
-
 
30
	/**
20
	/**
31
	 * Recherche de coordonnées suivant ce qui est fourni
21
	 * Recherche de coordonnées suivant ce qui est fourni
32
	 *
22
	 *
33
	 * $uid[0] = latitude (ou * si recherche coordonnées d'une commune)
23
	 * $uid[0] = latitude (ou * si recherche coordonnées d'une commune)
34
	 * $uid[1] = longitude (ou * si recherche coordonnées d'une commune)
24
	 * $uid[1] = longitude (ou * si recherche coordonnées d'une commune)
Line 39... Line 29...
39
	public function getElement($uid){
29
	public function getElement($uid){
40
		$header = '';
30
		$header = '';
41
		$retour = array();
31
		$retour = array();
Line 42... Line 32...
42
 
32
 
-
 
33
		$params = $this->traiterParametres($uid);
Line 43... Line 34...
43
		$params = $this->traiterParametres($uid);
34
		$recherche_zones_geo = new RechercheInfosZoneGeo($this->config);
44
 
-
 
45
		if ($this->estUneRequeteReverseGeocoding($params)) {
-
 
46
			$informations_communes = $this->effectuerRequeteReverseGeocodingCartoOsm($params['lat'], $params['lon']);
-
 
47
 
-
 
48
			if (!$informations_communes) {
-
 
Line -... Line 35...
-
 
35
 
-
 
36
		if ($this->estUneRequeteReverseGeocoding($params)) {
49
				$informations_communes = $this->effectuerRequeteReverseGeocodingMondiale($params['lat'], $params['lon']);
37
 
50
			}
38
			$coordonnees = array('latitude' => $params['lat'], 'longitude' => $params['lon']);
-
 
39
			$informations_communes = $recherche_zones_geo->obtenirInfosPourCoordonnees($coordonnees);
51
 
40
			$header = 'Content-Type: application/json; charset=UTF-8';
52
			$header = 'Content-Type: application/json; charset=UTF-8';
-
 
53
			$retour = json_encode($informations_communes);
-
 
54
		} elseif ($this->estUneRequeteGeocoding($params)) {
-
 
55
			$informations_coord = $this->chercherCentroideCommuneBdd($params['commune'],$params['code_postal']);
-
 
56
			if (!$informations_coord) {
41
			$retour = json_encode($informations_communes);	
-
 
42
				
57
				$informations_coord = $this->effectuerRequeteGeocodingMondiale($params['commune'],$params['code_postal'],$params['code_pays']);
43
		} elseif ($this->estUneRequeteGeocoding($params)) {	
58
			}
44
				
-
 
45
			$informations_coord = $recherche_zones_geo->obtenirInfosPourNoms($params['commune'], $params['pays'], $params['code']);
59
 
46
			$header = 'Content-Type: application/json; charset=UTF-8';
60
			$header = 'Content-Type: application/json; charset=UTF-8';
47
			$retour = json_encode($informations_coord);
61
			$retour = json_encode($informations_coord);
48
			
62
		} else {
49
		} else {
-
 
50
			$header = 'HTTP/1.0 400 Bad Request';
63
			$header = 'HTTP/1.0 400 Bad Request';
51
			$retour = 'Commune ou Coordonnées non spécifiées';
64
			$retour = 'Commune ou Coordonnées non spécifiées';
52
		}
65
		}
53
		
Line 66... Line 54...
66
		header($header);
54
		header($header);
67
		echo $retour;
55
		echo $retour;
68
	}
56
	}
69
 
57
 
70
	protected function traiterParametres($params) {
58
	protected function traiterParametres($params) {
71
		$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
59
		$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
72
		$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*');
60
		$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*');
73
		$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*');
61
		$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*');
74
		$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
62
		$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
75
		$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
63
		$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, '');
76
		return array(
64
		return array(
77
			'lat' => $lat,
65
			'lat' => $lat,
78
			'lon' => $lng,
66
			'lon' => $lng,
Line 79... Line 67...
79
			'commune' => $commune,
67
			'commune' => $commune,
80
			'code_postal' => $code_postal,
68
			'code' => $code_postal,
81
			'code_pays' => $code_pays);
69
			'pays' => $code_pays);
Line 90... Line 78...
90
	}
78
	}
Line 91... Line 79...
91
 
79
 
92
	private function estUneRequeteGeocoding($params) {
80
	private function estUneRequeteGeocoding($params) {
93
		return ($params['commune'] != '*');
81
		return ($params['commune'] != '*');
94
	}
-
 
95
 
-
 
96
	private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
-
 
97
		$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=$lat&lon=$lon");
-
 
98
		$infos_commune = json_decode($infos_commune_json);
-
 
99
		$retour = false;
-
 
100
		if ($this->estUnRetourOsmValide($infos_commune)) {
-
 
101
			$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
-
 
102
		}
-
 
103
		return $retour;
-
 
104
	}
-
 
105
 
-
 
106
	private function estUnretourOsmValide($retour) {
-
 
107
		return (is_a($retour, 'stdClass') && property_exists($retour, 'nom') && property_exists($retour, 'codeINSEE'));
-
 
108
	}
-
 
109
 
-
 
110
	private function chercherCentroideCommuneBdd($commune, $departement) {
-
 
111
		$commune_formatee = str_replace(array(' ', '-'), '_', $commune);
-
 
112
		if (strlen($departement) > 2) {
-
 
113
			$departement = substr($departement, 0, 2);
-
 
114
		}
-
 
115
		$requete = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
-
 
116
			'WHERE nom LIKE '.Cel::db()->proteger($commune_formatee).' '.
-
 
117
			'AND code LIKE '.Cel::db()->proteger($departement.'%').' '.
-
 
118
			' -- '.__FILE__.':'.__LINE__;
-
 
119
		$commune_coordonnees = Cel::db()->requeter($requete);
-
 
120
 
-
 
121
		$retour = false;
-
 
122
		if ($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
-
 
123
			$lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
-
 
124
 
-
 
125
			$retour = array(
-
 
126
				'lat' => (float) $lat_lon['lat'],
-
 
127
				'lng' => (float) $lat_lon['lng'],
-
 
128
				'nom' => $commune,
-
 
129
				'code_insee' => $commune_coordonnees[0]['code']
-
 
130
			);
-
 
131
		}
-
 
132
		return $retour;
-
 
133
	}
-
 
134
 
-
 
135
	private function convertirUtmVersLatLong($x, $y, $sector) {
-
 
136
		$convertisseur = new gPoint();
-
 
137
		$convertisseur->setUTM($x, $y, $sector);
-
 
138
		$convertisseur->convertTMtoLL();
-
 
139
 
-
 
140
		$lat_long = array();
-
 
141
		$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
-
 
142
		$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
-
 
143
		return $lat_long;
-
 
144
	}
-
 
145
	
-
 
146
	private function effectuerRequeteReverseGeocodingMondiale($lat, $lon) {
-
 
147
		$url = $this->adresse_service_mondial.'?lat='.$lat.'&lon='.$lon;
-
 
148
		// Pour limiter par niveau ajouter ce qui suit à la requête : 
-
 
149
		//.'&niveau=2,3,4';
-
 
150
		$res = json_decode(file_get_contents($url), true);
-
 
151
		
-
 
152
		$code = "";
-
 
153
		$localite = "";
-
 
154
		
-
 
155
		if(!empty($res)) {
-
 
156
			$infos_pays = $res[min(array_keys($res))];
-
 
157
			$infos_localite = $res[max(array_keys($res))];
-
 
158
			
-
 
159
			$localite = $infos_localite['intitule'];
-
 
160
			$code = $this->extraireCodePays($infos_pays);
-
 
161
		}
-
 
162
 
-
 
163
		return array('nom' => $localite, 'code_insee' => $code);
-
 
164
	}
-
 
165
	
-
 
166
	private function effectuerRequeteGeocodingMondiale($nom) {
-
 
167
		$url = $this->adresse_service_mondial.'?masque='.urlencode($nom);
-
 
168
		$res = json_decode(file_get_contents($url), true);
-
 
169
		
-
 
170
		$lat = "";
-
 
171
		$lon = "";
-
 
172
		$nom = "";
-
 
173
		$code ="";
-
 
174
		
-
 
175
		if(!empty($res)) {
-
 
176
			$infos_pays = $res[min(array_keys($res))];
-
 
177
			$infos_localite = $res[max(array_keys($res))];
-
 
178
			
-
 
179
			$code = $this->extraireCodePays($infos_pays);
-
 
180
				
-
 
181
			$nom = $infos_localite['intitule'];
-
 
182
			$lat = $infos_localite['centre_lat'];
-
 
183
			$lon = $infos_localite['centre_lng'];
-
 
184
		}
-
 
185
		
-
 
186
		$retour = array(
-
 
187
				'lat' => $lat,
-
 
188
				'lng' => $lon,
-
 
189
				'nom' => $nom,
-
 
190
				'code_insee' => $code
-
 
191
		);
-
 
192
		
-
 
193
		return $retour;
-
 
194
	}
-
 
195
	
-
 
196
	private function extraireCodePays($infos_pays) {
-
 
197
		$code = "";
-
 
198
		if(!empty($infos_pays['codeIso31661'])) {
-
 
199
			$code = $infos_pays['codeIso31661'];
-
 
200
		} elseif(!empty($infos_pays['codeIso31662'])) {
-
 
201
			$code = substr($infos_pays['codeIso31662'], 0, 2);
-
 
202
		}
-
 
203
		return $code;
-
 
204
	}
-
 
205
	
82
	}
206
}
83
}