Subversion Repositories Sites.obs-saisons.fr

Rev

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

Rev 104 Rev 171
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.COMMUNE WHERE COMMUNE_NOM LIKE '.$this->proteger($commune).' ORDER BY COMMUNE_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
    
68
    
69
    private function remplacerNomCommunePourRecherche($nom) {
69
    private function remplacerNomCommunePourRecherche($nom) {
70
    	$nom = str_replace(' ','_',$nom);
70
    	$nom = str_replace(' ','_',$nom);
71
    	$nom = str_replace('-','_',$nom);
71
    	$nom = str_replace('-','_',$nom);
72
    	$nom .= '%';
72
    	$nom .= '%';
73
    	
73
    	
74
    	return $nom;
74
    	return $nom;
75
    }
75
    }
76
    
76
    
77
    private function formaterTableauInformationsCoordsPourEnvoi($infos_altitude_json, $infos_commune_json) {
77
    private function formaterTableauInformationsCoordsPourEnvoi($infos_altitude_json, $infos_commune_json) {
78
    	
78
    	
79
    	$infos_altitude = json_decode($infos_altitude_json);
79
    	$infos_altitude = json_decode($infos_altitude_json);
80
    	$infos_commune = json_decode($infos_commune_json);
80
    	$infos_commune = json_decode($infos_commune_json);
81
    	    	
81
    	    	
82
    	$altitude = $infos_altitude->results[0]->elevation;
82
    	$altitude = $infos_altitude->results[0]->elevation;
83
    	$altitude = number_format($altitude, 0, '', '');
83
    	$altitude = number_format($altitude, 0, '', '');
84
    	
84
    	
85
    	$lat = $infos_altitude->results[0]->location->lat;
85
    	$lat = $infos_altitude->results[0]->location->lat;
86
    	$lon = $infos_altitude->results[0]->location->lng;
86
    	$lon = $infos_altitude->results[0]->location->lng;
87
    	
87
    	
88
    	$commune = $infos_commune->geonames[0]->adminName4;
88
    	$commune = $infos_commune->geonames[0]->adminName4;
89
    	$dpt = $infos_commune->geonames[0]->adminCode2;
89
    	$dpt = $infos_commune->geonames[0]->adminCode2;
90
    	
90
    	
91
    	return array( 
91
    	return array( 
92
    	    'commune' => $commune,
92
    	    'commune' => $commune,
93
    		'dpt' => $dpt,
93
    		'dpt' => $dpt,
94
            'lat' => $lat,
94
            'lat' => $lat,
95
            'lon' => $lon,
95
            'lon' => $lon,
96
            'alt' => $altitude
96
            'alt' => $altitude
97
        );
97
        );
98
    	
98
    	
99
    }
99
    }
100
    
100
    
101
	private function formaterTableauInformationsCommunePourEnvoi($infos_communes) {
101
	private function formaterTableauInformationsCommunePourEnvoi($infos_communes) {
102
		
102
		
103
		$infos_formatees = array();
103
		$infos_formatees = array();
104
		
104
		
105
		foreach($infos_communes as $commune) {
105
		foreach($infos_communes as $commune) {
-
 
106
			
-
 
107
			$cp = $commune['oc_code_insee'];
-
 
108
			$limite = 2;
-
 
109
			if(strlen($cp) == 4) {
-
 
110
				$limite = 1;
-
 
111
			}
-
 
112
			
-
 
113
			$dpt = substr($cp,0,$limite);
-
 
114
			
-
 
115
			if($limite == 1) {
-
 
116
				$dpt = '0'.$dpt;
-
 
117
			}
106
			
118
			
107
			$infos_formatees[] = array(
119
			$infos_formatees[] = array(
-
 
120
    			'commune' => $commune['oc_nom'],
108
    			'commune' => $commune['COMMUNE_NOM'],
121
    			'dpt' => $dpt,
109
    			'dpt' => $commune['COMMUNE_CODEPOSTAL'],
122
			    'code_insee' => $commune['oc_code_insee'],
110
    			'lat' => $commune['COMMUNE_LATITUDE'],
123
    			'lat' => $commune['oc_latitude'],
111
				'lon' => $commune['COMMUNE_LONGITUDE']
124
				'lon' => $commune['oc_longitude']
112
    		);
125
    		);
113
		}
126
		}
114
    	return $infos_formatees;
127
    	return $infos_formatees;
115
    }
128
    }
116
}
129
}
117
?>
130
?>