Rev 193 | Rev 210 | 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 getEstUneCommunePhenoclim() {return $this->estUneCommunePhenoclim($_GET);}private function estUneCommunePhenoclim($params) {$code_insee = null;$code_postal = null;if(!isset($params['code_postal']) && !isset($params['code_insee'])) {return false;}if(isset($params['code_postal'])) {$code_postal = $params['code_postal'];}if(isset($params['code_insee'])) {$code_insee = $params['code_insee'];}if($code_postal != null) {$requete_commune_phenoclim = 'SELECT occ_code_insee FROM ods_communes_crea WHERE occ_code_postal = '.$this->proteger($code_postal);} else {$requete_commune_phenoclim = 'SELECT occ_code_insee FROM ods_communes_crea WHERE occ_code_insee = '.$this->proteger($code_insee);}$resultat_requete_phenoclim = $this->executerRequete($requete_commune_phenoclim);if(!empty($resultat_requete_phenoclim)) {return true;}return false;}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;}$commune_phenoclim = $this->estUneCommunePhenoclim(array('code_postal' => $cp_recherche));$cp_recherche = substr($cp_recherche,0,2);$code_insee = '';if($commune != null) {$code_insee = $this->obtenirCodeInseeCommune($commune, $cp_recherche);}$infos_communes = array('commune' => $commune,'dpt' => $dpt,'lat' => $lat,'lon' => $lon,'alt' => $altitude,'code_insee' => $code_insee,'commune_phenoclim' => $commune_phenoclim);return $infos_communes;}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;}$commune_phenoclim = $this->estUneCommunePhenoclim(array('code_insee' => $commune['oc_code_insee']));$infos_formatees[] = array('commune' => $commune['oc_nom'],'dpt' => $dpt,'lat' => $commune['oc_latitude'],'lon' => $commune['oc_longitude'],'alt' => $commune['oc_altitude'],'code_insee' => $commune['oc_code_insee'],'commune_phenoclim' => $commune_phenoclim);}return $infos_formatees;}public function obtenirNomCommuneParCodeInsee($code_insee_commune) {if(!is_numeric($code_insee_commune)) {return '';}$requete_infos_commune = 'SELECT * FROM ods_communes WHERE oc_code_insee = '.$this->proteger($code_insee_commune);$infos_commune = $this->executerRequete($requete_infos_commune);return $infos_commune[0]['oc_nom'];}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 '';}}}?>