All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
main.cpp
Go to the documentation of this file.
1 // Terralib Raster Manager Tool
2 #include <RasterManager.h>
3 
4 // Terralib
5 #include <terralib/common.h>
6 
7 // Boost
8 #include <boost/program_options.hpp>
9 
10 // STL
11 #include <iostream>
12 
13 te::tools::rastermanager::RasterManager g_rm; //!< Raster Manager Object
14 
15 boost::program_options::variables_map g_vm; //!< Map o options after parse
16 
17 boost::program_options::options_description g_help("Help"); //!< Group of help options
18 boost::program_options::options_description g_functions("Functions"); //!< Group of functions options
19 boost::program_options::options_description g_parameters("Parameters"); //!< Group of parameters options
20 
21 boost::program_options::options_description g_all("All options"); //!< All options
22 boost::program_options::options_description g_hidden("Hidden options"); //!< Which will not be shown to the user
23 boost::program_options::options_description g_visible("Allowed options"); //!< Which will be shown to the user
24 
25 
26 
27 void showError(std::string errorMessage)
28 {
29  std::cout << ">>>> " << errorMessage << " <<<<" << std::endl << std::endl;
30  std::cout << g_visible << std::endl;
31  std::cout << "---------------------------" << std::endl;
32 }
33 
35 {
36  bool shown = false;
37  std::ostringstream helpText;
38 
39  if(g_vm.count("help-input"))
40  {
41  helpText << "input" << std::endl;
42  helpText << "-----" << std::endl << std::endl;
43  helpText << "The \"input\" is a required parameter for all functions. It's the full path of the raster, including the file name and extension." << std::endl;
44  shown = true;
45  }
46  else if(g_vm.count("help-info"))
47  {
48  helpText << "info" << std::endl;
49  helpText << "----" << std::endl << std::endl;
50  helpText << "The \"info\" function show raster informations, like:" << std::endl;
51  helpText << " - Raster Name" << std::endl;
52  helpText << " - Number of Columns" << std::endl;
53  helpText << " - Number of Rows" << std::endl;
54  helpText << " - Number of Bands" << std::endl;
55  helpText << " - SRID" << std::endl;
56  helpText << " - Resolution in X" << std::endl;
57  helpText << " - Resolution in Y" << std::endl;
58  helpText << " - Extent UR" << std::endl;
59  helpText << " - Extent LL" << std::endl;
60  helpText << "And informations about the raster bands, like:" << std::endl;
61  helpText << " - Min Values" << std::endl;
62  helpText << " - Max Values" << std::endl;
63  helpText << " - Mean Values" << std::endl;
64  helpText << " - Std Values" << std::endl;
65  helpText << " - Gain values" << std::endl;
66  helpText << " - Offset values" << std::endl << std::endl;
67  helpText << "Required parameters:" << std::endl;
68  helpText << " - input" << std::endl;
69  helpText << std::endl;
70  shown = true;
71  }
72  else if(g_vm.count("help-copy"))
73  {
74  helpText << "copy" << std::endl;
75  helpText << "----" << std::endl << std::endl;
76  helpText << "The \"copy\" is a function that make a copy of the input raster. In this function, the output parameter is required." << std::endl << std::endl;
77  helpText << "Required parameters:" << std::endl;
78  helpText << " - input" << std::endl;
79  helpText << " - output" << std::endl;
80  helpText << "Optional parameters:" << std::endl;
81  helpText << " - band" << std::endl;
82  helpText << " - band-type" << std::endl;
83  helpText << std::endl;
84  shown = true;
85  }
86  else if(g_vm.count("help-output"))
87  {
88  helpText << "output" << std::endl;
89  helpText << "------" << std::endl << std::endl;
90  helpText << "The \"output\" parameter is the full path of the output raster, including the file name and extension." << std::endl;
91  helpText << "It's used by the functions (copy, reproject, ...). Some functions overwrite the input raster if the output doesn't be declared." << std::endl;
92  helpText << std::endl;
93  shown = true;
94  }
95  else if(g_vm.count("help-band"))
96  {
97  helpText << "band" << std::endl;
98  helpText << "----" << std::endl << std::endl;
99  helpText << "The \"band\" is a copy function parameter that receive a list of bands indexes that the user would like to copy." << std::endl;
100  helpText << std::endl;
101  shown = true;
102  }
103  else if(g_vm.count("help-reproject"))
104  {
105  helpText << "reproject" << std::endl;
106  helpText << "---------" << std::endl << std::endl;
107  helpText << "The \"reproject\" is a function that reproject a raster based in the srid passed." << std::endl << std::endl;
108  helpText << "Required parameters:" << std::endl;
109  helpText << " - input" << std::endl;
110  helpText << " - srid" << std::endl;
111  helpText << "Optional parameters:" << std::endl;
112  helpText << " - output" << std::endl;
113  helpText << std::endl;
114  shown = true;
115  }
116  else if(g_vm.count("help-convert"))
117  {
118  helpText << "convert" << std::endl;
119  helpText << "-------" << std::endl << std::endl;
120  helpText << "The \"convert\" is a function that convert a raster based in the extension passed." << std::endl;
121  helpText << "If you pass the \"output\", the image will have these output path (including name and extension)." << std::endl;
122  helpText << "Else, if you pass the \"extension\", the image will have the sabe name, but the extension passed." << std::endl;
123  helpText << std::endl;
124  helpText << "Required parameters:" << std::endl;
125  helpText << " - input" << std::endl;
126  helpText << " - extension or output" << std::endl;
127  helpText << "Optional parameters:" << std::endl;
128  helpText << " - band" << std::endl;
129  helpText << std::endl;
130  shown = true;
131  }
132  else if(g_vm.count("help-resolution"))
133  {
134  helpText << "resolution" << std::endl;
135  helpText << "----------" << std::endl << std::endl;
136  helpText << "The \"resolution\" is a function that change the raster resolution based in the method and scale passed." << std::endl << std::endl;
137  helpText << "Required parameters:" << std::endl;
138  helpText << " - input" << std::endl;
139  helpText << " - method" << std::endl;
140  helpText << " - scale" << std::endl;
141  helpText << "Optional parameters:" << std::endl;
142  helpText << " - output" << std::endl;
143  helpText << std::endl;
144  shown = true;
145  }
146  else if(g_vm.count("help-method"))
147  {
148  helpText << "method" << std::endl;
149  helpText << "------" << std::endl << std::endl;
150  helpText << "The \"method\" is a Resolution function parameter that receive a method for the function is based." << std::endl;
151  helpText << "The options are:" << std::endl;
152  helpText << " - \"1\" to use Nearest Neighbor method" << std::endl;
153  helpText << " - \"2\" to use Bilinear method" << std::endl;
154  helpText << " - \"3\" to use Bicubic method" << std::endl;
155  helpText << std::endl;
156  shown = true;
157  }
158  else if(g_vm.count("help-scale"))
159  {
160  helpText << "scale" << std::endl;
161  helpText << "-----" << std::endl << std::endl;
162  helpText << "The \"scale\" is a Resolution function parameter that receive a scale integer for the function is based." << std::endl;
163  helpText << "For example: with a scale equal \"2\", all raster grid cells dimensions will be multiplied to 2." << std::endl;
164  helpText << " but if scale equal \"-2\", all raster grid cells dimensions will be divided to 2." << std::endl;
165  helpText << std::endl;
166  shown = true;
167  }
168  else if(g_vm.count("help-extension"))
169  {
170  helpText << "extension" << std::endl;
171  helpText << "---------" << std::endl << std::endl;
172  helpText << "The \"extension\" is a Convert function parameter that receive a extension for the function is based." << std::endl;
173  helpText << "The options are:" << std::endl;
174  helpText << " - jpg" << std::endl;
175  helpText << " - png" << std::endl;
176  helpText << " - tif" << std::endl;
177  helpText << std::endl;
178  shown = true;
179  }
180  else if(g_vm.count("help-srid"))
181  {
182  helpText << "srid" << std::endl;
183  helpText << "----" << std::endl << std::endl;
184  helpText << "The \"srid\" is a Reproject function parameter that receive a srid code for the function is based." << std::endl;
185  helpText << std::endl;
186  shown = true;
187  }
188  else if(g_vm.count("help-trim"))
189  {
190  helpText << "trim" << std::endl;
191  helpText << "----" << std::endl << std::endl;
192  helpText << "The \"trim\" is a function that trim a raster based in the envelope passed." << std::endl << std::endl;
193  helpText << "Required parameters:" << std::endl;
194  helpText << " - input" << std::endl;
195  helpText << " - envelope" << std::endl;
196  helpText << "Optional parameters:" << std::endl;
197  helpText << " - output" << std::endl;
198  helpText << std::endl;
199  shown = true;
200  }
201  else if(g_vm.count("help-envelope"))
202  {
203  helpText << "envelope" << std::endl;
204  helpText << "--------" << std::endl << std::endl;
205  helpText << "--envelope <llx> <lly> <urx> <urx>" << std::endl << std::endl;
206  helpText << "The \"envelope\" is a Trim function parameter that receive four doubles representind a envelope (box)." << std::endl;
207  helpText << "The order of passing parameters is:" << std::endl;
208  helpText << " - llx" << std::endl;
209  helpText << " - lly" << std::endl;
210  helpText << " - urx" << std::endl;
211  helpText << " - ury" << std::endl;
212  helpText << "Example: --envelope -40 -30 -20 5" << std::endl;
213  helpText << std::endl;
214  shown = true;
215  }
216  else if(g_vm.count("help-band-type"))
217  {
218  helpText << "band type" << std::endl;
219  helpText << "----" << std::endl << std::endl;
220  helpText << "The \"band type\" is a copy function parameter that define witch type the output raster will have." << std::endl;
221  helpText << "(Not implemented yet!)" << std::endl;
222  helpText << std::endl;
223  shown = true;
224  }
225  else if(g_vm.count("examples"))
226  {
227  helpText << "Use Examples" << std::endl;
228  helpText << "------------" << std::endl << std::endl;
229  helpText << "Info function" << std::endl << std::endl;
230  helpText << "--info --input D:\\MyRasters\\raster.tif" << std::endl;
231  helpText << std::endl << "Copy function" << std::endl << std::endl;
232  helpText << "--copy --input D:\\MyRasters\\raster.tif --output D:\\MyRasters\\raster_copy.tif" << std::endl;
233  helpText << "--copy --input D:\\MyRasters\\raster.tif --output D:\\MyRasters\\raster_copy.tif --band 2 3" << std::endl;
234  helpText << "--copy --input D:\\MyRasters\\raster.tif --output D:\\MyRasters\\raster_copy.tif --band-type 4" << std::endl;
235  helpText << std::endl << "Reproject function" << std::endl << std::endl;
236  helpText << "--reproject --input D:\\MyRasters\\raster.tif --srid 29181" << std::endl;
237  helpText << "--reproject --input D:\\MyRasters\\raster.tif --output D:\\MyRasters\\raster_reprojected.tif --srid 29181" << std::endl;
238  helpText << std::endl << "Convert function" << std::endl << std::endl;
239  helpText << "--convert --input D:\\MyRasters\\raster.tif --extension jpg" << std::endl;
240  helpText << "--convert --input D:\\MyRasters\\raster.tif" "D:\\MyRasters\\raster_converted.jpg" << std::endl;
241  helpText << std::endl << "Resolution function" << std::endl << std::endl;
242  helpText << "--resolution --input D:\\MyRasters\\raster.tif --method 1 --scale 2" << std::endl;
243  helpText << "--resolution --input D:\\MyRasters\\raster.tif --method 1 --scale -2" << std::endl;
244  helpText << "--resolution --input D:\\MyRasters\\raster.tif --output D:\\MyRasters\\raster_new.tif --method 1 --scale -2" << std::endl;
245  helpText << std::endl << "Trim function" << std::endl << std::endl;
246  helpText << "--trim --input D:\\MyRasters\\raster.tif --envelope -40 -30 -20 5" << std::endl;
247  helpText << "--trim --input D:\\MyRasters\\raster.tif --output D:\\MyRasters\\raster_trimmed.tif --envelope -40 -30 -20 5" << std::endl;
248  helpText << std::endl;
249  shown = true;
250  }
251 
252  std::cout << helpText.str() << std::endl;
253 
254  return shown;
255 
256 }
257 
258 int main(int argc, char* argv[])
259 {
260  try
261  {
262 
263  // Variables that receive the attributes
264  std::string input = std::string();
265  std::string output = std::string();
266  std::vector<int> bands = std::vector<int>();
267  int srid = -1;
268  std::string extension = std::string();
269  int method = int();
270  int scale = int();
271  std::vector<double> envelope = std::vector<double>();
272  int bandType = -1;
273 
274  std::string errorMessage = std::string();
275 
276 
277  std::cout << std::endl << "TerraLib Raster Manager" << std::endl;
278  std::cout << "-------------------------" << std::endl << std::endl;
279 
280  // Group of functions options
281  g_functions.add_options()
282  ("info,f", "Show Raster Informations")
283  ("copy,p", "Copy function")
284  ("reproject,r", "Reproject function")
285  ("convert,c", "Convert function")
286  ("resolution,l", "Change Resolution function")
287  ("trim,t", "Trim function")
288  ("help,h", "Terralib Raster Manager. --help-<function> or --help-<parameter>")
289  ("examples,x", "Show use examples")
290  ("supported,u", "Supported Extensions")
291  ;
292 
293  // Group of parameters options
294  g_parameters.add_options()
295  ("input,i", boost::program_options::value<std::string>(), "Input Raster (Required)")
296  ("output,o", boost::program_options::value<std::string>(), "Output Raster")
297  ("band,b", boost::program_options::value<std::vector<int>>()->multitoken(), "Band to copy")
298  ("method,m", boost::program_options::value<int>(), "Method used to change resolution (\"1\"-NearestNeighbor, \"2\"-Bilinear, \"3\"-Bicubic)")
299  ("scale,s", boost::program_options::value<int>(), "Scale used to change resolution")
300  ("srid,d", boost::program_options::value<int>(), "Srid used to reproject")
301  ("extension,e", boost::program_options::value<std::string>(), "Extension used to convert")
302  ("envelope,v", boost::program_options::value<std::vector<double>>()->multitoken(), "Envelope used to trim")
303  ("band-type,n", boost::program_options::value<int>(), "Type of the band")
304  ;
305 
306  // Group of help options
307  g_help.add_options()
308  ("help-input","")
309  ("help-info","")
310  ("help-copy","")
311  ("help-output","")
312  ("help-band","")
313  ("help-reproject","")
314  ("help-convert","")
315  ("help-resolution","")
316  ("help-method","")
317  ("help-extension","")
318  ("help-srid","")
319  ("help-scale","")
320  ("help-trim","")
321  ("help-band-type","")
322  ;
323 
324  // Which will not be shown to the user
325  g_hidden.add(g_help);
326 
327  // Which will be shown to the user
329 
330  // All options to parse
331  g_all.add(g_visible).add(g_hidden);
332 
333  // Parse the options
334  boost::program_options::store(boost::program_options::parse_command_line(argc, argv, g_all), g_vm);
335  boost::program_options::notify(g_vm);
336 
337  // Check if one of "help" was called and show
338  if(showSpecificHelp())
339  return EXIT_SUCCESS;
340 
341  if(g_vm.count("help") || g_vm.empty())
342  {
343  std::cout << g_visible << std::endl;
344  std::cout << "---------------------------" << std::endl;
345  return EXIT_SUCCESS;
346  }
347 
348  // Getting parameters
349  input = (g_vm.count("input") ? g_vm["input"].as<std::string>() : std::string());
350  output = (g_vm.count("output") ? g_vm["output"].as<std::string>() : std::string());
351  bands = (g_vm.count("band") ? g_vm["band"].as<std::vector<int>>() : std::vector<int>());
352  srid = (g_vm.count("srid") ? g_vm["srid"].as<int>() : -1);
353  extension = (g_vm.count("extension") ? g_vm["extension"].as<std::string>() : std::string());
354  method = (g_vm.count("method") ? g_vm["method"].as<int>() : int());
355  scale = (g_vm.count("scale") ? g_vm["scale"].as<int>() : int());
356  envelope = (g_vm.count("envelope") ? g_vm["envelope"].as<std::vector<double>>() : std::vector<double>());
357  bandType = (g_vm.count("band-type") ? g_vm["band-type"].as<int>() : -1);
358 
359 
360  // Starting Terralib
362 
363  // Supported Estensions
364  if(g_vm.count("supported"))
365  {
366  g_rm.showSupportedExtensions(errorMessage);
367  return EXIT_SUCCESS;
368  }
369 
370  // Required
371  if(input.empty())
372  {
373  showError("Comannd line error: input raster missing!");
374  return EXIT_SUCCESS;
375  }
376 
377  if(!g_rm.init(input, errorMessage))
378  {
379  showError(errorMessage);
380  return EXIT_SUCCESS;
381  }
382 
383  // Info
384  if(g_vm.count("info"))
385  {
386  std::ostringstream output;
387  if(!g_rm.getRasterInfo(output, errorMessage))
388  {
389  showError(errorMessage);
390  }
391  else
392  std::cout << output.str();
393  return EXIT_SUCCESS;
394  }
395 
396  // Copy
397  else if(g_vm.count("copy"))
398  {
399  if(output.empty())
400  {
401  showError("For copy, output is required!");
402  return EXIT_SUCCESS;
403  }
404  else if(!g_rm.copyRaster(output, bands, bandType, errorMessage))
405  {
406  showError(errorMessage);
407  }
408  else
409  {
410  std::cout << "Rester Copied!" << std::endl << std::endl;
411  }
412  }
413 
414  // Reproject
415  else if(g_vm.count("reproject"))
416  {
417  if(srid == -1)
418  {
419  showError("For reproject, srid is required!");
420  return EXIT_SUCCESS;
421  }
422  if(!g_rm.reproject(output, srid, errorMessage))
423  {
424  showError(errorMessage);
425  }
426  else
427  {
428  std::cout << "Rester Reprojected!" << std::endl << std::endl;
429  }
430  }
431 
432  // Convert
433  else if(g_vm.count("convert"))
434  {
435  if(extension.empty() && output.empty())
436  {
437  showError("For Convert, extension or output is required!");
438  return EXIT_SUCCESS;
439  }
440  if(!g_rm.convert(output, extension, bands, errorMessage))
441  {
442  showError(errorMessage);
443  }
444  else
445  {
446  std::cout << "Rester Converted!" << std::endl << std::endl;
447  }
448  }
449 
450  // Change Resolution
451  else if(g_vm.count("resolution"))
452  {
453  if(method == 0 || scale == 0)
454  {
455  showError("For change Resolution, method and scale are required!");
456  return EXIT_SUCCESS;
457  }
458  if(!g_rm.changeResolution(output, method, scale, errorMessage))
459  {
460  showError(errorMessage);
461  }
462  else
463  {
464  std::cout << "Resolution Changed!" << std::endl << std::endl;
465  }
466  }
467 
468  // Trim
469  else if(g_vm.count("trim"))
470  {
471  if(!g_rm.trim(output, envelope, errorMessage))
472  {
473  showError(errorMessage);
474  }
475  else
476  {
477  std::cout << "Trim executed!" << std::endl << std::endl;
478  }
479  }
480 
481  // Show Raster Info as Default
482  else
483  {
484  std::ostringstream output;
485  if(!g_rm.getRasterInfo(output, errorMessage))
486  {
487  showError(errorMessage);
488  }
489  else
490  std::cout << output.str();
491  }
492 
493  // Finalize Terralib
495 
496  }
497  catch(std::exception& e)
498  {
499  std::cout << "The application concluded erroneously: " << e.what() << std::endl;
500  }
501  catch(...)
502  {
503  std::cout << "The application concluded unexpectedly!" << std::endl;
504  }
505 
506  return EXIT_SUCCESS;
507 }
boost::program_options::options_description g_visible("Allowed options")
Which will be shown to the user.
Terralib Raster Manager Tool.
bool reproject(std::string output, int srid, std::string &errorMessage)
Reproject a raster.
boost::program_options::options_description g_functions("Functions")
Group of functions options.
Class with methods to manage and manipulate rasters.
Definition: RasterManager.h:44
boost::program_options::variables_map g_vm
Map o options after parse.
Definition: main.cpp:10
bool showSpecificHelp()
Definition: main.cpp:34
void showError(std::string errorMessage)
Definition: main.cpp:17
bool showSupportedExtensions(std::string &errorMessage)
Show Supported Extensions.
void finalize()
It finalizes the TerraLib Platform.
Definition: TerraLib.cpp:64
static TerraLib & getInstance()
It returns a reference to the singleton instance.
int main(int argc, char *argv[])
Definition: main.cpp:24
boost::program_options::options_description g_hidden("Hidden options")
Which will not be shown to the user.
te::tools::rastermanager::RasterManager g_rm
Raster Manager Object.
Definition: main.cpp:13
boost::program_options::options_description g_parameters("Parameters")
Group of parameters options.
bool getRasterInfo(std::ostringstream &output, std::string &errorMessage)
Show raster informations.
void initialize()
It initializes the TerraLib Platform.
Definition: TerraLib.cpp:33
This file contains include headers for the TerraLib Common Runtime module.
bool trim(std::string output, std::vector< double > env, std::string &errorMessage)
Trim a raster.
bool convert(std::string output, std::string extension, std::vector< int > bandVec, std::string &errorMessage)
Convert a raster.
bool init(std::string input, std::string &errorMessage)
Init the application.
boost::program_options::options_description g_help("Help")
Group of help options.
bool copyRaster(std::string output, std::vector< int > bandVec, int bandType, std::string &errorMessage)
Copy a raster.
boost::program_options::options_description g_all("All options")
All options.
bool changeResolution(std::string output, int method, int scale, std::string &errorMessage)
Change Raster resolution.