Subversion Repositories Sites.obs-saisons.fr

Compare Revisions

Ignore whitespace Rev 103 → Rev 104

/trunk/applications/jrest/services/OdsCommune.php
New file
0,0 → 1,117
<?php
 
class 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.COMMUNE WHERE COMMUNE_NOM LIKE '.$this->proteger($commune).' ORDER BY COMMUNE_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;
return array(
'commune' => $commune,
'dpt' => $dpt,
'lat' => $lat,
'lon' => $lon,
'alt' => $altitude
);
}
private function formaterTableauInformationsCommunePourEnvoi($infos_communes) {
$infos_formatees = array();
foreach($infos_communes as $commune) {
$infos_formatees[] = array(
'commune' => $commune['COMMUNE_NOM'],
'dpt' => $commune['COMMUNE_CODEPOSTAL'],
'lat' => $commune['COMMUNE_LATITUDE'],
'lon' => $commune['COMMUNE_LONGITUDE']
);
}
return $infos_formatees;
}
}
?>