Rev 171 | Rev 207 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?phpclass OdsCommune extends JRestService {const PREFIXE = 'get';/*** Méthode appelée avec une requête de type GET.**/function getElement($param = array()) {$type = $param[0];if ($type == '*' || is_numeric($type)) {$info = $this->getElementParDefaut($param);} else {$methode = self::PREFIXE.$type;if (method_exists($this, $methode)) {array_shift($param);$info = $this->$methode($param);} else {$this->messages[] = "Le type d'information demandé '$type' n'est pas disponible.";}}// Envoi sur la sortie standard$this->envoyer($info);}/** ======================= Methodes privées d'accès aux informations ================================ */private function getElementParDefaut() {return $this->getInformationsPourCoordonnees();}private function getInformationsPourCoordonnees($params) {$lat = $_GET['lat'];$lon = $_GET['lon'];$infos_altitude_json = file_get_contents('http://maps.googleapis.com/maps/api/elevation/json?sensor=false&locations='.$lat.','.$lon);$infos_commune_json = file_get_contents("http://ws.geonames.org/findNearbyJSON?featureClass=ADM4&lat=".urlencode($lat)."&lng=".urlencode($lon)."&style=full") ;// à voir l'utilisation de google places lors de la mise en place d'un compte google premier api//$infos_commune = file_get_contents('https://maps.googleapis.com/maps/api/place/search/json?sensor=false&locations='.$lat.','.$lon);$infos_localites = $this->formaterTableauInformationsCoordsPourEnvoi($infos_altitude_json, $infos_commune_json);return $infos_localites;}private function getInformationsPourCommune($params) {$commune = $_GET['commune'];$commune = $this->remplacerNomCommunePourRecherche($commune);$requete_infos_communes = 'SELECT * FROM ods_communes WHERE oc_nom LIKE '.$this->proteger($commune).' ORDER BY oc_nom LIMIT 0,10';$infos_communes = $this->executerRequete($requete_infos_communes);$infos_communes_formatees = $this->formaterTableauInformationsCommunePourEnvoi($infos_communes);return $infos_communes_formatees;}private function remplacerNomCommunePourRecherche($nom) {$nom = str_replace(' ','_',$nom);$nom = str_replace('-','_',$nom);$nom .= '%';return $nom;}private function formaterTableauInformationsCoordsPourEnvoi($infos_altitude_json, $infos_commune_json) {$infos_altitude = json_decode($infos_altitude_json);$infos_commune = json_decode($infos_commune_json);$altitude = $infos_altitude->results[0]->elevation;$altitude = number_format($altitude, 0, '', '');$lat = $infos_altitude->results[0]->location->lat;$lon = $infos_altitude->results[0]->location->lng;$commune = $infos_commune->geonames[0]->adminName4;$dpt = $infos_commune->geonames[0]->adminCode2;$cp_recherche = $dpt;if(strlen($cp_recherche) == 4) {$cp_recherche = '0'.$cp_recherche;}$cp_recherche = substr($cp_recherche,0,2);$code_insee = '';if($commune != null) {$code_insee = $this->obtenirCodeInseeCommune($commune, $cp_recherche);}return array('commune' => $commune,'dpt' => $dpt,'lat' => $lat,'lon' => $lon,'alt' => $altitude,'code_insee' => $code_insee);}private function formaterTableauInformationsCommunePourEnvoi($infos_communes) {$infos_formatees = array();foreach($infos_communes as $commune) {$cp = $commune['oc_code_insee'];$limite = 2;if(strlen($cp) == 4) {$limite = 1;}$dpt = substr($cp,0,$limite);if($limite == 1) {$dpt = '0'.$dpt;}$infos_formatees[] = array('commune' => $commune['oc_nom'],'dpt' => $dpt,'code_insee' => $commune['oc_code_insee'],'lat' => $commune['oc_latitude'],'lon' => $commune['oc_longitude']);}return $infos_formatees;}private function obtenirCodeInseeCommune($commune, $cp) {$commune = $this->remplacerNomCommunePourRecherche($commune);$requete_code_insee = 'SELECT oc_code_insee FROM ods_communes '.'WHERE oc_nom LIKE "'.$commune.'" '.'AND oc_code_insee LIKE "'.$cp.'%"';$resultat_requete = $this->executerRequete($requete_code_insee);if($resultat_requete) {return $resultat_requete[0]['oc_code_insee'];} else {return '';}}}?>