1 |
<?php
|
1 |
<?php
|
2 |
// declare(encoding='UTF-8');
|
2 |
// declare(encoding='UTF-8');
|
3 |
/**
|
3 |
/**
|
4 |
* Service recherche de commune par coordonnées et vice versa.
|
4 |
* Service recherche de commune par coordonnées et vice versa.
|
5 |
*
|
5 |
*
|
6 |
* @internal Mininum PHP version : 5.2
|
6 |
* @internal Mininum PHP version : 5.2
|
7 |
* @category CEL
|
7 |
* @category CEL
|
8 |
* @package Services
|
8 |
* @package Services
|
9 |
* @subpackage Cartes
|
9 |
* @subpackage Cartes
|
10 |
* @version 0.1
|
10 |
* @version 0.1
|
11 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
11 |
* @author Mathias CHOUET <mathias@tela-botanica.org>
|
12 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
12 |
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
|
13 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
13 |
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
|
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_geonames = null;
|
20 |
private $adresse_service_mondial = null;
|
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 |
|
26 |
public function __construct($config) {
|
23 |
public function __construct($config) {
|
27 |
parent::__construct($config);
|
24 |
parent::__construct($config);
|
28 |
|
25 |
|
29 |
$this->adresse_service_geonames = $this->config['cel']['url_service_geo_geonames'];
|
26 |
$this->adresse_service_mondial = $this->config['cel']['url_service_geo_mondial'];
|
30 |
$this->adresse_service_local = $this->config['cel']['url_service_geo_local'];
|
- |
|
31 |
|
- |
|
32 |
$this->nom_service_geocoding = $this->config['cel']['nom_service_geocoding_geonames'];
|
- |
|
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 |
}
|
35 |
|
29 |
|
36 |
/**
|
30 |
/**
|
37 |
* Recherche de coordonnées suivant ce qui est fourni
|
31 |
* Recherche de coordonnées suivant ce qui est fourni
|
38 |
*
|
32 |
*
|
39 |
* $uid[0] = latitude (ou * si recherche coordonnées d'une commune)
|
33 |
* $uid[0] = latitude (ou * si recherche coordonnées d'une commune)
|
40 |
* $uid[1] = longitude (ou * si recherche coordonnées d'une commune)
|
34 |
* $uid[1] = longitude (ou * si recherche coordonnées d'une commune)
|
41 |
* $uid[2] = commune (ou * si recherche d'une commune correspondant à des coordonnées)
|
35 |
* $uid[2] = commune (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)
|
36 |
* $uid[3] = code_postal (ou * si recherche d'une commune correspondant à des coordonnées)
|
43 |
* $uid[4] = code_pays (ou * si recherche d'une commune correspondant à des coordonnées, par défaut vaut FR)
|
37 |
* $uid[4] = code_pays (ou * si recherche d'une commune correspondant à des coordonnées, par défaut vaut FR)
|
44 |
*/
|
38 |
*/
|
45 |
public function getElement($uid){
|
39 |
public function getElement($uid){
|
46 |
$header = '';
|
40 |
$header = '';
|
47 |
$retour = array();
|
41 |
$retour = array();
|
48 |
|
42 |
|
49 |
$params = $this->traiterParametres($uid);
|
43 |
$params = $this->traiterParametres($uid);
|
50 |
|
44 |
|
51 |
if ($this->estUneRequeteReverseGeocoding($params)) {
|
45 |
if ($this->estUneRequeteReverseGeocoding($params)) {
|
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) {
|
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']);
|
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 |
}
|
65 |
|
59 |
|
66 |
$header = 'Content-Type: application/json; charset=UTF-8';
|
60 |
$header = 'Content-Type: application/json; charset=UTF-8';
|
67 |
$retour = json_encode($informations_coord);
|
61 |
$retour = json_encode($informations_coord);
|
68 |
} else {
|
62 |
} else {
|
69 |
$header = 'HTTP/1.0 400 Bad Request';
|
63 |
$header = 'HTTP/1.0 400 Bad Request';
|
70 |
$retour = 'Commune ou Coordonnées non spécifiées';
|
64 |
$retour = 'Commune ou Coordonnées non spécifiées';
|
71 |
}
|
65 |
}
|
72 |
header($header);
|
66 |
header($header);
|
73 |
echo $retour;
|
67 |
echo $retour;
|
74 |
}
|
68 |
}
|
75 |
|
69 |
|
76 |
protected function traiterParametres($params) {
|
70 |
protected function traiterParametres($params) {
|
77 |
$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
|
71 |
$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
|
78 |
$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*');
|
72 |
$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*');
|
79 |
$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*');
|
73 |
$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*');
|
80 |
$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
|
74 |
$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
|
81 |
$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
|
75 |
$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
|
82 |
return array(
|
76 |
return array(
|
83 |
'lat' => $lat,
|
77 |
'lat' => $lat,
|
84 |
'lon' => $lng,
|
78 |
'lon' => $lng,
|
85 |
'commune' => $commune,
|
79 |
'commune' => $commune,
|
86 |
'code_postal' => $code_postal,
|
80 |
'code_postal' => $code_postal,
|
87 |
'code_pays' => $code_pays);
|
81 |
'code_pays' => $code_pays);
|
88 |
}
|
82 |
}
|
89 |
|
83 |
|
90 |
private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
|
84 |
private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
|
91 |
return isset($params[$indice]) ? str_replace('"','',urldecode($params[$indice])) : $valeur_si_non_present;
|
85 |
return isset($params[$indice]) ? str_replace('"','',urldecode($params[$indice])) : $valeur_si_non_present;
|
92 |
}
|
86 |
}
|
93 |
|
87 |
|
94 |
private function estUneRequeteReverseGeocoding($params) {
|
88 |
private function estUneRequeteReverseGeocoding($params) {
|
95 |
return ($params['lat'] != '*' && $params['lon'] != '*');
|
89 |
return ($params['lat'] != '*' && $params['lon'] != '*');
|
96 |
}
|
90 |
}
|
97 |
|
91 |
|
98 |
private function estUneRequeteGeocoding($params) {
|
92 |
private function estUneRequeteGeocoding($params) {
|
99 |
return ($params['commune'] != '*');
|
93 |
return ($params['commune'] != '*');
|
100 |
}
|
94 |
}
|
101 |
|
95 |
|
102 |
private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
|
96 |
private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
|
103 |
$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=$lat&lon=$lon");
|
97 |
$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=$lat&lon=$lon");
|
104 |
$infos_commune = json_decode($infos_commune_json);
|
98 |
$infos_commune = json_decode($infos_commune_json);
|
105 |
$retour = false;
|
99 |
$retour = false;
|
106 |
if ($this->estUnRetourOsmValide($infos_commune)) {
|
100 |
if ($this->estUnRetourOsmValide($infos_commune)) {
|
107 |
$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
|
101 |
$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
|
108 |
}
|
102 |
}
|
109 |
return $retour;
|
103 |
return $retour;
|
110 |
}
|
104 |
}
|
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'));
|
114 |
}
|
108 |
}
|
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 |
}
|
- |
|
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) {
|
148 |
$departement = substr($departement, 0, 2);
|
113 |
$departement = substr($departement, 0, 2);
|
149 |
}
|
114 |
}
|
150 |
$requete = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
|
115 |
$requete = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
|
151 |
'WHERE nom LIKE '.Cel::db()->proteger($commune_formatee).' '.
|
116 |
'WHERE nom LIKE '.Cel::db()->proteger($commune_formatee).' '.
|
152 |
'AND code LIKE '.Cel::db()->proteger($departement.'%').' '.
|
117 |
'AND code LIKE '.Cel::db()->proteger($departement.'%').' '.
|
153 |
' -- '.__FILE__.':'.__LINE__;
|
118 |
' -- '.__FILE__.':'.__LINE__;
|
154 |
$commune_coordonnees = Cel::db()->requeter($requete);
|
119 |
$commune_coordonnees = Cel::db()->requeter($requete);
|
155 |
|
120 |
|
156 |
$retour = false;
|
121 |
$retour = false;
|
157 |
if ($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
|
122 |
if ($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
|
158 |
$lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
|
123 |
$lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
|
159 |
|
124 |
|
160 |
$retour = array(
|
125 |
$retour = array(
|
161 |
'lat' => (float) $lat_lon['lat'],
|
126 |
'lat' => (float) $lat_lon['lat'],
|
162 |
'lng' => (float) $lat_lon['lng'],
|
127 |
'lng' => (float) $lat_lon['lng'],
|
163 |
'nom' => $commune,
|
128 |
'nom' => $commune,
|
164 |
'code_insee' => $commune_coordonnees[0]['code']
|
129 |
'code_insee' => $commune_coordonnees[0]['code']
|
165 |
);
|
130 |
);
|
166 |
}
|
131 |
}
|
167 |
return $retour;
|
132 |
return $retour;
|
168 |
}
|
133 |
}
|
169 |
|
134 |
|
170 |
private function convertirUtmVersLatLong($x, $y, $sector) {
|
135 |
private function convertirUtmVersLatLong($x, $y, $sector) {
|
171 |
$convertisseur = new gPoint();
|
136 |
$convertisseur = new gPoint();
|
172 |
$convertisseur->setUTM($x, $y, $sector);
|
137 |
$convertisseur->setUTM($x, $y, $sector);
|
173 |
$convertisseur->convertTMtoLL();
|
138 |
$convertisseur->convertTMtoLL();
|
174 |
|
139 |
|
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 |
}
|