Subversion Repositories eFlore/Applications.moissonnage

Compare Revisions

Ignore whitespace Rev 26 → Rev 31

/trunk/services/bibliotheque/Maillage.php
10,7 → 10,7
private $indexLatitude;
private $mailles;
private $bdd = null;
private $bdd = null;
66,6 → 66,7
") ORDER BY axe, position";
$indexMailles = $this->getBdd()->recupererTous($requete);
foreach ($indexMailles as $index) {
if ($index['axe'] == 'lng') {
$this->indexLongitude[$index['position']] = array($index['debut'], $index['fin']);
89,7 → 90,7
public function ajouterPoints(& $points) {
foreach ($points as $index => $point) {
foreach ($points as $point) {
list($longitude, $latitude) = $this->obtenirCoordonneesPoint($point);
list($indexLongitude, $indexLatitude) = $this->rechercherIndexMaille($longitude, $latitude);
$this->mailles[$indexLatitude][$indexLongitude]->ajouterPoint($point);
96,6 → 97,15
}
}
public function ajouterMailles(& $mailles) {
foreach ($mailles as $maille) {
$longitude = ($maille->longitudeOuest + $maille->longitudeEst) / 2;
$latitude = ($maille->latitudeSud + $maille->latitudeNord) / 2;
list($indexLongitude, $indexLatitude) = $this->rechercherIndexMaille($longitude, $latitude);
$this->mailles[$indexLatitude][$indexLongitude]->combinerMailles($maille);
}
}
private function obtenirCoordonneesPoint($point) {
$longitude = 0;
$latitude = 0;
123,107 → 133,31
return array($indexLongitude, $indexLatitude);
}
 
public function formaterSortie() {
public function formaterSortie($toutesLesMailles = false) {
$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 formaterPourInsertionBdd() {
$mailles_resume = array();
foreach ($this->mailles as $ligne) {
foreach ($ligne as $maille) {
if ($maille->getNombrePoints() > 0) {
if ($nombrePoints > 0 || $toutesLesMailles) {
$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()
'zoom' => $this->zoom,
'sud' => $maille->getLatitudeSud(),
'ouest' => $maille->getLongitudeOuest(),
'nord' => $maille->getLatitudeNord(),
'est' => $maille->getLongitudeEst(),
'points' => $nombrePoints,
'observations' => $maille->getNombreObservations(),
'type_site' => 'MAILLE'
);
}
}
}
if (count($mailles_resume[0]) == 0)
if (count($mailles_resume) == 0 || count($mailles_resume[0]) == 0)
return array();
return $mailles_resume;
}
 
public function zoomer() {
$this->zoom += 1;
}
// redecoupage des mailles en 4 (fonction quadtree)
// TODO : revoir le fonctionnement de cette methode (pour utilisation par les scripts uniquement)
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/bibliotheque/VerificateurParametres.php
22,6 → 22,7
* On va verifier la disponibilite du referentiel pour ce service
*
* - num_taxon : numero taxonomique d'une espece
* - nn : numero nomenclatural d'une espece
* On va rechercher sa presence dans les referentiels disponibles
* (les informations principales sur ce taxon seront renvoyees)
*
30,6 → 31,13
*
* - auteur : l'auteur de l'observation
*
* - type_site : pour les observations, le type de point correspondant a la station cliquee
* (STATION ou COMMUNE). Cela indique le niveau de precision ramene aux coordonnees des observations
*
* - date_debut et date_fin : intervalle de dates d'observation. On verifie que ce sont des dates valides
* et que date_debut <= date_fin. Le programme peut accepter la presence d'un seul de ces deux
* parametres : soit une date de debut, soit une date de fin
*
* La fonction principale de verification des parametres va parcourir tous les parametres, et verifier
* pour chacun d'eux la validite de leurs valeurs. Dans le cas ou une valeur anormale est detectee
* ou qu'elle se trouve en dehors de l'intervalle des resultats acceptes, une liste de messages d'erreur
55,6 → 63,7
private $parametres = array();
private $validation = null;
private $erreurs = array();
private $dateTraitee = false;
private $bboxMonde = null;
93,6 → 102,22
break;
case 'num_taxon' : $this->traiterParametreTaxon($valeur);
break;
case 'nn' : $this->traiterParametreNomenclatural($valeur);
break;
case 'type_site' : $this->traiterParametreTypeSite($valeur);
break;
case 'nb_jours' : $this->traiterParametreNbJours($valeur);
break;
case 'date_debut' :
case 'date_fin' : {
$dateDebut = isset($this->parametres['date_debut']) ? $this->parametres['date_debut'] : null;
$dateFin = isset($this->parametres['date_fin']) ? $this->parametres['date_fin'] : null;
if (!$this->dateTraitee) {
$this->traiterParametresDate($dateDebut, $dateFin);
$this->dateTraitee = true;
}
break;
}
// autres parametres ==> les ignorer
default : break;
}
207,7 → 232,9
private function estUnCodeDepartementValide($departement) {
// expression reguliere pour verifier que c'est un nombre entier a 2 ou 3 chiffres
$estUnDepartementValide = true;
if (preg_match('/^\d{2,3}$/', $departement) != 1) {
if ($departement == '2A' || $departement == '2B') {
$estUnDepartementValide = true;
} elseif (preg_match('/^\d{2,3}$/', $departement) != 1) {
$estUnDepartementValide = false;
} else {
if ((intval($departement) < 1 || intval($departement) > 95)
214,7 → 241,7
&& (intval($departement) < 971 || intval($departement) > 978)) {
$estUnDepartementValide = false;
}
}
}
return $estUnDepartementValide;
}
224,6 → 251,10
}
}
private function traiterParametreTypeSite($typeSite) {
$this->validation->typeSite = strtolower(trim($typeSite));
}
private function traiterParametreReferentiel($referentiel) {
if (!isset($this->validation->referentiel) && $referentiel != '*') {
$referentielAUtiliser = $this->affecterNomCompletReferentiel($referentiel);
251,29 → 282,51
private function traiterParametreTaxon($numeroTaxon) {
if ($numeroTaxon != '*') {
$taxon = null;
if (isset($this->validation->referentiel)) {
$taxon = $this->renvoyerTaxonDansReferentiel($numeroTaxon, $this->validation->referentiel);
} else {
// parcours de tous les referentiels jusqu'a retrouver le taxon dans sa liste
$taxon = $this->rechercherTaxonDansReferentiels($numeroTaxon);
$listeTaxons = explode(',', $numeroTaxon);
foreach ($listeTaxons as $nt) {
$taxon = null;
if (isset($this->validation->referentiel)) {
$taxon = $this->renvoyerTaxonDansReferentiel($nt, 'nt', $this->validation->referentiel);
} else {
$taxon = $this->rechercherTaxonDansReferentiels($nt, 'nt');
}
if (is_null($taxon)) {
$message = "Le numéro de taxon n'a pas permis de retrouver le taxon associé.";
$this->ajouterErreur($message);
} else {
$this->ajouterTaxonAListe($taxon);
$this->validation->referentiel = $taxon['referentiel'];
}
}
if (is_null($taxon)) {
$message = "Le numéro de taxon n'a pas permis de retrouver le taxon associé.";
$this->ajouterErreur($message);
} else {
$this->validation->taxon = $taxon;
$this->validation->referentiel = $taxon['referentiel'];
}
}
private function traiterParametreNomenclatural($numeroNomenclatural) {
if ($numeroNomenclatural != '*') {
$listeTaxons = explode(',', $numeroNomenclatural);
foreach ($listeTaxons as $nn) {
$taxon = null;
if (isset($this->validation->referentiel)) {
$taxon = $this->renvoyerTaxonDansReferentiel($nn, 'nn', $this->validation->referentiel);
} else {
$taxon = $this->rechercherTaxonDansReferentiels($nn, 'nn');
}
if (is_null($taxon)) {
$message = "Le numéro nomenclatural n'a pas permis de retrouver le taxon associé.";
$this->ajouterErreur($message);
} else {
$this->ajouterTaxonAListe($taxon);
$this->validation->referentiel = $taxon['referentiel'];
}
}
}
}
private function rechercherTaxonDansReferentiels($numeroTaxon) {
private function rechercherTaxonDansReferentiels($numeroTaxon, $typeNumero) {
$taxon = null;
$listeReferentiels = Referentiel::recupererListeReferentielsDisponibles();
foreach ($listeReferentiels as $nomReferentiel) {
$taxon = $this->renvoyerTaxonDansReferentiel($numeroTaxon, $nomReferentiel);
$taxon = $this->renvoyerTaxonDansReferentiel($numeroTaxon, $typeNumero, $nomReferentiel);
if (!is_null($taxon)) {
break;
}
281,13 → 334,69
return $taxon;
}
private function renvoyerTaxonDansReferentiel($numeroTaxon, $nomReferentiel) {
private function renvoyerTaxonDansReferentiel($numeroTaxon, $typeNumero, $nomReferentiel) {
$referentiel = new Referentiel($nomReferentiel);
$referentiel->chargerTaxon($numeroTaxon);
$referentiel->chargerTaxon($typeNumero, $numeroTaxon);
$taxon = $referentiel->renvoyerTaxon();
return $taxon;
}
private function ajouterTaxonAListe($taxon) {
if (!isset($this->validation->taxon)) {
$this->validation->taxon = array($taxon);
} elseif (!in_array($taxon, $this->validation->taxon)) {
$this->validation->taxon[] = $taxon;
}
}
private function traiterParametresDate($dateDebut, $dateFin) {
$statutValidite = $this->verifierValiditeDate($dateDebut, 'début')
+ $this->verifierValiditeDate($dateFin, 'fin');
if ($statutValidite == 2) {
if (!is_null($dateDebut) && !is_null($dateFin) && $dateDebut > $dateFin) {
$message = "Intervalle de dates incorrect : date de fin avant la date de début";
$this->ajouterErreur($message);
} else {
$this->validation->dateDebut = $dateDebut;
$this->validation->dateFin = $dateFin;
}
}
}
private function verifierValiditeDate(& $date, $position) {
$statutValidite = is_null($date) ? 1 : 0;
$moisParDefaut = $position == 'début' ? 1 : 12;
$jourParDefaut = $position == 'début' ? 1 : 31;
if (!is_null($date)) {
$split = explode("-", $date);
$annee = intval($split[0]);
$mois = isset($split[1]) ? intval($split[1]) : $moisParDefaut;
$jour = isset($split[2]) ? intval($split[2]) : $jourParDefaut;
if (!$this->estDateValide($jour, $mois, $annee)) {
$message = "Date de {$position} saisie non valide";
$this->ajouterErreur($message);
} else {
$date = $annee."-".(($mois<10)?"0".$mois:$mois)."-".(($jour<10)?"0".$jour:$jour);
$dateDuJour = date('Y-m-d');
if ($date > $dateDuJour) {
$message = "La date de {$position} saisie postérieure à la date du jour : {$dateDuJour}";
$this->ajouterErreur($message);
} else {
$statutValidite = 1;
}
}
}
return $statutValidite;
}
private function estDateValide($jour, $mois, $annee) {
$estBissextile = (($annee % 4 == 0 && $annee % 100 != 0) || ($annee % 400 == 0));
$dureeFevrier = $estBissextile ? 29 : 28;
$dureeMois = array(1 => 31, 2 => $dureeFevrier, 3 => 31, 4 => 30, 5 => 31, 6 => 30, 7 => 31,
8 => 31, 9 => 30, 10 => 31, 11 => 30, 12 => 31);
return ($annee >= 1900 && $mois >= 1 && $mois <= 12 && $jour >= 1 && $jour <= $dureeMois[$mois]);
}
private function verifierPresenceParametreSpatial() {
$presenceParametreSpatial = false;
if (isset($this->parametres['bbox'])
300,6 → 409,16
}
}
private function traiterParametreNbJours($nbJours) {
// verifier que c'est un nombre entier positif (avec expression reguliere)
if (preg_match('/^\d+$/', $nbJours) != 1) {
$message = "La valeur passée pour le nombre de jours doit être un entier positif.";
$this->ajouterErreur($message);
} elseif ($nbJours > 0) {
$this->validation->nbJours = intval($nbJours);
}
}
}
 
?>
/trunk/services/bibliotheque/FormateurJson.php
5,7 → 5,7
private $sourceDonnees;
public function __construct($source) {
public function __construct($source = '') {
$this->sourceDonnees = $source;
}
14,23 → 14,24
$objetJSON = new StdClass();
$objetJSON->type = "FeatureCollection";
$objetJSON->stats = new StdClass();
$objetJSON->stats->communes = 0;
$objetJSON->stats->stations = 0;
$objetJSON->stats->source = $this->sourceDonnees;
$objetJSON->stats->formeDonnees = '';
if (count($stations) > 0) {
$objetJSON->stats->formeDonnees = ($stations[0]['type_site'] == 'MAILLE') ? 'maille' : 'point';
}
$objetJSON->stats->sites = 0;
$objetJSON->stats->observations = 0;
$objetJSON->features = array();
foreach ($stations as $station) {
$stationJSON = NULL;
// construction d'un objet feature adapte a la structure des donnees spatiales
if ($station['type_site'] == 'MAILLE') {
$stationJSON = $this->formaterMaille($station);
$objetJSON->stats->sites += $station['points'];
} else {
if ($station['type_site'] == 'STATION') {
$objetJSON->stats->communes ++;
} else {
$objetJSON->stats->stations ++;
}
$objetJSON->stats->sites ++;
$stationJSON = $this->formaterPoint($station);
}
$objetJSON->stats->observations += $station['observations'];
if (!is_null($stationJSON)) {
$objetJSON->features[] = $stationJSON;
}
44,6 → 45,7
$json->geometry = new StdClass();
$json->properties = new StdClass();
$json->geometry->type = "Point";
$json->properties->source = $this->sourceDonnees;
$json->properties->typeSite = $station['type_site'];
if ($this->sourceDonnees == 'floradata' && $station['type_site'] == 'COMMUNE') {
$json->geometry->coordinates = array($station['lat_commune'], $station['lng_commune']);
54,7 → 56,6
if ($this->sourceDonnees == 'floradata') {
$json->properties->nom = $this->construireNomStationFloradata($station);
} else {
$station['code_insee'] = '';
$codeDepartement = $this->extraireCodeDepartement($station['code_insee']);
$json->properties->nom = trim($station['nom'])." ({$codeDepartement})";
}
71,7 → 72,7
if (strlen($nom) == 0) {
$nom = 'station sans nom, '.trim($station['zone_geo']);
}
$nom .= " ({$codeDepartement})";;
$nom .= " ({$codeDepartement})";
}
return $nom;
}
87,9 → 88,6
private function formaterMaille($maille) {
if ($maille['points'] == 0) {
return null;
}
$json = new StdClass();
$json->type = "Feature";
$json->geometry = new StdClass();
102,7 → 100,8
array(floatval($maille['sud']), floatval($maille['ouest']))
);
$json->properties = new StdClass();
$json->properties->typeSite = $maille['type_site'];
$json->properties->source = $this->sourceDonnees;
$json->properties->typeSite = 'MAILLE';
$json->properties->nombrePoints = $maille['points'];
return $json;
}
136,6 → 135,20
return $url;
}
public function formaterMaillesVides($mailles) {
$objetJSON = new StdClass();
$objetJSON->type = "FeatureCollection";
$objetJSON->stats = new StdClass();
$objetJSON->stats->source = '';
$objetJSON->stats->formeDonnees = 'maille';
$objetJSON->stats->sites = 0;
$objetJSON->features = array();
foreach ($mailles as $maille) {
$objetJSON->features[] = $this->formaterMaille($maille);
}
return $objetJSON;
}
}
 
?>
/trunk/services/bibliotheque/Maille.php
10,7 → 10,9
private $indexLongitude;
private $points = array();
private $nombrePoints = 0;
private $nombrePoints;
private $observations = array();
private $nombreObservations;
public function __construct($sud, $ouest, $nord, $est, $indexLat, $indexLng) {
22,9 → 24,11
$this->indexLongitude = $indexLng;
}
public function ajouterPoint(& $point) {
public function ajouterPoint($point) {
$this->points[] = $point;
$this->nombrePoints ++;
$this->observations[] = $point['observations'];
$this->nombreObservations += $point['observations'];
}
public function getLatitudeNord() {
51,18 → 55,31
return $this->indexLongitude;
}
public function getPoints() {
return $this->points;
}
public function getNombrePoints() {
return $this->nombrePoints;
}
public function getPoint($index = 0) {
return (!isset($this->points[$index])) ? NULL : $this->points[$index];
public function getObservations() {
return $this->observations;
}
public function getNombreObservations() {
return $this->nombreObservations;
}
 
public function totalNonNul() {
return !is_null($this->nombrePoints);
return count($this->points) > 0;
}
public function combinerMailles(& $maille) {
$this->nombrePoints += $maille->nombre_sites;
$this->nombreObservations += $maille->nombre_observations;
}
}
 
?>
/trunk/services/bibliotheque/Referentiel.php
31,24 → 31,19
*
*/
 
define("RANG_FAMILLE", 180);
 
 
 
class Referentiel {
private $nomComplet;
private $nomCourt;
private $taxon;
private $bdd = null;
private $nomsChampsBdtfx = array('nn' => 'num_nom_retenu', 'nt' => 'num_taxonomique', 'ns' => 'nom_sci');
private $nomsChampsBdtxa = array('nn' => 'num_nom_retenu', 'nt' => 'num_tax', 'ns' => 'nom_sci');
private $champs;
public function __construct($nomReferentiel, $taxon = null) {
$this->nomComplet = $nomReferentiel;
$this->nomCourt = current(explode('_', $nomReferentiel));
58,9 → 53,7
}
$this->taxon = $taxon;
}
public function renvoyerNomCompletReferentiel() {
return $this->nomComplet;
}
69,15 → 62,16
return $this->taxon;
}
public function chargerTaxon($numTaxon) {
public function chargerTaxon($typeNumero, $numTaxon) {
$taxon = null;
// verifier que le numero de taxon soit bien une chaine de caracteres composee uniquement
// que de nombres entiers avant de construire la requete SQL a executer
if (preg_match('/^\d+$/', $numTaxon) == 1) {
$champWhere = $typeNumero == 'nt' ? $this->champs['nt']: 'num_nom';
$requete =
"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet." WHERE ".
$this->champs['nt']."={$numTaxon} AND num_nom=".$this->champs['nn']." ".
$champWhere."={$numTaxon} AND num_nom=".$this->champs['nn']." ".
"ORDER BY rang, If(num_nom=".$this->champs['nn'].", 0, 1) LIMIT 0, 1";
$taxon = $this->getBdd()->recuperer($requete);
if ($taxon == false) {
89,10 → 83,21
$this->taxon = $taxon;
}
private function getBdd() {
if (is_null($this->bdd)) {
$this->bdd = new Bdd();
}
$nomBdd = Config::get('bdd_eflore');
if (is_null($nomBdd)) {
$nomBdd = Config::get('bdd_nom');
}
$this->bdd->requeter("USE ".$nomBdd);
return $this->bdd;
}
public function recupererSousTaxons($listeNn = null) {
public function recupererTaxonsSousEspeces($listeNn = null) {
$sousTaxons = array();
if (!is_null($this->taxon) && $this->taxon['rang'] >= RANG_FAMILLE) {
if (!is_null($this->taxon) && $this->taxon['rang'] == Config::get('rang.espece')) {
if (is_null($listeNn)) {
$listeNn = $this->taxon['nn'];
}
109,28 → 114,24
}
// recherche de sous taxons au niveau inferieur (parcours recursif de la methode)
if (count($resultats) > 0) {
$sousTaxons = array_merge($sousTaxons, $this->recupererSousTaxons($nouvelleListeNn));
$sousTaxons = array_merge($sousTaxons, $this->recupererTaxonsSousEspeces($nouvelleListeNn));
}
}
return $sousTaxons;
}
private function getBdd() {
if (is_null($this->bdd)) {
$this->bdd = new Bdd();
public function recupererTaxonsFamilles() {
$sousTaxons = array();
if (!is_null($this->taxon) && $this->taxon['rang'] == Config::get('rang.genre')) {
$requete =
"SELECT ".$this->champs['nn']." AS nn, ".$this->champs['nt']." AS nt, ".
$this->champs['ns']." AS nom, rang FROM ".$this->nomComplet." WHERE ".
"num_tax_sup=".$this->taxon['nn']." AND num_nom=".$this->champs['nn'];
$sousTaxons = $this->getBdd()->recupererTous($requete);
}
$nomBdd = Config::get('bdd_eflore');
if (is_null($nomBdd)) {
$nomBdd = Config::get('bdd_nom');
}
$this->bdd->requeter("USE ".$nomBdd);
return $this->bdd;
return $sousTaxons;
}
public static function recupererListeReferentielsDisponibles() {
$tableau = array();
$tableauPartiel = explode(',', Config::get('referentiels_dispo'));