openModeller  Version 1.4.0
TeOccurrences.cpp
Go to the documentation of this file.
00001 
00030 #include <openmodeller/occ_io/TeOccurrences.hh>
00031 
00032 #include <openmodeller/TeDatabaseManager.hh>
00033 #include <openmodeller/TeStringParser.hh>
00034 
00035 #include <openmodeller/Occurrences.hh>  // List of occurrences.
00036 #include <openmodeller/Log.hh>
00037 
00038 #include <TeDatabase.h>
00039 #include <TeUtils.h>
00040 
00041 #include <vector>
00042 using std::vector;
00043 
00044 #include <stdio.h>
00045 #include <stdlib.h>
00046 #include <string.h>
00047 using std::string;
00048 
00049 // Win32 defines
00050 #ifdef WIN32
00051 #include <fcntl.h>
00052 #include <io.h>
00053 #define strcasecmp _stricmp
00054 
00055 #else
00056 #include <fcntl.h>
00057 #include <unistd.h>
00058 #endif
00059 
00060 
00064 OccurrencesReader* 
00065 TeOccurrences::CreateOccurrencesReaderCallback( const char * source, const char * coordSystem )
00066 {
00067   return new TeOccurrences( source, coordSystem );
00068 }
00069 
00070 /*******************/
00071 /*** Constructor ***/
00072 TeOccurrences::TeOccurrences( const char * source, const char * coordSystem ): 
00073   _db()
00074 {
00075   _source = (char *) source; // Terralib string
00076 
00077   _coord_system = (char *) coordSystem;
00078 
00079   _loaded = false;
00080 }
00081 
00082 /******************/
00083 /*** Destructor ***/
00084 TeOccurrences::~TeOccurrences()
00085 {
00086   /*if( _db )
00087   {
00088     _db->close();
00089     delete _db;
00090   }*/
00091 }
00092 
00093 /**********************/
00094 /*** add Ocurrences ***/
00095 bool
00096 TeOccurrences::load()
00097 {
00098   if ( _loaded ) {
00099 
00100     return true;
00101   }
00102 
00103   _te_str_parser = new TeStringParser();
00104   _te_str_parser->str_ = _source;
00105 
00106   // Parser the string.
00107   if ( !_te_str_parser->parse() ) {
00108 
00109     Log::instance()->error( "TeOccurrences::load - Invalid TerraLib string" );
00110     return false;
00111   }
00112 
00113   // Connect to the database
00114   _db = TeDatabaseManager::instance().create( *_te_str_parser );
00115 
00116   if ( !_db->isConnected() ) {
00117 
00118     Log::instance()->error( "TeOccurrences::load - Cannot connect to database: %s.", _db->errorMessage().c_str() );
00119     //delete _db;
00120     return false;
00121   }
00122 
00123   // Get the layer
00124   if ( !_db->layerExist( _te_str_parser->layerName_ ) ) {
00125 
00126     Log::instance()->error( "TeOccurrences::load - Cannot open layer." );
00127     //delete _db;
00128     return false;
00129   }
00130   TeLayer* layer = new TeLayer(_te_str_parser->layerName_, _db);
00131 
00132   // Check Species Table
00133   TeTable speciesTable;
00134   // Get the first table in layer, if species table is not specified.
00135   if ( _te_str_parser->tableName_.length() == 0 ) {
00136 
00137     TeAttrTableVector attr;
00138     layer->getAttrTables(attr);
00139     speciesTable = attr[0];
00140   }
00141   else {
00142 
00143     // Get species table by name.
00144     if ( !layer->getAttrTablesByName(_te_str_parser->tableName_, speciesTable) ) {
00145 
00146       Log::instance()->error( "TeOccurrences::load - Cannot open species table." );
00147       //delete _db;
00148       return false;
00149     }
00150   }
00151 
00152   // Get TablePoints
00153   string tablePoints = layer->tableName(TePOINTS);
00154 
00155   // If column name is not specified, default column name is "Species".
00156   if ( _te_str_parser->columnName_.length() == 0 ) {
00157 
00158     _te_str_parser->columnName_ = "Species";
00159   }
00160 
00161   // Building the sql statement
00162   string object_id = speciesTable.linkName();
00163 
00164   string sql = "select " + tablePoints + ".x, " + tablePoints + ".y, " + _te_str_parser->tableName_ + "." + _te_str_parser->columnName_ ;
00165   sql += " from " + _te_str_parser->tableName_ + " inner join " + tablePoints + " on " + _te_str_parser->tableName_ + "." + object_id;
00166   sql+= " = " + tablePoints +".object_id";
00167 
00168   // Executing the select statement
00169   TeDatabasePortal* portal = _db->getPortal();
00170   if ( !portal || !portal->query(sql) ) {
00171 
00172     Log::instance()->error( "TeOccurrences::load - Cannot execute SQL statement." );
00173     delete portal;
00174     portal = 0;
00175     return false;
00176   }
00177 
00178   // Fixme: read this from file.
00179   Scalar error       = -1.0;
00180   Scalar abundance   = 1.0;
00181   int num_attributes = 0;
00182   Scalar *attributes = 0;
00183   string sp;
00184   int sequence = 0;
00185 
00186   // Get the occurrences.
00187   while ( portal->fetchRow() ) {
00188 
00189     ++sequence;
00190     sp = portal->getData(2);
00191 
00192     Coord lg = Coord( portal->getDouble(0) );
00193     Coord lt = Coord( portal->getDouble(1) );
00194 
00195     _addOccurrence( Te2String(sequence).c_str(), sp.c_str(), lg, lt, error, abundance, num_attributes, attributes );
00196   }
00197 
00198   delete portal;
00199   portal = 0;
00200   delete _te_str_parser;
00201 
00202   _loaded = true;
00203 
00204   return true;
00205 }