openModeller  Version 1.5.0
om_histogram.cpp
Go to the documentation of this file.
1 
6 #include <openmodeller/om.hh>
7 
9 
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 
14 #include <stdexcept>
15 
16 #include <string>
17 #include <vector>
18 #include <map>
19 using std::string;
20 using std::vector;
21 using std::map;
22 
23 
24 /**************************************************************/
25 /**************************** main ****************************/
26 
27 int
28 main( int argc, char **argv )
29 {
30  // Reconfigure the global logger.
32  Log::instance()->setPrefix( "" );
33 
34  if ( argc < 3 ) {
35 
36  Log::instance()->info( "\n%s <map_file> <num_intervals>\n\n", argv[0] );
37  exit( 1 );
38  }
39 
40  string map_file( argv[1] );
41  int intervals = atoi( argv[2] );
42 
43  vector<int> histogram;
44  vector<double> min_val;
45  vector<double> max_val;
46  map<double,int> values;
47 
48  if ( intervals > 0 ) {
49 
50  double interval = 1.0/intervals;
51 
52  for ( int i = 0; i < intervals; ++i ) {
53 
54  histogram.push_back( 0 );
55  min_val.push_back( i*interval );
56  max_val.push_back( min_val[i] + interval );
57  }
58 
59  max_val[intervals-1] = max_val[intervals-1] + 0.1;
60  }
61 
62  try {
63 
64  Map * map = new Map( RasterFactory::instance().create( map_file ) );
65 
66  Coord xmin, ymin, xmax, ymax, xstep, ystep;
67  map->getRegion( &xmin, &ymin, &xmax, &ymax );
68  map->getCell( &xstep, &ystep );
69 
70  Scalar val[ map->numBand() ];
71 
72  for ( Coord y = ymin; y < ymax; y += ystep ) {
73 
74  for ( Coord x = xmin; x < xmax; x += xstep ) {
75 
76  if ( map->get( x, y, val ) ) {
77 
78  if ( intervals > 0 ) {
79 
80  for ( int i = 0; i < intervals; ++i ) {
81 
82  if ( val[0] >= min_val[i] && val[0] < max_val[i] ) {
83 
84  ++histogram[i];
85  break;
86  }
87  }
88  }
89  else {
90 
91  if ( values.find( val[0] ) != values.end() ) {
92 
93  ++values[val[0]];
94  }
95  else {
96 
97  values[val[0]] = 1;
98  }
99  }
100  }
101  }
102  }
103 
104  Log::instance()->info( "Histogram:\n" );
105 
106  if ( intervals > 0 ) {
107 
108  for ( int i = 0; i < intervals; ++i ) {
109 
110  Log::instance()->info( "[%f-%f): %d\n", min_val[i], max_val[i], histogram[i] );
111  }
112  }
113  else {
114 
115  for ( std::map<double, int>::const_iterator it = values.begin(); it != values.end(); it++ ) {
116 
117  Log::instance()->info( "%f: %d\n", (*it).first, (*it).second );
118  }
119  }
120 
121  delete map;
122  }
123  catch ( std::exception& e ) {
124  Log::instance()->info( "Exception occurred\n" );
125  Log::instance()->info( "Message is %s\n", e.what() );
126  }
127  catch ( ... ) {
128  Log::instance()->info( "Unknown Error occurred\n" );
129  }
130 }
131 
132 
133 
134 
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
void setLevel(Level level)
Definition: Log.hh:107
int getCell(Coord *xcel, Coord *ycel) const
Definition: Map.hh:102
Definition: Log.hh:57
Definition: Map.hh:49
int main(int argc, char **argv)
static RasterFactory & instance()
void info(const char *format,...)
'Info' level.
Definition: Log.cpp:256
double Coord
Type of map coordinates.
Definition: om_defs.hh:38