openModeller  Version 1.5.0
TeOccurrences.cpp
Go to the documentation of this file.
1 
31 
34 
35 #include <openmodeller/Occurrences.hh> // List of occurrences.
36 #include <openmodeller/Log.hh>
37 
38 #include <TeDatabase.h>
39 #include <TeUtils.h>
40 
41 #include <vector>
42 using std::vector;
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 using std::string;
48 
49 // Win32 defines
50 #ifdef WIN32
51 #include <fcntl.h>
52 #include <io.h>
53 #define strcasecmp _stricmp
54 
55 #else
56 #include <fcntl.h>
57 #include <unistd.h>
58 #endif
59 
60 
65 TeOccurrences::CreateOccurrencesReaderCallback( const char * source, const char * coordSystem )
66 {
67  return new TeOccurrences( source, coordSystem );
68 }
69 
70 /*******************/
71 /*** Constructor ***/
72 TeOccurrences::TeOccurrences( const char * source, const char * coordSystem ):
73  _db()
74 {
75  _source = (char *) source; // Terralib string
76 
77  _coord_system = (char *) coordSystem;
78 
79  _loaded = false;
80 }
81 
82 /******************/
83 /*** Destructor ***/
85 {
86  /*if( _db )
87  {
88  _db->close();
89  delete _db;
90  }*/
91 }
92 
93 /**********************/
94 /*** add Ocurrences ***/
95 bool
97 {
98  if ( _loaded ) {
99 
100  return true;
101  }
102 
105 
106  // Parser the string.
107  if ( !_te_str_parser->parse() ) {
108 
109  Log::instance()->error( "TeOccurrences::load - Invalid TerraLib string" );
110  return false;
111  }
112 
113  // Connect to the database
115 
116  if ( !_db->isConnected() ) {
117 
118  Log::instance()->error( "TeOccurrences::load - Cannot connect to database: %s.", _db->errorMessage().c_str() );
119  //delete _db;
120  return false;
121  }
122 
123  // Get the layer
124  if ( !_db->layerExist( _te_str_parser->layerName_ ) ) {
125 
126  Log::instance()->error( "TeOccurrences::load - Cannot open layer." );
127  //delete _db;
128  return false;
129  }
130  TeLayer* layer = new TeLayer(_te_str_parser->layerName_, _db);
131 
132  // Check Species Table
133  TeTable speciesTable;
134  // Get the first table in layer, if species table is not specified.
135  if ( _te_str_parser->tableName_.length() == 0 ) {
136 
137  TeAttrTableVector attr;
138  layer->getAttrTables(attr);
139  speciesTable = attr[0];
140  }
141  else {
142 
143  // Get species table by name.
144  if ( !layer->getAttrTablesByName(_te_str_parser->tableName_, speciesTable) ) {
145 
146  Log::instance()->error( "TeOccurrences::load - Cannot open species table." );
147  //delete _db;
148  return false;
149  }
150  }
151 
152  // Get TablePoints
153  string tablePoints = layer->tableName(TePOINTS);
154 
155  // If column name is not specified, default column name is "Species".
156  if ( _te_str_parser->columnName_.length() == 0 ) {
157 
158  _te_str_parser->columnName_ = "Species";
159  }
160 
161  // Building the sql statement
162  string object_id = speciesTable.linkName();
163 
164  string sql = "select " + tablePoints + ".x, " + tablePoints + ".y, " + _te_str_parser->tableName_ + "." + _te_str_parser->columnName_ ;
165  sql += " from " + _te_str_parser->tableName_ + " inner join " + tablePoints + " on " + _te_str_parser->tableName_ + "." + object_id;
166  sql+= " = " + tablePoints +".object_id";
167 
168  // Executing the select statement
169  TeDatabasePortal* portal = _db->getPortal();
170  if ( !portal || !portal->query(sql) ) {
171 
172  Log::instance()->error( "TeOccurrences::load - Cannot execute SQL statement." );
173  delete portal;
174  portal = 0;
175  return false;
176  }
177 
178  // Fixme: read this from file.
179  Scalar error = -1.0;
180  Scalar abundance = 1.0;
181  int num_attributes = 0;
182  Scalar *attributes = 0;
183  string sp;
184  int sequence = 0;
185 
186  // Get the occurrences.
187  while ( portal->fetchRow() ) {
188 
189  ++sequence;
190  sp = portal->getData(2);
191 
192  Coord lg = Coord( portal->getDouble(0) );
193  Coord lt = Coord( portal->getDouble(1) );
194 
195  _addOccurrence( Te2String(sequence).c_str(), sp.c_str(), lg, lt, error, abundance, num_attributes, attributes );
196  }
197 
198  delete portal;
199  portal = 0;
200  delete _te_str_parser;
201 
202  _loaded = true;
203 
204  return true;
205 }
TeStringParser * _te_str_parser
double Scalar
Type of map values.
Definition: om_defs.hh:39
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
int _addOccurrence(const char *id, const char *groupId, Coord lg, Coord lt, Scalar error, Scalar abundance, int num_attributes, Scalar *attributes)
TeOccurrences(const char *source, const char *coordSystem)
TeDatabase * _db
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
string columnName_
Column Name (Species)
bool parse()
Parser the url.
TeDatabase * create(const TeDatabaseFactoryParams &params)
Return a TeDatabase pointer if it exists or create a new one.
static OccurrencesReader * CreateOccurrencesReaderCallback(const char *source, const char *coordSystem)
static TeDatabaseManager & instance()
Singleton pattern.
string str_
Terralib string.
string tableName_
Table Name (Species)
double Coord
Type of map coordinates.
Definition: om_defs.hh:38
string layerName_
Layer in Database.
static char error[256]
Definition: FileParser.cpp:42