Orfeo Toolbox  4.0
otbWrapperApplication.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17  =========================================================================*/
18 #ifndef __otbWrapperApplication_h
19 #define __otbWrapperApplication_h
20 
21 #include <string>
22 #include "otbMacro.h"
23 #include "itkObject.h"
24 #include "itkObjectFactory.h"
25 #include "otbWrapperTypes.h"
26 #include "otbWrapperTags.h"
28 
29 #include "itkLogger.h"
30 #include "otbWrapperMacros.h"
37 
38 namespace otb
39 {
40 namespace Wrapper
41 {
42 
48 class ITK_EXPORT Application: public itk::Object
49 {
50 public:
52  typedef Application Self;
56 
58  itkTypeMacro(Application, itk::Object);
59 
61  //itkSetStringMacro(Name);
62 
63  virtual void SetName( const std::string & name )
64  {
65  m_Name = name;
66  GetDocExample()->SetApplicationName(name);
67  this->Modified();
68  }
69 
70  itkGetStringMacro(Name);
71 
73  itkSetStringMacro(Description);
74 
76  itkGetStringMacro(Description);
77 
79  void Init();
80 
82  bool IsInitialized() const;
83 
85  itkSetMacro(HaveInXML, bool);
86 
88  itkGetConstMacro(HaveInXML, bool);
89 
91  itkSetMacro(HaveOutXML, bool);
92 
94  itkGetConstMacro(HaveOutXML, bool);
95 
97  void UpdateParameters();
98 
110  int Execute();
111 
118  int ExecuteAndWriteOutput();
119 
120  /* Get the internal application parameters
121  *
122  * WARNING: this method may disappear from the API */
123  ParameterGroup* GetParameterList();
124 
125  /* Get the internal application parameter specified
126  *
127  * WARNING: this method may disappear from the API */
128  Parameter* GetParameterByKey(std::string parameter);
129 
130  /* Get the internal application parameter specified
131  *
132  * WARNING: this method may disappear from the API */
133  const Parameter* GetParameterByKey(std::string parameter) const;
134 
135  /* Returns the description of a parameter */
136  std::string GetParameterName(std::string paramKey);
137 
138  /* Returns the description of a parameter */
139  std::string GetParameterDescription(std::string paramKey);
140 
141  /* Set the description of a parameter */
142  void SetParameterDescription(std::string paramKey, std::string dec);
143 
144  /* Enable the use of an optional parameter. Returns the previous state */
145  void EnableParameter(std::string paramKey);
146 
147  /* Disable the use of an optional parameter. Returns the previous state */
148  void DisableParameter(std::string paramKey);
149 
150  /* Return the enable state of an optional parameter */
151  bool IsParameterEnabled(std::string paramKey, bool recurseParents = false) const;
152 
153  /* Return true if the specified parameter is mandatory */
154  bool IsMandatory(std::string paramKey) const;
155 
156  /* Return true if the specified parameter was set automatically in
157  * the application
158  */
159  bool HasAutomaticValue(std::string paramKey) const;
160 
161  /* Returns true if the parameter has an associated value provided externally
162  * (not automatically computed by the application) */
163  bool HasUserValue(std::string paramKey) const;
164 
165  /* If a user value was provided clear it and update the other parameters */
166  void ClearValue(std::string paramKey);
167 
168  /* Returns true if the parameter has an associated value.
169  * This value can be an automatically computed value or default value,
170  * or a value set externally by user */
171  bool HasValue(std::string paramKey) const;
172 
173  /* Activate or deactivate the bool parameter
174  */
175  void SetParameterEmpty(std::string paramKey, bool active);
176 
177  /* Get active flag of parameter with key paramKey
178  */
179  bool GetParameterEmpty(std::string paramKey);
180 
183  void SetParameterUserValue(std::string paramKey, bool value);
184 
185  /* Return the user level of access to a parameter */
186  UserLevel GetParameterUserLevel(std::string paramKey) const;
187 
189  Role GetParameterRole(std::string paramKey) const;
190 
191  /* Get the parameter type from its name */
192  ParameterType GetParameterType(std::string paramKey) const;
193 
194  /* Returns the description of a parameter */
195  std::vector<std::string> GetChoiceKeys(std::string paramKey);
196 
197  /* Returns the description of a parameter */
198  std::vector<std::string> GetChoiceNames(std::string paramKey);
199 
200  /* Is the application ready to be executed : All the mandatory
201  * parameters have to be set
202  */
203  /* Set the Parameter value and Update the UserFlag. used by xml parameter
204  */
205  void SetParameterInt(std::string parameter, int value, bool hasUserValueFlag);
206  void SetParameterFloat(std::string parameter, float value, bool hasUserValueFlag);
207  void SetParameterString(std::string parameter, std::string value, bool hasUserValueFlag);
208  void SetParameterStringList(std::string parameter, std::vector<std::string> values, bool hasUserValueFlag);
209  void SetParameterEmpty(std::string parameter, bool value, bool hasUserValueFlag);
210 
211  bool IsApplicationReady();
212 
213  /* Set an integer value
214  *
215  * Can be called for types :
216  * \li ParameterType_Int
217  * \li ParameterType_Float
218  * \li ParameterType_Radius
219  * \li ParameterType_Choice
220  */
221  void SetParameterInt(std::string parameter, int value);
222 
223  /* Set a floating value
224  *
225  * Can be called for types :
226  * \li ParameterType_Float
227  */
228  void SetParameterFloat(std::string parameter, float value);
229 
230  /* Set an default integer value, must used in the
231  * DoInit when setting a value by default
232  * for the parameter
233  *
234  * Can be called for types :
235  * \li ParameterType_Int
236  * \li ParameterType_Float
237  * \li ParameterType_Radius
238  * \li ParameterType_Choice
239  */
240  void SetDefaultParameterInt(std::string parameter, int value);
241 
242  /* Set a default floating value, must used in the
243  * DoInit when setting a value by default
244  * for the parameter
245  *
246  * Can be called for types :
247  * \li ParameterType_Float
248  */
249  void SetDefaultParameterFloat(std::string parameter, float value);
250 
251  /* Set a minimum int value, must used in the
252  * DoInit when setting a value by default
253  * for the parameter
254  *
255  * Can be called for types :
256  * \li ParameterType_Int
257  */
258  void SetMinimumParameterIntValue(std::string parameter, int value);
259 
260  /* Set a maximum int value, must used in the
261  * DoInit when setting a value by default
262  * for the parameter
263  *
264  * Can be called for types :
265  * \li ParameterType_Int
266  */
267  void SetMaximumParameterIntValue(std::string parameter, int value);
268 
269  /* Set a minimum int value, must used in the
270  * DoInit when setting a value by default
271  * for the parameter
272  *
273  * Can be called for types :
274  * \li ParameterType_Float
275  */
276  void SetMinimumParameterFloatValue(std::string parameter, float value);
277 
278  /* Set a maximum int value, must used in the
279  * DoInit when setting a value by default
280  * for the parameter
281  *
282  * Can be called for types :
283  * \li ParameterType_Float
284  */
285  void SetMaximumParameterFloatValue(std::string parameter, float value);
286 
287  /* Set a string value
288  *
289  * Can be called for types :
290  * \li ParameterType_String
291  * \li ParameterType_InputFilename
292  * \li ParameterType_OutputFilename
293  * \li ParameterType_Directory
294  * \li ParameterType_Choice
295  * \li ParameterType_Float
296  * \li ParameterType_Int
297  * \li ParameterType_Radius
298  * \li ParameterType_InputImageParameter
299  * \li ParameterType_InputComplexImageParameter
300  * \li ParameterType_InputVectorDataParameter
301  * \li ParameterType_OutputImageParameter
302  * \li ParameterType_OutputVectorDataParameter
303  */
304  void SetParameterString(std::string parameter, std::string value);
305 
306  /* Set a string value
307  *
308  * Can be called for types :
309  * \li ParameterType_InputImageListParameter
310  * \li ParameterType_InputVectorDataListParameter
311  * \li ParameterType_InputFilenameListParameter
312  * \li ParameterType_StringList
313  */
314  void SetParameterStringList(std::string parameter, std::vector<std::string> value);
315 
316  /* Set an output image value
317  *
318  * Can be called for types :
319  * \li ParameterType_OutputImage
320  */
321  void SetParameterOutputImage(std::string parameter, FloatVectorImageType* value);
322 
323  /* Set a complex output image value
324  *
325  * Can be called for types :
326  * \li ParameterType_ComplexOutputImage
327  */
328  void SetParameterComplexOutputImage(std::string parameter, ComplexFloatVectorImageType* value);
329 
330  /* Set the pixel type in which the image will be saved
331  *
332  * Can be called for types :
333  * \li ParameterType_OutputImage
334  */
335  void SetParameterOutputImagePixelType(std::string parameter, ImagePixelType pixelType);
336 
337  /* Set the complex pixel type in which the image will be saved
338  *
339  * Can be called for types :
340  * \li ParameterType_ComplexOutputImage
341  */
342  void SetParameterComplexOutputImagePixelType(std::string parameter, ComplexImagePixelType cpixelType);
343 
344  /* Set an output vector data value
345  *
346  * Can be called for types :
347  * \li ParameterType_OutputVectorData
348  */
349  void SetParameterOutputVectorData(std::string parameter, VectorDataType* value);
350 
351  /* Get an integer parameter value
352  *
353  * Can be called for types :
354  * \li ParameterType_Int
355  * \li ParameterType_Float
356  * \li ParameterType_Radius
357  * \li ParameterType_Choice
358  */
359  int GetParameterInt(std::string parameter);
360 
361  /* Get a floating parameter value
362  *
363  * Can be called for types :
364  * \li ParameterType_Float
365  */
366  float GetParameterFloat(std::string parameter);
367 
368  /* Get a string parameter value
369  *
370  * Can be called for types :
371  * \li ParameterType_String
372  * \li ParameterType_InputFilename
373  * \li ParameterType_OutputFilename
374  * \li ParameterType_Directory
375  * \li ParameterType_InputImage
376  * \li ParameterType_InputComplexImage
377  * \li ParameterType_InputVectorData
378  * \li ParameterType_OutputImage
379  * \li ParameterType_OutputVectorData
380  */
381  std::string GetParameterString(std::string parameter);
382 
383  /* Get a string list parameter value
384  *
385  * Can be called for types :
386  * \li ParameterType_StringList
387  * \li ParameterType_InputImageList
388  * \li ParameterType_InputFilenameList
389  */
390  std::vector<std::string> GetParameterStringList(std::string parameter);
391 
392  /* Get an image value
393  *
394  * Can be called for types :
395  * \li ParameterType_InputImage
396  */
397  FloatVectorImageType* GetParameterImage(std::string parameter);
398 
399 #define otbGetParameterImageMacro( Image ) \
400  Image##Type * GetParameter##Image( std::string parameter ) \
401  { \
402  Image##Type::Pointer ret; \
403  Parameter* param = GetParameterByKey(parameter); \
404  if (dynamic_cast<InputImageParameter*>(param)) \
405  { \
406  InputImageParameter* paramDown = dynamic_cast<InputImageParameter*>(param); \
407  ret = paramDown->Get##Image(); \
408  } \
409  return ret; \
410  }
411 
412  otbGetParameterImageMacro(UInt8Image);
413  otbGetParameterImageMacro(UInt16Image);
414  otbGetParameterImageMacro(Int16Image);
415  otbGetParameterImageMacro(UInt32Image);
416  otbGetParameterImageMacro(Int32Image);
417  otbGetParameterImageMacro(FloatImage);
418  otbGetParameterImageMacro(DoubleImage);
419 
420  otbGetParameterImageMacro(UInt8VectorImage);
421  otbGetParameterImageMacro(UInt16VectorImage);
422  otbGetParameterImageMacro(Int16VectorImage);
423  otbGetParameterImageMacro(UInt32VectorImage);
424  otbGetParameterImageMacro(Int32VectorImage);
425  otbGetParameterImageMacro(FloatVectorImage);
426  otbGetParameterImageMacro(DoubleVectorImage);
427 
428  otbGetParameterImageMacro(UInt8RGBImage);
429  otbGetParameterImageMacro(UInt8RGBAImage);
430 
431  /* Get a complex image value
432  *
433  * Can be called for types :
434  * \li ParameterType_ComplexInputImage
435  */
436 
437 #define otbGetParameterComplexImageMacro( Image ) \
438  Image##Type * GetParameter##Image( std::string parameter ) \
439  { \
440  Image##Type::Pointer ret; \
441  Parameter* param = GetParameterByKey(parameter); \
442  if (dynamic_cast<ComplexInputImageParameter*>(param)) \
443  { \
444  ComplexInputImageParameter* paramDown = dynamic_cast<ComplexInputImageParameter*>(param); \
445  ret = paramDown->Get##Image(); \
446  } \
447  return ret; \
448  }
449 
450  otbGetParameterComplexImageMacro(ComplexFloatImage);
451  otbGetParameterComplexImageMacro(ComplexDoubleImage);
452 
453  otbGetParameterComplexImageMacro(ComplexFloatVectorImage);
454  otbGetParameterComplexImageMacro(ComplexDoubleVectorImage);
455 
456  /* Get an image list value
457  *
458  * Can be called for types :
459  * \li ParameterType_InputImageList
460  */
461  FloatVectorImageListType* GetParameterImageList(std::string parameter);
462 
463  /* Get a complex image value
464  *
465  * Can be called for types :
466  * \li ParameterType_InputComplexImage
467  */
468  ComplexFloatVectorImageType* GetParameterComplexImage(std::string parameter);
469 
470  /* GetParameterVectorData
471  *
472  * Can be called for types :
473 
474  * \li ParameterType_InputVectorData
475  */
476  VectorDataType* GetParameterVectorData(std::string parameter);
477 
478  /* GetParameteVetorDataList
479  *
480  * Can be called for types :
481 
482  * \li ParameterType_InputVectorDatalist
483  */
484  VectorDataListType* GetParameterVectorDataList(std::string parameter);
485 
486  /* Get the parameter as a std::string
487  *
488  * Can be called for types :
489  * \li ParameterType_Float
490  * \li ParameterType_Int
491  * \li ParameterType_Choice
492  * \li ParameterType_Radius
493  * \li ParameterType_String
494  * \li ParameterType_InputFilename
495  * \li ParameterType_OutputFilename
496  * \li ParameterType_Directory
497  * \li ParameterType_InputImage
498  * \li ParameterType_InputComplexImage
499  * \li ParameterType_InputVectorData
500  * \li ParameterType_OutputImage
501  * \li ParameterType_OutputVectorData
502  */
503  std::string GetParameterAsString(std::string paramKey);
504 
505  /* Get the list of all parameters
506  */
507  std::vector<std::string> GetParametersKeys(bool recursive = true);
508 
509  /* Get the pixel type in which the image will be saved
510  *
511  * Can be called for types :
512  * \li ParameterType_OutputImage
513  */
514  ImagePixelType GetParameterOutputImagePixelType(std::string parameter);
515 
516  void SetParameterList(ParameterGroup::Pointer paramGroup)
517  {
518  m_ParameterList = paramGroup;
519  }
520 
521  /* Get the pixel type in which the complex image will be saved
522  *
523  * Can be called for types :
524  * \li ParameterType_ComplexOutputImage
525  */
526  ComplexImagePixelType GetParameterComplexOutputImagePixelType(std::string parameter);
527 
528  itk::Logger* GetLogger();
529 
530  itk::ProcessObject* GetProgressSource() const;
531 
532  std::string GetProgressDescription() const;
533 
535  itkSetStringMacro(DocName);
536  itkGetStringMacro(DocName);
537  itkSetStringMacro(DocLongDescription);
538  itkGetStringMacro(DocLongDescription);
539  itkSetStringMacro(DocAuthors);
540  itkGetStringMacro(DocAuthors);
541  itkSetStringMacro(DocLimitations);
542  itkGetStringMacro(DocLimitations);
543  itkSetStringMacro(DocSeeAlso);
544  itkGetStringMacro(DocSeeAlso);
545  std::vector<std::string> GetDocTags(){
546  return m_DocTags;
547  }
548  void SetDocTags( std::vector<std::string> val ){
549  m_DocTags = val;
550  this->Modified();
551  }
552 
553  void AddDocTag( const std::string & tag )
554  {
555  for (unsigned int i=0; i<m_DocTags.size(); i++)
556  {
557  if (m_DocTags[i].compare(tag) == 0) return;
558  }
559  m_DocTags.push_back( tag );
560  this->Modified();
561  }
562 
564  {
565  if (! IsInitialized())
566  {
567  Init();
568  }
569 
570  return m_DocExample;
571  }
572 
573  unsigned int GetNumberOfExamples()
574  {
575  return GetDocExample()->GetNbOfExamples();
576  }
577 
578  std::string GetExampleComment(unsigned int id)
579  {
580  return GetDocExample()->GetExampleComment(id);
581  }
582 
583  unsigned int GetExampleNumberOfParameters(unsigned int id)
584  {
585  return GetDocExample()->GetNumberOfParameters(id);
586  }
587 
588  std::string GetExampleParameterKey(unsigned int exId, unsigned int paramId)
589  {
590  return GetDocExample()->GetParameterKey(paramId, exId);
591  }
592 
593  std::string GetExampleParameterValue(unsigned int exId, unsigned int paramId)
594  {
595  return GetDocExample()->GetParameterValue(paramId, exId);
596  }
597 
598  void SetDocExampleParameterValue( const std::string key, const std::string value, unsigned int exId=0 )
599  {
600  GetDocExample()->AddParameter( key, value, exId );
601  this->Modified();
602  }
603 
604  void SetExampleComment( const std::string & comm, unsigned int i )
605  {
606  GetDocExample()->SetExampleComment( comm, i );
607  this->Modified();
608  }
609 
610  unsigned int AddExample( const std::string & comm="" )
611  {
612  unsigned int id = GetDocExample()->AddExample( comm );
613  this->Modified();
614  return id;
615  }
616 
617  std::string GetCLExample()
618  {
619  return GetDocExample()->GenerateCLExample();
620  }
621 
622  std::string GetHtmlExample()
623  {
624  return GetDocExample()->GenerateHtmlExample();
625  }
626 
630  std::vector< std::pair<std::string, std::string> > GetOutputParametersSumUp();
631 
633  void ForceInXMLParseFlag()
634  {
635  m_IsInXMLParsed = false;
636  }
637 
638 protected:
640  Application();
641 
643  virtual ~Application();
644 
645  /* Register a ProcessObject as a new progress source */
646  void AddProcess(itk::ProcessObject* object, std::string description);
647 
649  void AddChoice(std::string paramKey, std::string paramName);
650 
654  void AddParameter(ParameterType type, std::string paramKey, std::string paramName);
655 
657  void AddRAMParameter(std::string paramKey="ram");
658 
660  void AddRAMParameter(std::string paramKey, std::string paramName, unsigned int defaultValue);
661 
663  void AddRANDParameter(std::string paramKey="rand");
664 
667  void AddRANDParameter(std::string paramKey, std::string paramName, unsigned int defaultValue);
668 
669  void AddInXMLParameter()
670  {
671  GetParameterList()->AddInXMLParameter();
672  }
673 
674  void AddOutXMLParameter()
675  {
676  GetParameterList()->AddOutXMLParameter();
677  }
678 
680  void ClearChoices(std::string key);
681 
683  std::vector<int> GetSelectedItems(std::string paramKey);
684 
686  void MandatoryOn(std::string paramKey);
687 
689  void MandatoryOff(std::string paramKey);
690 
691  /* Set the user level of access to a parameter */
692  void SetParameterUserLevel(std::string paramKey, UserLevel level);
693 
694  /* Set the parameter role (input/output) */
695  void SetParameterRole(std::string paramKey, Role role);
696 
697  /* Get an image value
698  *
699  * Can be called for types :
700  * \li ParameterType_InputImage
701  */
702  template <class TImageType>
703  TImageType* GetParameterImage(std::string parameter)
704  {
705  typename TImageType::Pointer ret;
706  Parameter* param = GetParameterByKey(parameter);
707  if (dynamic_cast<InputImageParameter*>(param))
708  {
709  InputImageParameter* paramDown = dynamic_cast<InputImageParameter*>(param);
710  ret = paramDown->GetImage<TImageType>();
711  }
712 
713  //TODO: exception if not found ?
714  return ret;
715  }
716 
718  void AutomaticValueOn(std::string paramKey);
719 
721  void AutomaticValueOff(std::string paramKey);
722 
723  /* Set an output image value
724  *
725  * Can be called for types :
726  * \li ParameterType_OutputImage
727  */
728  template <class TImageType>
729  void SetParameterOutputImage(std::string parameter, TImageType* value)
730  {
731  Parameter* param = GetParameterByKey(parameter);
732 
733  if (dynamic_cast<OutputImageParameter*>(param))
734  {
735  OutputImageParameter* paramDown = dynamic_cast<OutputImageParameter*>(param);
736  paramDown->SetValue(value);
737  }
738  }
739 
740  /* Set a complex output image value
741  *
742  * Can be called for types :
743  * \li ParameterType_ComplexOutputImage
744  */
745  template <class TImageType>
746  void SetParameterComplexOutputImage(std::string parameter, TImageType* value)
747  {
748  Parameter* param = GetParameterByKey(parameter);
749 
750  if (dynamic_cast<ComplexOutputImageParameter*>(param))
751  {
752  ComplexOutputImageParameter* paramDown = dynamic_cast<ComplexOutputImageParameter*>(param);
753  paramDown->SetValue(value);
754  }
755  }
756 
757 
758 private:
759  /* Implement this method to add parameters */
760  virtual void DoInit() = 0;
761 
762  /* Implement this method to update non valued parameters */
763  virtual void DoUpdateParameters() = 0;
764 
765  /* Implement this method to build the output */
766  virtual void DoExecute() = 0;
767 
768  /* This method will be called after the
769  * ExecuteAndWriteOutput() call to allow for cleanup. Default
770  * implementation does nothing */
771  virtual void AfterExecuteAndWriteOutputs();
772 
773  Application(const Application &); //purposely not implemented
774  void operator =(const Application&); //purposely not implemented
775 
776  std::string m_Name;
777  std::string m_Description;
780 
783 
785  std::string m_DocName;
787  std::string m_DocLongDescription;
791  std::string m_DocAuthors;
793  std::string m_DocLimitations;
795  std::string m_DocSeeAlso;
797  std::vector<std::string> m_DocTags;
798 
799  //rashad:: controls adding of -xml parameter. set to true by default
812 
813 }; //end class
814 
815 } // end namespace Wrapper
816 } //end namespace otb
817 
818 
819 //#ifndef OTB_MANUAL_INSTANTIATION
820 //#include "otbWrapperApplication.txx"
821 //#endif
822 
823 #endif // __otbWrapperApplication_h_

Generated at Sat Mar 8 2014 16:28:47 for Orfeo Toolbox with doxygen 1.8.3.1