Subversion Repositories Sites.obs-saisons.fr

Rev

Rev 193 | Rev 210 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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