Subversion Repositories eFlore/Projets.eflore-projets

Compare Revisions

Ignore whitespace Rev 459 → Rev 460

/trunk/scripts/modules/sophy/Insertionflore.php
New file
0,0 → 1,295
<?php
// Encodage : UTF-8
// +-------------------------------------------------------------------------------------------------------------------+
/**
* Traitement des fichiers de la banque de données SOPHY pour insertion
*
* Description : classe permettant d'insérer les flores nécessaires à l'étude des données issues de SOPHY.
* Avant d'utiliser cette classe nettoyer les fichiers pour ne garder que les lignes à insérer.
* Utilisation : php script.php insertionFlore -a test
*
* @category PHP 5.3
* @package phytosocio
//Auteur original :
* @author Delphine CAUQUIL <delphine@tela-botanica.org>
* @copyright Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
* @license http://www.gnu.org/licenses/gpl.html Licence GNU-GPL-v3
* @license http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL-v2
* @version $Id$
*/
// +-------------------------------------------------------------------------------------------------------------------+
class Insertionflore extends Script {
protected $dao;
protected $dossier;
protected $flore;
protected $parametres_autorises = array(
'-n' => array(true, true, 'Nom du fichier ou du dossier à traiter'));
public function executer() {
include_once dirname(__FILE__).'/bibliotheque/FloreDao.php';
Config::charger(dirname(__FILE__).'/sophy.ini');
$this->dossier = Config::get('dossierDonneesSophy').'FLORE/';
$this->dao = new FloreDao();
// Récupération de paramètres
// Lancement de l'action demandée
$cmd = $this->getParametre('a');
switch ($cmd) {
case 'florenbi' : // fournier
$this->executerFlorenbi();
break;
case 'bdnff' : // bdnff
$this->executerBdnff();
break;
case 'listepla' : // fournier/bdnff
$this->executerListePla();
break;
case 'floeur' : // flora europea
$this->executerFloeur();
break;
case 'bryo' : // bryophyte
$this->executerBryo();
break;
case 'syntri' : //syntri : numéro complémentaire
$this->executerSyntri();
break;
case 'ciff' : // CIFF CIFF/BDNFF
$this->executerCiff();
break;
case 'donneebb' : // Num_nom num_tax bdnff
$this->executerDonneebb();
break;
case 'codefr' : // CODEFR94
$this->executerCodefr();
break;
default :
$this->traiterErreur('Erreur : la commande "%s" n\'existe pas!', array($cmd));
}
}
 
// +-------------------------------------------------------------------------------------------------------------------+
// Traitement du fichier flore florenbi qui contient tous codes, numéro et nom fournier
// /opt/lampp/bin/php cli.php sophy/insertionflore -a florenbi -n ./../donnees/sophy/2010-12-02/FLORE/FLORENBI
private function executerFlorenbi() {
$nomFichier = $this->dossier.'FLORENBI';
if (file_exists($nomFichier) === true) {
if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
$nom = '';
while ($ligne = fgets($fichierOuvert)) {
if (preg_match('/^\s{2}[\d\s]{4}\s([\d\s]{3}\d)\s{2}([\d\s]\d\.\d\d\.\d)\s[\d\s]{5}[A-Z\s]\s(\d)\s*(.+)$/', $ligne, $champs)) {
// si le rang taxonomique est inf à 4 (subsp et var)
if ($champs[3] < 4) {
$flore[$champs[1]] = $champs[4];
$nom = trim($champs[4]);
} else {
$flore[$champs[1]] = $nom." ".$champs[4];
}
}
}
$info = $this->dao->integrerFlore($flore, 'fournier');
$this->traiterErreur($info);
}
fclose($fichierOuvert);
} else {
$this->traiterErreur("Le fichier {$fichier} n'existe pas.");
}
}
// +-------------------------------------------------------------------------------------------------------------------+
// Traitement du fichier flore florenbi qui contient tous codes, numéro et nom fournier
// /opt/lampp/bin/php cli.php sophy/insertionflore -a florenbi -n ./../donnees/sophy/2010-12-02/FLORE/FLORENBI
private function executerBdnff() {
$nomFichier = $this->dossier.'bdnffv5.csv';
if (file_exists($nomFichier) === true) {
$this->dao->chargerDonnees($nomFichier, 'sophy_bdnff');
} else {
$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
}
}
// +-------------------------------------------------------------------------------------------------------------------+
// Traitement du fichier flore listePla qui contient numéro de fournier, nom de fournier, numéro bdnff, nom bdnff
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a listepla -n ./../doc/jeux_test/FLORE/LISTEPLA.csv
private function executerListePla() {
// Parcours le fichier .csv et enregistre chaque ligne dans un tableau.
$nomFichier = $this->dossier.'LISTEPLA.csv';
if ($nomFichier && file_exists($nomFichier) ){
$extensionFichier = strtolower(strrchr($nomFichier, '.'));
if ($extensionFichier === ".csv"){
$file = new SplFileObject($nomFichier);
$file->setFlags(SplFileObject::SKIP_EMPTY);
$i = 0;
echo "Traitement de LISTEPLA : ";
while (!$file->eof()){
$flore = $this->transformerFournierBdnff($file->fgetcsv());
echo str_repeat(chr(8), ( strlen( $i ) + 1 ))."\t".$i++;
}
echo "\n";
//$info = $this->dao->integrerFlore($this->flore['bdnff'], 'bdnff');
$info .= $this->dao->integrerFlore($this->flore['correspondance'], 'fournier_bdnff');
$this->traiterErreur($info);
} else {
$this->traiterErreur("Le fichier $nomFichier n'est pas au format csv.");
}
} else {
$this->traiterErreur("Le fichier $nomFichier n'existe pas.");
}
}
private function transformerFournierBdnff($ligne_csv){
//$this->flore['bdnff'][$ligne_csv[5]] = $ligne_csv[6];
$this->flore['correspondance'][$ligne_csv[3]] = $ligne_csv[5];
}
// +-------------------------------------------------------------------------------------------------------------------+
// Traitement du fichier flore floeur.bis qui contient numéro et nom de flora europea
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a floeur -n ./../doc/jeux_test/FLORE/FLOEUR.BIS
private function executerFloeur() {
$nomFichier = $this->dossier.'FLOEUR.BIS';
if (file_exists($nomFichier) === true) {
if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
$i = 0;
while ($ligne = fgets($fichierOuvert)) {
if (preg_match('/^\s[\d\s]{9}[\d\sA-Z]\s([\d\s]{4}\d)\s(.{71})[\s\d]{2}/', $ligne, $champs)) {
$this->flore[$champs[1]] = trim($champs[2]);
}
}
$info = $this->dao->integrerFlore($this->flore, 'flora_europea');
$this->traiterErreur($info);
}
fclose($fichierOuvert);
} else {
$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
}
}
 
// +-------------------------------------------------------------------------------------------------------------------+
// Traitement du fichier flore codebry qui contient numéro et nom des bryophytes
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a bryo -n ./../doc/jeux_test/FLORE/codebry.txt
private function executerBryo() {
$nomFichier = $this->dossier.'codebry.txt';
if (file_exists($nomFichier) === true) {
if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
$i = 0;
while ($ligne = fgets($fichierOuvert)) {
if (preg_match('/^\s([\d\s]{4})\s[\d\s][\d\s]{4}\s{2}\d\s*(.*)\s[\d\s]{4}/', $ligne, $champs)) {
if ($champs[1] != 0) {
$this->flore[$champs[1]] = trim($champs[2]);
}
}
}
$info = $this->dao->integrerFlore($this->flore, 'bryophyte');
$this->traiterErreur($info);
}
fclose($fichierOuvert);
} else {
$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
}
}
// +-------------------------------------------------------------------------------------------------------------------+
// Traitement du fichier flore Syntri qui contient numéro de fournier, numéro syntri, nom syntri
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a syntri -n ./../doc/jeux_test/FLORE/SYNTRI.TXT
private function executerSyntri() {
$nomFichier = $this->dossier.'SYNTRI.TXT';
if (file_exists($nomFichier) === true) {
if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
$i = 0;
while ($ligne = fgets($fichierOuvert)) {
if (preg_match('/^(\d+)\s{2}\d\s([A-Z,\-\'\.()\s]+[A-Z,\-\'\.()])\s+([\d\s]+)$/', trim($ligne), $champs)) {
$syntri = preg_split('/\s+/', $champs[3]);
if (count($syntri) == 3) {
$num = $syntri[1];
} elseif (count($syntri) == 1){
$num = $syntri[0];
}
if (isset($flore['correspondance'][$num])) {
$flore['syntri'][$num] = $flore['syntri'][$num].$champs[2];
} else {
$flore['correspondance'][$num] = $champs[1];
$flore['syntri'][$num] = $champs[2];
}
$i++;
}
}
$info = $this->dao->integrerFlore($flore['syntri'], 'syntri');
$info .= $this->dao->integrerFlore($flore['correspondance'], 'syntri_fournier');
$this->traiterErreur($info);
}
fclose($fichierOuvert);
} else {
$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
}
}
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a ciff -n ./../doc/jeux_test/FLORE/ciff.txt
private function executerCiff() {
$nomFichier = $this->dossier.'ciff.txt';
if (file_exists($nomFichier) === true) {
if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
$i = 0;
while ($ligne = fgets($fichierOuvert)) {
if (preg_match('/^(\d*)\t([A-Za-z].*)\t(\d*)\t\d*/', $ligne, $champs)) {
$flore['ciff'][$champs[1]] = $champs[2];
if ($champs[3] != '') {
$flore['correspondance'][$champs[1]] = $champs[3];
}
}
}
$info = $this->dao->integrerFlore($flore['ciff'], 'ciff');
$info .= $this->dao->integrerFlore($flore['correspondance'], 'ciff_bdnff');
$this->traiterErreur($info);
}
fclose($fichierOuvert);
} else {
$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
}
}
// +-------------------------------------------------------------------------------------------------------------------+
// Traitement du fichier flore donneebb.tri qui contient le numero tax t numero nomenclatural de la bdnff
// num_tax||num_nom||num_nom_retenu||?||nom
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a donneebb -n ./../doc/jeux_test/FLORE/donneebb.tri
private function executerDonneebb() {
$nomFichier = $this->dossier.'donneebb.tri';
if (file_exists($nomFichier) === true) {
if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
while ($ligne = fgets($fichierOuvert)) {
if (preg_match('/^([\d\s]{4}\d)\|([\d\s]{5})\|([\d\s]{5})\|/', $ligne, $champs)) {
if (!isset($this->flore[$champs[1]])) {
$this->flore[$champs[2]]['num_tax'] = $champs[1];
if (trim($champs[3]) != '') {
$this->flore[$champs[2]]['num_nom_retenu'] = $champs[3];
} else {
$this->flore[$champs[2]]['num_nom_retenu'] = $champs[2];
}
}
}
}
$info = $this->dao->ajouterColonnes($this->flore, 'sophy_bdnff');
$this->traiterErreur($info);
}
fclose($fichierOuvert);
} else {
$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
}
}
// +-------------------------------------------------------------------------------------------------------------------+
// Traitement du fichier flore codefr94 qui contient numéro et nom codefr94
// /opt/lampp/bin/php -d memory_limit=2048M cli.php insertionFlore -a ciff -n ./../doc/jeux_test/FLORE/CODEFR94
private function executerCodefr() {
$nomFichier = $this->dossier.'CODEFR94';
if (file_exists($nomFichier) === true) {
if ( $fichierOuvert = fopen($nomFichier, 'r') ) {
$i = 0;
while ($ligne = fgets($fichierOuvert)) {
if (preg_match('/^([\d\s]{5})[\d\s]{7}(.*)/', $ligne, $champs)) {
$flore[$champs[1]] = trim($champs[2]);
}
}
$info = $this->dao->integrerFlore($flore, 'codefr94');
$this->traiterErreur($info);
}
fclose($fichierOuvert);
} else {
$this->traiterErreur("Le fichier {$nomFichier} n'existe pas.");
}
}
}