Rev 2462 | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php// declare(encoding='UTF-8');/*** Service recherche de commune par coordonnées et vice versa.** @internal Mininum PHP version : 5.2* @category CEL* @package Services* @subpackage Cartes* @version 0.1* @author Mathias CHOUET <mathias@tela-botanica.org>* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>* @author Aurelien PERONNET <aurelien@tela-botanica.org>* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>*/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;public function __construct($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'];}/*** 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)* $uid[3] = code_postal (ou * si recherche d'une commune correspondant à des coordonnées)* $uid[4] = code_pays (ou * si recherche d'une commune correspondant à des coordonnées, par défaut vaut FR)*/public function getElement($uid){$header = '';$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']);}$header = 'Content-Type: application/json; charset=UTF-8';$retour = json_encode($informations_coord);} else {$header = 'HTTP/1.0 400 Bad Request';$retour = 'Commune ou Coordonnées non spécifiées';}header($header);echo $retour;}protected function traiterParametres($params) {$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*');$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;}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) {$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) {$departement = substr($departement, 0, 2);}$requete = '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.'%').' '.' -- '.__FILE__.':'.__LINE__;$commune_coordonnees = Cel::db()->requeter($requete);$retour = false;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,'code_insee' => $commune_coordonnees[0]['code']);}return $retour;}private function convertirUtmVersLatLong($x, $y, $sector) {$convertisseur = new gPoint();$convertisseur->setUTM($x, $y, $sector);$convertisseur->convertTMtoLL();$lat_long = array();$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) {$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);}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;}}