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 {
|
13 |
|
19 |
|
14 |
private $adresse_service_geonames = null;
|
20 |
private $adresse_service_geonames = null;
|
15 |
private $adresse_service_local = null;
|
21 |
private $adresse_service_local = null;
|
16 |
|
22 |
|
17 |
private $nom_service_geocoding = null;
|
23 |
private $nom_service_geocoding = null;
|
18 |
private $nom_service_reverse_geocoding = null;
|
24 |
private $nom_service_reverse_geocoding = null;
|
19 |
|
25 |
|
20 |
function CoordSearch($config) {
|
- |
|
21 |
|
26 |
public function __construct($config) {
|
22 |
parent::__construct($config);
|
27 |
parent::__construct($config);
|
23 |
|
28 |
|
24 |
$this->adresse_service_geonames = $this->config['cel']['url_service_geo_geonames'];
|
29 |
$this->adresse_service_geonames = $this->config['cel']['url_service_geo_geonames'];
|
25 |
$this->adresse_service_local = $this->config['cel']['url_service_geo_local'];
|
30 |
$this->adresse_service_local = $this->config['cel']['url_service_geo_local'];
|
26 |
|
31 |
|
27 |
$this->nom_service_geocoding = $this->config['cel']['nom_service_geocoding_geonames'];
|
32 |
$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'];
|
33 |
$this->nom_service_reverse_geocoding = $this->config['cel']['nom_service_reverse_geocoding_geonames'];
|
29 |
}
|
34 |
}
|
30 |
|
35 |
|
31 |
/**
|
36 |
/**
|
32 |
* Recherche de coordonnées suivant ce qui est fourni
|
37 |
* Recherche de coordonnées suivant ce qui est fourni
|
33 |
*
|
38 |
*
|
34 |
* $uid[0] = latitude (ou * si recherche coordonnées d'une commune)
|
39 |
* $uid[0] = latitude (ou * si recherche coordonnées d'une commune)
|
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();
|
44 |
|
48 |
|
45 |
$params = $this->traiterParametres($uid);
|
49 |
$params = $this->traiterParametres($uid);
|
46 |
|
50 |
|
47 |
if ($this->estUneRequeteReverseGeocoding($params)) {
|
51 |
if ($this->estUneRequeteReverseGeocoding($params)) {
|
48 |
|
- |
|
49 |
$informations_communes = $this->effectuerRequeteReverseGeocodingCartoOsm($params['lat'], $params['lon']);
|
52 |
$informations_communes = $this->effectuerRequeteReverseGeocodingCartoOsm($params['lat'], $params['lon']);
|
50 |
|
53 |
|
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 |
}
|
56 |
}
|
54 |
|
57 |
|
55 |
$header = 'Content-Type: application/json; charset=UTF-8';
|
58 |
$header = 'Content-Type: application/json; charset=UTF-8';
|
56 |
$retour = json_encode($informations_communes) ;
|
59 |
$retour = json_encode($informations_communes);
|
57 |
|
- |
|
58 |
} elseif ($this->estUneRequeteGeocoding($params)) {
|
60 |
} elseif ($this->estUneRequeteGeocoding($params)) {
|
59 |
|
- |
|
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 |
$informations_coord = $this->effectuerRequeteGeocodingGeonames($params['commune'],$params['code_postal'],$params['code_pays']);
|
63 |
}
|
64 |
}
|
64 |
|
65 |
|
65 |
$header = 'Content-Type: application/json; charset=UTF-8';
|
66 |
$header = 'Content-Type: application/json; charset=UTF-8';
|
66 |
$retour = json_encode($informations_coord);
|
67 |
$retour = json_encode($informations_coord);
|
67 |
|
- |
|
68 |
} else {
|
68 |
} else {
|
69 |
|
- |
|
70 |
$header = 'HTTP/1.0 400 Bad Request';
|
69 |
$header = 'HTTP/1.0 400 Bad Request';
|
71 |
$retour = 'Commune ou Coordonnées non spécifiées' ;
|
70 |
$retour = 'Commune ou Coordonnées non spécifiées';
|
72 |
}
|
71 |
}
|
73 |
|
- |
|
74 |
header($header);
|
72 |
header($header);
|
75 |
echo $retour;
|
73 |
echo $retour;
|
76 |
}
|
74 |
}
|
77 |
|
75 |
|
78 |
protected function traiterParametres($params) {
|
76 |
protected function traiterParametres($params) {
|
79 |
|
- |
|
80 |
$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
|
77 |
$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
|
81 |
$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*');
|
78 |
$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*');
|
82 |
|
- |
|
83 |
$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*');
|
79 |
$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*');
|
84 |
$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
|
80 |
$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
|
85 |
|
- |
|
86 |
$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
|
81 |
$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
|
87 |
|
82 |
return array(
|
- |
|
83 |
'lat' => $lat,
|
- |
|
84 |
'lon' => $lng,
|
88 |
return array('lat' => $lat, 'lon' => $lng, 'commune' => $commune,
|
85 |
'commune' => $commune,
|
89 |
'code_postal' => $code_postal, 'code_pays' => $code_pays);
|
86 |
'code_postal' => $code_postal,
|
- |
|
87 |
'code_pays' => $code_pays);
|
90 |
}
|
88 |
}
|
91 |
|
89 |
|
92 |
private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
|
90 |
private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
|
93 |
return isset($params[$indice]) ? str_replace('"','',urldecode($params[$indice])) : $valeur_si_non_present;
|
91 |
return isset($params[$indice]) ? str_replace('"','',urldecode($params[$indice])) : $valeur_si_non_present;
|
94 |
}
|
92 |
}
|
95 |
|
93 |
|
96 |
private function estUneRequeteReverseGeocoding($params) {
|
94 |
private function estUneRequeteReverseGeocoding($params) {
|
97 |
|
- |
|
98 |
return ($params['lat'] != '*' && $params['lon'] != '*');
|
95 |
return ($params['lat'] != '*' && $params['lon'] != '*');
|
99 |
}
|
96 |
}
|
100 |
|
97 |
|
101 |
private function estUneRequeteGeocoding($params) {
|
98 |
private function estUneRequeteGeocoding($params) {
|
102 |
return ($params['commune'] != '*');
|
99 |
return ($params['commune'] != '*');
|
103 |
}
|
100 |
}
|
104 |
|
101 |
|
105 |
private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
|
102 |
private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
|
106 |
|
- |
|
107 |
$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=".$lat."&lon=".$lon);
|
103 |
$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=$lat&lon=$lon");
|
108 |
$infos_commune = json_decode($infos_commune_json);
|
104 |
$infos_commune = json_decode($infos_commune_json);
|
109 |
|
- |
|
110 |
$retour = false;
|
105 |
$retour = false;
|
111 |
|
- |
|
112 |
if ($this->estUnRetourOsmValide($infos_commune)) {
|
106 |
if ($this->estUnRetourOsmValide($infos_commune)) {
|
113 |
$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
|
107 |
$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
|
114 |
}
|
108 |
}
|
115 |
|
- |
|
116 |
return $retour;
|
109 |
return $retour;
|
117 |
}
|
110 |
}
|
118 |
|
111 |
|
119 |
private function estUnretourOsmValide($retour) {
|
112 |
private function estUnretourOsmValide($retour) {
|
120 |
return (is_a($retour,'stdClass') && property_exists($retour,'nom') && property_exists($retour,'codeINSEE'));
|
113 |
return (is_a($retour, 'stdClass') && property_exists($retour, 'nom') && property_exists($retour, 'codeINSEE'));
|
121 |
}
|
114 |
}
|
122 |
|
115 |
|
123 |
private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) {
|
116 |
private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) {
|
124 |
|
- |
|
125 |
$infos_commune_json = @file_get_contents($this->adresse_service_geonames.
|
117 |
$url = $this->adresse_service_geonames.
|
126 |
$this->nom_service_reverse_geocoding.
|
118 |
$this->nom_service_reverse_geocoding.
|
127 |
"?lat=".urlencode($lat)."&lng=".urlencode($lon).
|
119 |
'?lat='.urlencode($lat).'&lng='.urlencode($lon).
|
128 |
"&style=full");
|
120 |
'&style=full';
|
- |
|
121 |
$infos_commune_json = @file_get_contents($url);
|
129 |
$objet_retour = json_decode($infos_commune_json);
|
122 |
$objet_retour = json_decode($infos_commune_json);
|
130 |
|
123 |
|
131 |
$retour = false;
|
124 |
$retour = false;
|
132 |
if($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) {
|
125 |
if ($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) {
|
- |
|
126 |
$retour = array(
|
- |
|
127 |
'nom' => $objet_retour->geonames[0]->adminName4,
|
133 |
$retour = array('nom' => $objet_retour->geonames[0]->adminName4, 'code_insee' => $objet_retour->geonames[0]->adminCode4);
|
128 |
'code_insee' => $objet_retour->geonames[0]->adminCode4);
|
134 |
}
|
129 |
}
|
135 |
|
- |
|
136 |
return $retour;
|
130 |
return $retour;
|
137 |
}
|
131 |
}
|
138 |
|
132 |
|
139 |
private function estUnRetourReverseGeocodingGeonamesValide($retour) {
|
133 |
private function estUnRetourReverseGeocodingGeonamesValide($retour) {
|
140 |
|
- |
|
141 |
$valide = false;
|
134 |
$valide = false;
|
142 |
|
- |
|
143 |
if (is_a($retour,'stdClass') && property_exists($retour,'geonames')
|
135 |
if (is_a($retour, 'stdClass') && property_exists($retour, 'geonames')
|
144 |
&& is_array($retour->geonames) && count($retour->geonames) > 0) {
|
136 |
&& is_array($retour->geonames) && count($retour->geonames) > 0) {
|
145 |
$objet_resultats = $retour->geonames[0];
|
137 |
$objet_resultats = $retour->geonames[0];
|
146 |
if (property_exists($objet_resultats,'adminName4') && property_exists($objet_resultats,'adminCode2')) {
|
138 |
if (property_exists($objet_resultats, 'adminName4') && property_exists($objet_resultats, 'adminCode2')) {
|
147 |
$valide = true;
|
139 |
$valide = true;
|
148 |
}
|
140 |
}
|
149 |
}
|
141 |
}
|
150 |
|
- |
|
151 |
return $valide;
|
142 |
return $valide;
|
152 |
}
|
143 |
}
|
153 |
|
144 |
|
154 |
private function chercherCentroideCommuneBdd($commune, $departement) {
|
145 |
private function chercherCentroideCommuneBdd($commune, $departement) {
|
155 |
|
- |
|
156 |
$commune_formatee = str_replace(' ','_',$commune);
|
146 |
$commune_formatee = str_replace(array(' ', '-'), '_', $commune);
|
157 |
$commune_formatee = str_replace('-','_',$commune_formatee);
|
- |
|
158 |
|
- |
|
159 |
if(strlen($departement) > 2) {
|
147 |
if (strlen($departement) > 2) {
|
160 |
$departement = substr($departement,0,2);
|
148 |
$departement = substr($departement, 0, 2);
|
161 |
}
|
149 |
}
|
162 |
$requete_selection_commune = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
|
150 |
$requete = '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.'%');
|
151 |
'WHERE nom LIKE '.Cel::db()->proteger($commune_formatee).' '.
|
164 |
|
- |
|
- |
|
152 |
'AND code LIKE '.Cel::db()->proteger($departement.'%').' '.
|
- |
|
153 |
' -- '.__FILE__.':'.__LINE__;
|
165 |
$commune_coordonnees = Cel::db()->requeter($requete_selection_commune);
|
154 |
$commune_coordonnees = Cel::db()->requeter($requete);
|
166 |
|
155 |
|
- |
|
156 |
$retour = false;
|
- |
|
157 |
if ($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
|
167 |
$retour = false;
|
158 |
$lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
|
168 |
|
- |
|
169 |
if($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
|
- |
|
170 |
|
- |
|
171 |
$lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
|
159 |
|
172 |
|
160 |
$retour = array(
|
173 |
$retour = array('lat' => (float)$lat_lon['lat'],
|
161 |
'lat' => (float) $lat_lon['lat'],
|
174 |
'lng' => (float)$lat_lon['lng'],
|
162 |
'lng' => (float) $lat_lon['lng'],
|
175 |
'nom' => $commune,
|
163 |
'nom' => $commune,
|
176 |
'code_insee' => $commune_coordonnees[0]['code']
|
- |
|
177 |
);
|
164 |
'code_insee' => $commune_coordonnees[0]['code']
|
178 |
|
- |
|
179 |
}
|
165 |
);
|
180 |
|
166 |
}
|
181 |
return $retour;
|
167 |
return $retour;
|
182 |
}
|
168 |
}
|
183 |
|
169 |
|
184 |
private function convertirUtmVersLatLong($x, $y, $sector) {
|
170 |
private function convertirUtmVersLatLong($x, $y, $sector) {
|
185 |
|
- |
|
186 |
$lat_long = array();
|
- |
|
187 |
|
- |
|
188 |
$convertisseur = new gPoint();
|
171 |
$convertisseur = new gPoint();
|
189 |
$convertisseur->setUTM($x, $y, $sector);
|
172 |
$convertisseur->setUTM($x, $y, $sector);
|
190 |
$convertisseur->convertTMtoLL();
|
173 |
$convertisseur->convertTMtoLL();
|
- |
|
174 |
|
- |
|
175 |
$lat_long = array();
|
191 |
$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
|
176 |
$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
|
192 |
$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
|
177 |
$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
|
193 |
|
- |
|
194 |
return $lat_long;
|
178 |
return $lat_long;
|
195 |
}
|
179 |
}
|
196 |
|
180 |
|
197 |
private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
|
181 |
private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
|
198 |
|
- |
|
199 |
$requete = $this->adresse_service_geonames.
|
182 |
$url = $this->adresse_service_geonames.
|
200 |
$this->nom_service_geocoding.
|
183 |
$this->nom_service_geocoding.
|
201 |
"?placename_startsWith=".urlencode($commune);
|
184 |
'?placename_startsWith='.urlencode($commune).
|
202 |
|
- |
|
203 |
if($code_postal != '*') {
|
- |
|
204 |
$requete .= "&postalcode_startsWith=".urlencode($code_postal);
|
185 |
(($code_postal != '*') ? '&postalcode_startsWith='.urlencode($code_postal) : '').
|
205 |
}
|
- |
|
206 |
$requete .= "&country=".urlencode($code_pays)."&maxRows=10" ;
|
186 |
'&country='.urlencode($code_pays).'&maxRows=10';
|
207 |
|
- |
|
208 |
$coord_json = @file_get_contents($requete);
|
187 |
$coord_json = @file_get_contents($url);
|
209 |
$objet_retour = json_decode($coord_json);
|
188 |
$objet_retour = json_decode($coord_json);
|
210 |
|
189 |
|
211 |
$retour = false;
|
190 |
$retour = false;
|
212 |
|
- |
|
213 |
if($this->estUnRetourGeocodingGeonamesValide($objet_retour)) {
|
191 |
if ($this->estUnRetourGeocodingGeonamesValide($objet_retour)) {
|
- |
|
192 |
$retour = array(
|
214 |
$retour = array('lat' => $objet_retour->postalCodes[0]->lat,
|
193 |
'lat' => $objet_retour->postalCodes[0]->lat,
|
215 |
'lng' => $objet_retour->postalCodes[0]->lng,
|
194 |
'lng' => $objet_retour->postalCodes[0]->lng,
|
216 |
'nom' => $objet_retour->postalCodes[0]->placeName,
|
195 |
'nom' => $objet_retour->postalCodes[0]->placeName,
|
217 |
'code_insee' => $objet_retour->postalCodes[0]->postalCode
|
196 |
'code_insee' => $objet_retour->postalCodes[0]->postalCode
|
218 |
);
|
197 |
);
|
219 |
}
|
198 |
}
|
220 |
|
- |
|
221 |
return $retour;
|
199 |
return $retour;
|
222 |
}
|
200 |
}
|
223 |
|
201 |
|
224 |
private function estUnRetourGeocodingGeonamesValide($retour) {
|
202 |
private function estUnRetourGeocodingGeonamesValide($retour) {
|
225 |
$valide = false;
|
203 |
$valide = false;
|
226 |
|
- |
|
227 |
if (is_a($retour,'stdClass') && property_exists($retour,'postalCodes')
|
204 |
if (is_a($retour, 'stdClass') && property_exists($retour, 'postalCodes')
|
228 |
&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
|
205 |
&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
|
229 |
$objet_resultats = $retour->postalCodes[0];
|
206 |
$objet_resultats = $retour->postalCodes[0];
|
230 |
if (property_exists($objet_resultats,'lat') && property_exists($objet_resultats,'lng')) {
|
207 |
if (property_exists($objet_resultats, 'lat') && property_exists($objet_resultats, 'lng')) {
|
231 |
$valide = true;
|
208 |
$valide = true;
|
232 |
}
|
209 |
}
|
233 |
}
|
210 |
}
|
234 |
|
- |
|
235 |
return $valide;
|
211 |
return $valide;
|
236 |
}
|
212 |
}
|
237 |
}
|
- |
|
238 |
?>
|
- |
|
239 |
|
213 |
}
|
- |
|
214 |
|
240 |
|
215 |
|