Subversion Repositories eFlore/Applications.cel

Rev

Rev 2453 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
class CelRadiusPoints extends Cel {
        
        /**
         * Méthode appelée avec une requête de type GET.
         */
        public function getRessource() {
                return $this->getElement(array());
        }
        
        /**
         * Méthode appelée avec une requête de type GET.
         */
        public function getElement($params) {

                $lat_centre = str_replace(',', '.', floatval($_GET['lat']));
                $lon_centre = str_replace(',', '.', floatval($_GET['lon']));
                $radius = str_replace(',', '.', floatval($_GET['radius']/1000));
                
                $retour = array();
                
                $retour['points'] = $this->obtenirPointsPourCentreEtRadius($lat_centre, $lon_centre, $radius);
                if(empty($retour['points'])) {
                        $retour['plus_proche'] = $this->obtenirPointPlusProche($lat_centre, $lon_centre);
                }

                $this->envoyerJson($retour);
        }

        public function obtenirPointsPourCentreEtRadius($lat_centre, $lon_centre, $radius) {
                $requete = "SELECT ".
                        "id_observation, nom_sel, ( ".
                                        "6371 * acos ( ".
                                                        "cos ( radians(".$lat_centre.") ) ".
                                                        "* cos( radians( latitude ) ) ".
                                                        "* cos( radians( longitude ) - radians(".$lon_centre.") ) ".
                                                        "+ sin ( radians(".$lat_centre.") ) ".
                                                        "* sin( radians( latitude ) ) ".
                                        ") ".
                        ") AS distance, latitude, longitude, COUNT(id_observation) as nb_obs ".
                        "FROM cel_obs ".
                        "WHERE latitude != 0 AND longitude != 0 ".
                        "GROUP BY latitude, longitude ".
                        "HAVING distance < ".$radius." ".
                        "ORDER BY distance ";

                $points = Cel::db()->requeter($requete);
                return $points;
        }
        
        public function obtenirPointPlusProche($lat_centre, $lon_centre) {
                $requete = "SELECT ".
                                "id_observation, nom_sel, ( ".
                                "6371 * acos ( ".
                                "cos ( radians(".$lat_centre.") ) ".
                                "* cos( radians( latitude ) ) ".
                                "* cos( radians( longitude ) - radians(".$lon_centre.") ) ".
                                "+ sin ( radians(".$lat_centre.") ) ".
                                "* sin( radians( latitude ) ) ".
                                ") ".
                                ") AS distance, latitude, longitude, COUNT(id_observation) as nb_obs ".
                                "FROM cel_obs ".
                                "WHERE latitude != 0 AND longitude != 0 ".
                                "GROUP BY latitude, longitude ".
                                "HAVING distance = MIN(distance) ".
                                "ORDER BY distance ";
                
                $point = Cel::db()->requeterLigne($requete);
                return $point;
        }
        
        
}