openModeller  Version 1.5.0
om_layer.cpp
Go to the documentation of this file.
1 #include <openmodeller/om.hh>
3 #include <openmodeller/Log.hh>
5 
6 #include "getopts/getopts.h"
7 
8 #include "om_cmd_utils.hh"
9 #include "om_layer_utils.hh"
10 
11 #include <fstream> // file I/O for XML
12 #include <string>
13 #include <stdexcept>
14 
15 using namespace std;
16 
17 int main( int argc, char **argv ) {
18 
19  Options opts;
20  int option;
21 
22  opts.addOption( "" , "log-level" , "Set the log level (debug, warn, info, error)", true );
23  opts.addOption( "v", "version" , "Display version info" , false );
24  opts.addOption( "d", "scan-dir" , "Directory to be scanned for layers" , true );
25  opts.addOption( "s", "result" , "File to store scan result" , true );
26  opts.addOption( "l", "check" , "Layer to be checked" , true );
27  opts.addOption( "c", "config-file", "Configuration file for openModeller" , true );
28 
29  std::string log_level("info");
30  std::string scan_dir;
31  std::string layer_id;
32  std::string config_file;
33  std::string result_file;
34 
35  if ( ! opts.parse( argc, argv ) ) {
36 
37  opts.showHelp( argv[0] );
38  exit(0);
39  }
40 
41  OpenModeller om;
42 
43  while ( ( option = opts.cycle() ) >= 0 ) {
44 
45  switch ( option ) {
46 
47  case 0:
48  log_level = opts.getArgs( option );
49  break;
50  case 1:
51  printf( "om_layer %s\n", om.getVersion().c_str() );
52  printf("This is free software; see the source for copying conditions. There is NO\n");
53  printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
54  exit(0);
55  break;
56  case 2:
57  scan_dir = opts.getArgs( option );
58  break;
59  case 3:
60  result_file = opts.getArgs( option );
61  break;
62  case 4:
63  layer_id = opts.getArgs( option );
64  break;
65  case 5:
66  config_file = opts.getArgs( option );
67  break;
68  default:
69  break;
70  }
71  }
72 
73  // Check parameters
74 
75  if ( (scan_dir.empty() && layer_id.empty()) || (!scan_dir.empty() && !layer_id.empty()) ) {
76 
77  printf( "Please specify one and only one of the parameters: --check layer_id, --scan-dir directory\n");
78  exit(1);
79  }
80 
81  // Log stuff
82  Log::Level level_code = getLogLevel( log_level );
83 
84  Log::instance()->setLevel( level_code );
85 
86  // Config file
87  if ( ! config_file.empty() ) {
88 
89  Settings::loadConfig( config_file );
90  }
91 
92  if ( layer_id.size() > 0 ) {
93 
94  try {
95 
96  Raster * r = RasterFactory::instance().create( layer_id, 0 );
97 
98  delete r;
99  }
100  catch ( std::runtime_error e ) {
101 
102  printf( "Failed to process raster %s:\n\n%s\n", layer_id.c_str(), e.what() );
103  exit(1);
104  }
105  }
106  else {
107 
108  // Here we assume scan-dir was specified
109 
110  ostringstream oss;
111 
112  int seq = 1;
113 
114  if ( ! readDirectory( scan_dir.c_str(), "Layers", oss, 0, &seq ) ) {
115 
116  printf( "Failed to scan specified directory\n" );
117  exit(1);
118  }
119 
120  std::cerr << flush;
121 
122  // Write result to file, if requested
123  if ( ! result_file.empty() ) {
124 
125  ofstream file( result_file.c_str() );
126  file << oss.str();
127  file.close();
128  }
129  else {
130 
131  // Otherwise send it to stdout
132  std::cout << oss.str().c_str() << endl << flush;
133  }
134  }
135 
136  return 0;
137 }
static void loadConfig(const std::string configFile)
Definition: Settings.cpp:100
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
A common interface to rasters.
Definition: Raster.hh:44
Log::Level getLogLevel(std::string level)
int main(int argc, char **argv)
Definition: om_layer.cpp:17
Level
Definition: Log.hh:54
void setLevel(Level level)
Definition: Log.hh:107
bool readDirectory(const char *dir, const char *label, ostream &xml, int depth, int *seq)
static RasterFactory & instance()
std::string getVersion()
Raster * create(const string &source, int categ=0)