openModeller
Version 1.4.0
|
00001 00030 #include <openmodeller/occ_io/DelimitedTextOccurrences.hh> 00031 00032 #include <openmodeller/Occurrences.hh> // List of occurrences. 00033 00034 #include <vector> 00035 using std::vector; 00036 00037 #include <stdio.h> 00038 #include <stdlib.h> 00039 #include <string.h> 00040 00041 // Win32 defines 00042 #ifdef WIN32 00043 #include <fcntl.h> 00044 #include <io.h> 00045 #define strcasecmp _stricmp 00046 00047 #define open _open 00048 #define close _close 00049 #define fdopen _fdopen 00050 00051 #else 00052 #include <fcntl.h> 00053 #include <unistd.h> 00054 #endif 00055 00056 /*****************************************/ 00057 /*** create OccurrencesReader callback ***/ 00058 OccurrencesReader * 00059 DelimitedTextOccurrences::CreateOccurrencesReaderCallback( const char *source, const char *coordSystem ) 00060 { 00061 return new DelimitedTextOccurrences( source, coordSystem ); 00062 } 00063 00064 00065 /*******************/ 00066 /*** Constructor ***/ 00067 DelimitedTextOccurrences::DelimitedTextOccurrences( const char *source, const char *coordSystem ) 00068 { 00069 _source = (char *) source; // should be a file name 00070 00071 _coord_system = (char *) coordSystem; 00072 00073 _loaded = false; 00074 } 00075 00076 00077 /******************/ 00078 /*** Destructor ***/ 00079 DelimitedTextOccurrences::~DelimitedTextOccurrences() 00080 { 00081 } 00082 00083 00084 /***********************/ 00085 /*** load Ocurrences ***/ 00086 bool 00087 DelimitedTextOccurrences::load() 00088 { 00089 if ( _loaded ) { 00090 00091 return true; 00092 } 00093 00094 // Opens the input file. 00095 FILE *file = fopen( _source, "r" ); 00096 00097 if ( ! file ) { 00098 00099 Log::instance()->error( "Can't open file %s.\n", _source ); 00100 return false; 00101 } 00102 00103 Log::instance()->debug( "Reading occurrences from file %s.\n", _source ); 00104 00105 // Fixme: read this from file. 00106 Scalar error = -1.0; 00107 int num_attributes = 0; 00108 Scalar *attributes = 0; 00109 00110 // Columns to be read. 00111 char id[100]; 00112 char label[150]; 00113 double x, y; 00114 int abundance; 00115 00116 // Read all occurrences line by line inserting into the 00117 // appropriate object. 00118 const int line_len = 600; 00119 char line[line_len]; 00120 char *pos; 00121 00122 while ( fgets( line, line_len, file ) ) { 00123 00124 // Remove \r that DOS loves to use. 00125 pos = strchr( line, '\r' ); 00126 00127 if ( pos ) { 00128 00129 *pos = '\0'; 00130 } 00131 00132 if ( *line != '#' && sscanf( line, "%[^\t] %[^\t] %lf %lf %u", id, label, &x, &y, &abundance ) == 5 ) { 00133 00134 Coord lg = Coord( x ); 00135 Coord lt = Coord( y ); 00136 00137 _addOccurrence( id, label, lg, lt, error, (Scalar)abundance, num_attributes, attributes ); 00138 } 00139 else if ( *line != '#' && sscanf( line, "%[^\t] %[^\t] %lf %lf", id, label, &x, &y ) == 4 ) { 00140 00141 // When no abundance is provided, assume 1 (single presence) 00142 00143 Coord lg = Coord( x ); 00144 Coord lt = Coord( y ); 00145 00146 _addOccurrence( id, label, lg, lt, error, 1.0, num_attributes, attributes ); 00147 } 00148 else if ( *line != '#' && sscanf( line, "%[^,],%[^,],%lf,%lf,%u", id, label, &x, &y, &abundance ) == 5 ) { 00149 00150 Coord lg = Coord( x ); 00151 Coord lt = Coord( y ); 00152 00153 _addOccurrence( id, label, lg, lt, error, (Scalar)abundance, num_attributes, attributes ); 00154 } 00155 else if ( *line != '#' && sscanf( line, "%[^,],%[^,],%lf,%lf", id, label, &x, &y ) == 4 ) { 00156 00157 // When no abundance is provided, assume 1 (single presence) 00158 00159 Coord lg = Coord( x ); 00160 Coord lt = Coord( y ); 00161 00162 _addOccurrence( id, label, lg, lt, error, 1.0, num_attributes, attributes ); 00163 } 00164 else { 00165 00166 Log::instance()->debug( "Skipping line: %s", line ); 00167 } 00168 } 00169 00170 fclose( file ); 00171 00172 _loaded = true; 00173 00174 return true; 00175 }