8 #include "getopts/getopts.h"
25 int main(
int argc,
char **argv ) {
31 opts.addOption(
"v",
"version" ,
"Display version info" ,
false );
32 opts.addOption(
"s",
"source" ,
"Source with references to points and layers" ,
true );
33 opts.addOption(
"e",
"dump-env" ,
"Dump environment data for a specified cell range",
false );
34 opts.addOption(
"" ,
"cell-start" ,
"Cell position to start environment dumping (default 0)" ,
true );
35 opts.addOption(
"" ,
"cell-end" ,
"Cell position to end environment dumping (default 1000)" ,
true );
36 opts.addOption(
"" ,
"log-level" ,
"Set the log level (debug, warn, info, error)",
true );
37 opts.addOption(
"c",
"config-file",
"Configuration file for openModeller" ,
true );
39 std::string log_level(
"info");
40 std::string source(
"");
41 bool dump_env =
false;
42 std::string start_string(
"0");
43 std::string end_string(
"1000");
44 std::string config_file;
46 if ( ! opts.parse( argc, argv ) ) {
48 opts.showHelp( argv[0] );
57 while ( ( option = opts.cycle() ) >= 0 ) {
62 printf(
"om_sampler %s\n", om.
getVersion().c_str() );
63 printf(
"This is free software; see the source for copying conditions. There is NO\n");
64 printf(
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
68 source = opts.getArgs( option );
74 start_string = opts.getArgs( option );
77 end_string = opts.getArgs( option );
80 log_level = opts.getArgs( option );
83 config_file = opts.getArgs( option );
91 if ( source.empty() ) {
93 printf(
"Please specify a source (request file in txt or xml) with references to points and layers.\n");
102 cell_start = atoi( start_string.c_str() );
104 cell_end = atoi( end_string.c_str() );
114 if ( ! config_file.empty() ) {
129 std::ifstream fs( source.c_str(), std::ios_base::in );
133 printf(
"Could not open source file.\n");
137 std::string line(
"");
139 while ( getline( fs, line ) ) {
141 for (
unsigned int i = 0; i < line.size(); ++i ) {
144 if ( line[i] ==
'\r' || line[i] ==
'\n' || line[i] ==
' ' ) {
150 if ( line[i] ==
'<' ) {
167 sampler =
createSampler( config->getSubsection(
"Sampler" ) );
179 std::string oc_cs = fp.
get(
"WKT coord system" );
182 std::string oc_file = fp.get(
"Occurrences source" );
185 std::string oc_name = fp.get(
"Occurrences group" );
189 if ( oc_cs.empty() ) {
191 printf(
"'WKT coord system' keyword not specified in the request file!\n" );
195 if ( oc_file.empty() ) {
197 printf(
"'Occurrences source' keyword not specified in the request file!\n" );
213 std::string input_mask = fp.get(
"Mask" );
216 std::vector<std::string> categ_map = fp.getAll(
"Categorical map" );
217 std::vector<std::string> cont_map = fp.getAll(
"Map" );
220 if ( ! (categ_map.size() + cont_map.size()) ) {
222 printf(
"At least one 'Map' or 'Categorical map' needs to be specified in the request file!\n" );
227 if ( input_mask.empty() ) {
229 if ( cont_map.size() ) {
231 input_mask = cont_map[0];
235 input_mask = categ_map[0];
243 std::string spatially_unique = fp.get(
"Spatially unique" );
244 if ( spatially_unique ==
"true" ) {
246 sampler->spatiallyUnique();
249 std::string environmentally_unique = fp.get(
"Environmentally unique" );
250 if ( environmentally_unique ==
"true" ) {
252 sampler->environmentallyUnique();
258 int dim = sampler->numIndependent();
261 cout <<
"#id\tlabel\tlongitude\tlatitude\tabundance";
263 for (
int i = 0; i < dim; ++i ) {
265 cout <<
"\tattr" << i+1;
284 if ( cnt < cell_start) {
292 pair<Coord,Coord> lonlat = *it;
295 Sample s = e->get( lonlat.first, lonlat.second );
297 cout <<
"\n" << cnt <<
"\t" <<
"env data" <<
"\t" << lonlat.first <<
"\t" << lonlat.second <<
"\t" <<
"-";
304 if ( s.
size() > 0 ) {
306 for (
int i = 0; i < dim; ++i ) {
308 cout <<
"\t" << s[i];
313 cout <<
"\t" <<
"nodata";
319 if ( cnt > cell_end ) {
346 catch ( runtime_error e ) {
348 printf(
"om_sampler: %s\n", e.what() );
356 int dim = occ->dimension();
361 while ( it != fin ) {
363 stream <<
"\n" << ((*it)->id()).c_str() <<
"\t" << occ->label() <<
"\t" << (*it)->x() <<
"\t" << (*it)->y() <<
"\t" << (*it)->abundance();
365 Sample s = (*it)->environment();
367 for (
int i = 0; i < dim; ++i ) {
369 stream <<
"\t" << s[i];
static void loadConfig(const std::string configFile)
static ConfigurationPtr readXml(char const *filename)
static Log * instance()
Returns the instance pointer, creating the object on the first call.
static OccurrencesFactory & instance()
Log::Level getLogLevel(std::string level)
virtual OccurrencesPtr getAbsences(const char *groupId)
void setLevel(Level level)
EnvironmentPtr createEnvironment(const std::vector< std::string > &categs, const std::vector< std::string > &maps, const std::string &mask_file)
SamplerPtr createSampler(const EnvironmentPtr &env, const OccurrencesPtr &presence, const OccurrencesPtr &absence)
int main(int argc, char **argv)
void setupExternalResources()
virtual OccurrencesPtr getPresences(const char *groupId)
std::vector< OccurrencePtr >::const_iterator const_iterator
std::string get(const std::string &key) const
void printOccurrences(ostream &stream, ConstOccurrencesPtr occ)
OccurrencesReader * create(const char *source, const char *coordSystem)