openModeller  Version 1.5.0
om_viewer.cpp
Go to the documentation of this file.
1 
28 #include <openmodeller/om.hh>
30 
34 
35 #include "graph/graphic.hh"
36 
37 #include <string.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 
41 #include <stdexcept>
42 
43 #include <string>
44 #include <vector>
45 using std::string;
46 using std::vector;
47 
48 float _zoom;
49 int _redraw = 1;
50 int _last_draw = 0;
51 
52 GColor _bg( 0, 140, 150 );
53 
54 int _nmap;
56 
58 
59 GImage *_cnv;
60 GImage *_pm;
61 GGraph *_graph;
62 
63 
64 void draw();
65 void draw_map( GGraph *graph, Map *map );
66 void draw_occur( GGraph *graph, Map *map, const OccurrencesPtr& );
67 void findRegion( int nmap, Map **map, Coord *xmin, Coord *ymin, Coord *xmax, Coord *ymax );
68 OccurrencesPtr readOccurrences( char const *source, char const *name, char const *coord_system );
69 
70 
71 /**************************************************************/
72 /**************************** main ****************************/
73 
74 int
75 main( int argc, char **argv )
76 {
77  // Reconfigure the global logger.
79  Log::instance()->setPrefix( "" );
80 
81  if ( argc < 2 ) {
82 
83  Log::instance()->info( "\n%s [-r] <request>\n\n", argv[0] );
84  exit( 1 );
85  }
86 
87  Log::instance()->info( "\nopenModeller Viewer - CRIA\n" );
88 
89  int show_map = strcmp( argv[1], "-r" );
90  char *request = (show_map ? argv[1] : argv[2]);
91 
92  try {
93 
94  FileParser fp( request );
95 
96  if ( show_map ) {
97 
98  // Maps to be shown.
99  _nmap = fp.count( "Map" );
100 
101  if ( ! _nmap ) {
102 
103  Log::instance()->error( "No map to be shown!?!\n" );
104  exit(1);
105  }
106 
107  _maps = new Map * [_nmap];
108 
109  vector<string> mapfile = fp.getAll( "Map" );
110 
111  for ( int i = 0; i < _nmap; i++ ) {
112 
113  // Generate a raster using map "i".
114  _maps[i] = new Map( RasterFactory::instance().create( mapfile[i] ) );
115  //_maps[i]->normalize( 0.0, 255.0 );
116  }
117  }
118  // Visualize result.
119  else {
120 
121  _nmap = 1;
122  _maps = new Map * [_nmap];
123 
124  string result = fp.get( "Output file" );
125 
126  if ( result.empty() ) {
127 
128  Log::instance()->error( "'Output file' was not specified!\n" );
129  exit(1);
130  }
131 
132  _maps[0] = new Map( RasterFactory::instance().create( result ) );
133  //_maps[0]->normalize( 0.0, 255.0 );
134  }
135 
136  // Region to be visualized. Must include all maps.
137  Coord xmin, ymin, xmax, ymax;
138  findRegion( _nmap, _maps, &xmin, &ymin, &xmax, &ymax );
139 
140  // Image dimensions.
141  int dimx = 1024;
142  int dimy = int( dimx * (ymax - ymin) / (xmax - xmin) );
143  if ( dimy > 700 ) {
144 
145  dimy = 700;
146  dimx = int( dimy * (xmax - xmin) / (ymax - ymin) );
147  }
148 
149  Log::instance()->info( "Dimensions: %d x %d\n", dimx, dimy );
150 
151  // Occurrences file.
152  string oc_cs = fp.get( "WKT Coord System" );
153  string oc_file = fp.get( "Occurrences source" );
154 
155  if ( oc_file.empty() ) {
156 
157  string oc_file = fp.get( "Species file" );
158  }
159 
160  string oc_name = fp.get( "Occurrences group" );
161 
162  if ( oc_name.empty() ) {
163 
164  string oc_name = fp.get( "Species" );
165  }
166 
167  if ( ! oc_cs.empty() && ! oc_file.empty() ) {
168 
169  _occurs = readOccurrences( oc_file.c_str(), oc_name.c_str(), oc_cs.c_str() );
170  }
171  else {
172 
173  if ( oc_file.empty() )
174  Log::instance()->warn( "'Occurrences source' was not specified!\n" );
175 
176  if ( oc_cs.empty() )
177  Log::instance()->warn( "'WKT coord system' was not specified!\n" );
178  }
179 
180  // Instantiate graphical window.
181  GFrame *frame = createFrame( "openModeller Viewer", 1, dimx, dimy );
182  _cnv = frame->newCanvas( 0, 0, dimx, dimy );
183  _pm = frame->newPixmap( _cnv, dimx, dimy );
184 
185  // Drawing area.
186  _graph = new GGraph( _pm );
187  _graph->scale( float(xmin), float(ymin), float(xmax),
188  float(ymax) );
189  _graph->background( _bg );
190  _graph->clear();
191 
192 
193  // Zoom in and out with mouse buttons.
194  _zoom = 2.0;
195  // frame->FuncBtPress( _cnv, set_float_coord, 1 );
196  // frame->FuncBtPress( _cnv, set_int_coord, 3 );
197  frame->funcShow( draw );
198 
199  frame->exec();
200 
201  delete _graph;
202  delete frame;
203 
204  delete[] _maps;
205  }
206  catch ( std::exception& e ) {
207  Log::instance()->info( "Exception occurred\n" );
208  Log::instance()->info( "Message is %s\n", e.what() );
209  }
210  catch ( ... ) {
211  Log::instance()->info( "Unknown Error occurred\n" );
212  }
213 }
214 
215 
216 /************/
217 /*** draw ***/
218 void
220 {
221  if ( _redraw )
222  {
223  for ( int i = 0; i < _nmap; i++ )
224  {
225  draw_map( _graph, _maps[i] );
226  if ( _occurs )
227  draw_occur( _graph, _maps[i], _occurs );
228  }
229 
230  _redraw = 0;
231  }
232 
233  _cnv->put( _graph );
234 }
235 
236 
237 /****************/
238 /*** draw map ***/
239 void
240 draw_map( GGraph *graph, Map *map )
241 {
242  Coord xmin = graph->minX();
243  Coord ymin = graph->minY();
244  Coord xmax = graph->maxX();
245  Coord ymax = graph->maxY();
246  Coord xstep = graph->stepX();
247  Coord ystep = graph->stepY();
248 
249  Scalar val[ map->numBand() ];
250 
251  int i = 0;
252  GColor color;
253  for ( Coord y = ymin; y < ymax; y += ystep )
254  {
255  for ( Coord x = xmin; x < xmax; x += xstep )
256  if ( map->get( x, y, val ) )
257  {
258  color = int( *val );
259  graph->pixel( float(x), float(y), color );
260  }
261 
262 
263  if ( ! (++i % 10) )
264  _cnv->put( graph );
265  }
266 }
267 
268 
269 /******************/
270 /*** draw occur ***/
271 void
272 draw_occur( GGraph *graph, Map *map, const OccurrencesPtr& occurs )
273 {
274  GColor color;
275 
276  // Draw each set of occurrences.
277  float x, y;
278  int abundance;
279  OccurrencesImpl::const_iterator oc = occurs->begin();
280  for( ; oc != occurs->end(); ++oc ) {
281  // gt->transfIn( &x, &y, occur->x, occur->y );
282  x = float( (*oc)->x() );
283  y = float( (*oc)->y() );
284  abundance = int( (*oc)->abundance() );
285 
286  color = ( abundance == 1 ) ? GColor::Green : GColor::Red;
287 
288  graph->markAxe( x, y, 1, color);
289  }
290 }
291 
292 
293 /*******************/
294 /*** find Region ***/
295 void
296 findRegion( int nmap, Map **map, Coord *xmin, Coord *ymin, Coord *xmax, Coord *ymax )
297 {
298  map[0]->getExtent( xmin, ymin, xmax, ymax );
299 }
300 
301 
302 /************************/
303 /*** read Occurrences ***/
305 readOccurrences( char const *source, char const *name, char const *coord_system )
306 {
307  OccurrencesReader* oc_reader = OccurrencesFactory::instance().create( source, coord_system );
308 
309  OccurrencesPtr occurrences = oc_reader->getPresences( name );
310 
311  OccurrencesPtr absences = oc_reader->getAbsences( name );
312 
313  if ( absences->numOccurrences() ) {
314 
315  occurrences->appendFrom( absences );
316  }
317 
318  delete oc_reader;
319 
320  return occurrences;
321 }
int count(const std::string &key) const
Definition: FileParser.cpp:165
void draw_occur(GGraph *graph, Map *map, const OccurrencesPtr &)
Definition: om_viewer.cpp:272
void warn(const char *format,...)
'Warn' level.
Definition: Log.cpp:273
void setPrefix(const char *pref)
Definition: Log.cpp:222
int get(Coord x, Coord y, Scalar *val) const
Definition: Map.cpp:104
int numBand() const
Definition: Map.hh:90
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
static OccurrencesFactory & instance()
virtual OccurrencesPtr getAbsences(const char *groupId)
void setLevel(Level level)
Definition: Log.hh:107
void draw_map(GGraph *graph, Map *map)
Definition: om_viewer.cpp:240
Definition: Log.hh:57
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
GImage * _cnv
Definition: om_viewer.cpp:59
Map ** _maps
Definition: om_viewer.cpp:55
virtual OccurrencesPtr getPresences(const char *groupId)
void draw()
Definition: om_viewer.cpp:219
OccurrencesPtr readOccurrences(char const *source, char const *name, char const *coord_system)
Definition: om_viewer.cpp:305
int getExtent(Coord *xmin, Coord *ymin, Coord *xmax, Coord *ymax) const
Definition: Map.cpp:63
float _zoom
Definition: om_viewer.cpp:48
GColor _bg(0, 140, 150)
std::vector< std::string > getAll(const std::string &key) const
Definition: FileParser.cpp:185
Definition: Map.hh:49
static RasterFactory & instance()
int _nmap
Definition: om_viewer.cpp:54
GImage * _pm
Definition: om_viewer.cpp:60
void findRegion(int nmap, Map **map, Coord *xmin, Coord *ymin, Coord *xmax, Coord *ymax)
Definition: om_viewer.cpp:296
OccurrencesPtr _occurs
Definition: om_viewer.cpp:57
int _last_draw
Definition: om_viewer.cpp:50
void info(const char *format,...)
'Info' level.
Definition: Log.cpp:256
int _redraw
Definition: om_viewer.cpp:49
double Coord
Type of map coordinates.
Definition: om_defs.hh:38
std::vector< OccurrencePtr >::const_iterator const_iterator
Definition: Occurrences.hh:85
GGraph * _graph
Definition: om_viewer.cpp:61
int main(int argc, char **argv)
Definition: om_viewer.cpp:75
std::string get(const std::string &key) const
Definition: FileParser.cpp:145
OccurrencesReader * create(const char *source, const char *coordSystem)