/trunk/services/modules/0.1/floradata/Observations.php |
---|
New file |
0,0 → 1,225 |
<?php |
class Observations { |
private $longitude = null; |
private $latitude = null; |
private $utilisateur = ''; |
private $taxon = null; |
private $referentiel = ''; |
private $sousTaxons = array(); |
private $bdd; |
public function __construct() {} |
public function consulter($ressources, $parametres) { |
extract($parametres); |
$this->utilisateur = $utilisateur != '*' ? $utilisateur : ''; |
$this->departement = $dept != '*' ? $dept : ''; |
$this->traiterParametresCoordonnees($parametres); |
if (is_null($this->longitude) || is_null($this->latitude)) { |
$message = "Recherche des observations impossible : coordonnees de la station hors du plan"; |
return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
// recherche du taxon dans les referentiels bdtfx et bdtxa et verification de sa validite |
if ($referentiel != '*') { |
$this->referentiel = $this->verifierExistenceReferentiel($referentiel); |
if (is_null($this->referentiel)) { |
$message = "Recherche des stations impossible : referentiel inconnu\n" |
+ "Voici les referentiels disponibles : " |
+ implode(', ', Referentiel::listeReferentielsDisponibles()); |
return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
} |
if ($num_taxon != '*') { |
$this->traiterParametresTaxon($num_taxon); |
if (is_null($this->taxon)) { |
$message = "Recherche des stations impossible : taxon non trouve dans les referentiels"; |
return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
// recuperer les sous-taxons |
$referentiel = new Referentiel($this->referentiel); |
$this->sousTaxons = $referentiel->recupererSousTaxons(); |
} |
// recuperer les informations sur les stations repondant a ces parametres |
$observations = $this->recupererObservations(); |
$nomSite = $this->obtenirNomSite(); |
// mettre en forme les informations au format JSON |
$formateurJSON = new FormateurJson(); |
$donneesFormatees = $formateurJSON->formaterObservations($observations, $nomSite); |
return $donneesFormatees; |
} |
//------------------------------------------------------------------------------- |
// Recuperation et verification des parametres |
//------------------------------------------------------------------------------- |
private function traiterParametresCoordonnees($parametres) { |
$this->longitude = $this->verifierCoordonnees($parametres['longitude'], 'lng'); |
$this->latitude = $this->verifierCoordonnees($parametres['latitude'], 'lat'); |
} |
private function verifierCoordonnees($valeur, $axe) { |
$limites = array( |
'ouest' => floatval(Config::get('carte.limite_ouest')), |
'est' => floatval(Config::get('carte.limite_est')), |
'sud' => floatval(Config::get('carte.limite_sud')), |
'nord' => floatval(Config::get('carte.limite_nord')) |
); |
if (($axe=='lng' && floatval($valeur) >= $limites['ouest'] && floatval($valeur) <= $limites['est']) |
|| ($axe=='lat' && floatval($valeur) >= $limites['sud'] && floatval($valeur) <= $limites['nord'])) { |
return $valeur; |
} |
return null; |
} |
private function traiterParametresTaxon($numeroTaxon) { |
if ($numeroTaxon != '*') { |
if ($this->referentiel != '') { |
$referentiel = new Referentiel($this->referentiel); |
$this->taxon = $referentiel->recupererTaxon($numeroTaxon); |
} else { |
$this->taxon = $this->verifierExistenceTaxon($numeroTaxon); |
} |
} |
} |
private function verifierExistenceReferentiel($referentiel) { |
if ($referentiel == '*') { |
return ''; |
} |
$listeReferentiels = Referentiel::listeReferentielsDisponibles(); |
foreach ($listeReferentiels as $nomReferentiel) { |
if (strstr($nomReferentiel, $referentiel) !== false) { |
$this->referentiel = $nomReferentiel; |
break; |
} |
} |
return null; |
} |
private function verifierExistenceTaxon($numTaxon) { |
$listeReferentiels = Referentiel::listeReferentielsDisponibles(); |
// tester chaque referentiel jusqu'a trouver un taxon correspondant |
// a tous les criteres demandes dans les masques |
foreach ($listeReferentiels as $nomReferentiel) { |
$referentiel = new Referentiel($nomReferentiel); |
$taxon = $referentiel->recupererTaxon($numTaxon); |
if (!is_null($taxon)) { |
$this->referentiel = $nomReferentiel; |
return $taxon; |
} |
} |
return null; |
} |
//------------------------------------------------------------------------------- |
// Recuperation des donnees dans la base de donnees |
// et mise sous forme d'objet geographique en fonction du niveau de zoom |
//------------------------------------------------------------------------------- |
private function recupererObservations() { |
$requete = 'SELECT ' . $this->construireColonnesSelection() . ' FROM cel_obs' |
. ' WHERE transmission=1' . $this->construireWhereUtilisateur() |
. $this->construireWhereTaxon() . $this->construireWhereCoordonnees() |
. ' ORDER BY nom_ret, date, observateur'; |
return $this->getBdd()->recupererTous($requete); |
} |
private function getBdd() { |
if (!isset($this->bdd)) { |
$this->bdd = new Bdd(); |
} |
$this->bdd->requeter("USE tb_cel"); |
return $this->bdd; |
} |
private function construireColonnesSelection() { |
$colonnes = array( |
'idObs' => 'id_observation', |
'nn' => 'nom_ret_nn', |
'nomSci' => 'nom_ret', |
'date' => 'Date(date_transmission)', |
'lieu' => 'milieu', |
'observateur' => "Concat(prenom_utilisateur, ' ', nom_utilisateur)", |
'observateurId' => 'ce_utilisateur', |
'urlEflore' => "If(nom_ret_nn IS NULL, NULL, Concat('http://www.tela-botanica.org/bdtfx-nn-', nom_ret_nn))" |
); |
$selectSql = ''; |
foreach ($colonnes as $renvoi => $colonne) { |
$selectSql .= (strlen($selectSql) == 0 ? '' : ', ') . "{$colonne} AS {$renvoi}"; |
} |
return $selectSql; |
} |
private function construireWhereTaxon() { |
$sql = ''; |
if (!is_null($this->taxon)) { |
$criteres = "nom_ret LIKE '" . addslashes($this->taxon['nom']) . "%'"; |
foreach ($this->sousTaxons as $sousTaxon) { |
$criteres .= " OR nom_ret LIKE '" . addslashes($sousTaxon['nom']) . "%'"; |
} |
$sql = " AND ($criteres)"; |
} |
return $sql; |
} |
private function construireWhereUtilisateur() { |
$sql = ''; |
if ($this->utilisateur != '') { |
$utilisateur = $this->getBdd()->proteger($this->utilisateur); |
$sql = " AND courriel_utilisateur = $utilisateur "; |
} |
return $sql; |
} |
private function construireWhereCoordonnees() { |
// coordonnees du point qui correspondent a une commune ? |
$requete = "SELECT id_zone_geo FROM cel_zones_geo WHERE wgs84_longitude=" . $this->longitude |
. " AND wgs84_latitude=" . $this->latitude; |
$commune = $this->getBdd()->recuperer($requete); |
$sql = ' AND ((longitude=' . $this->longitude . ' AND latitude=' . $this->latitude . ')'; |
if ($commune != false) { |
$sql .= " OR (longitude IS NULL AND latitude IS NULL AND ce_zone_geo='" |
. $commune['id_zone_geo'] . "')"; |
} |
$sql .= ')'; |
return $sql; |
} |
private function obtenirNomSite() { |
// coordonnees du point qui correspondent a une commune ? |
$requete = 'SELECT code, nom FROM cel_zones_geo WHERE wgs84_longitude=' . $this->longitude |
. ' AND wgs84_latitude=' . $this->latitude; |
$site = $this->getBdd()->recuperer($requete); |
if (!$site) { |
// recuperer les informations de la station |
$requete = 'SELECT DISTINCT lieudit AS "nom" FROM cel_obs WHERE longitude=' |
. $this->longitude . ' AND latitude = ' . $this->latitude; |
$site = $this->getBdd()->recuperer($requete); |
} |
if (!$site) |
return null; |
return is_null($site['nom']) ? '' : $site['nom']; |
} |
} |
?> |
/trunk/services/modules/0.1/floradata/Stations.php |
---|
New file |
0,0 → 1,275 |
<?php |
/** |
* Recuperer les stations ou se sont realisees des observations effectuees |
* par les utilisateurs du Carnet En Ligne (http://tela-botanica.org/page:cel |
* |
* @author Alexandre GALIBERT |
* @copyright 2013 Tela Botanica (accueil@tela-botanica.org) |
* |
*/ |
class Stations { |
private $zoom = null; |
private $bbox = null; |
private $utilisateur = ''; |
private $departement = ''; |
private $taxon = null; |
private $referentiel = ''; |
private $sousTaxons = array(); |
private $bdd; |
public function __construct() {} |
public function consulter($ressources, $parametres) { |
extract($parametres); |
$zoomMaxMaillage = Config::get('zoom_maximal_maillage'); |
$seuilMaillage = Config::get('seuil_maillage'); |
$this->utilisateur = $utilisateur != '*' ? $utilisateur : ''; |
$this->departement = $dept != '*' ? $dept : ''; |
// recuperation, traitement de tous les parametres et vertification du zoom |
// et des coordonnees des limites de l'espace de recherche (parametre bbox) |
$statutParametresEspace = $this->traiterParametresEspace($parametres); |
if ($statutParametresEspace == false) { |
$message = "Recherche des stations impossible : parametres de l'espace de recherche incorrects"; |
return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
// recherche du taxon dans les referentiels bdtfx et bdtxa et verification de sa validite |
if ($referentiel != '*') { |
$this->referentiel = $this->verifierExistenceReferentiel($referentiel); |
if (is_null($this->referentiel)) { |
$message = "Recherche des stations impossible : referentiel inconnu\n" |
+ "Voici les referentiels disponibles : " |
+ implode(', ', Referentiel::listeReferentielsDisponibles()); |
return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
} |
if ($num_taxon != '*') { |
$this->traiterParametresTaxon($num_taxon); |
if (is_null($this->taxon)) { |
$message = "Recherche des stations impossible : taxon non trouve dans les referentiels"; |
return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
// recuperer les sous-taxons |
$referentiel = new Referentiel($this->referentiel); |
$this->sousTaxons = $referentiel->recupererSousTaxons(); |
} |
// recuperer les informations sur les stations repondant a ces parametres |
$stations = $this->recupererStations(); |
if (count($stations) > $seuilMaillage && intval($this->zoom)<= $zoomMaxMaillage) { |
// partitionnement des donnees en mailles |
$maillage = new Maillage($this->bbox, $this->zoom); |
$maillage->genererMaillesVides(); |
$maillage->ajouterPoints($stations); |
$stations = $maillage->resumePourReponseAJAX(); |
} |
// mettre en forme les informations au format JSON |
$formateurJSON = new FormateurJson(); |
$donneesFormatees = $formateurJSON->formaterStations($stations); |
return $donneesFormatees; |
} |
//------------------------------------------------------------------------------- |
// Recuperation et verification des parametres |
//------------------------------------------------------------------------------- |
private function traiterParametresEspace($parametres) { |
$this->zoom = $this->verifierZoom($parametres['zoom']); |
$this->bbox = $this->verifierCoordonneesBbox($parametres['bbox']); |
return (!is_null($this->zoom) && !is_null($this->bbox)); |
} |
private function traiterParametresTaxon($numeroTaxon) { |
if ($numeroTaxon != '*') { |
if ($this->referentiel != '') { |
$referentiel = new Referentiel($this->referentiel); |
$this->taxon = $referentiel->recupererTaxon($numeroTaxon); |
} else { |
$this->taxon = $this->verifierExistenceTaxon($numeroTaxon); |
} |
} |
} |
private function verifierZoom($niveauZoom) { |
$zoom = intval($niveauZoom); |
$limitesZoom = array( |
'min' => intval(Config::get('carte.zoom_minimal')), |
'max' => intval(Config::get('carte.zoom_maximal')) |
); |
if ($zoom < $limitesZoom['min'] || $zoom > $limitesZoom['max']) { |
return null; |
} |
return $zoom; |
} |
private function verifierCoordonneesBbox($limitesBbox) { |
$coordonnees = explode(',', $limitesBbox); |
if (count($coordonnees) != 4) { |
return null; |
} |
// index du tableau des coordonnees : ouest/sud/est/nord |
$nomsIndexBbox = array("ouest", "sud", "est", "nord"); |
$bbox = array(); |
for ($i = 0; $i < count($coordonnees); $i ++) { |
$bbox[$nomsIndexBbox[$i]] = $coordonnees[$i]; |
} |
// verifier que les coordonnees de chaque bord de la bbox sont valides |
$limites = array( |
'ouest' => floatval(Config::get('carte.limite_ouest')), |
'est' => floatval(Config::get('carte.limite_est')), |
'sud' => floatval(Config::get('carte.limite_sud')), |
'nord' => floatval(Config::get('carte.limite_nord')) |
); |
if (floatval($bbox['ouest']) >= $limites['ouest'] && floatval($bbox['ouest']) <= $limites['est'] |
&& floatval($bbox['est']) >= $limites['ouest'] && floatval($bbox['est']) <= $limites['est'] |
&& floatval($bbox['nord']) >= $limites['sud'] && floatval($bbox['nord']) <= $limites['nord'] |
&& floatval($bbox['sud']) >= $limites['sud'] && floatval($bbox['sud']) <= $limites['nord']) { |
return $bbox; |
} |
return null; |
} |
private function verifierExistenceReferentiel($referentiel) { |
if ($referentiel == '*') { |
return ''; |
} |
$listeReferentiels = Referentiel::listeReferentielsDisponibles(); |
foreach ($listeReferentiels as $nomReferentiel) { |
if (strstr($nomReferentiel, $referentiel) !== false) { |
$this->referentiel = $nomReferentiel; |
break; |
} |
} |
return null; |
} |
private function verifierExistenceTaxon($numTaxon) { |
$listeReferentiels = Referentiel::listeReferentielsDisponibles(); |
// tester chaque referentiel jusqu'a trouver un taxon correspondant |
// a tous les criteres demandes dans les masques |
foreach ($listeReferentiels as $nomReferentiel) { |
$referentiel = new Referentiel($nomReferentiel); |
$taxon = $referentiel->recupererTaxon($numTaxon); |
if (!is_null($taxon)) { |
$this->referentiel = $nomReferentiel; |
return $taxon; |
} |
} |
return null; |
} |
//------------------------------------------------------------------------------- |
// Recuperation des donnees dans la base de donnees |
// et mise sous forme d'objet geographique en fonction du niveau de zoom |
//------------------------------------------------------------------------------- |
private function recupererStations() { |
$this->getBdd()->requeter("USE tb_cel"); |
$selectTypeSite = "If((longitude IS NULL AND latitude IS NULL) OR (longitude=0 AND latitude=0)" |
. " OR (longitude=999.99999 AND latitude=999.99999), 'COMMUNE', 'STATION')"; |
$requete = 'SELECT ce_zone_geo, zone_geo, station, longitude, latitude, nom AS "nom_commune",' |
. ' wgs84_longitude AS "lng_commune", wgs84_latitude AS "lat_commune", ' . $selectTypeSite |
. ' AS "type_site" FROM cel_obs LEFT JOIN cel_zones_geo cz ON ce_zone_geo=id_zone_geo WHERE transmission=1' |
. $this->construireWhereDepartement() . $this->construireWhereUtilisateur() |
. $this->construireWhereTaxon() . $this->construireWhereCoordonnees() |
. " GROUP BY longitude, latitude, wgs84_longitude, wgs84_latitude"; |
return $this->getBdd()->recupererTous($requete); |
/* |
// requete n°1 : recuperer dans la base cel les informations sur les stations |
// dans l'espace de recherche dont la precision de la localisation est au niveau de la commune |
$requete = 'SELECT id_zone_geo AS "ce_zone_geo", nom AS "zone_geo", \'\' AS "nom",' |
. ' wgs84_latitude AS "latitude", wgs84_longitude AS "longitude", \'COMMUNE\' AS "type_site"' |
. ' FROM cel_zones_geo WHERE 1 ' . $this->construireWhereDepartement('id') |
. $this->construireWhereCoordonnees('wgs84_') . ' AND id_zone_geo IN(' |
. 'SELECT ce_zone_geo FROM cel_obs WHERE transmission=1' . $this->construireWhereTaxon() . ')'; |
$stations = $this->getBdd()->recupererTous($requete); |
// requete 2 : recuperer la liste des stations ayant des observations publiques |
// dans l'espace de recherche dont la precision de la localisation est au lieu-dit |
$requete = 'SELECT ce_zone_geo, zone_geo, station AS "nom", latitude, longitude, \'STATION\'' |
. ' AS "type_site" FROM cel_obs WHERE transmission=1' |
. $this->construireWhereTaxon() . $this->construireWhereCoordonnees() |
. $this->construireWhereDepartement('ce') . ' GROUP BY longitude, latitude'; |
return array_merge($stations, $this->getBdd()->recupererTous($requete));*/ |
} |
private function getBdd() { |
if (!isset($this->bdd)) { |
$this->bdd = new Bdd(); |
} |
return $this->bdd; |
} |
private function construireWhereTaxon() { |
$sql = ''; |
if (!is_null($this->taxon)) { |
$criteres = "nom_ret LIKE '" . addslashes($this->taxon['nom']) . "%'"; |
foreach ($this->sousTaxons as $sousTaxon) { |
$criteres .= " OR nom_ret LIKE '" . addslashes($sousTaxon['nom']) . "%'"; |
} |
$sql = " AND ($criteres)"; |
} |
return $sql; |
} |
private function construireWhereDepartement() { |
$sql = ''; |
if (strlen(trim($this->departement)) > 0) { |
$valeurs_a_proteger = explode(',',trim($this->departement)); |
foreach ($valeurs_a_proteger as $valeur) { |
$valeurs_protegees[] = "ce_zone_geo LIKE " . $this->getBdd()->proteger('INSEE-C:' . $valeur . '%'); |
} |
$valeurs = implode(' OR ', $valeurs_protegees); |
$sql = " AND ($valeurs)"; |
} |
return $sql; |
} |
private function construireWhereUtilisateur() { |
$sql = ''; |
if ($this->utilisateur != '') { |
$utilisateur = $this->getBdd()->proteger($this->utilisateur); |
$sql = " AND courriel_utilisateur = $utilisateur "; |
} |
return $sql; |
} |
private function construireWhereCoordonnees() { |
$sql = " AND ((longitude BETWEEN " . $this->bbox['ouest'] . " AND " . $this->bbox['est'] |
. " AND latitude BETWEEN " . $this->bbox['sud'] . " AND " . $this->bbox['nord'] |
. " AND wgs84_longitude IS NULL AND wgs84_latitude IS NULL) OR (wgs84_longitude BETWEEN " |
. $this->bbox['ouest'] . " AND " . $this->bbox['est'] . " AND wgs84_latitude BETWEEN " |
. $this->bbox['sud'] . " AND " . $this->bbox['nord'] . "))"; |
/*$sql = " AND {$suffixeChamp}latitude BETWEEN " . $this->bbox['sud'] . " AND " |
. $this->bbox['nord'] . " AND "; |
if ($this->bbox['ouest'] > $this->bbox['est']) { |
$sql .= "({$suffixeChamp}longitude >= " . $this->bbox['ouest'] |
. " OR {$suffixeChamp}longitude <= " . $this->bbox['est'] . ")"; |
} else { |
$sql .= "{$suffixeChamp}longitude BETWEEN " . $this->bbox['ouest'] . " AND " |
. $this->bbox['est']; |
}*/ |
return $sql; |
} |
} |
?> |
/trunk/services/modules/0.1/commun/Maillage.php |
---|
New file |
0,0 → 1,227 |
<?php |
class Maillage { |
/** |
* @var array les coordonnees des limites de l'espace de recherche |
*/ |
private $bbox; |
/** |
* @var int le niveau actuel de zoom sur la carte (cote navigateur client) |
*/ |
private $zoom; |
/** |
* @var array le tableau indiquant la coordonnee de debut de maille sur la longitude |
*/ |
private $indexLongitude; |
/** |
* @var array le tableau indiquant la coordonnee de debut de maille sur la latitude |
*/ |
private $indexLatitude; |
/** |
* @var array le tableau a deux dimensions recouvrant l'espace de la bbox de mailles |
*/ |
private $mailles; |
public function __construct($bbox, $zoom) { |
$this->bbox = $bbox; |
$this->zoom = $zoom; |
$this->indexLongitude = array(); |
$this->indexLatitude = array(); |
$this->mailles = array(); |
} |
public function __destruct() { |
while (count($this->indexLatitude) > 0) { |
array_pop($this->indexLatitude); |
} |
while (count($this->indexLongitude) > 0) { |
array_pop($this->indexLongitude); |
} |
while (count($this->mailles) > 0) { |
array_pop($this->mailles); |
} |
unset($this); |
} |
public function genererMaillesVides() { |
$this->getIndexDansBbox(); |
foreach ($this->indexLatitude as $indexLat => $intervalleLat) { |
$ligne = array(); |
foreach ($this->indexLongitude as $indexLng => $intervalleLng) { |
$ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1], |
$intervalleLng[1], $indexLat, $indexLng); |
} |
$this->mailles[] = $ligne; |
} |
} |
private function getIndexDansBbox() { |
// recuperer toutes les mailles qui couvrent l'espace a requeter |
$bdd = new Bdd(); |
$requete = "SELECT axe, position, debut, fin FROM mailles_index WHERE zoom=" . $this->zoom |
. " AND ((axe='lat' AND fin >= " . $this->bbox['sud'] . " AND debut <= " |
. $this->bbox['nord']. ")"; |
if ($this->bbox['ouest'] > $this->bbox['est']) { |
$requete .= " OR (axe='lng' AND NOT(debut >= " . $this->bbox['ouest'] . " AND fin <= " |
. $this->bbox['est'] . ")))"; |
} else { |
$requete .= " OR (axe='lng' AND fin >= " . $this->bbox['ouest'] . " AND debut <= " |
. $this->bbox['est'] . "))"; |
} |
$requete .= " ORDER BY axe, position"; |
$bdd->requeter("USE tb_eflore"); |
$indexMailles = $bdd->recupererTous($requete); |
foreach ($indexMailles as $index) { |
if ($index['axe'] == 'lng') { |
$this->indexLongitude[$index['position']] = array($index['debut'], $index['fin']); |
} else { |
$this->indexLatitude[$index['position']] = array($index['debut'], $index['fin']); |
} |
} |
} |
public function ajouterPoints($points) { |
foreach ($points as $index =>$point) { |
if (!isset($point['type_site']) || $point['type_site'] == 'STATION') { |
$longitude = $point['longitude']; |
$latitude = $point['latitude']; |
} else { |
$longitude = $point['lng_commune']; |
$latitude = $point['lat_commune']; |
} |
// recherche de l'index de la maille dans lequel placer le point |
$indexLat = 0; |
$indexLng = 0; |
while ($indexLat < count($this->indexLatitude) - 1 |
&& $this->mailles[$indexLat][0]->getLatitudeNord() < $latitude) { |
$indexLat++; |
} |
while ($indexLng < count($this->indexLongitude) - 1 |
&& $this->mailles[$indexLat][$indexLng]->getLongitudeEst() < $longitude) { |
$indexLng++; |
} |
// ajout du point dans la maille correspondante |
$this->mailles[$indexLat][$indexLng]->ajouterPoint($point); |
} |
} |
public function resumePourReponseAJAX() { |
$mailles_resume = array(); |
foreach ($this->mailles as $ligne) { |
foreach ($ligne as $maille) { |
$nombrePoints = $maille->getNombrePoints(); |
if ($nombrePoints == 0) |
continue; |
$mailles_resume[] = array( |
'zoom' => $this->zoom, |
'sud' => $maille->getLatitudeSud(), |
'ouest' => $maille->getLongitudeOuest(), |
'nord' => $maille->getLatitudeNord(), |
'est' => $maille->getLongitudeEst(), |
'points' => $nombrePoints, |
'type_site' => 'MAILLE' |
); |
} |
} |
if (count($mailles_resume) == 0 || count($mailles_resume[0]) == 0) |
return array(); |
return $mailles_resume; |
} |
public function resumePourInsertionBdd() { |
$mailles_resume = array(); |
foreach ($this->mailles as $ligne) { |
foreach ($ligne as $maille) { |
if ($maille->getNombrePoints() > 0) { |
$mailles_resume[] = array( |
'zoom' => $this->zoom, |
'sud' => $maille->getLatitudeSud(), |
'ouest' => $maille->getLongitudeOuest(), |
'nord' => $maille->getLatitudeNord(), |
'est' => $maille->getLongitudeEst(), |
'indexLat' => $maille->getIndexLatitude(), |
'indexLng' => $maille->getIndexLongitude(), |
'points' => $maille->getNombrePoints() |
); |
} |
} |
} |
if (count($mailles_resume[0]) == 0) |
return array(); |
return $mailles_resume; |
} |
public function zoomer() { |
$this->zoom += 1; |
} |
// redecoupage des mailles en 4 (fonction quadtree) |
public function redecouperMailles() { |
$bdd = new Bdd(); |
while (count($this->indexLatitude) > 0) { |
array_pop($this->indexLatitude); |
} |
while (count($this->indexLongitude) > 0) { |
array_pop($this->indexLongitude); |
} |
$mailles = $this->resumePourInsertionBdd(); |
while (count($this->mailles) > 0) { |
array_pop($this->mailles); |
} |
foreach ($mailles as $maille) { |
$indexLat = 2 * $maille['indexLat']; |
$indexLng = 2 * $maille['indexLng']; |
// rechercher les nouvelles coordonnees des mailles au niveau de zoom inferieur |
$requete = "SELECT axe,position,debut,fin FROM mailles_index WHERE zoom=". $this->zoom ." AND ((axe='lat'" |
. " AND (position={$indexLat} OR position=" . ($indexLat+1) . ")) OR (axe='lng' AND" |
. " (position={$indexLng} OR position=" . ($indexLng+1) . "))) ORDER BY If(axe='lat',0,1)"; |
$resultats = $bdd->recupererTous($requete); |
$this->indexLatitude[$indexLat] = array($resultats[0]['debut'], $resultats[0]['fin']); |
$this->indexLatitude[$indexLat+1] = array($resultats[1]['debut'], $resultats[1]['fin']); |
$this->indexLongitude[$indexLng] = array($resultats[2]['debut'], $resultats[2]['fin']); |
$this->indexLongitude[$indexLng+1] = array($resultats[3]['debut'], $resultats[3]['fin']); |
} |
ksort($this->indexLatitude); |
ksort($this->indexLongitude); |
// creer et ajouter les nouvelles mailles a partir des nouveaux index |
foreach ($this->indexLatitude as $indexLat => $intervalleLat) { |
$ligne = array(); |
foreach ($this->indexLongitude as $indexLng => $intervalleLng) { |
$ligne[] = new Maille($intervalleLat[0], $intervalleLng[0], $intervalleLat[1], |
$intervalleLng[1], $indexLat, $indexLng); |
} |
$this->mailles[] = $ligne; |
} |
} |
public function getNombreMailles() { |
return count($this->mailles); |
} |
} |
?> |
/trunk/services/modules/0.1/commun/FormateurJson.php |
---|
New file |
0,0 → 1,108 |
<?php |
class FormateurJson { |
public function __construct() {} |
public function formaterStations($stations) { |
$objetJSON = new StdClass(); |
$objetJSON->type = "FeatureCollection"; |
$objetJSON->features = array(); |
if (count($stations) == 0) { |
return $objetJSON; |
} |
foreach ($stations as $station) { |
$stationJSON = NULL; |
// construction d'un objet feature adapte a la structure des donnees spatiales |
if (isset($station['sud'])) { |
$stationJSON = $this->formaterMaille($station); |
} else { |
$stationJSON = $this->formaterPoint($station); |
} |
if (!is_null($stationJSON)) { |
$objetJSON->features[] = $stationJSON; |
} |
} |
return $objetJSON; |
} |
private function formaterPoint($station) { |
$json = new StdClass(); |
$json->type = "Feature"; |
$json->geometry = new StdClass(); |
$json->properties = new StdClass(); |
$json->geometry->type = "Point"; |
if (is_null($station['latitude']) || is_null($station['longitude'])) { |
$json->properties->typeSite = 'COMMUNE'; |
$json->geometry->coordinates = array($station['lat_commune'], $station['lng_commune']); |
} else { |
$json->properties->typeSite = 'STATION'; |
$json->geometry->coordinates = array($station['latitude'], $station['longitude']); |
} |
$nom = $json->properties->typeSite == 'COMMUNE' ? $station['nom_commune'] : $station['station']; |
$nomDefaut = 'station sans nom'; |
if ($json->properties->typeSite == 'COMMUNE') { |
$ce = substr($station['ce_zone_geo'],-5); |
$codeDept = ((int)substr($ce, 0, 2) > 95 ? substr($ce, 0, 3) : substr($ce, 0, 2)); |
$nom = $station['zone_geo'] . " ($codeDept)"; |
} else { |
if (strlen(trim($station['zone_geo']))==0) { |
$nom = $nomDefaut; |
} else { |
$nom = $station['zone_geo']; |
if (strlen(trim($station['ce_zone_geo']))!=0) { |
$ce = substr($station['ce_zone_geo'],-5); |
$nom .= ' ('.((int)substr($ce,0,2)>95 ? substr($ce,0,3) : substr($ce,0,2)).')'; |
} |
} |
} |
$json->properties->nom = $nom; |
return $json; |
} |
private function formaterMaille($maille) { |
if ($maille['points'] == 0) { |
return null; |
} |
$json = new StdClass(); |
$json->type = "Feature"; |
$json->geometry = new StdClass(); |
$json->geometry->type = "Polygon"; |
$json->geometry->coordinates = array( |
array(floatval($maille['sud']), floatval($maille['ouest'])), |
array(floatval($maille['sud']), floatval($maille['est'])), |
array(floatval($maille['nord']), floatval($maille['est'])), |
array(floatval($maille['nord']), floatval($maille['ouest'])), |
array(floatval($maille['sud']), floatval($maille['ouest'])) |
); |
$json->properties = new StdClass(); |
$json->properties->typeSite = $maille['type_site']; |
$json->properties->nombrePoints = $maille['points']; |
return $json; |
} |
public function formaterObservations($observations, $nomSite) { |
$objetJSON = new StdClass(); |
$objetJSON->site = $nomSite; |
$objetJSON->total = count($observations); |
$objetJSON->observations = array(); |
foreach ($observations as $observation) { |
$observationJson = new stdClass(); |
foreach ($observation as $colonne => $valeur) { |
$observationJson->$colonne = $valeur; |
} |
$objetJSON->observations[] = $observationJson; |
} |
return $objetJSON; |
} |
} |
?> |
/trunk/services/modules/0.1/commun/Maille.php |
---|
New file |
0,0 → 1,74 |
<?php |
class Maille { |
private $latitudeSud; |
private $longitudeOuest; |
private $latitudeNord; |
private $longitudeEst; |
private $points; |
private $nombrePoints = NULL; |
private $indexLatitude; |
private $indexLongitude; |
public function __construct($sud, $ouest, $nord, $est, $indexLat, $indexLng) { |
$this->latitudeSud = $sud; |
$this->longitudeOuest = $ouest; |
$this->latitudeNord = $nord; |
$this->longitudeEst = $est; |
$this->indexLatitude = $indexLat; |
$this->indexLongitude = $indexLng; |
$this->points = array(); |
} |
public function ajouterPoint($point) { |
$this->points[] = $point; |
} |
public function getLatitudeNord() { |
return $this->latitudeNord; |
} |
public function getLongitudeOuest() { |
return $this->longitudeOuest; |
} |
public function getLatitudeSud() { |
return $this->latitudeSud; |
} |
public function getLongitudeEst() { |
return $this->longitudeEst; |
} |
public function getIndexLatitude() { |
return $this->indexLatitude; |
} |
public function getIndexLongitude() { |
return $this->indexLongitude; |
} |
public function getNombrePoints() { |
if (!is_null($this->nombrePoints)) |
return $this->nombrePoints; |
return count($this->points); |
} |
public function setNombrePoints($nombrePoints) { |
$this->nombrePoints = $nombrePoints; |
} |
public function getPoint($index = 0) { |
return (!isset($this->points[$index])) ? NULL : $this->points[$index]; |
} |
public function totalNonNul() { |
return !is_null($this->nombrePoints); |
} |
} |
?> |
/trunk/services/modules/0.1/commun/Referentiel.php |
---|
New file |
0,0 → 1,139 |
<?php |
define("RANG_FAMILLE", 180); |
class Referentiel { |
private $nomComplet; |
private $nomCourt; |
private $taxon = null; |
private $bdd = null; |
public function __construct($nomReferentiel) { |
$this->nomComplet = $nomReferentiel; |
$this->nomCourt = current(explode('_', $nomReferentiel)); |
} |
public function getReferentiel() { |
return $this->nomComplet; |
} |
public function getTaxon() { |
return $this->taxon; |
} |
public function setTaxon($taxon) { |
$this->taxon = $taxon; |
} |
public function recupererTaxon($numTaxon) { |
if (is_null($numTaxon) || $numTaxon == 0) { |
return null; |
} |
$methodeChamps = 'nomsChamps' . ucfirst($this->nomCourt); |
extract($this->$methodeChamps()); |
// requete SQL a executer |
$requete = "SELECT {$nn} AS nn, {$nt} AS nt, {$ns} AS nom, rang FROM {$this->nomComplet}" |
. " WHERE {$nt}={$numTaxon} AND num_nom={$nn} ORDER BY rang, If(num_nom={$nn},0,1) LIMIT 0,1"; |
$taxon = $this->getBdd()->recuperer($requete); |
return ($taxon == false ? null : $taxon); |
} |
public function recupererSousTaxons($listeNn = null) { |
if ($this->taxon['rang'] < RANG_FAMILLE) { |
// recherche de sous taxons seulement si le rang du taxon precedemment recupere |
// est du niveau de la famille ou inferieur (genre, espece) |
return array(); |
} |
$methodeChamps = 'nomsChamps' . ucfirst($this->nomCourt); |
extract($this->$methodeChamps()); |
if (is_null($listeNn)) { |
$listeNn = $this->taxon['nn']; |
} |
$requete = "SELECT {$nn} AS nn, {$nt} As nt, {$ns} AS nom FROM {$this->nomComplet} WHERE" |
. " num_tax_sup IN ({$listeNn}) AND num_nom={$nn}"; |
$sousTaxons = $this->getBdd()->recupererTous($requete); |
$listeComplete = array(); |
$nouvelleListeNn = ''; |
foreach ($sousTaxons as $sousTaxon) { |
$listeComplete[] = $sousTaxon; |
$nouvelleListeNn .= (strlen($nouvelleListeNn) == 0 ? '' : ',') . $sousTaxon['nn']; |
} |
// recherche de sous taxons au niveau inferieur (parcours recursif de la methode) |
if (count($listeComplete) > 0) { |
$listeComplete = array_merge($listeComplete, $this->recupererSousTaxons($nouvelleListeNn)); |
} |
return $listeComplete; |
} |
private function getBdd() { |
if (is_null($this->bdd)) { |
$this->bdd = new Bdd(); |
} |
$this->bdd->requeter("USE tb_eflore"); |
return $this->bdd; |
} |
private function nomsChampsBdtfx() { |
return array('nn' => 'num_nom_retenu', 'nt' => 'num_taxonomique', 'ns' => 'nom_sci'); |
} |
private function nomsChampsBdtxa() { |
return array('nn' => 'num_nom_retenu', 'nt' => 'num_tax', 'ns' => 'nom_sci'); |
} |
public static function listeReferentielsDisponibles() { |
$tableau = array(); |
$tableauPartiel = explode(',', Config::get('referentielsDispo')); |
$tableauPartiel = array_map('trim', $tableauPartiel); |
foreach ($tableauPartiel as $champ) { |
if (strpos($champ, '=') === false) { |
$tableau[] = $champ; |
} else { |
list($cle, $val) = explode('=', $champ); |
$clePropre = trim($cle); |
$valeurPropre = trim($val); |
$tableau[$clePropre] = $valeurPropre; |
} |
} |
return $tableau; |
} |
} |
/*$nombreCriteres = 0; |
foreach ($masques as $masque => $valeur) { |
if ($masque == 'masque.nt') { |
$requete .= ($nombreCriteres == 0 ? '' : " AND ") . ""; |
$nombreCriteres ++; |
} elseif ($masque == 'masque.nn') { |
$requete .= ($nombreCriteres == 0 ? '' : " AND ") . "num_nom = (SELECT num_nom_retenu" |
. " FROM {$this->nomComplet} WHERE num_nom={$valeur})"; |
$nombreCriteres ++; |
} elseif ($masque == 'masque.ns') { |
$requete .= ($nombreCriteres == 0 ? '' : " AND ") . "({$ns} LIKE '{$valeur}%'" |
. " OR num_nom=(SELECT {$nn} FROM {$this->nomComplet} WHERE {$ns} LIKE '{$valeur}%'" |
. " ORDER BY rang LIMIT 0, 1))"; |
$nombreCriteres ++; |
} |
} |
$requete .= " ";*/ |
?> |
/trunk/services/modules/0.1/sophy/Observations.php |
---|
New file |
0,0 → 1,234 |
<?php |
class Observations { |
private $longitude = null; |
private $latitude = null; |
private $utilisateur = ''; |
private $taxon = null; |
private $referentiel = ''; |
private $sousTaxons = array(); |
private $bdd; |
public function __construct() {} |
public function consulter($ressources, $parametres) { |
extract($parametres); |
$this->utilisateur = $utilisateur != '*' ? $utilisateur : ''; |
$this->departement = $dept != '*' ? $dept : ''; |
$this->traiterParametresCoordonnees($parametres); |
if (is_null($this->longitude) || is_null($this->latitude)) { |
$message = "Recherche des observations impossible : coordonnees de la station hors du plan"; |
return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
// recherche du taxon dans les referentiels bdtfx et bdtxa et verification de sa validite |
if ($referentiel != '*') { |
$this->referentiel = $this->verifierExistenceReferentiel($referentiel); |
if (is_null($this->referentiel)) { |
$message = "Recherche des stations impossible : referentiel inconnu\n" |
+ "Voici les referentiels disponibles : " |
+ implode(', ', Referentiel::listeReferentielsDisponibles()); |
return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
} |
if ($num_taxon != '*') { |
$this->traiterParametresTaxon($num_taxon); |
if (is_null($this->taxon)) { |
$message = "Recherche des stations impossible : taxon non trouve dans les referentiels"; |
return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
// recuperer les sous-taxons |
$referentiel = new Referentiel($this->referentiel); |
$this->sousTaxons = $referentiel->recupererSousTaxons(); |
} |
// recuperer les informations sur les stations repondant a ces parametres |
$observations = $this->recupererObservations(); |
$nomSite = $this->obtenirNomsStations(); |
// mettre en forme les informations au format JSON |
$formateurJSON = new FormateurJson(); |
$donneesFormatees = $formateurJSON->formaterObservations($observations, $nomSite); |
return $donneesFormatees; |
} |
//------------------------------------------------------------------------------- |
// Recuperation et verification des parametres |
//------------------------------------------------------------------------------- |
private function traiterParametresCoordonnees($parametres) { |
$this->longitude = $this->verifierCoordonnees($parametres['longitude'], 'lng'); |
$this->latitude = $this->verifierCoordonnees($parametres['latitude'], 'lat'); |
} |
private function verifierCoordonnees($valeur, $axe) { |
$limites = array( |
'ouest' => floatval(Config::get('carte.limite_ouest')), |
'est' => floatval(Config::get('carte.limite_est')), |
'sud' => floatval(Config::get('carte.limite_sud')), |
'nord' => floatval(Config::get('carte.limite_nord')) |
); |
if (($axe=='lng' && floatval($valeur) >= $limites['ouest'] && floatval($valeur) <= $limites['est']) |
|| ($axe=='lat' && floatval($valeur) >= $limites['sud'] && floatval($valeur) <= $limites['nord'])) { |
return $valeur; |
} |
return null; |
} |
private function traiterParametresTaxon($numeroTaxon) { |
if ($numeroTaxon != '*') { |
if ($this->referentiel != '') { |
$referentiel = new Referentiel($this->referentiel); |
$this->taxon = $referentiel->recupererTaxon($numeroTaxon); |
} else { |
$this->taxon = $this->verifierExistenceTaxon($numeroTaxon); |
} |
} |
} |
private function verifierExistenceReferentiel($referentiel) { |
if ($referentiel == '*') { |
return ''; |
} |
$listeReferentiels = Referentiel::listeReferentielsDisponibles(); |
foreach ($listeReferentiels as $nomReferentiel) { |
if (strstr($nomReferentiel, $referentiel) !== false) { |
$this->referentiel = $nomReferentiel; |
break; |
} |
} |
return null; |
} |
private function verifierExistenceTaxon($numTaxon) { |
$listeReferentiels = Referentiel::listeReferentielsDisponibles(); |
// tester chaque referentiel jusqu'a trouver un taxon correspondant |
// a tous les criteres demandes dans les masques |
foreach ($listeReferentiels as $nomReferentiel) { |
$referentiel = new Referentiel($nomReferentiel); |
$taxon = $referentiel->recupererTaxon($numTaxon); |
if (!is_null($taxon)) { |
$this->referentiel = $nomReferentiel; |
return $taxon; |
} |
} |
return null; |
} |
//------------------------------------------------------------------------------- |
// Recuperation des donnees dans la base de donnees |
// et mise sous forme d'objet geographique en fonction du niveau de zoom |
//------------------------------------------------------------------------------- |
private function getBdd() { |
if (!isset($this->bdd)) { |
$this->bdd = new Bdd(); |
} |
$this->bdd->requeter("USE tb_eflore"); |
return $this->bdd; |
} |
private function recupererObservations() { |
$requete = 'SELECT ' . $this->construireColonnesSelection() . ' FROM sophy_tapir WHERE ' |
. $this->construireCriteresTaxon() . ' lieu_station_latitude=' . $this->latitude |
. ' AND lieu_station_longitude=' . $this->longitude; |
$observations = $this->getBdd()->recupererTous($requete); |
foreach ($observations as $obs) { |
// recuperer le numero du taxon en parcourant les referentiels |
// jusqu'a retrouver le taxon |
$nn = null; |
if (!is_null($this->taxon)) { |
$nn = $this->taxon['nn']; |
} elseif (strlen(trim($obs['nomSci'])) > 0) { |
// recuperer le nom du taxon |
$taxon = $this->obtenirNumeroTaxon($obs['nomSci']); |
$nn = $taxon['nn']; |
} |
$obs['nn'] = $nn; |
if (!is_null($nn)) { |
$obs['urlEflore'] = "http://www.tela-botanica.org/bdtfx-nn-{$nn}"; |
} |
} |
return $observations; |
} |
private function construireColonnesSelection() { |
$colonnes = array( |
'idObs' => 'observation_id', |
'nomSci' => 'nom_scientifique_complet', |
'date' => 'observation_date', |
'lieu' => 'lieu_station_nom', |
'observateur' => "observateur_nom_complet", |
); |
$selectSql = ''; |
foreach ($colonnes as $renvoi => $colonne) { |
$selectSql .= (strlen($selectSql) == 0 ? '' : ', ') . "{$colonne} AS {$renvoi}"; |
} |
return $selectSql; |
} |
private function construireCriteresTaxon() { |
if (is_null($this->taxon)) { |
return ''; |
} |
$criteres = '(nom_scientifique_complet LIKE \'' . addslashes($this->taxon['nom']) . '%\''; |
foreach ($this->sousTaxons as $sousTaxon) { |
$criteres .= ' OR nom_scientifique_complet LIKE \'' . addslashes($sousTaxon['nom']) . '%\''; |
} |
$criteres .= ') AND '; |
return $criteres; |
} |
private function obtenirNomsStations() { |
$critereTaxon = ''; |
if (!is_null($this->taxon)) { |
$critereTaxon = 'nom_scientifique_complet LIKE \'' . addslashes($this->taxon['nom_sci']) |
. '%\' AND '; |
} |
$requete = 'SELECT DISTINCTROW lieu_station_nom FROM sophy_tapir WHERE ' . $critereTaxon |
. ' lieu_station_latitude=' . $this->latitude . ' AND lieu_station_longitude=' |
. $this->longitude; |
$stations = $this->getBdd()->recupererTous($requete); |
$nomsStations = array(); |
foreach ($stations as $station) { |
$nomsStations[] = $station['lieu_station_nom']; |
} |
return implode(', ', $nomsStations); |
} |
private function obtenirNumeroTaxon($nomScientifique) { |
$masque = array('masque.ns' => $nomScientifique); |
$listeReferentiels = Referentiel::listeReferentielsDisponibles(); |
// tester chaque referentiel jusqu'a trouver un taxon correspondant |
// a tous les criteres demandes dans les masques |
foreach ($listeReferentiels as $nomReferentiel) { |
$referentiel = new Referentiel($nomReferentiel); |
$taxon = $referentiel->recupererTaxon($masque); |
if (!is_null($taxon)) { |
return $taxon; |
} |
} |
} |
} |
?> |
/trunk/services/modules/0.1/sophy/Stations.php |
---|
New file |
0,0 → 1,290 |
<?php |
/** |
* Recuperer les stations ou se sont realisees des observations effectuees |
* par les utilisateurs du Carnet En Ligne (http://tela-botanica.org/page:cel |
* |
* @author Alexandre GALIBERT |
* @copyright 2013 Tela Botanica (accueil@tela-botanica.org) |
* |
*/ |
class Stations { |
private $zoom = null; |
private $bbox = null; |
private $utilisateur = ''; |
private $departement = ''; |
private $taxon = null; |
private $referentiel = ''; |
private $sousTaxons = array(); |
private $bdd; |
public function __construct() {} |
public function consulter($ressources, $parametres) { |
extract($parametres); |
$zoomMaxMaillage = Config::get('zoom_maximal_maillage'); |
$seuilMaillage = Config::get('seuil_maillage'); |
$this->utilisateur = $utilisateur != '*' ? $utilisateur : ''; |
$this->departement = $dept != '*' ? $dept : ''; |
// recuperation, traitement de tous les parametres et vertification du zoom |
// et des coordonnees des limites de l'espace de recherche (parametre bbox) |
$statutParametresEspace = $this->traiterParametresEspace($parametres); |
if ($statutParametresEspace == false) { |
$message = "Recherche des stations impossible : parametres de l'espace de recherche incorrects"; |
return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
// recherche du taxon dans les referentiels bdtfx et bdtxa et verification de sa validite |
if ($referentiel != '*') { |
$this->referentiel = $this->verifierExistenceReferentiel($referentiel); |
if (is_null($this->referentiel)) { |
$message = "Recherche des stations impossible : referentiel inconnu\n" |
+ "Voici les referentiels disponibles : " |
+ implode(', ', Referentiel::listeReferentielsDisponibles()); |
return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
} |
if ($num_taxon != '*') { |
$this->traiterParametresTaxon($num_taxon); |
if (is_null($this->taxon)) { |
$message = "Recherche des stations impossible : taxon non trouve dans les referentiels"; |
return new Exception($message, RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
// recuperer les sous-taxons |
$referentiel = new Referentiel($this->referentiel); |
$this->sousTaxons = $referentiel->recupererSousTaxons(); |
} |
$stations = array(); |
if ($this->zoom <= $zoomMaxMaillage && is_null($this->taxon) && $this->departement != '*' |
&& $this->utilisateur != '*' && $this->nombreStations() >= $seuilMaillage) { |
// recuperer les mailles se trouvant dans l'espace de recherche demande |
$stations = $this->getMailles(); |
} else { |
// recuperer les informations sur les stations repondant a ces parametres |
$stations = $this->recupererStations(); |
if ($this->zoom <= $zoomMaxMaillage && count($stations) >= $seuilMaillage) { |
// generer un maillage a partir des stations recuperees dans la base de donnees |
$maillage = new Maillage($this->bbox, $this->zoom, 'sophy'); |
$maillage->genererMaillesVides(); |
$maillage->ajouterPoints($stations); |
$stations = $maillage->resumePourReponseAJAX(); |
} |
} |
// mettre en forme les informations au format JSON |
$formateurJSON = new FormateurJson(); |
$donneesFormatees = $formateurJSON->formaterStations($stations); |
return $donneesFormatees; |
} |
//------------------------------------------------------------------------------- |
// Recuperation et verification des parametres |
//------------------------------------------------------------------------------- |
private function traiterParametresEspace($parametres) { |
$this->zoom = $this->verifierZoom($parametres['zoom']); |
$this->bbox = $this->verifierCoordonneesBbox($parametres['bbox']); |
return (!is_null($this->zoom) && !is_null($this->bbox)); |
} |
private function traiterParametresTaxon($numeroTaxon) { |
if ($numeroTaxon != '*') { |
if ($this->referentiel != '') { |
$referentiel = new Referentiel($this->referentiel); |
$this->taxon = $referentiel->recupererTaxon($numeroTaxon); |
} else { |
$this->taxon = $this->verifierExistenceTaxon($numeroTaxon); |
} |
} |
} |
private function verifierZoom($niveauZoom) { |
$zoom = intval($niveauZoom); |
$limitesZoom = array( |
'min' => intval(Config::get('carte.zoom_minimal')), |
'max' => intval(Config::get('carte.zoom_maximal')) |
); |
if ($zoom < $limitesZoom['min'] || $zoom > $limitesZoom['max']) { |
return null; |
} |
return $zoom; |
} |
private function verifierCoordonneesBbox($limitesBbox) { |
$coordonnees = explode(',', $limitesBbox); |
if (count($coordonnees) != 4) { |
return null; |
} |
// index du tableau des coordonnees : ouest/sud/est/nord |
$nomsIndexBbox = array("ouest", "sud", "est", "nord"); |
$bbox = array(); |
for ($i = 0; $i < count($coordonnees); $i ++) { |
$bbox[$nomsIndexBbox[$i]] = $coordonnees[$i]; |
} |
// verifier que les coordonnees de chaque bord de la bbox sont valides |
$limites = array( |
'ouest' => floatval(Config::get('carte.limite_ouest')), |
'est' => floatval(Config::get('carte.limite_est')), |
'sud' => floatval(Config::get('carte.limite_sud')), |
'nord' => floatval(Config::get('carte.limite_nord')) |
); |
if (floatval($bbox['ouest']) >= $limites['ouest'] && floatval($bbox['ouest']) <= $limites['est'] |
&& floatval($bbox['est']) >= $limites['ouest'] && floatval($bbox['est']) <= $limites['est'] |
&& floatval($bbox['nord']) >= $limites['sud'] && floatval($bbox['nord']) <= $limites['nord'] |
&& floatval($bbox['sud']) >= $limites['sud'] && floatval($bbox['sud']) <= $limites['nord']) { |
return $bbox; |
} |
return null; |
} |
private function verifierExistenceReferentiel($referentiel) { |
if ($referentiel == '*') { |
return ''; |
} |
$listeReferentiels = Referentiel::listeReferentielsDisponibles(); |
foreach ($listeReferentiels as $nomReferentiel) { |
if (strstr($nomReferentiel, $referentiel) !== false) { |
$this->referentiel = $nomReferentiel; |
break; |
} |
} |
return null; |
} |
private function verifierExistenceTaxon($numTaxon) { |
$listeReferentiels = Referentiel::listeReferentielsDisponibles(); |
// tester chaque referentiel jusqu'a trouver un taxon correspondant |
// a tous les criteres demandes dans les masques |
foreach ($listeReferentiels as $nomReferentiel) { |
$referentiel = new Referentiel($nomReferentiel); |
$taxon = $referentiel->recupererTaxon($numTaxon); |
if (!is_null($taxon)) { |
$this->referentiel = $nomReferentiel; |
return $taxon; |
} |
} |
return null; |
} |
//------------------------------------------------------------------------------- |
// Recuperation des donnees dans la base de donnees |
// et mise sous forme d'objet geographique en fonction du niveau de zoom |
//------------------------------------------------------------------------------- |
private function getBdd() { |
if (!isset($this->bdd)) { |
$this->bdd = new Bdd(); |
} |
$this->bdd->requeter("USE tb_eflore"); |
return $this->bdd; |
} |
/** |
* Recuperer les points localisant les stations d'observation |
*/ |
private function recupererStations() { |
$requete = 'SELECT DISTINCT lieu_station_nom AS "nom", lieu_station_latitude AS "latitude",' |
. ' lieu_station_longitude AS "longitude", \'STATION\' AS type_site FROM sophy_tapir WHERE ' |
. $this->construireWhereDepartement() . $this->construireWhereUtilisateur() |
. $this->construireWhereTaxon() . $this->construireWhereCoordonnees() |
. ' GROUP BY lieu_station_latitude, lieu_station_longitude'; |
return $this->getBdd()->recupererTous($requete); |
} |
private function construireWhereTaxon() { |
if (is_null($this->taxon)) { |
return ''; |
} |
$criteres = '(nom_scientifique_complet LIKE \'' . addslashes($this->taxon['nom']) . '%\''; |
foreach ($this->sousTaxons as $sousTaxon) { |
$criteres .= ' OR nom_scientifique_complet LIKE \'' . addslashes($sousTaxon['nom']) . '%\''; |
} |
$criteres .= ') AND '; |
return $criteres; |
} |
private function construireWhereDepartement() { |
$sql = ''; |
return $sql; |
} |
private function construireWhereUtilisateur() { |
$sql = ''; |
if ($this->utilisateur != '') { |
$utilisateur = $this->getBdd()->proteger($this->utilisateur); |
$sql = " AND observateur_nom_complet = $utilisateur "; |
} |
return $sql; |
} |
private function construireWhereCoordonnees() { |
$criteres = ' AND lieu_station_latitude BETWEEN ' . $this->bbox['sud'] . ' AND ' |
. $this->bbox['nord'] . ' AND '; |
if ($this->bbox['ouest'] > $this->bbox['est']) { |
$criteres .= '(lieu_station_longitude >= ' . $this->bbox['ouest'] |
. ' OR lieu_station_latitude <= ' . $this->bbox['est'] . ')'; |
} else { |
$criteres .= 'lieu_station_longitude BETWEEN ' . $this->bbox['ouest'] . ' AND ' |
. $this->bbox['est']; |
} |
return $criteres; |
} |
private function nombreStations() { |
$requete = 'SELECT zoom, Sum(nombre_sites) AS "total_points" FROM mailles_sophy' |
. ' WHERE zoom=' . $this->zoom . ' AND limite_sud <= ' . $this->bbox['nord'] |
. ' AND limite_nord >= ' . $this->bbox['sud']; |
if ($this->bbox['ouest'] > $this->bbox['est']) { |
$requete .= ' AND NOT (limite_ouest >= ' . $this->bbox['ouest'] |
. ' AND limite_est <= ' . $this->bbox['est'] . ')'; |
} else { |
$requete .= ' AND limite_ouest <= ' . $this->bbox['est'] |
. ' AND limite_est >= ' . $this->bbox['ouest']; |
} |
$requete .= ' GROUP BY zoom'; |
$resultat = $this->getBdd()->recuperer($requete); |
return $resultat['total_points']; |
} |
/** |
* Recuperer les mailles contenant des points dans l'espace de recherche de la bbox |
*/ |
private function getMailles() { |
$requete = 'SELECT zoom, position_latitude, position_longitude, limite_sud AS "sud",' |
. ' limite_ouest AS "ouest", limite_nord AS "nord", limite_est AS "est", nombre_sites' |
. ' AS "points", \'MAILLE\' AS "type_site" FROM mailles_sophy WHERE zoom=' . $this->zoom |
. ' AND limite_sud <= ' . $this->bbox['nord'] . ' AND limite_nord >= ' . $this->bbox['sud']; |
if ($this->bbox['ouest'] > $this->bbox['est']) { |
$requete .= ' AND NOT (limite_ouest >= ' . $this->bbox['ouest'] . ' AND limite_est <= ' |
. $this->bbox['est'] . ')'; |
} else { |
$requete .= ' AND limite_ouest <= ' . $this->bbox['est'] . ' AND limite_est >= ' |
. $this->bbox['ouest']; |
} |
return $this->getBdd()->recupererTous($requete); |
} |
} |
?> |
/trunk/services/modules/0.1/Moissonnage.php |
---|
New file |
0,0 → 1,216 |
<?php |
class Moissonnage extends RestService { |
private $parametres = array(); |
private $ressources = array(); |
private $squeletteDossier = ''; |
private $nomService = ''; |
private $nomSource = ''; |
private $cache; |
public function __construct() { |
$this->squeletteDossier = dirname(__FILE__) . DIRECTORY_SEPARATOR; |
} |
public function consulter($ressources, $parametres) { |
$resultat = ''; |
$reponseHttp = new ReponseHttp(); |
try { |
$this->initialiserRessourcesEtParametres($ressources, $parametres); |
$resultat = $this->traiterRessources(); |
$reponseHttp->setResultatService($resultat); |
} catch (Exception $e) { |
$reponseHttp->ajouterErreur($e); |
} |
$reponseHttp->emettreLesEntetes(); |
$corps = $reponseHttp->getCorps(); |
return $corps; |
} |
private function initialiserRessourcesEtParametres($ressources, $parametres) { |
$this->ressources = $ressources; |
$this->parametres = $parametres; |
} |
private function traiterRessources() { |
$retour = ''; |
if ($this->avoirRessources()) { |
if ($this->avoirRessourceSource()) { |
$this->initialiserSource(); |
if ($this->avoirRessourceService()) { |
$retour = $this->initialiserService(); |
} |
} |
} |
return $retour; |
} |
private function avoirRessources() { |
$presenceDeRessources = false; |
if (isset($this->ressources) && count($this->ressources) > 0) { |
$presenceDeRessources = true; |
} else { |
$message = "Accès au service refusé : aucune ressource indiquée.\n" |
. "Veuillez indiquer au moins une source de données à interroger" |
. " et le type informations a renvoyer."; |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE; |
throw new Exception($message, $code); |
} |
return $presenceDeRessources; |
} |
private function avoirRessourceSource() { |
$presenceRessourceDemandee = false; |
$source = $this->ressources[0]; |
$sourcesDispo = $this->recupererTableauConfig('sourcesDispo'); |
if (in_array($source, $sourcesDispo)) { |
$presenceRessourceDemandee = true; |
} else { |
$message = "La source de données '{$source}' n'est pas référencée dans nos services.\n" |
. "Les sources disponibles sont les suivantes : " . implode(', ', $sourcesDispo) . ".\n"; |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE; |
throw new Exception($message, $code); |
} |
return $presenceRessourceDemandee; |
} |
private function recupererTableauConfig($parametres) { |
$tableau = array(); |
$tableauPartiel = explode(',', Config::get($parametres)); |
$tableauPartiel = array_map('trim', $tableauPartiel); |
foreach ($tableauPartiel as $champ) { |
if (strpos($champ, '=') === false) { |
$tableau[] = $champ; |
} else { |
list($cle, $val) = explode('=', $champ); |
$clePropre = trim($cle); |
$valeurPropre = trim($val); |
$tableau[$clePropre] = $valeurPropre; |
} |
} |
return $tableau; |
} |
private function initialiserSource() { |
$this->chargerNomSource(); |
$this->chargerConfigSource(); |
// php5.3 : Enregistrement en première position des autoload de la méthode gérant les classes des services |
if (phpversion() < 5.3) { |
spl_autoload_register(array($this, 'chargerClasseSource')); |
} else { |
spl_autoload_register(array($this, 'chargerClasseSource'), true , true); |
} |
} |
private function chargerNomSource() { |
$this->nomSource = $this->ressources[0]; |
} |
private function chargerConfigSource() { |
$source = $this->nomSource; |
$chemin = Config::get('chemin_configurations')."config_$source.ini"; |
Config::charger($chemin); |
} |
private function chargerClasseSource($classe) { |
if (class_exists($classe)) { |
return null; |
} |
$cheminBiblio = Config::get('chemin_bibliotheque'); |
$chemins = array(); |
$chemins[] = $this->squeletteDossier . $this->nomSource . DIRECTORY_SEPARATOR; |
$chemins[] = $this->squeletteDossier . 'commun' . DIRECTORY_SEPARATOR; |
$chemins[] = $cheminBiblio; |
$chemins[] = $cheminBiblio . 'robots' . DIRECTORY_SEPARATOR; |
foreach ($chemins as $chemin) { |
$chemin = $chemin . $classe . '.php'; |
if (file_exists($chemin)) { |
require_once $chemin; |
break; |
} |
} |
} |
private function avoirRessourceService() { |
$presenceRessourceService = false; |
$servicesDispo = $this->recupererTableauConfig('servicesDispo'); |
if (isset($this->ressources[1])) { |
$service = $this->ressources[1]; |
if (in_array($service, $servicesDispo)) { |
$presenceRessourceService = true; |
} else { |
$message = "La service demandé '$service' n'est pas disponible pour le projet " |
. $this->nomSource . " !\n" |
. "Les services disponibles sont : " . implode(', ', $servicesDispo); |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE; |
throw new Exception($message, $code); |
} |
} else { |
$message = "Vous n'avez pas indiqué de service pour le projet {$this->nomSource} !\n" |
. "Les services disponibles sont : " . implode(', ', $servicesDispo); |
$code = RestServeur::HTTP_CODE_MAUVAISE_REQUETE; |
throw new Exception($message, $code); |
} |
return $presenceRessourceService; |
} |
private function initialiserService() { |
$this->chargerNomDuService(); |
$classe = $this->obtenirNomClasseService($this->nomService); |
$chemins = array(); |
$chemins[] = $this->squeletteDossier . $this->nomSource . DS . $classe . '.php'; |
$chemins[] = $this->squeletteDossier . 'commun'.DS.$classe.'.php'; |
$retour = ''; |
$service = null; |
foreach ($chemins as $chemin) { |
if (file_exists($chemin)) { |
$service = new $classe($this->getBdd()); |
$ressourcesPourService = $this->filtrerRessourcesPourService(); |
$this->cache = new CacheMoissonnage($service, $this->nomSource, $this->nomService, |
Config::get('cache')); |
$retour = $this->cache->consulter($ressourcesPourService, $this->parametres); |
if (get_class($retour) == 'Exception') { |
throw new Exception($retour->getMessage(), RestServeur::HTTP_CODE_MAUVAISE_REQUETE); |
} |
} |
} |
if (is_null($service)) { |
$message = "La service demandé '{$this->nomService}' n'existe pas dans le projet {$this->nomSource} !"; |
$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE; |
throw new Exception($message, $code); |
} |
return $retour; |
} |
private function chargerNomDuService() { |
$this->nomService = $this->ressources[1]; |
} |
private function obtenirNomClasseService($mot) { |
$classeNom = str_replace(' ', '', ucwords(strtolower(str_replace('-', ' ', $mot)))); |
return $classeNom; |
} |
private function filtrerRessourcesPourService() { |
$ressourcesPourService = array(); |
$nbreDeRessources = count($this->ressources); |
for ($i = 2; $i < $nbreDeRessources; $i++) { |
$ressourcesPourService[] = $this->ressources[$i]; |
} |
return $ressourcesPourService; |
} |
} |
?> |
Property changes: |
Added: svn:executable |
+* |
\ No newline at end of property |