298 |
aurelien |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
class Maintenance extends JRestService {
|
|
|
4 |
|
|
|
5 |
/**
|
|
|
6 |
* Méthode appelée avec une requête de type GET.
|
|
|
7 |
*
|
|
|
8 |
*/
|
|
|
9 |
function executer($param = array()) {
|
|
|
10 |
|
|
|
11 |
$debut = microtime(true);
|
|
|
12 |
|
|
|
13 |
echo "Recherche des stations ayant des coordonnées mais aucune commune associee ...";
|
|
|
14 |
$requete = "SELECT * FROM `ods_stations` WHERE os_ce_commune NOT REGEXP '^-?[0-9]+$' AND os_ce_commune NOT IN (SELECT oc_code_insee FROM ods_communes)";
|
|
|
15 |
$liste_stations = $this->executerRequete($requete);
|
|
|
16 |
echo " ".count($liste_stations)." stations trouvées \n";
|
|
|
17 |
|
|
|
18 |
echo "Début de la recherche des communes associées \n";
|
|
|
19 |
$recherche_infos = new OdsCommune($this->config);
|
|
|
20 |
|
|
|
21 |
$communes_inconnues = array();
|
|
|
22 |
|
|
|
23 |
$infos_stations_completees = array();
|
|
|
24 |
foreach($liste_stations as $station) {
|
|
|
25 |
$infos_code_insee = $this->effectuerRechercheCommune($station['os_latitude'], $station['os_longitude']);
|
|
|
26 |
$infos_code_insee['ancien_nom'] = $station['os_ce_commune'];
|
|
|
27 |
$infos_code_insee['lat'] = $station['os_latitude'];
|
|
|
28 |
$infos_code_insee['lon'] = $station['os_longitude'];
|
|
|
29 |
|
|
|
30 |
if($infos_code_insee['certitude'] == "ok") {
|
|
|
31 |
$infos_stations_completees[$station['os_id_station']] = $infos_code_insee;
|
|
|
32 |
} else {
|
|
|
33 |
$communes_inconnues[$station['os_id_station']] = $infos_code_insee;
|
|
|
34 |
}
|
|
|
35 |
}
|
|
|
36 |
echo "Fin de la recherche : \n";
|
|
|
37 |
echo " - ".count($infos_stations_completees)." stations se verront affecter un code insee \n";
|
|
|
38 |
echo " - ".count($communes_inconnues)." stations ne correspondent à aucune commune connue \n";
|
|
|
39 |
|
|
|
40 |
file_put_contents($this->config["appli"]["chemin_fichiers_temp"]."/communes_inconnues.txt", print_r($communes_inconnues, true));
|
|
|
41 |
file_put_contents($this->config["appli"]["chemin_fichiers_temp"]."/communes_localisees.txt", print_r($infos_stations_completees, true));
|
|
|
42 |
|
|
|
43 |
$fin = microtime(true) - $debut;
|
|
|
44 |
|
|
|
45 |
echo "La recherche à pris ".$fin." secondes \n";
|
|
|
46 |
echo "Modification des stations dont la certitude est ok \n";
|
|
|
47 |
|
|
|
48 |
$suite = microtime(true);
|
|
|
49 |
|
|
|
50 |
foreach($infos_stations_completees as $id_station => $infos_station) {
|
|
|
51 |
$requete = "UPDATE ods_stations SET os_ce_commune = ".$this->proteger($infos_station['codeINSEE'])." WHERE os_id_station = ".$this->proteger($id_station);
|
|
|
52 |
$this->executerRequeteSimple($requete);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
$suite_et_fin = microtime(true) - $suite;
|
|
|
56 |
|
|
|
57 |
echo "Fin de modification des stations concernées \n";
|
|
|
58 |
echo "La mise à jour de la base a pris ".$suite_et_fin." secondes \n";
|
|
|
59 |
echo "Les stations non modifiées sont disponibles dans le fichier situé dans ".$this->config["appli"]["chemin_fichiers_temp"]."/communes_inconnues.txt \n";
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
function effectuerRechercheCommune($lat, $lon) {
|
|
|
63 |
$url_commune = "http://api.tela-botanica.org/service:eflore:0.1/osm/nom-commune?lon=".urlencode($lon)."&lat=".urlencode($lat);
|
|
|
64 |
$infos_commune_json = @file_get_contents($url_commune);
|
|
|
65 |
|
|
|
66 |
$infos = null;
|
|
|
67 |
if($infos_commune_json != null) {
|
|
|
68 |
$infos_d = json_decode($infos_commune_json, true);
|
|
|
69 |
if(isset($infos_d['codeINSEE']) && trim($infos_d['codeINSEE']) != "") {
|
|
|
70 |
$infos_d['source'] = 'osm';
|
|
|
71 |
$infos = $infos_d;
|
|
|
72 |
$infos['certitude'] = 'ok';
|
|
|
73 |
} else {
|
|
|
74 |
$infos = $this->rechercherParGoogleMaps($lat, $lon);
|
|
|
75 |
}
|
|
|
76 |
} else {
|
|
|
77 |
$infos = $this->rechercherParGoogleMaps($lat, $lon);
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
return $infos;
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
private function rechercherParGoogleMaps($lat, $lon) {
|
|
|
84 |
$url_commune_fallback = "https://maps.googleapis.com/maps/api/geocode/json?latlng=".urlencode($lat).",".urlencode($lon)."&sensor=false";
|
|
|
85 |
$infos = $this->decoderRetourGoogleMaps(@file_get_contents($url_commune_fallback));
|
|
|
86 |
|
|
|
87 |
return $infos;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
private function decoderRetourGoogleMaps($retour) {
|
|
|
91 |
$nom = null;
|
|
|
92 |
$code_postal = null;
|
|
|
93 |
|
|
|
94 |
$retour_infos_tpl = array("certitude" => "bof", "source" => "google", "nom" => "", "codeINSEE" => "");
|
|
|
95 |
|
|
|
96 |
if($retour != null) {
|
|
|
97 |
$retour = json_decode($retour, true);
|
|
|
98 |
if(isset($retour['results']) && count($retour['results']) != 0) {
|
|
|
99 |
$infos_commune = $retour['results'][0];
|
|
|
100 |
foreach($infos_commune['address_components'] as $tranche) {
|
|
|
101 |
if(isset($tranche['types'])) {
|
|
|
102 |
if(in_array('locality',$tranche['types'])) {
|
|
|
103 |
$nom = $tranche['long_name'];
|
|
|
104 |
}
|
|
|
105 |
if(in_array('postal_code',$tranche['types'])) {
|
|
|
106 |
$code_postal = $tranche['long_name'];
|
|
|
107 |
}
|
|
|
108 |
if($nom != null && $code_postal != null) {
|
|
|
109 |
break;
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if($nom != null && $code_postal != null) {
|
|
|
117 |
$dpt = substr($code_postal, 0, 2);
|
|
|
118 |
$dpt = $dpt."___";
|
|
|
119 |
// le retour de google maps est un code postal et pas un code insee
|
|
|
120 |
$requete = "SELECT oc_code_insee FROM ods_communes WHERE oc_nom LIKE ".$this->proteger($nom."%")." AND oc_code_insee LIKE ".$this->proteger($dpt);
|
|
|
121 |
$resultat = $this->executerRequete($requete);
|
|
|
122 |
if(!empty($resultat)) {
|
|
|
123 |
$retour_infos[1] = $resultat[0]['oc_code_insee'];
|
|
|
124 |
$retour_infos_tpl = array(
|
|
|
125 |
"certitude" => "ok",
|
|
|
126 |
"source" => "google",
|
|
|
127 |
"nom" => $nom,
|
|
|
128 |
"codeINSEE" => $resultat[0]['oc_code_insee']);
|
|
|
129 |
} else {
|
|
|
130 |
$retour_infos_tpl = array(
|
|
|
131 |
"certitude" => "douteux",
|
|
|
132 |
"source" => "google",
|
|
|
133 |
"nom" => $nom,
|
|
|
134 |
"codeINSEE" => $code_postal);
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
return $retour_infos_tpl;
|
|
|
139 |
}
|
|
|
140 |
}
|
|
|
141 |
?>
|