openModeller  Version 1.5.0
DelimitedTextOccurrences.cpp
Go to the documentation of this file.
1 
31 
32 #include <openmodeller/Occurrences.hh> // List of occurrences.
33 
34 #include <vector>
35 using std::vector;
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 
41 // Win32 defines
42 #ifdef WIN32
43 #include <fcntl.h>
44 #include <io.h>
45 #define strcasecmp _stricmp
46 
47 #define open _open
48 #define close _close
49 #define fdopen _fdopen
50 
51 #else
52 #include <fcntl.h>
53 #include <unistd.h>
54 #endif
55 
56 /*****************************************/
57 /*** create OccurrencesReader callback ***/
59 DelimitedTextOccurrences::CreateOccurrencesReaderCallback( const char *source, const char *coordSystem )
60 {
61  return new DelimitedTextOccurrences( source, coordSystem );
62 }
63 
64 
65 /*******************/
66 /*** Constructor ***/
67 DelimitedTextOccurrences::DelimitedTextOccurrences( const char *source, const char *coordSystem )
68 {
69  _source = (char *) source; // should be a file name
70 
71  _coord_system = (char *) coordSystem;
72 
73  _loaded = false;
74 }
75 
76 
77 /******************/
78 /*** Destructor ***/
80 {
81 }
82 
83 
84 /***********************/
85 /*** load Ocurrences ***/
86 bool
88 {
89  if ( _loaded ) {
90 
91  return true;
92  }
93 
94  // Opens the input file.
95  FILE *file = fopen( _source, "r" );
96 
97  if ( ! file ) {
98 
99  Log::instance()->error( "Can't open file %s.\n", _source );
100  return false;
101  }
102 
103  Log::instance()->debug( "Reading occurrences from file %s.\n", _source );
104 
105  // Fixme: read this from file.
106  Scalar error = -1.0;
107  int num_attributes = 0;
108  Scalar *attributes = 0;
109 
110  // Columns to be read.
111  char id[100];
112  char label[150];
113  double x, y;
114  int abundance;
115 
116  // Read all occurrences line by line inserting into the
117  // appropriate object.
118  const int line_len = 600;
119  char line[line_len];
120  char *pos;
121 
122  while ( fgets( line, line_len, file ) ) {
123 
124  // Remove \r that DOS loves to use.
125  pos = strchr( line, '\r' );
126 
127  if ( pos ) {
128 
129  *pos = '\0';
130  }
131 
132  if ( *line != '#' && sscanf( line, "%[^\t] %[^\t] %lf %lf %u", id, label, &x, &y, &abundance ) == 5 ) {
133 
134  Coord lg = Coord( x );
135  Coord lt = Coord( y );
136 
137  _addOccurrence( id, label, lg, lt, error, (Scalar)abundance, num_attributes, attributes );
138  }
139  else if ( *line != '#' && sscanf( line, "%[^\t] %[^\t] %lf %lf", id, label, &x, &y ) == 4 ) {
140 
141  // When no abundance is provided, assume 1 (single presence)
142 
143  Coord lg = Coord( x );
144  Coord lt = Coord( y );
145 
146  _addOccurrence( id, label, lg, lt, error, 1.0, num_attributes, attributes );
147  }
148  else if ( *line != '#' && sscanf( line, "%[^,],%[^,],%lf,%lf,%u", id, label, &x, &y, &abundance ) == 5 ) {
149 
150  Coord lg = Coord( x );
151  Coord lt = Coord( y );
152 
153  _addOccurrence( id, label, lg, lt, error, (Scalar)abundance, num_attributes, attributes );
154  }
155  else if ( *line != '#' && sscanf( line, "%[^,],%[^,],%lf,%lf", id, label, &x, &y ) == 4 ) {
156 
157  // When no abundance is provided, assume 1 (single presence)
158 
159  Coord lg = Coord( x );
160  Coord lt = Coord( y );
161 
162  _addOccurrence( id, label, lg, lt, error, 1.0, num_attributes, attributes );
163  }
164  else {
165 
166  Log::instance()->debug( "Skipping line: %s", line );
167  }
168  }
169 
170  fclose( file );
171 
172  _loaded = true;
173 
174  return true;
175 }
DelimitedTextOccurrences(const char *source, const char *coordSystem)
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)
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
static OccurrencesReader * CreateOccurrencesReaderCallback(const char *source, const char *coordSystem)
double Coord
Type of map coordinates.
Definition: om_defs.hh:38
void debug(const char *format,...)
'Debug' level.
Definition: Log.cpp:237
static char error[256]
Definition: FileParser.cpp:42