104 |
aurelien |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
class OdsCommune extends JRestService {
|
|
|
4 |
|
|
|
5 |
const PREFIXE = 'get';
|
|
|
6 |
|
|
|
7 |
/**
|
|
|
8 |
* Méthode appelée avec une requête de type GET.
|
|
|
9 |
*
|
|
|
10 |
*/
|
|
|
11 |
function getElement($param = array()) {
|
|
|
12 |
|
|
|
13 |
$type = $param[0];
|
|
|
14 |
|
|
|
15 |
if ($type == '*' || is_numeric($type)) {
|
|
|
16 |
$info = $this->getElementParDefaut($param);
|
|
|
17 |
} else {
|
|
|
18 |
$methode = self::PREFIXE.$type;
|
|
|
19 |
if (method_exists($this, $methode)) {
|
|
|
20 |
array_shift($param);
|
|
|
21 |
$info = $this->$methode($param);
|
|
|
22 |
} else {
|
|
|
23 |
$this->messages[] = "Le type d'information demandé '$type' n'est pas disponible.";
|
|
|
24 |
}
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
// Envoi sur la sortie standard
|
|
|
28 |
$this->envoyer($info);
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
/** ======================= Methodes privées d'accès aux informations ================================ */
|
|
|
33 |
|
|
|
34 |
private function getElementParDefaut() {
|
|
|
35 |
return $this->getInformationsPourCoordonnees();
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
private function getInformationsPourCoordonnees($params) {
|
|
|
39 |
|
|
|
40 |
$lat = $_GET['lat'];
|
|
|
41 |
$lon = $_GET['lon'];
|
|
|
42 |
|
|
|
43 |
$infos_altitude_json = file_get_contents('http://maps.googleapis.com/maps/api/elevation/json?sensor=false&locations='.$lat.','.$lon);
|
|
|
44 |
$infos_commune_json = file_get_contents("http://ws.geonames.org/findNearbyJSON?featureClass=ADM4&lat=".urlencode($lat)."&lng=".urlencode($lon)."&style=full") ;
|
171 |
aurelien |
45 |
|
104 |
aurelien |
46 |
// à voir l'utilisation de google places lors de la mise en place d'un compte google premier api
|
|
|
47 |
//$infos_commune = file_get_contents('https://maps.googleapis.com/maps/api/place/search/json?sensor=false&locations='.$lat.','.$lon);
|
|
|
48 |
|
|
|
49 |
$infos_localites = $this->formaterTableauInformationsCoordsPourEnvoi($infos_altitude_json, $infos_commune_json);
|
|
|
50 |
|
|
|
51 |
return $infos_localites;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
private function getInformationsPourCommune($params) {
|
|
|
55 |
|
|
|
56 |
$commune = $_GET['commune'];
|
|
|
57 |
|
|
|
58 |
$commune = $this->remplacerNomCommunePourRecherche($commune);
|
|
|
59 |
|
171 |
aurelien |
60 |
$requete_infos_communes = 'SELECT * FROM ods_communes WHERE oc_nom LIKE '.$this->proteger($commune).' ORDER BY oc_nom LIMIT 0,10';
|
104 |
aurelien |
61 |
|
|
|
62 |
$infos_communes = $this->executerRequete($requete_infos_communes);
|
|
|
63 |
|
|
|
64 |
$infos_communes_formatees = $this->formaterTableauInformationsCommunePourEnvoi($infos_communes);
|
|
|
65 |
|
|
|
66 |
return $infos_communes_formatees;
|
|
|
67 |
}
|
|
|
68 |
|
207 |
aurelien |
69 |
private function getEstUneCommunePhenoclim() {
|
|
|
70 |
return $this->estUneCommunePhenoclim($_GET);
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
private function estUneCommunePhenoclim($params) {
|
|
|
74 |
|
|
|
75 |
$code_insee = null;
|
|
|
76 |
$code_postal = null;
|
|
|
77 |
|
|
|
78 |
if(!isset($params['code_postal']) && !isset($params['code_insee'])) {
|
|
|
79 |
return false;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
if(isset($params['code_postal'])) {
|
|
|
83 |
$code_postal = $params['code_postal'];
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
if(isset($params['code_insee'])) {
|
|
|
87 |
$code_insee = $params['code_insee'];
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
if($code_postal != null) {
|
|
|
91 |
$requete_commune_phenoclim = 'SELECT occ_code_insee FROM ods_communes_crea WHERE occ_code_postal = '.$this->proteger($code_postal);
|
|
|
92 |
} else {
|
|
|
93 |
$requete_commune_phenoclim = 'SELECT occ_code_insee FROM ods_communes_crea WHERE occ_code_insee = '.$this->proteger($code_insee);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
$resultat_requete_phenoclim = $this->executerRequete($requete_commune_phenoclim);
|
|
|
97 |
|
|
|
98 |
if(!empty($resultat_requete_phenoclim)) {
|
|
|
99 |
return true;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
return false;
|
|
|
103 |
}
|
|
|
104 |
|
104 |
aurelien |
105 |
private function remplacerNomCommunePourRecherche($nom) {
|
|
|
106 |
$nom = str_replace(' ','_',$nom);
|
|
|
107 |
$nom = str_replace('-','_',$nom);
|
|
|
108 |
$nom .= '%';
|
|
|
109 |
|
|
|
110 |
return $nom;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
private function formaterTableauInformationsCoordsPourEnvoi($infos_altitude_json, $infos_commune_json) {
|
|
|
114 |
|
|
|
115 |
$infos_altitude = json_decode($infos_altitude_json);
|
|
|
116 |
$infos_commune = json_decode($infos_commune_json);
|
|
|
117 |
|
|
|
118 |
$altitude = $infos_altitude->results[0]->elevation;
|
|
|
119 |
$altitude = number_format($altitude, 0, '', '');
|
|
|
120 |
|
|
|
121 |
$lat = $infos_altitude->results[0]->location->lat;
|
|
|
122 |
$lon = $infos_altitude->results[0]->location->lng;
|
|
|
123 |
|
|
|
124 |
$commune = $infos_commune->geonames[0]->adminName4;
|
|
|
125 |
$dpt = $infos_commune->geonames[0]->adminCode2;
|
|
|
126 |
|
193 |
aurelien |
127 |
$cp_recherche = $dpt;
|
|
|
128 |
if(strlen($cp_recherche) == 4) {
|
|
|
129 |
$cp_recherche = '0'.$cp_recherche;
|
|
|
130 |
}
|
207 |
aurelien |
131 |
|
|
|
132 |
$commune_phenoclim = $this->estUneCommunePhenoclim(array('code_postal' => $cp_recherche));
|
193 |
aurelien |
133 |
|
|
|
134 |
$cp_recherche = substr($cp_recherche,0,2);
|
|
|
135 |
|
|
|
136 |
$code_insee = '';
|
|
|
137 |
if($commune != null) {
|
|
|
138 |
$code_insee = $this->obtenirCodeInseeCommune($commune, $cp_recherche);
|
|
|
139 |
}
|
|
|
140 |
|
207 |
aurelien |
141 |
$infos_communes = array(
|
104 |
aurelien |
142 |
'commune' => $commune,
|
|
|
143 |
'dpt' => $dpt,
|
|
|
144 |
'lat' => $lat,
|
|
|
145 |
'lon' => $lon,
|
193 |
aurelien |
146 |
'alt' => $altitude,
|
207 |
aurelien |
147 |
'code_insee' => $code_insee,
|
|
|
148 |
'commune_phenoclim' => $commune_phenoclim
|
104 |
aurelien |
149 |
);
|
207 |
aurelien |
150 |
|
|
|
151 |
return $infos_communes;
|
104 |
aurelien |
152 |
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
private function formaterTableauInformationsCommunePourEnvoi($infos_communes) {
|
|
|
156 |
|
|
|
157 |
$infos_formatees = array();
|
|
|
158 |
|
|
|
159 |
foreach($infos_communes as $commune) {
|
|
|
160 |
|
171 |
aurelien |
161 |
$cp = $commune['oc_code_insee'];
|
|
|
162 |
$limite = 2;
|
|
|
163 |
if(strlen($cp) == 4) {
|
|
|
164 |
$limite = 1;
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
$dpt = substr($cp,0,$limite);
|
|
|
168 |
|
|
|
169 |
if($limite == 1) {
|
|
|
170 |
$dpt = '0'.$dpt;
|
|
|
171 |
}
|
|
|
172 |
|
207 |
aurelien |
173 |
$commune_phenoclim = $this->estUneCommunePhenoclim(array('code_insee' => $commune['oc_code_insee']));
|
|
|
174 |
|
104 |
aurelien |
175 |
$infos_formatees[] = array(
|
171 |
aurelien |
176 |
'commune' => $commune['oc_nom'],
|
|
|
177 |
'dpt' => $dpt,
|
|
|
178 |
'lat' => $commune['oc_latitude'],
|
207 |
aurelien |
179 |
'lon' => $commune['oc_longitude'],
|
|
|
180 |
'alt' => $commune['oc_altitude'],
|
|
|
181 |
'code_insee' => $commune['oc_code_insee'],
|
|
|
182 |
'commune_phenoclim' => $commune_phenoclim
|
104 |
aurelien |
183 |
);
|
|
|
184 |
}
|
|
|
185 |
return $infos_formatees;
|
|
|
186 |
}
|
193 |
aurelien |
187 |
|
207 |
aurelien |
188 |
public function obtenirNomCommuneParCodeInsee($code_insee_commune) {
|
|
|
189 |
|
|
|
190 |
if(!is_numeric($code_insee_commune)) {
|
|
|
191 |
return '';
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
$requete_infos_commune = 'SELECT * FROM ods_communes WHERE oc_code_insee = '.$this->proteger($code_insee_commune);
|
|
|
195 |
$infos_commune = $this->executerRequete($requete_infos_commune);
|
|
|
196 |
return $infos_commune[0]['oc_nom'];
|
|
|
197 |
}
|
|
|
198 |
|
210 |
aurelien |
199 |
public function obtenirTableauNomsCommunesParTableauCodesInsee($tableau_codes_insee_communes) {
|
|
|
200 |
|
280 |
aurelien |
201 |
$noms_communes = array();
|
|
|
202 |
|
|
|
203 |
if(!empty($tableau_codes_insee_communes)) {
|
|
|
204 |
$tableau_codes_insee_communes = array_unique($tableau_codes_insee_communes);
|
|
|
205 |
$chaine_codes_insee = implode($tableau_codes_insee_communes,',');
|
|
|
206 |
|
|
|
207 |
$requete_infos_communes = 'SELECT * FROM ods_communes WHERE oc_code_insee IN ('.$chaine_codes_insee.')';
|
|
|
208 |
$infos_commune = $this->executerRequete($requete_infos_communes);
|
|
|
209 |
|
|
|
210 |
foreach($infos_commune as $commune) {
|
|
|
211 |
$code_insee = $commune['oc_code_insee'];
|
|
|
212 |
$noms_communes[$code_insee] = $commune['oc_nom'];
|
|
|
213 |
}
|
|
|
214 |
}
|
210 |
aurelien |
215 |
|
|
|
216 |
return $noms_communes;
|
|
|
217 |
}
|
|
|
218 |
|
193 |
aurelien |
219 |
private function obtenirCodeInseeCommune($commune, $cp) {
|
|
|
220 |
|
|
|
221 |
$commune = $this->remplacerNomCommunePourRecherche($commune);
|
|
|
222 |
$requete_code_insee = 'SELECT oc_code_insee FROM ods_communes '.
|
|
|
223 |
'WHERE oc_nom LIKE "'.$commune.'" '.
|
|
|
224 |
'AND oc_code_insee LIKE "'.$cp.'%"';
|
|
|
225 |
|
|
|
226 |
$resultat_requete = $this->executerRequete($requete_code_insee);
|
|
|
227 |
|
|
|
228 |
if($resultat_requete) {
|
|
|
229 |
return $resultat_requete[0]['oc_code_insee'];
|
|
|
230 |
} else {
|
|
|
231 |
return '';
|
|
|
232 |
}
|
|
|
233 |
}
|
270 |
aurelien |
234 |
|
|
|
235 |
public function obtenirCommunesLieesAStationIndexeesCodeInsee() {
|
|
|
236 |
$communes = $this->obtenirCommunesLieesAStation();
|
|
|
237 |
$communes_assoc = array();
|
|
|
238 |
|
|
|
239 |
foreach($communes as $commune) {
|
|
|
240 |
$communes_assoc[$commune['oc_code_insee']] = $commune;
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
return $communes_assoc;
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
private function obtenirCommunesLieesAStation() {
|
|
|
247 |
|
|
|
248 |
$requete = 'SELECT * FROM ods_communes '.
|
|
|
249 |
'INNER JOIN ods_stations '.
|
|
|
250 |
'ON oc_code_insee = os_ce_commune ';
|
|
|
251 |
|
|
|
252 |
$resultat_requete = $this->executerRequete($requete);
|
|
|
253 |
|
|
|
254 |
return $resultat_requete;
|
|
|
255 |
}
|
104 |
aurelien |
256 |
}
|
|
|
257 |
?>
|