10,20 → 10,20 |
* @version $Id$ |
*/ |
class CoordSearch extends Cel { |
|
|
private $adresse_service_geonames = null; |
private $adresse_service_local = null; |
|
|
private $nom_service_geocoding = null; |
private $nom_service_reverse_geocoding = null; |
|
|
function CoordSearch($config) { |
|
|
parent::__construct($config); |
|
|
$this->adresse_service_geonames = $this->config['cel']['url_service_geo_geonames']; |
$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']; |
} |
30,7 → 30,7 |
|
/** |
* Recherche de coordonnées suivant ce qui est fourni |
* |
* |
* $uid[0] = latitude (ou * si recherche coordonnées d'une commune) |
* $uid[1] = longitude (ou * si recherche coordonnées d'une commune) |
* $uid[2] = commune (ou * si recherche d'une commune correspondant à des coordonnées) |
43,20 → 43,20 |
$retour = array(); |
|
$params = $this->traiterParametres($uid); |
|
|
if ($this->estUneRequeteReverseGeocoding($params)) { |
|
$informations_communes = $this->effectuerRequeteReverseGeocodingCartoOsm($params['lat'], $params['lon']); |
|
|
if (!$informations_communes) { |
$informations_communes = $this->effectuerRequeteReverseGeocodingGeonames($params['lat'], $params['lon']); |
} |
|
|
$header = 'Content-Type: application/json; charset=UTF-8'; |
$retour = json_encode($informations_communes) ; |
|
} 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']); |
74,80 → 74,80 |
header($header); |
echo $retour; |
} |
|
|
protected function traiterParametres($params) { |
|
|
$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*'); |
$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*'); |
$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*'); |
|
$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*'); |
$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*'); |
$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*'); |
|
$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR'); |
|
|
return array('lat' => $lat, 'lon' => $lng, 'commune' => $commune, |
'code_postal' => $code_postal, 'code_pays' => $code_pays); |
} |
|
|
private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) { |
return isset($params[$indice]) ? str_replace('"','',urldecode($params[$indice])) : $valeur_si_non_present; |
return isset($params[$indice]) ? str_replace('"','',urldecode($params[$indice])) : $valeur_si_non_present; |
} |
|
|
private function estUneRequeteReverseGeocoding($params) { |
|
|
return ($params['lat'] != '*' && $params['lon'] != '*'); |
} |
|
|
private function estUneRequeteGeocoding($params) { |
return ($params['commune'] != '*'); |
} |
|
|
private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) { |
|
|
$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=".$lat."&lon=".$lon); |
$infos_commune = json_decode($infos_commune_json); |
|
|
$retour = false; |
|
|
if ($this->estUnRetourOsmValide($infos_commune)) { |
$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE); |
} |
|
|
return $retour; |
} |
|
|
private function estUnretourOsmValide($retour) { |
return (is_a($retour,'stdClass') && property_exists($retour,'nom') && property_exists($retour,'codeINSEE')); |
} |
|
|
private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) { |
|
|
$infos_commune_json = @file_get_contents($this->adresse_service_geonames. |
$this->nom_service_reverse_geocoding. |
"?lat=".urlencode($lat)."&lng=".urlencode($lon). |
"&style=full"); |
$objet_retour = json_decode($infos_commune_json); |
|
|
$retour = false; |
if($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) { |
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') |
|
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; |
} |
|
162,7 → 162,7 |
$requete_selection_commune = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '. |
'WHERE nom LIKE '.Cel::db()->proteger($commune_formatee).' AND code LIKE '.Cel::db()->proteger($departement.'%'); |
|
$commune_coordonnees = Cel::db()->executerRequete($requete_selection_commune); |
$commune_coordonnees = Cel::db()->requeter($requete_selection_commune); |
|
$retour = false; |
|
169,7 → 169,7 |
if($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) { |
|
$lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']); |
|
|
$retour = array('lat' => (float)$lat_lon['lat'], |
'lng' => (float)$lat_lon['lng'], |
'nom' => $commune, |
182,20 → 182,20 |
} |
|
private function convertirUtmVersLatLong($x, $y, $sector) { |
|
|
$lat_long = array(); |
|
|
$convertisseur = new gPoint(); |
$convertisseur->setUTM($x, $y, $sector); |
$convertisseur->convertTMtoLL(); |
$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat()); |
$lat_long['lng'] = str_replace(',','.',$convertisseur->Long()); |
|
|
return $lat_long; |
} |
|
|
private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) { |
|
|
$requete = $this->adresse_service_geonames. |
$this->nom_service_geocoding. |
"?placename_startsWith=".urlencode($commune); |
207,11 → 207,11 |
|
$coord_json = @file_get_contents($requete); |
$objet_retour = json_decode($coord_json); |
|
|
$retour = false; |
|
if($this->estUnRetourGeocodingGeonamesValide($objet_retour)) { |
$retour = array('lat' => $objet_retour->postalCodes[0]->lat, |
|
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 |
220,18 → 220,18 |
|
return $retour; |
} |
|
|
private function estUnRetourGeocodingGeonamesValide($retour) { |
$valide = false; |
|
if (is_a($retour,'stdClass') && property_exists($retour,'postalCodes') |
|
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; |
} |
} |