Subversion Repositories eFlore/Applications.cel

Compare Revisions

Ignore whitespace Rev 2457 → Rev 2458

/trunk/jrest/services/CoordSearch.php
1,13 → 1,19
<?php
// declare(encoding='UTF-8');
/**
* Service recherche de commune par coordonnées et vice versa
* Encodage en entrée : utf8
* Encodage en sortie : utf8
* Service recherche de commune par coordonnées et vice versa.
*
* @author Aurélien PERONNET <aurelien@tela-botanica.org>
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
* @version $Id$
* @internal Mininum PHP version : 5.2
* @category CEL
* @package Services
* @subpackage Images
* @version 0.1
* @author Mathias CHOUET <mathias@tela-botanica.org>
* @author Jean-Pascal MILCENT <jpm@tela-botanica.org>
* @author Aurelien PERONNET <aurelien@tela-botanica.org>
* @license GPL v3 <http://www.gnu.org/licenses/gpl.txt>
* @license CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
* @copyright 1999-2014 Tela Botanica <accueil@tela-botanica.org>
*/
class CoordSearch extends Cel {
 
17,8 → 23,7
private $nom_service_geocoding = null;
private $nom_service_reverse_geocoding = null;
 
function CoordSearch($config) {
 
public function __construct($config) {
parent::__construct($config);
 
$this->adresse_service_geonames = $this->config['cel']['url_service_geo_geonames'];
37,8 → 42,7
* $uid[3] = code_postal (ou * si recherche d'une commune correspondant à des coordonnées)
* $uid[4] = code_pays (ou * si recherche d'une commune correspondant à des coordonnées, par défaut vaut FR)
*/
function getElement($uid){
 
public function getElement($uid){
$header = '';
$retour = array();
 
45,7 → 49,6
$params = $this->traiterParametres($uid);
 
if ($this->estUneRequeteReverseGeocoding($params)) {
 
$informations_communes = $this->effectuerRequeteReverseGeocodingCartoOsm($params['lat'], $params['lon']);
 
if (!$informations_communes) {
53,40 → 56,35
}
 
$header = 'Content-Type: application/json; charset=UTF-8';
$retour = json_encode($informations_communes) ;
 
} elseif ($this->estUneRequeteGeocoding($params)) {
 
$retour = json_encode($informations_communes);
} elseif ($this->estUneRequeteGeocoding($params)) {
$informations_coord = $this->chercherCentroideCommuneBdd($params['commune'],$params['code_postal']);
if(!$informations_coord) {
if (!$informations_coord) {
$informations_coord = $this->effectuerRequeteGeocodingGeonames($params['commune'],$params['code_postal'],$params['code_pays']);
}
 
$header = 'Content-Type: application/json; charset=UTF-8';
$retour = json_encode($informations_coord);
 
} else {
 
$header = 'HTTP/1.0 400 Bad Request';
$retour = 'Commune ou Coordonnées non spécifiées' ;
}
 
} else {
$header = 'HTTP/1.0 400 Bad Request';
$retour = 'Commune ou Coordonnées non spécifiées';
}
header($header);
echo $retour;
}
 
protected function traiterParametres($params) {
 
$lat = $this->affecterValeurParametreOuDefaut($params, 0, '*');
$lng = $this->affecterValeurParametreOuDefaut($params, 1, '*');
 
$commune = $this->affecterValeurParametreOuDefaut($params, 2, '*');
$code_postal = $this->affecterValeurParametreOuDefaut($params, 3, '*');
 
$code_pays = $this->affecterValeurParametreOuDefaut($params, 4, 'FR');
 
return array('lat' => $lat, 'lon' => $lng, 'commune' => $commune,
'code_postal' => $code_postal, 'code_pays' => $code_pays);
return array(
'lat' => $lat,
'lon' => $lng,
'commune' => $commune,
'code_postal' => $code_postal,
'code_pays' => $code_pays);
}
 
private function affecterValeurParametreOuDefaut($params, $indice, $valeur_si_non_present) {
94,7 → 92,6
}
 
private function estUneRequeteReverseGeocoding($params) {
 
return ($params['lat'] != '*' && $params['lon'] != '*');
}
 
103,136 → 100,114
}
 
private function effectuerRequeteReverseGeocodingCartoOsm($lat, $lon) {
 
$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=".$lat."&lon=".$lon);
$infos_commune_json = @file_get_contents($this->adresse_service_local."?lat=$lat&lon=$lon");
$infos_commune = json_decode($infos_commune_json);
 
$retour = false;
 
if ($this->estUnRetourOsmValide($infos_commune)) {
$retour = array('nom' => $infos_commune->nom, 'code_insee' => $infos_commune->codeINSEE);
}
 
return $retour;
}
 
private function estUnretourOsmValide($retour) {
return (is_a($retour,'stdClass') && property_exists($retour,'nom') && property_exists($retour,'codeINSEE'));
return (is_a($retour, 'stdClass') && property_exists($retour, 'nom') && property_exists($retour, 'codeINSEE'));
}
 
private function effectuerRequeteReverseGeocodingGeonames($lat, $lon) {
 
$infos_commune_json = @file_get_contents($this->adresse_service_geonames.
$url = $this->adresse_service_geonames.
$this->nom_service_reverse_geocoding.
"?lat=".urlencode($lat)."&lng=".urlencode($lon).
"&style=full");
'?lat='.urlencode($lat).'&lng='.urlencode($lon).
'&style=full';
$infos_commune_json = @file_get_contents($url);
$objet_retour = json_decode($infos_commune_json);
 
$retour = false;
if($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) {
$retour = array('nom' => $objet_retour->geonames[0]->adminName4, 'code_insee' => $objet_retour->geonames[0]->adminCode4);
if ($this->estUnRetourReverseGeocodingGeonamesValide($objet_retour)) {
$retour = array(
'nom' => $objet_retour->geonames[0]->adminName4,
'code_insee' => $objet_retour->geonames[0]->adminCode4);
}
 
return $retour;
}
 
private function estUnRetourReverseGeocodingGeonamesValide($retour) {
 
$valide = false;
 
if (is_a($retour,'stdClass') && property_exists($retour,'geonames')
&& is_array($retour->geonames) && count($retour->geonames) > 0) {
if (is_a($retour, 'stdClass') && property_exists($retour, 'geonames')
&& is_array($retour->geonames) && count($retour->geonames) > 0) {
$objet_resultats = $retour->geonames[0];
if (property_exists($objet_resultats,'adminName4') && property_exists($objet_resultats,'adminCode2')) {
if (property_exists($objet_resultats, 'adminName4') && property_exists($objet_resultats, 'adminCode2')) {
$valide = true;
}
}
 
return $valide;
}
 
private function chercherCentroideCommuneBdd($commune, $departement) {
 
$commune_formatee = str_replace(' ','_',$commune);
$commune_formatee = str_replace('-','_',$commune_formatee);
 
if(strlen($departement) > 2) {
$departement = substr($departement,0,2);
$commune_formatee = str_replace(array(' ', '-'), '_', $commune);
if (strlen($departement) > 2) {
$departement = substr($departement, 0, 2);
}
$requete_selection_commune = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
'WHERE nom LIKE '.Cel::db()->proteger($commune_formatee).' AND code LIKE '.Cel::db()->proteger($departement.'%');
$requete = 'SELECT utm_x, utm_y, utm_secteur, code FROM cel_zones_geo '.
'WHERE nom LIKE '.Cel::db()->proteger($commune_formatee).' '.
'AND code LIKE '.Cel::db()->proteger($departement.'%').' '.
' -- '.__FILE__.':'.__LINE__;
$commune_coordonnees = Cel::db()->requeter($requete);
 
$commune_coordonnees = Cel::db()->requeter($requete_selection_commune);
 
$retour = false;
if ($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
$lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
 
if($commune_coordonnees && is_array($commune_coordonnees) && count($commune_coordonnees) > 0) {
 
$lat_lon = $this->convertirUtmVersLatLong($commune_coordonnees[0]['utm_x'],$commune_coordonnees[0]['utm_y'],$commune_coordonnees[0]['utm_secteur']);
 
$retour = array('lat' => (float)$lat_lon['lat'],
'lng' => (float)$lat_lon['lng'],
'nom' => $commune,
'code_insee' => $commune_coordonnees[0]['code']
);
 
$retour = array(
'lat' => (float) $lat_lon['lat'],
'lng' => (float) $lat_lon['lng'],
'nom' => $commune,
'code_insee' => $commune_coordonnees[0]['code']
);
}
 
return $retour;
}
 
private function convertirUtmVersLatLong($x, $y, $sector) {
 
$lat_long = array();
 
$convertisseur = new gPoint();
$convertisseur->setUTM($x, $y, $sector);
$convertisseur->convertTMtoLL();
 
$lat_long = array();
$lat_long['lat'] = str_replace(',','.',$convertisseur->Lat());
$lat_long['lng'] = str_replace(',','.',$convertisseur->Long());
 
return $lat_long;
}
 
private function effectuerRequeteGeocodingGeonames($commune, $code_postal, $code_pays) {
 
$requete = $this->adresse_service_geonames.
$this->nom_service_geocoding.
"?placename_startsWith=".urlencode($commune);
 
if($code_postal != '*') {
$requete .= "&postalcode_startsWith=".urlencode($code_postal);
}
$requete .= "&country=".urlencode($code_pays)."&maxRows=10" ;
 
$coord_json = @file_get_contents($requete);
$url = $this->adresse_service_geonames.
$this->nom_service_geocoding.
'?placename_startsWith='.urlencode($commune).
(($code_postal != '*') ? '&postalcode_startsWith='.urlencode($code_postal) : '').
'&country='.urlencode($code_pays).'&maxRows=10';
$coord_json = @file_get_contents($url);
$objet_retour = json_decode($coord_json);
 
$retour = false;
 
if($this->estUnRetourGeocodingGeonamesValide($objet_retour)) {
$retour = array('lat' => $objet_retour->postalCodes[0]->lat,
'lng' => $objet_retour->postalCodes[0]->lng,
'nom' => $objet_retour->postalCodes[0]->placeName,
'code_insee' => $objet_retour->postalCodes[0]->postalCode
);
if ($this->estUnRetourGeocodingGeonamesValide($objet_retour)) {
$retour = array(
'lat' => $objet_retour->postalCodes[0]->lat,
'lng' => $objet_retour->postalCodes[0]->lng,
'nom' => $objet_retour->postalCodes[0]->placeName,
'code_insee' => $objet_retour->postalCodes[0]->postalCode
);
}
 
return $retour;
}
 
private function estUnRetourGeocodingGeonamesValide($retour) {
$valide = false;
 
if (is_a($retour,'stdClass') && property_exists($retour,'postalCodes')
&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
if (is_a($retour, 'stdClass') && property_exists($retour, 'postalCodes')
&& is_array($retour->postalCodes) && count($retour->postalCodes) > 0) {
$objet_resultats = $retour->postalCodes[0];
if (property_exists($objet_resultats,'lat') && property_exists($objet_resultats,'lng')) {
if (property_exists($objet_resultats, 'lat') && property_exists($objet_resultats, 'lng')) {
$valide = true;
}
}
 
return $valide;
}
}
?>
}