Subversion Repositories eFlore/Applications.cel

Rev

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

Rev 2143 Rev 2458
Line 1... Line 1...
1
<?php
1
<?php
-
 
2
// declare(encoding='UTF-8');
2
/**
3
/**
3
 * Service recherche de commune par coordonnées et vice versa
4
 * Service recherche de commune par coordonnées et vice versa.
4
 * Encodage en entrée : utf8
-
 
5
 * Encodage en sortie : utf8
-
 
6
 *
5
 *
-
 
6
 * @internal   Mininum PHP version : 5.2
-
 
7
 * @category   CEL
-
 
8
 * @package    Services
-
 
9
 * @subpackage Images
-
 
10
 * @version    0.1
-
 
11
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
-
 
12
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
7
 * @author Aurélien PERONNET <aurelien@tela-botanica.org>
13
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
8
 * @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
14
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
9
 * @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>
10
 * @version $Id$
16
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
11
 */
17
 */
12
class CoordSearch extends Cel {
18
class CoordSearch extends Cel {
Line 13... Line 19...
13
 
19
 
14
	private $adresse_service_geonames = null;
20
	private $adresse_service_geonames = null;
Line 15... Line 21...
15
	private $adresse_service_local = null;
21
	private $adresse_service_local = null;
16
 
22
 
Line 17... Line 23...
17
	private $nom_service_geocoding = null;
23
	private $nom_service_geocoding = null;
18
	private $nom_service_reverse_geocoding = null;
-
 
19
 
24
	private $nom_service_reverse_geocoding = null;
Line 20... Line 25...
20
	function CoordSearch($config) {
25
 
21
 
26
	public function __construct($config) {
Line 35... Line 40...
35
	 * $uid[1] = longitude (ou * si recherche coordonnées d'une commune)
40
	 * $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)
41
	 * $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)
42
	 * $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)
43
	 * $uid[4] = code_pays (ou * si recherche d'une commune correspondant à des coordonnées, par défaut vaut FR)
39
	 */
44
	 */
40
	function getElement($uid){
45
	public function getElement($uid){
41
 
-
 
42
		$header = '';
46
		$header = '';
43
		$retour = array();
47
		$retour = array();
Line 44... Line 48...
44
 
48
 
Line 45... Line 49...
45
		$params = $this->traiterParametres($uid);
49
		$params = $this->traiterParametres($uid);
46
 
-
 
47
		if ($this->estUneRequeteReverseGeocoding($params)) {
50
 
Line 48... Line 51...
48
 
51
		if ($this->estUneRequeteReverseGeocoding($params)) {
49
			$informations_communes = $this->effectuerRequeteReverseGeocodingCartoOsm($params['lat'], $params['lon']);
52
			$informations_communes = $this->effectuerRequeteReverseGeocodingCartoOsm($params['lat'], $params['lon']);
50
 
53
 
Line 51... Line 54...
51
			if (!$informations_communes) {
54
			if (!$informations_communes) {
52
				$informations_communes = $this->effectuerRequeteReverseGeocodingGeonames($params['lat'], $params['lon']);
55
				$informations_communes = $this->effectuerRequeteReverseGeocodingGeonames($params['lat'], $params['lon']);
53
			}
-
 
54
 
56
			}
55
			$header = 'Content-Type: application/json; charset=UTF-8';
-
 
56
			$retour = json_encode($informations_communes) ;
57
 
57
 
58
			$header = 'Content-Type: application/json; charset=UTF-8';
58
	    } elseif ($this->estUneRequeteGeocoding($params)) {
59
			$retour = json_encode($informations_communes);
59
 
60
		} elseif ($this->estUneRequeteGeocoding($params)) {
Line 60... Line 61...
60
			$informations_coord = $this->chercherCentroideCommuneBdd($params['commune'],$params['code_postal']);
61
			$informations_coord = $this->chercherCentroideCommuneBdd($params['commune'],$params['code_postal']);
61
			if(!$informations_coord) {
62
			if (!$informations_coord) {
62
				$informations_coord = $this->effectuerRequeteGeocodingGeonames($params['commune'],$params['code_postal'],$params['code_pays']);
-
 
63
			}
63
				$informations_coord = $this->effectuerRequeteGeocodingGeonames($params['commune'],$params['code_postal'],$params['code_pays']);
64
 
-
 
65
			$header = 'Content-Type: application/json; charset=UTF-8';
64
			}
66
			$retour = json_encode($informations_coord);
65
 
67
 
66
			$header = 'Content-Type: application/json; charset=UTF-8';
68
	    } else {
-
 
69
 
67
			$retour = json_encode($informations_coord);
70
	      $header = 'HTTP/1.0 400 Bad Request';
68
		} else {
71
	      $retour = 'Commune ou Coordonnées non spécifiées' ;
69
			$header = 'HTTP/1.0 400 Bad Request';
Line 72... Line 70...
72
	    }
70
			$retour = 'Commune ou Coordonnées non spécifiées';
73
 
-
 
74
		header($header);
71
		}
75
		echo $retour;
72
		header($header);
76
	}
-
 
77
 
73
		echo $retour;
78
	protected function traiterParametres($params) {
74
	}
79
 
-
 
80
		$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
75
 
81
		$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*');
76
	protected function traiterParametres($params) {
-
 
77
		$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
-
 
78
		$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*');
82
 
79
		$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*');
83
		$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*');
80
		$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
-
 
81
		$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
84
		$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
82
		return array(
Line 85... Line 83...
85
 
83
			'lat' => $lat,
86
		$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
84
			'lon' => $lng,
87
 
85
			'commune' => $commune,
Line 88... Line 86...
88
		return array('lat' => $lat, 'lon' => $lng, 'commune' => $commune,
86
			'code_postal' => $code_postal,
89
					'code_postal' => $code_postal, 'code_pays' => $code_pays);
-
 
90
	}
87
			'code_pays' => $code_pays);
91
 
88
	}
Line 92... Line 89...
92
	private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
89
 
93
		return isset($params[$indice]) ? str_replace('"','',urldecode($params[$indice])) : $valeur_si_non_present;
90
	private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
94
	}
91
		return isset($params[$indice]) ? str_replace('"','',urldecode($params[$indice])) : $valeur_si_non_present;
Line 95... Line 92...
95
 
92
	}
96
	private function estUneRequeteReverseGeocoding($params) {
-
 
97
 
93
 
98
		return ($params['lat'] != '*' && $params['lon'] != '*');
94
	private function estUneRequeteReverseGeocoding($params) {
99
	}
-
 
100
 
95
		return ($params['lat'] != '*' && $params['lon'] != '*');
101
	private function estUneRequeteGeocoding($params) {
-
 
102
		return ($params['commune'] != '*');
96
	}
103
	}
97
 
104
 
98
	private function estUneRequeteGeocoding($params) {
105
	private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
-
 
106
 
99
		return ($params['commune'] != '*');
107
		$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=".$lat."&lon=".$lon);
100
	}
Line 108... Line 101...
108
		$infos_commune = json_decode($infos_commune_json);
101
 
109
 
102
	private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
110
		$retour = false;
103
		$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=$lat&lon=$lon");
Line 111... Line 104...
111
 
104
		$infos_commune = json_decode($infos_commune_json);
112
		if ($this->estUnRetourOsmValide($infos_commune)) {
-
 
113
			$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
105
		$retour = false;
114
		}
106
		if ($this->estUnRetourOsmValide($infos_commune)) {
115
 
107
			$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
116
		return $retour;
108
		}
-
 
109
		return $retour;
117
	}
110
	}
Line 118... Line 111...
118
 
111
 
119
	private function estUnretourOsmValide($retour) {
112
	private function estUnretourOsmValide($retour) {
-
 
113
		return (is_a($retour, 'stdClass') && property_exists($retour, 'nom') && property_exists($retour, 'codeINSEE'));
-
 
114
	}
120
		return (is_a($retour,'stdClass') && property_exists($retour,'nom') && property_exists($retour,'codeINSEE'));
115
 
121
	}
116
	private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) {
122
 
-
 
123
	private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) {
117
		$url = $this->adresse_service_geonames.
124
 
118
			$this->nom_service_reverse_geocoding.
Line 125... Line 119...
125
		$infos_commune_json = @file_get_contents($this->adresse_service_geonames.
119
			'?lat='.urlencode($lat).'&lng='.urlencode($lon).
126
			$this->nom_service_reverse_geocoding.
-
 
127
			"?lat=".urlencode($lat)."&lng=".urlencode($lon).
120
			'&style=full';
128
			"&style=full");
-
 
129
		$objet_retour = json_decode($infos_commune_json);
121
		$infos_commune_json = @file_get_contents($url);
130
 
122
		$objet_retour = json_decode($infos_commune_json);
131
		$retour = false;
123
 
132
		if($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) {
124
		$retour = false;
133
			$retour = array('nom' => $objet_retour->geonames[0]->adminName4, 'code_insee' => $objet_retour->geonames[0]->adminCode4);
125
		if ($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) {
134
		}
126
			$retour = array(
135
 
127
				'nom' => $objet_retour->geonames[0]->adminName4,
136
		return $retour;
-
 
137
	}
128
				'code_insee' => $objet_retour->geonames[0]->adminCode4);
138
 
129
		}
Line 139... Line 130...
139
	private function estUnRetourReverseGeocodingGeonamesValide($retour) {
130
		return $retour;
140
 
-
 
141
		$valide = false;
131
	}
142
 
-
 
143
		if (is_a($retour,'stdClass') && property_exists($retour,'geonames')
-
 
144
			&& is_array($retour->geonames) && count($retour->geonames) > 0) {
132
 
145
			$objet_resultats = $retour->geonames[0];
133
	private function estUnRetourReverseGeocodingGeonamesValide($retour) {
146
			if (property_exists($objet_resultats,'adminName4') && property_exists($objet_resultats,'adminCode2')) {
134
		$valide = false;
147
				$valide = true;
135
		if (is_a($retour, 'stdClass') && property_exists($retour, 'geonames')
148
			}
136
				&& is_array($retour->geonames) && count($retour->geonames) > 0) {
149
		}
-
 
-
 
137
			$objet_resultats = $retour->geonames[0];
-
 
138
			if (property_exists($objet_resultats, 'adminName4') && property_exists($objet_resultats, 'adminCode2')) {
150
 
139
				$valide = true;
Line 151... Line 140...
151
		return $valide;
140
			}
-
 
141
		}
-
 
142
		return $valide;
Line 152... Line 143...
152
	}
143
	}
153
 
-
 
154
	private function chercherCentroideCommuneBdd($commune, $departement) {
-
 
155
 
-
 
156
		$commune_formatee = str_replace(' ','_',$commune);
144
 
157
		$commune_formatee = str_replace('-','_',$commune_formatee);
145
	private function chercherCentroideCommuneBdd($commune, $departement) {
158
 
146
		$commune_formatee = str_replace(array(' ', '-'), '_', $commune);
159
		if(strlen($departement) > 2) {
147
		if (strlen($departement) > 2) {
160
			$departement = substr($departement,0,2);
148
			$departement = substr($departement, 0, 2);
161
		}
-
 
162
		$requete_selection_commune = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
149
		}
163
					      'WHERE nom LIKE '.Cel::db()->proteger($commune_formatee).' AND code LIKE '.Cel::db()->proteger($departement.'%');
-
 
164
 
150
		$requete = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
165
		$commune_coordonnees = Cel::db()->requeter($requete_selection_commune);
151
			'WHERE nom LIKE '.Cel::db()->proteger($commune_formatee).' '.
Line 166... Line 152...
166
 
152
			'AND code LIKE '.Cel::db()->proteger($departement.'%').' '.
167
		$retour = false;
-
 
168
 
-
 
169
		if($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
-
 
170
 
153
			' -- '.__FILE__.':'.__LINE__;
171
			  $lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
154
		$commune_coordonnees = Cel::db()->requeter($requete);
172
 
155
 
-
 
156
		$retour = false;
-
 
157
		if ($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
173
			  $retour = array('lat' => (float)$lat_lon['lat'],
158
			$lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
174
			  'lng' => (float)$lat_lon['lng'],
159
 
175
			  'nom' => $commune,
-
 
176
			  'code_insee' => $commune_coordonnees[0]['code']
160
			$retour = array(
177
			  );
161
				'lat' => (float) $lat_lon['lat'],
Line 178... Line 162...
178
 
162
				'lng' => (float) $lat_lon['lng'],
179
		}
-
 
180
 
163
				'nom' => $commune,
181
		return $retour;
164
				'code_insee' => $commune_coordonnees[0]['code']
182
	}
165
			);
183
 
-
 
184
	private function convertirUtmVersLatLong($x, $y, $sector) {
-
 
185
 
166
		}
186
		$lat_long = array();
-
 
187
 
167
		return $retour;
188
		$convertisseur = new gPoint();
-
 
189
		$convertisseur->setUTM($x, $y, $sector);
168
	}
190
		$convertisseur->convertTMtoLL();
169
 
Line 191... Line 170...
191
		$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
170
	private function convertirUtmVersLatLong($x, $y, $sector) {
192
		$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
-
 
193
 
171
		$convertisseur = new gPoint();
-
 
172
		$convertisseur->setUTM($x, $y, $sector);
194
		return $lat_long;
173
		$convertisseur->convertTMtoLL();
195
	}
174
 
196
 
175
		$lat_long = array();
197
	private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
176
		$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
198
 
177
		$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
199
		$requete = $this->adresse_service_geonames.
178
		return $lat_long;
200
				$this->nom_service_geocoding.
-
 
201
				"?placename_startsWith=".urlencode($commune);
179
	}
202
 
180
 
Line 203... Line 181...
203
		if($code_postal != '*') {
181
	private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
204
			$requete .= "&postalcode_startsWith=".urlencode($code_postal);
182
		$url = $this->adresse_service_geonames.
205
		}
-
 
206
		$requete .= "&country=".urlencode($code_pays)."&maxRows=10" ;
183
			$this->nom_service_geocoding.
207
 
184
			'?placename_startsWith='.urlencode($commune).
208
		$coord_json = @file_get_contents($requete);
185
			(($code_postal != '*') ? '&postalcode_startsWith='.urlencode($code_postal) : '').
209
		$objet_retour = json_decode($coord_json);
186
			'&country='.urlencode($code_pays).'&maxRows=10';
210
 
187
		$coord_json = @file_get_contents($url);
211
		$retour = false;
188
		$objet_retour = json_decode($coord_json);
212
 
189
 
213
		if($this->estUnRetourGeocodingGeonamesValide($objet_retour)) {
-
 
214
			$retour = array('lat' => $objet_retour->postalCodes[0]->lat,
190
		$retour = false;
215
							'lng' => $objet_retour->postalCodes[0]->lng,
191
		if ($this->estUnRetourGeocodingGeonamesValide($objet_retour)) {
216
							'nom' => $objet_retour->postalCodes[0]->placeName,
-
 
217
							'code_insee' => $objet_retour->postalCodes[0]->postalCode
-
 
218
							);
192
			$retour = array(
-
 
193
				'lat' => $objet_retour->postalCodes[0]->lat,
219
		}
194
				'lng' => $objet_retour->postalCodes[0]->lng,