openModeller  Version 1.4.0
om_layer.cpp
Go to the documentation of this file.
00001 #include <openmodeller/om.hh>
00002 #include <openmodeller/om_defs.hh>
00003 #include <openmodeller/Log.hh>
00004 #include <openmodeller/os_specific.hh>
00005 
00006 #include "getopts/getopts.h"
00007 
00008 #include "om_cmd_utils.hh"
00009 #include "om_layer_utils.hh"
00010 
00011 #include <fstream>   // file I/O for XML
00012 #include <string>
00013 #include <stdexcept>
00014 
00015 using namespace std;
00016 
00017 int main( int argc, char **argv ) {
00018 
00019   Options opts;
00020   int option;
00021 
00022   opts.addOption( "" , "log-level"  , "Set the log level (debug, warn, info, error)", true );
00023   opts.addOption( "v", "version"    , "Display version info"                        , false );
00024   opts.addOption( "d", "scan-dir"   , "Directory to be scanned for layers"          , true );
00025   opts.addOption( "s", "result"     , "File to store scan result"                   , true );
00026   opts.addOption( "l", "check"      , "Layer to be checked"                         , true );
00027   opts.addOption( "c", "config-file", "Configuration file for openModeller"         , true );
00028 
00029   std::string log_level("info");
00030   std::string scan_dir;
00031   std::string layer_id;
00032   std::string config_file;
00033   std::string result_file;
00034 
00035   if ( ! opts.parse( argc, argv ) ) {
00036 
00037     opts.showHelp( argv[0] ); 
00038     exit(0);
00039   }
00040 
00041   OpenModeller om;
00042 
00043   while ( ( option = opts.cycle() ) >= 0 ) {
00044 
00045     switch ( option ) {
00046 
00047       case 0:
00048         log_level = opts.getArgs( option );
00049         break;
00050       case 1:
00051         printf( "om_layer %s\n", om.getVersion().c_str() );
00052         printf("This is free software; see the source for copying conditions. There is NO\n");
00053         printf("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
00054         exit(0);
00055         break;
00056       case 2:
00057         scan_dir = opts.getArgs( option );
00058         break;
00059       case 3:
00060         result_file = opts.getArgs( option );
00061         break;
00062       case 4:
00063         layer_id = opts.getArgs( option );
00064         break;
00065       case 5:
00066         config_file = opts.getArgs( option );
00067         break;
00068       default:
00069         break;
00070     }
00071   }
00072 
00073   // Check parameters
00074 
00075   if ( (scan_dir.empty() && layer_id.empty()) || (!scan_dir.empty() && !layer_id.empty()) ) {
00076 
00077     printf( "Please specify one and only one of the parameters: --check layer_id, --scan-dir directory\n");
00078     exit(1);
00079   }
00080 
00081   // Log stuff
00082   Log::Level level_code = getLogLevel( log_level );
00083 
00084   Log::instance()->setLevel( level_code );
00085 
00086   // Config file
00087   if ( ! config_file.empty() ) {
00088 
00089     Settings::loadConfig( config_file );
00090   }
00091 
00092   if ( layer_id.size() > 0 ) {
00093 
00094     try {
00095 
00096       Raster * r = RasterFactory::instance().create( layer_id, 0 );
00097 
00098       delete r;
00099     }
00100     catch ( std::runtime_error e ) {
00101 
00102       printf( "Failed to process raster %s:\n\n%s\n", layer_id.c_str(), e.what() );
00103       exit(1);
00104     }
00105   }
00106   else {
00107 
00108     // Here we assume scan-dir was specified
00109 
00110     ostringstream oss;
00111 
00112     int seq = 1;
00113 
00114     if ( ! readDirectory( scan_dir.c_str(), "Layers", oss, 0, &seq ) ) {
00115 
00116       printf( "Failed to scan specified directory\n" );
00117       exit(1);
00118     }
00119 
00120     std::cerr << flush;
00121 
00122     // Write result to file, if requested
00123     if ( ! result_file.empty() ) {
00124 
00125       ofstream file( result_file.c_str() );
00126       file << oss.str();
00127       file.close();
00128     }
00129     else {
00130 
00131       // Otherwise send it to stdout
00132       std::cout << oss.str().c_str() << endl << flush;
00133     }
00134   }
00135 
00136   return 0;
00137 }