17,20 → 17,14 |
*/ |
class CoordSearch extends Cel { |
|
private $adresse_service_geonames = null; |
private $adresse_service_mondial = null; |
private $adresse_service_local = null; |
|
private $nom_service_geocoding = null; |
private $nom_service_reverse_geocoding = null; |
|
public function __construct($config) { |
parent::__construct($config); |
|
$this->adresse_service_geonames = $this->config['cel']['url_service_geo_geonames']; |
$this->adresse_service_mondial = $this->config['cel']['url_service_geo_mondial']; |
$this->adresse_service_local = $this->config['cel']['url_service_geo_local']; |
|
$this->nom_service_geocoding = $this->config['cel']['nom_service_geocoding_geonames']; |
$this->nom_service_reverse_geocoding = $this->config['cel']['nom_service_reverse_geocoding_geonames']; |
} |
|
/** |
52,7 → 46,7 |
$informations_communes = $this->effectuerRequeteReverseGeocodingCartoOsm($params['lat'], $params['lon']); |
|
if (!$informations_communes) { |
$informations_communes = $this->effectuerRequeteReverseGeocodingGeonames($params['lat'], $params['lon']); |
$informations_communes = $this->effectuerRequeteReverseGeocodingMondiale($params['lat'], $params['lon']); |
} |
|
$header = 'Content-Type: application/json; charset=UTF-8'; |
60,7 → 54,7 |
} elseif ($this->estUneRequeteGeocoding($params)) { |
$informations_coord = $this->chercherCentroideCommuneBdd($params['commune'],$params['code_postal']); |
if (!$informations_coord) { |
$informations_coord = $this->effectuerRequeteGeocodingGeonames($params['commune'],$params['code_postal'],$params['code_pays']); |
$informations_coord = $this->effectuerRequeteGeocodingMondiale($params['commune'],$params['code_postal'],$params['code_pays']); |
} |
|
$header = 'Content-Type: application/json; charset=UTF-8'; |
113,35 → 107,6 |
return (is_a($retour, 'stdClass') && property_exists($retour, 'nom') && property_exists($retour, 'codeINSEE')); |
} |
|
private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) { |
$url = $this->adresse_service_geonames. |
$this->nom_service_reverse_geocoding. |
'?lat='.urlencode($lat).'&lng='.urlencode($lon). |
'&style=full'; |
$infos_commune_json = @file_get_contents($url); |
$objet_retour = json_decode($infos_commune_json); |
|
$retour = false; |
if ($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) { |
$retour = array( |
'nom' => $objet_retour->geonames[0]->adminName4, |
'code_insee' => $objet_retour->geonames[0]->adminCode4); |
} |
return $retour; |
} |
|
private function estUnRetourReverseGeocodingGeonamesValide($retour) { |
$valide = false; |
if (is_a($retour, 'stdClass') && property_exists($retour, 'geonames') |
&& is_array($retour->geonames) && count($retour->geonames) > 0) { |
$objet_resultats = $retour->geonames[0]; |
if (property_exists($objet_resultats, 'adminName4') && property_exists($objet_resultats, 'adminCode2')) { |
$valide = true; |
} |
} |
return $valide; |
} |
|
private function chercherCentroideCommuneBdd($commune, $departement) { |
$commune_formatee = str_replace(array(' ', '-'), '_', $commune); |
if (strlen($departement) > 2) { |
177,37 → 142,28 |
$lat_long['lng'] = str_replace(',','.',$convertisseur->Long()); |
return $lat_long; |
} |
|
private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) { |
$url = $this->adresse_service_geonames. |
$this->nom_service_geocoding. |
'?placename_startsWith='.urlencode($commune). |
(($code_postal != '*') ? '&postalcode_startsWith='.urlencode($code_postal) : ''). |
'&country='.urlencode($code_pays).'&maxRows=10'; |
$coord_json = @file_get_contents($url); |
$objet_retour = json_decode($coord_json); |
|
$retour = false; |
if ($this->estUnRetourGeocodingGeonamesValide($objet_retour)) { |
$retour = array( |
'lat' => $objet_retour->postalCodes[0]->lat, |
'lng' => $objet_retour->postalCodes[0]->lng, |
'nom' => $objet_retour->postalCodes[0]->placeName, |
'code_insee' => $objet_retour->postalCodes[0]->postalCode |
); |
|
private function effectuerRequeteReverseGeocodingMondiale($lat, $lon) { |
$url = $this->adresse_service_mondial.'?lat='.$lat.'&lon='.$lon; |
// Pour limiter par niveau ajouter ce qui suit à la requête : |
//.'&niveau=2,3,4'; |
$res = json_decode(file_get_contents($url), true); |
|
$code = ""; |
$localite = ""; |
|
if(!empty($res)) { |
$infos_pays = $res[min(array_keys($res))]; |
$infos_localite = $res[max(array_keys($res))]; |
|
$localite = $infos_localite['intitule']; |
if(!empty($infos_pays['codeIso31661'])) { |
$code = $infos_pays['codeIso31661']; |
} elseif(!empty($infos_pays['codeIso31662'])) { |
$code = substr($infos_pays['codeIso31662'], 0, 2); |
} |
} |
return $retour; |
} |
|
private function estUnRetourGeocodingGeonamesValide($retour) { |
$valide = false; |
if (is_a($retour, 'stdClass') && property_exists($retour, 'postalCodes') |
&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) { |
$objet_resultats = $retour->postalCodes[0]; |
if (property_exists($objet_resultats, 'lat') && property_exists($objet_resultats, 'lng')) { |
$valide = true; |
} |
} |
return $valide; |
return array('nom' => $localite, 'code_insee' => $code); |
} |
} |