openModeller
Version 1.4.0
|
00001 00028 #include <openmodeller/om.hh> 00029 #include <openmodeller/FileParser.hh> 00030 00031 #include <openmodeller/env_io/GeoTransform.hh> 00032 #include <openmodeller/env_io/Raster.hh> 00033 #include <openmodeller/Occurrence.hh> 00034 00035 #include "graph/graphic.hh" 00036 00037 #include <string.h> 00038 #include <stdio.h> 00039 #include <stdlib.h> 00040 00041 #include <stdexcept> 00042 00043 #include <string> 00044 #include <vector> 00045 using std::string; 00046 using std::vector; 00047 00048 float _zoom; 00049 int _redraw = 1; 00050 int _last_draw = 0; 00051 00052 GColor _bg( 0, 140, 150 ); 00053 00054 int _nmap; 00055 Map **_maps; 00056 00057 OccurrencesPtr _occurs; 00058 00059 GImage *_cnv; 00060 GImage *_pm; 00061 GGraph *_graph; 00062 00063 00064 void draw(); 00065 void draw_map( GGraph *graph, Map *map ); 00066 void draw_occur( GGraph *graph, Map *map, const OccurrencesPtr& ); 00067 void findRegion( int nmap, Map **map, Coord *xmin, Coord *ymin, Coord *xmax, Coord *ymax ); 00068 OccurrencesPtr readOccurrences( char const *source, char const *name, char const *coord_system ); 00069 00070 00071 /**************************************************************/ 00072 /**************************** main ****************************/ 00073 00074 int 00075 main( int argc, char **argv ) 00076 { 00077 // Reconfigure the global logger. 00078 Log::instance()->setLevel( Log::Info ); 00079 Log::instance()->setPrefix( "" ); 00080 00081 if ( argc < 2 ) { 00082 00083 Log::instance()->info( "\n%s [-r] <request>\n\n", argv[0] ); 00084 exit( 1 ); 00085 } 00086 00087 Log::instance()->info( "\nopenModeller Viewer - CRIA\n" ); 00088 00089 int show_map = strcmp( argv[1], "-r" ); 00090 char *request = (show_map ? argv[1] : argv[2]); 00091 00092 try { 00093 00094 FileParser fp( request ); 00095 00096 if ( show_map ) { 00097 00098 // Maps to be shown. 00099 _nmap = fp.count( "Map" ); 00100 00101 if ( ! _nmap ) { 00102 00103 Log::instance()->error( "No map to be shown!?!\n" ); 00104 exit(1); 00105 } 00106 00107 _maps = new Map * [_nmap]; 00108 00109 vector<string> mapfile = fp.getAll( "Map" ); 00110 00111 for ( int i = 0; i < _nmap; i++ ) { 00112 00113 // Generate a raster using map "i". 00114 _maps[i] = new Map( RasterFactory::instance().create( mapfile[i] ) ); 00115 //_maps[i]->normalize( 0.0, 255.0 ); 00116 } 00117 } 00118 // Visualize result. 00119 else { 00120 00121 _nmap = 1; 00122 _maps = new Map * [_nmap]; 00123 00124 string result = fp.get( "Output file" ); 00125 00126 if ( result.empty() ) { 00127 00128 Log::instance()->error( "'Output file' was not specified!\n" ); 00129 exit(1); 00130 } 00131 00132 _maps[0] = new Map( RasterFactory::instance().create( result ) ); 00133 //_maps[0]->normalize( 0.0, 255.0 ); 00134 } 00135 00136 // Region to be visualized. Must include all maps. 00137 Coord xmin, ymin, xmax, ymax; 00138 findRegion( _nmap, _maps, &xmin, &ymin, &xmax, &ymax ); 00139 00140 // Image dimensions. 00141 int dimx = 1024; 00142 int dimy = int( dimx * (ymax - ymin) / (xmax - xmin) ); 00143 if ( dimy > 700 ) { 00144 00145 dimy = 700; 00146 dimx = int( dimy * (xmax - xmin) / (ymax - ymin) ); 00147 } 00148 00149 Log::instance()->info( "Dimensions: %d x %d\n", dimx, dimy ); 00150 00151 // Occurrences file. 00152 string oc_cs = fp.get( "WKT Coord System" ); 00153 string oc_file = fp.get( "Occurrences source" ); 00154 00155 if ( oc_file.empty() ) { 00156 00157 string oc_file = fp.get( "Species file" ); 00158 } 00159 00160 string oc_name = fp.get( "Occurrences group" ); 00161 00162 if ( oc_name.empty() ) { 00163 00164 string oc_name = fp.get( "Species" ); 00165 } 00166 00167 if ( ! oc_cs.empty() && ! oc_file.empty() ) { 00168 00169 _occurs = readOccurrences( oc_file.c_str(), oc_name.c_str(), oc_cs.c_str() ); 00170 } 00171 else { 00172 00173 if ( oc_file.empty() ) 00174 Log::instance()->warn( "'Occurrences source' was not specified!\n" ); 00175 00176 if ( oc_cs.empty() ) 00177 Log::instance()->warn( "'WKT coord system' was not specified!\n" ); 00178 } 00179 00180 // Instantiate graphical window. 00181 GFrame *frame = createFrame( "openModeller Viewer", 1, dimx, dimy ); 00182 _cnv = frame->newCanvas( 0, 0, dimx, dimy ); 00183 _pm = frame->newPixmap( _cnv, dimx, dimy ); 00184 00185 // Drawing area. 00186 _graph = new GGraph( _pm ); 00187 _graph->scale( float(xmin), float(ymin), float(xmax), 00188 float(ymax) ); 00189 _graph->background( _bg ); 00190 _graph->clear(); 00191 00192 00193 // Zoom in and out with mouse buttons. 00194 _zoom = 2.0; 00195 // frame->FuncBtPress( _cnv, set_float_coord, 1 ); 00196 // frame->FuncBtPress( _cnv, set_int_coord, 3 ); 00197 frame->funcShow( draw ); 00198 00199 frame->exec(); 00200 00201 delete _graph; 00202 delete frame; 00203 00204 delete[] _maps; 00205 } 00206 catch ( std::exception& e ) { 00207 Log::instance()->info( "Exception occurred\n" ); 00208 Log::instance()->info( "Message is %s\n", e.what() ); 00209 } 00210 catch ( ... ) { 00211 Log::instance()->info( "Unknown Error occurred\n" ); 00212 } 00213 } 00214 00215 00216 /************/ 00217 /*** draw ***/ 00218 void 00219 draw() 00220 { 00221 if ( _redraw ) 00222 { 00223 for ( int i = 0; i < _nmap; i++ ) 00224 { 00225 draw_map( _graph, _maps[i] ); 00226 if ( _occurs ) 00227 draw_occur( _graph, _maps[i], _occurs ); 00228 } 00229 00230 _redraw = 0; 00231 } 00232 00233 _cnv->put( _graph ); 00234 } 00235 00236 00237 /****************/ 00238 /*** draw map ***/ 00239 void 00240 draw_map( GGraph *graph, Map *map ) 00241 { 00242 Coord xmin = graph->minX(); 00243 Coord ymin = graph->minY(); 00244 Coord xmax = graph->maxX(); 00245 Coord ymax = graph->maxY(); 00246 Coord xstep = graph->stepX(); 00247 Coord ystep = graph->stepY(); 00248 00249 Scalar val[ map->numBand() ]; 00250 00251 int i = 0; 00252 GColor color; 00253 for ( Coord y = ymin; y < ymax; y += ystep ) 00254 { 00255 for ( Coord x = xmin; x < xmax; x += xstep ) 00256 if ( map->get( x, y, val ) ) 00257 { 00258 color = int( *val ); 00259 graph->pixel( float(x), float(y), color ); 00260 } 00261 00262 00263 if ( ! (++i % 10) ) 00264 _cnv->put( graph ); 00265 } 00266 } 00267 00268 00269 /******************/ 00270 /*** draw occur ***/ 00271 void 00272 draw_occur( GGraph *graph, Map *map, const OccurrencesPtr& occurs ) 00273 { 00274 GColor color; 00275 00276 // Draw each set of occurrences. 00277 float x, y; 00278 int abundance; 00279 OccurrencesImpl::const_iterator oc = occurs->begin(); 00280 for( ; oc != occurs->end(); ++oc ) { 00281 // gt->transfIn( &x, &y, occur->x, occur->y ); 00282 x = float( (*oc)->x() ); 00283 y = float( (*oc)->y() ); 00284 abundance = int( (*oc)->abundance() ); 00285 00286 color = ( abundance == 1 ) ? GColor::Green : GColor::Red; 00287 00288 graph->markAxe( x, y, 1, color); 00289 } 00290 } 00291 00292 00293 /*******************/ 00294 /*** find Region ***/ 00295 void 00296 findRegion( int nmap, Map **map, Coord *xmin, Coord *ymin, Coord *xmax, Coord *ymax ) 00297 { 00298 map[0]->getExtent( xmin, ymin, xmax, ymax ); 00299 } 00300 00301 00302 /************************/ 00303 /*** read Occurrences ***/ 00304 OccurrencesPtr 00305 readOccurrences( char const *source, char const *name, char const *coord_system ) 00306 { 00307 OccurrencesReader* oc_reader = OccurrencesFactory::instance().create( source, coord_system ); 00308 00309 OccurrencesPtr occurrences = oc_reader->getPresences( name ); 00310 00311 OccurrencesPtr absences = oc_reader->getAbsences( name ); 00312 00313 if ( absences->numOccurrences() ) { 00314 00315 occurrences->appendFrom( absences ); 00316 } 00317 00318 delete oc_reader; 00319 00320 return occurrences; 00321 }