Orfeo Toolbox  3.16
otbWrapperApplication.cxx
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 #include "otbWrapperApplication.h"
19 
34 #include "otbWrapperRAMParameter.h"
35 
37 
39 
40 
41 #include "otbWrapperTypes.h"
42 #include <exception>
43 #include "itkMacro.h"
44 
45 namespace otb
46 {
47 namespace Wrapper
48 {
49 
51  : m_Name(""),
52  m_Description(""),
53  m_Logger(itk::Logger::New()),
54  m_ProgressSourceDescription(""),
55  m_DocName(""),
56  m_DocLongDescription(""),
57  m_DocAuthors(""),
58  m_DocLimitations(""),
59  m_DocSeeAlso(""),
60  m_DocTags()
61 {
62  // Don't call Init from the constructor, since it calls a virtual method !
63 
64  m_Logger->SetName("Application.logger");
65  m_Logger->SetPriorityLevel(itk::LoggerBase::DEBUG);
66  m_Logger->SetLevelForFlushing(itk::LoggerBase::CRITICAL);
67 }
68 
70 {
71 }
72 
74 {
75  return m_Logger;
76 }
77 
78 std::vector<std::string>
80 {
81  return GetParameterList()->GetParametersKeys(recursive);
82 }
83 
85 {
87 }
88 
90 {
91  if (!IsInitialized())
92  {
93  Init();
94  }
95 
96  return m_ParameterList;
97 }
98 
100 {
101  return GetParameterList()->GetParameterByKey(name);
102 }
103 
104 const Parameter* Application::GetParameterByKey(std::string name) const
105 {
106  // GetParameterList is non const...
107  Application* _this = const_cast<Application*>(this);
108  return _this->GetParameterByKey(name);
109 }
110 
112 {
115  this->DoInit();
116 }
117 
119 {
120  this->DoUpdateParameters();
121 }
122 
124 {
125  int ret = 0;
126  // before execute we set the seed of mersenne twister
127  std::vector<std::string> paramList = GetParametersKeys(true);
128  bool UseSpecificSeed = false;
129 
130  for (std::vector<std::string>::const_iterator it = paramList.begin(); it != paramList.end(); ++it)
131  {
132  std::string key = *it;
133 
134  if ((key.compare(0, 4, "rand") == 0) && HasValue("rand"))
135  {
136  UseSpecificSeed = true;
137  Parameter* param = GetParameterByKey(key);
138  IntParameter* randParam = dynamic_cast<IntParameter*> (param);
139  int seed = randParam->GetValue();
141  }
142 
143  }
144 
145  if (!UseSpecificSeed)
146  {
148  }
149 
150  this->DoExecute();
151 
152  return ret;
153 }
154 
156 {
157  int status = this->Execute();
158 
159  if (status == 0)
160  {
161  std::vector<std::string> paramList = GetParametersKeys(true);
162  // First Get the value of the available memory to use with the
163  // writer if a RAMParameter is set
164  bool useRAM = false;
165  unsigned int ram = 0;
166  for (std::vector<std::string>::const_iterator it = paramList.begin();
167  it != paramList.end();
168  ++it)
169  {
170  std::string key = *it;
171 
173  && IsParameterEnabled(key))
174  {
175  Parameter* param = GetParameterByKey(key);
176  RAMParameter* ramParam = dynamic_cast<RAMParameter*>(param);
177  ram = ramParam->GetValue();
178  useRAM = true;
179  }
180  }
181 
182  for (std::vector<std::string>::const_iterator it = paramList.begin();
183  it != paramList.end();
184  ++it)
185  {
186  std::string key = *it;
188  && IsParameterEnabled(key) && HasValue(key) )
189  {
190  Parameter* param = GetParameterByKey(key);
191  OutputImageParameter* outputParam = dynamic_cast<OutputImageParameter*>(param);
192  outputParam->InitializeWriters();
193  if (useRAM)
194  {
195  outputParam->SetRAMValue(ram);
196  }
197  std::ostringstream progressId;
198  progressId << "Writing " << outputParam->GetFileName() << "...";
199  AddProcess(outputParam->GetWriter(), progressId.str());
200  outputParam->Write();
201  }
203  && IsParameterEnabled(key) && HasValue(key) )
204  {
205  Parameter* param = GetParameterByKey(key);
206  OutputVectorDataParameter* outputParam = dynamic_cast<OutputVectorDataParameter*>(param);
207  outputParam->InitializeWriters();
208  std::ostringstream progressId;
209  progressId << "Writing " << outputParam->GetFileName() << "...";
210  AddProcess(outputParam->GetWriter(), progressId.str());
211  outputParam->Write();
212  }
214  && IsParameterEnabled(key) && HasValue(key) )
215  {
216  Parameter* param = GetParameterByKey(key);
217  ComplexOutputImageParameter* outputParam = dynamic_cast<ComplexOutputImageParameter*>(param);
218  outputParam->InitializeWriters();
219  if (useRAM)
220  {
221  outputParam->SetRAMValue(ram);
222  }
223  std::ostringstream progressId;
224  progressId << "Writing " << outputParam->GetFileName() << "...";
225  AddProcess(outputParam->GetWriter(), progressId.str());
226  outputParam->Write();
227  }
228  }
229  }
230 
231  return status;
232 }
233 
234 /* Enable the use of an optional parameter. Returns the previous state */
235 void Application::EnableParameter(std::string paramKey)
236 {
237  Parameter* param = GetParameterByKey(paramKey);
238  param->SetActive(true);
239 }
240 
241 /* Disable the use of an optional parameter. Returns the previous state */
242 void Application::DisableParameter(std::string paramKey)
243 {
244  GetParameterByKey(paramKey)->SetActive(false);
245 }
246 
247 /* Return the enable state of an optional parameter */
248 bool Application::IsParameterEnabled(std::string paramKey) const
249 {
250  return GetParameterByKey(paramKey)->GetActive();
251 }
252 
253 /* Return true if the specified parameter is mandatory */
254 bool Application::IsMandatory(std::string paramKey) const
255 {
256  return GetParameterByKey(paramKey)->GetMandatory();
257 }
258 
259 void Application::MandatoryOn(std::string paramKey)
260 {
261  GetParameterByKey(paramKey)->SetMandatory(true);
262 }
263 
264 void Application::MandatoryOff(std::string paramKey)
265 {
266  GetParameterByKey(paramKey)->SetMandatory(false);
267 }
268 
269 /* Return true if the specified parameter was set automatically in the
270  * application
271  */
272 bool Application::HasAutomaticValue(std::string paramKey) const
273 {
274  return GetParameterByKey(paramKey)->GetAutomaticValue();
275 }
276 
277 void Application::AutomaticValueOn(std::string paramKey)
278 {
279  GetParameterByKey(paramKey)->SetAutomaticValue(true);
280 }
281 
282 void Application::AutomaticValueOff(std::string paramKey)
283 {
284  GetParameterByKey(paramKey)->SetAutomaticValue(false);
285 }
286 
287 /* Returns true if the parameter has an associated value provided externally
288  * (not automatically computed by the application) */
289 bool Application::HasUserValue(std::string paramKey) const
290 {
291  return GetParameterByKey(paramKey)->HasUserValue();
292 }
293 
294 /* If a user value was provided clear it and update the other parameters */
295 void Application::ClearValue(std::string paramKey)
296 {
297  GetParameterByKey(paramKey)->ClearValue();
298 }
299 
300 /* Returns true if the parameter has an associated value.
301  * This value can be an automatically computed value or default value,
302  * or a value set externally by user */
303 bool Application::HasValue(std::string paramKey) const
304 {
305  return GetParameterByKey(paramKey)->HasValue();
306 }
307 
308 /* Return the user level of access to a parameter */
310 {
311  return GetParameterByKey(paramKey)->GetUserLevel();
312 }
313 
314 
315 /* Return the role (input/output) of a parameter */
316 Role Application::GetParameterRole(std::string paramKey) const
317 {
318  return GetParameterByKey(paramKey)->GetRole();
319 }
320 
321 /* Return the role (input/output) of a parameter */
322 void Application::SetParameterRole(std::string paramKey, Role role)
323 {
324  GetParameterByKey(paramKey)->SetRole(role);
325 }
326 
327 /* Get the parameter type from its name */
328 ParameterType Application::GetParameterType(std::string paramKey) const
329 {
330  const Parameter* param = GetParameterByKey(paramKey);
331  ParameterType type;
332 
333  if (dynamic_cast<const ChoiceParameter*>(param))
334  {
335  type = ParameterType_Choice;
336  }
337  else if (dynamic_cast<const ListViewParameter*>(param))
338  {
339  type = ParameterType_ListView;
340  }
341  else if (dynamic_cast<const RadiusParameter*>(param))
342  {
343  type = ParameterType_Radius;
344  }
345  else if (dynamic_cast<const EmptyParameter*>(param))
346  {
347  type = ParameterType_Empty;
348  }
349  else if (dynamic_cast<const IntParameter*>(param))
350  {
351  type = ParameterType_Int;
352  }
353  else if (dynamic_cast<const FloatParameter*>(param))
354  {
355  type = ParameterType_Float;
356  }
357  else if (dynamic_cast<const InputFilenameParameter*>(param))
358  {
360  }
361  else if (dynamic_cast<const OutputFilenameParameter*>(param))
362  {
364  }
365  else if (dynamic_cast<const DirectoryParameter*>(param))
366  {
368  }
369  else if (dynamic_cast<const InputImageParameter*>(param))
370  {
372  }
373  else if (dynamic_cast<const InputImageListParameter*>(param))
374  {
376  }
377  else if (dynamic_cast<const ComplexInputImageParameter*>(param))
378  {
380  }
381  else if (dynamic_cast<const InputVectorDataParameter*>(param))
382  {
384  }
385  else if (dynamic_cast<const InputVectorDataListParameter*>(param))
386  {
388  }
389  else if (dynamic_cast<const OutputImageParameter*>(param))
390  {
392  }
393  else if (dynamic_cast<const ComplexOutputImageParameter*>(param))
394  {
396  }
397  else if (dynamic_cast<const OutputVectorDataParameter*>(param))
398  {
400  }
401  else if (dynamic_cast<const StringParameter*>(param))
402  {
403  type = ParameterType_String;
404  }
405  else if (dynamic_cast<const StringListParameter*>(param))
406  {
408  }
409  else if (dynamic_cast<const RAMParameter*>(param))
410  {
411  type = ParameterType_RAM;
412  }
413  else if (dynamic_cast<const ParameterGroup*>(param))
414  {
415  type = ParameterType_Group;
416  }
417  else
418  {
419  itkExceptionMacro(<< "Unknown parameter : " << paramKey);
420  }
421 
422  return type;
423 }
424 
425 std::vector<std::string> Application::GetChoiceKeys(std::string name)
426 {
427  Parameter* param = GetParameterByKey(name);
428  if (dynamic_cast<ChoiceParameter*>(param))
429  {
430  ChoiceParameter* paramChoice = dynamic_cast<ChoiceParameter*>(param);
431  return paramChoice->GetChoiceKeys();
432  }
433  else if (dynamic_cast<ListViewParameter*>(param))
434  {
435  ListViewParameter* paramList = dynamic_cast<ListViewParameter*>(param);
436  return paramList->GetChoiceKeys();
437  }
438  itkExceptionMacro(<< name << " is not a choice parameter");
439 }
440 
441 std::vector<std::string> Application::GetChoiceNames(std::string name)
442 {
443  Parameter* param = GetParameterByKey(name);
444  if (dynamic_cast<ChoiceParameter*>(param))
445  {
446  ChoiceParameter* paramChoice = dynamic_cast<ChoiceParameter*>(param);
447  return paramChoice->GetChoiceNames();
448  }
449  else if (dynamic_cast<ListViewParameter*>(param))
450  {
451  ListViewParameter* paramList = dynamic_cast<ListViewParameter*>(param);
452  return paramList->GetChoiceNames();
453  }
454  itkExceptionMacro(<< name << " is not a choice parameter");
455 }
456 
457 void Application::SetParameterInt(std::string parameter, int value)
458 {
459  Parameter* param = GetParameterByKey(parameter);
460 
461  if (dynamic_cast<IntParameter*>(param))
462  {
463  IntParameter* paramInt = dynamic_cast<IntParameter*>(param);
464  paramInt->SetValue(value);
465  }
466  else if (dynamic_cast<FloatParameter*>(param))
467  {
468  FloatParameter* paramFloat = dynamic_cast<FloatParameter*>(param);
469  paramFloat->SetValue(static_cast<float>(value));
470  }
471  else if (dynamic_cast<RadiusParameter*>(param))
472  {
473  RadiusParameter* paramRadius = dynamic_cast<RadiusParameter*>(param);
474  paramRadius->SetValue(static_cast<unsigned int>(value));
475  }
476  else if (dynamic_cast<ChoiceParameter*>(param))
477  {
478  ChoiceParameter* paramChoice = dynamic_cast<ChoiceParameter*>(param);
479  paramChoice->SetValue(value);
480  }
481 }
482 
483 void Application::SetParameterFloat(std::string parameter, float value)
484 {
485  Parameter* param = GetParameterByKey(parameter);
486 
487  if (dynamic_cast<FloatParameter*>(param))
488  {
489  FloatParameter* paramFloat = dynamic_cast<FloatParameter*>(param);
490  paramFloat->SetValue(value);
491  }
492 }
493 
494 void Application::SetDefaultParameterInt(std::string parameter, int value)
495 {
496  Parameter* param = GetParameterByKey(parameter);
497 
498  if (dynamic_cast<RadiusParameter*>(param))
499  {
500  RadiusParameter* paramRadius = dynamic_cast<RadiusParameter*>(param);
501  paramRadius->SetDefaultValue(value);
502  paramRadius->SetValue(value);
503  }
504  else if (dynamic_cast<IntParameter*>(param))
505  {
506  IntParameter* paramInt = dynamic_cast<IntParameter*>(param);
507  paramInt->SetDefaultValue(value);
508  paramInt->SetValue(value);
509  }
510  else if (dynamic_cast<FloatParameter*>(param))
511  {
512  FloatParameter* paramFloat = dynamic_cast<FloatParameter*>(param);
513  paramFloat->SetDefaultValue(static_cast<float>(value));
514  paramFloat->SetValue(static_cast<float>(value));
515  }
516  else if (dynamic_cast<RAMParameter*>(param))
517  {
518  RAMParameter* paramRAM = dynamic_cast<RAMParameter*>(param);
519  paramRAM->SetDefaultValue(static_cast<unsigned int>(value));
520  paramRAM->SetValue(static_cast<unsigned int>(value));
521  }
522 }
523 
524 void Application::SetDefaultParameterFloat(std::string parameter, float value)
525 {
526  Parameter* param = GetParameterByKey(parameter);
527 
528  if (dynamic_cast<FloatParameter*>(param))
529  {
530  FloatParameter* paramFloat = dynamic_cast<FloatParameter*>(param);
531  paramFloat->SetDefaultValue(value);
532  paramFloat->SetValue(value);
533  }
534 }
535 
536 void Application::SetMinimumParameterIntValue(std::string parameter, int value)
537 {
538  Parameter* param = GetParameterByKey(parameter);
539 
540  if (dynamic_cast<IntParameter*>(param))
541  {
542  IntParameter* paramInt = dynamic_cast<IntParameter*>(param);
543  paramInt->SetMinimumValue(value);
544  }
545  else
546  itkExceptionMacro(<<parameter << "parameter can't be casted to int");
547 }
548 
549 void Application::SetMaximumParameterIntValue(std::string parameter, int value)
550 {
551  Parameter* param = GetParameterByKey(parameter);
552 
553  if (dynamic_cast<IntParameter*>(param))
554  {
555  IntParameter* paramInt = dynamic_cast<IntParameter*>(param);
556  paramInt->SetMaximumValue(value);
557  }
558  else
559  itkExceptionMacro(<<parameter << "parameter can't be casted to int");
560 
561 }
562 
563 void Application::SetMinimumParameterFloatValue(std::string parameter, float value)
564 {
565  Parameter* param = GetParameterByKey(parameter);
566 
567  if (dynamic_cast<FloatParameter*>(param))
568  {
569  FloatParameter* paramFloat = dynamic_cast<FloatParameter*>(param);
570  paramFloat->SetMinimumValue(value);
571  }
572  else
573  itkExceptionMacro(<<parameter << "parameter can't be casted to float");
574 }
575 
576 void Application::SetMaximumParameterFloatValue(std::string parameter, float value)
577 {
578  Parameter* param = GetParameterByKey(parameter);
579 
580  if (dynamic_cast<FloatParameter*>(param))
581  {
582  FloatParameter* paramFloat = dynamic_cast<FloatParameter*>(param);
583  paramFloat->SetMaximumValue(value);
584  }
585  else
586  itkExceptionMacro(<<parameter << "parameter can't be casted to float");
587 
588 }
589 
590 
591 void Application::SetParameterString(std::string parameter, std::string value)
592 {
593  Parameter* param = GetParameterByKey(parameter);
594 
595  if (dynamic_cast<ChoiceParameter*>(param))
596  {
597  ChoiceParameter* paramDown = dynamic_cast<ChoiceParameter*>(param);
598  paramDown->SetValue(value);
599  }
600  else if (dynamic_cast<ListViewParameter*>(param))
601  {
602  ListViewParameter* paramDown = dynamic_cast<ListViewParameter*>(param);
603  paramDown->SetValue(value);
604  }
605  else if (dynamic_cast<StringParameter*>(param))
606  {
607  StringParameter* paramDown = dynamic_cast<StringParameter*>(param);
608  paramDown->SetValue(value);
609  }
610  else if (dynamic_cast<InputFilenameParameter*>(param))
611  {
612  InputFilenameParameter* paramDown = dynamic_cast<InputFilenameParameter*>(param);
613  paramDown->SetValue(value);
614  }
615  else if (dynamic_cast<OutputFilenameParameter*>(param))
616  {
617  OutputFilenameParameter* paramDown = dynamic_cast<OutputFilenameParameter*>(param);
618  paramDown->SetValue(value);
619  }
620  else if (dynamic_cast<DirectoryParameter*>(param))
621  {
622  DirectoryParameter* paramDown = dynamic_cast<DirectoryParameter*>(param);
623  paramDown->SetValue(value);
624  }
625  else if (dynamic_cast<FloatParameter*>(param))
626  {
627  FloatParameter* paramDown = dynamic_cast<FloatParameter*>(param);
628  paramDown->SetValue(value);
629  }
630  else if (dynamic_cast<RadiusParameter*>(param))
631  {
632  RadiusParameter* paramDown = dynamic_cast<RadiusParameter*>(param);
633  paramDown->SetValue(value);
634  }
635  else if (dynamic_cast<IntParameter*>(param))
636  {
637  IntParameter* paramDown = dynamic_cast<IntParameter*>(param);
638  paramDown->SetValue(value);
639  }
640  else if (dynamic_cast<InputImageParameter*>(param))
641  {
642  InputImageParameter* paramDown = dynamic_cast<InputImageParameter*>(param);
643  if ( !paramDown->SetFromFileName(value) )
644  otbAppLogCRITICAL( <<"Invalid image filename " << value <<".");
645 
646  }
647  else if (dynamic_cast<ComplexInputImageParameter*>(param))
648  {
649  ComplexInputImageParameter* paramDown = dynamic_cast<ComplexInputImageParameter*>(param);
650  paramDown->SetFromFileName(value);
651  }
652  else if (dynamic_cast<InputVectorDataParameter*>(param))
653  {
654  InputVectorDataParameter* paramDown = dynamic_cast<InputVectorDataParameter*>(param);
655  if ( !paramDown->SetFromFileName(value) )
656  otbAppLogCRITICAL( <<"Invalid vector data filename " << value <<".");
657  }
658  else if (dynamic_cast<OutputImageParameter*>(param))
659  {
660  OutputImageParameter* paramDown = dynamic_cast<OutputImageParameter*>(param);
661  paramDown->SetFileName(value);
662  }
663  else if (dynamic_cast<ComplexOutputImageParameter*>(param))
664  {
665  ComplexOutputImageParameter* paramDown = dynamic_cast<ComplexOutputImageParameter*>(param);
666  paramDown->SetFileName(value);
667  }
668  else if (dynamic_cast<OutputVectorDataParameter*>(param))
669  {
670  OutputVectorDataParameter* paramDown = dynamic_cast<OutputVectorDataParameter*>(param);
671  paramDown->SetFileName(value);
672  }
673  else if (dynamic_cast<RAMParameter*>(param))
674  {
675  RAMParameter* paramDown = dynamic_cast<RAMParameter*>(param);
676  paramDown->SetValue(value);
677  }
678 }
679 
680 void Application::SetParameterStringList(std::string parameter, std::vector<std::string> value)
681 {
682  Parameter* param = GetParameterByKey(parameter);
683 
684  if (dynamic_cast<InputImageListParameter*>(param))
685  {
686  InputImageListParameter* paramDown = dynamic_cast<InputImageListParameter*>(param);
687  if( !paramDown->SetListFromFileName(value) )
688  otbAppLogCRITICAL( <<"At least one image filename is invalid.");
689  }
690  else if (dynamic_cast<InputVectorDataListParameter*>(param))
691  {
692  InputVectorDataListParameter* paramDown = dynamic_cast<InputVectorDataListParameter*>(param);
693  if( !paramDown->SetListFromFileName(value) )
694  otbAppLogCRITICAL( <<"At least one vector data filename is invalid..");
695  }
696  else if (dynamic_cast<StringListParameter*>(param))
697  {
698  StringListParameter* paramDown = dynamic_cast<StringListParameter*>(param);
699  paramDown->SetValue(value);
700  }
701 }
702 
704 {
705  Parameter* param = GetParameterByKey(parameter);
706 
707  if (dynamic_cast<OutputImageParameter*>(param))
708  {
709  OutputImageParameter* paramDown = dynamic_cast<OutputImageParameter*>(param);
710  paramDown->SetValue(value);
711  }
712 }
713 
715 {
716  Parameter* param = GetParameterByKey(parameter);
717 
718  if (dynamic_cast<ComplexOutputImageParameter*>(param))
719  {
720  ComplexOutputImageParameter* paramDown = dynamic_cast<ComplexOutputImageParameter*>(param);
721  paramDown->SetValue(value);
722  }
723 }
724 
725 void Application::SetParameterOutputImagePixelType(std::string parameter, ImagePixelType pixelType)
726 {
727  Parameter* param = GetParameterByKey(parameter);
728 
729  if (dynamic_cast<OutputImageParameter*>(param))
730  {
731  OutputImageParameter* paramDown = dynamic_cast<OutputImageParameter*>(param);
732  paramDown->SetPixelType(pixelType);
733  }
734 }
735 
737  ComplexImagePixelType cpixelType)
738 {
739  Parameter* param = GetParameterByKey(parameter);
740 
741  if (dynamic_cast<ComplexOutputImageParameter*>(param))
742  {
743  ComplexOutputImageParameter* paramDown = dynamic_cast<ComplexOutputImageParameter*>(param);
744  paramDown->SetComplexPixelType(cpixelType);
745  }
746 }
748 {
749  Parameter* param = GetParameterByKey(parameter);
750 
751  if (dynamic_cast<OutputVectorDataParameter*>(param))
752  {
753  OutputVectorDataParameter* paramDown = dynamic_cast<OutputVectorDataParameter*>(param);
754  paramDown->SetValue(value);
755  }
756 }
757 
758 std::string Application::GetParameterName(std::string parameter)
759 {
760  Parameter* param = GetParameterByKey(parameter);
761  return param->GetName();
762 }
763 
764 std::string Application::GetParameterDescription(std::string parameter)
765 {
766  Parameter* param = GetParameterByKey(parameter);
767  return param->GetDescription();
768 }
769 
770 void Application::SetParameterDescription(std::string parameter, std::string desc)
771 {
772  Parameter* param = GetParameterByKey(parameter);
773  param->SetDescription(desc);
774 }
775 
776 int Application::GetParameterInt(std::string parameter)
777 {
778  int ret = 0;
779  Parameter* param = GetParameterByKey(parameter);
780 
781  if (dynamic_cast<IntParameter*>(param))
782  {
783  IntParameter* paramInt = dynamic_cast<IntParameter*>(param);
784  ret = paramInt->GetValue();
785  }
786  else if (dynamic_cast<FloatParameter*>(param))
787  {
788  FloatParameter* paramFloat = dynamic_cast<FloatParameter*>(param);
789  ret = static_cast<int>(paramFloat->GetValue());
790  }
791  else if (dynamic_cast<RadiusParameter*>(param))
792  {
793  RadiusParameter* paramRadius = dynamic_cast<RadiusParameter*>(param);
794  ret = paramRadius->GetValue();
795  }
796  else if (dynamic_cast<RAMParameter*>(param))
797  {
798  RAMParameter* paramRAM = dynamic_cast<RAMParameter*>(param);
799  ret = paramRAM->GetValue();
800  }
801  else if (dynamic_cast<ChoiceParameter*>(param))
802  {
803  ChoiceParameter* paramChoice = dynamic_cast<ChoiceParameter*>(param);
804  ret = paramChoice->GetValue();
805  }
806  else
807  {
808  itkExceptionMacro(<<parameter << "parameter can't be casted to int");
809  }
810 
811  return ret;
812 }
813 
814 float Application::GetParameterFloat(std::string parameter)
815 {
816  float ret = 0.0;
817  Parameter* param = GetParameterByKey(parameter);
818 
819  if (dynamic_cast<FloatParameter*> (param))
820  {
821  FloatParameter* paramFloat = dynamic_cast<FloatParameter*> (param);
822  ret = paramFloat->GetValue();
823  }
824  else
825  {
826  itkExceptionMacro(<<parameter << "parameter can't be casted to float");
827  }
828 
829  return ret;
830 }
831 
832 std::string Application::GetParameterString(std::string parameter)
833 {
834  std::string ret="";
835  Parameter* param = GetParameterByKey(parameter);
836 
837  if (dynamic_cast<ChoiceParameter*>(param))
838  {
839  ChoiceParameter* paramDown = dynamic_cast<ChoiceParameter*>(param);
840  std::string choiceKey = paramDown->GetChoiceKey( paramDown->GetValue() );
841  size_t lastPointPos = choiceKey.find_last_of('.');
842  if(lastPointPos != std::string::npos)
843  {
844  ret = choiceKey.substr(lastPointPos);
845  }
846  else
847  {
848  ret = choiceKey;
849  }
850  }
851  else if (dynamic_cast<ListViewParameter*>(param))
852  {
853  ListViewParameter* paramDown = dynamic_cast<ListViewParameter*>(param);
854  std::string choiceKey = paramDown->GetChoiceKey( paramDown->GetValue() );
855  size_t lastPointPos = choiceKey.find_last_of('.');
856  if(lastPointPos != std::string::npos)
857  {
858  ret = choiceKey.substr(lastPointPos);
859  }
860  else
861  {
862  ret = choiceKey;
863  }
864  }
865  else if (dynamic_cast<StringParameter*>(param))
866  {
867  StringParameter* paramDown = dynamic_cast<StringParameter*>(param);
868  ret = paramDown->GetValue();
869  }
870  else if (dynamic_cast<InputFilenameParameter*>(param))
871  {
872  InputFilenameParameter* paramDown = dynamic_cast<InputFilenameParameter*>(param);
873  ret = paramDown->GetValue();
874  }
875  else if (dynamic_cast<OutputFilenameParameter*>(param))
876  {
877  OutputFilenameParameter* paramDown = dynamic_cast<OutputFilenameParameter*>(param);
878  ret = paramDown->GetValue();
879  }
880  else if (dynamic_cast<DirectoryParameter*>(param))
881  {
882  DirectoryParameter* paramDown = dynamic_cast<DirectoryParameter*>(param);
883  ret = paramDown->GetValue();
884  }
885  else if (dynamic_cast<InputImageParameter*>(param))
886  {
887  InputImageParameter* paramDown = dynamic_cast<InputImageParameter*>(param);
888  ret = paramDown->GetFileName();
889  }
890  else if (dynamic_cast<ComplexInputImageParameter*>(param))
891  {
892  ComplexInputImageParameter* paramDown = dynamic_cast<ComplexInputImageParameter*>(param);
893  ret = paramDown->GetFileName();
894  }
895  else if (dynamic_cast<InputVectorDataParameter*>(param))
896  {
897  InputVectorDataParameter* paramDown = dynamic_cast<InputVectorDataParameter*>(param);
898  ret = paramDown->GetFileName();
899  }
900  else if (dynamic_cast<OutputImageParameter*>(param))
901  {
902  OutputImageParameter* paramDown = dynamic_cast<OutputImageParameter*>(param);
903  ret = paramDown->GetFileName();
904  }
905  else if (dynamic_cast<OutputVectorDataParameter*>(param))
906  {
907  OutputVectorDataParameter* paramDown = dynamic_cast<OutputVectorDataParameter*>(param);
908  ret = paramDown->GetFileName();
909  }
910  else
911  {
912  itkExceptionMacro(<<parameter << "parameter can't be casted to string");
913  }
914 
915  return ret;
916 }
917 
918 std::vector<std::string> Application::GetParameterStringList(std::string parameter)
919 {
920  std::vector<std::string> ret;
921  Parameter* param = GetParameterByKey(parameter);
922 
923  if (dynamic_cast<InputImageListParameter*> (param))
924  {
925  InputImageListParameter* paramDown = dynamic_cast<InputImageListParameter*> (param);
926  ret = paramDown->GetFileNameList();
927  }
928  else
929  if (dynamic_cast<InputVectorDataListParameter*> (param))
930  {
931  InputVectorDataListParameter* paramDown = dynamic_cast<InputVectorDataListParameter*> (param);
932  ret = paramDown->GetFileNameList();
933  }
934  else
935  if (dynamic_cast<StringListParameter*> (param))
936  {
937  StringListParameter* paramDown = dynamic_cast<StringListParameter*> (param);
938  ret = paramDown->GetValue();
939  }
940  else
941  {
942  itkExceptionMacro(<<parameter << "parameter can't be casted to StringList");
943  }
944 
945  return ret;
946 }
947 
948 
950 {
952  Parameter* param = GetParameterByKey(parameter);
953 
954  if (dynamic_cast<InputImageParameter*> (param))
955  {
956  InputImageParameter* paramDown = dynamic_cast<InputImageParameter*> (param);
957  ret = paramDown->GetImage();
958  }
959  else
960  {
961  itkExceptionMacro(<<parameter << "parameter can't be casted to ImageType");
962  }
963 
964  return ret;
965 }
966 
968 {
970  Parameter* param = GetParameterByKey(parameter);
971 
972  if (dynamic_cast<InputImageListParameter*>(param))
973  {
974  InputImageListParameter* paramDown = dynamic_cast<InputImageListParameter*>(param);
975  ret = paramDown->GetImageList();
976  }
977  else
978  {
979  itkExceptionMacro(<<parameter << "parameter can't be casted to ImageListType");
980  }
981 
982  return ret;
983 }
984 
986 {
988  Parameter* param = GetParameterByKey(parameter);
989 
990  if (dynamic_cast<ComplexInputImageParameter*>(param))
991  {
992  ComplexInputImageParameter* paramDown = dynamic_cast<ComplexInputImageParameter*>(param);
993  ret = paramDown->GetImage();
994  }
995  else
996  {
997  itkExceptionMacro(<<parameter << "parameter can't be casted to ComplexImageType");
998  }
999 
1000  return ret;
1001 }
1002 
1004 {
1006  Parameter* param = GetParameterByKey(parameter);
1007 
1008  if (dynamic_cast<InputVectorDataParameter*>(param))
1009  {
1010  InputVectorDataParameter* paramDown = dynamic_cast<InputVectorDataParameter*>(param);
1011  ret = paramDown->GetVectorData();
1012  }
1013  else
1014  {
1015  itkExceptionMacro(<<parameter << "parameter can't be casted to Vector Data");
1016  }
1017  return ret;
1018 }
1019 
1021 {
1023  Parameter* param = GetParameterByKey(parameter);
1024 
1025  if (dynamic_cast<InputVectorDataListParameter*>(param))
1026  {
1027  InputVectorDataListParameter* paramDown = dynamic_cast<InputVectorDataListParameter*>(param);
1028  ret = paramDown->GetVectorDataList();
1029  }
1030  else
1031  {
1032  itkExceptionMacro(<<parameter << "parameter can't be casted to Vector Data List");
1033  }
1034  return ret;
1035 }
1036 
1037 
1038 std::string Application::GetParameterAsString(std::string paramKey)
1039 {
1040  std::string ret="";
1041  ParameterType type = this->GetParameterType( paramKey );
1042 
1047  || type == ParameterType_ListView || type == ParameterType_Choice)
1048  {
1049  ret = this->GetParameterString( paramKey );
1050  }
1051  else if ( type == ParameterType_Int || type == ParameterType_Radius
1052  || type == ParameterType_RAM)
1053  {
1054  std::ostringstream oss;
1055  oss << this->GetParameterInt( paramKey );
1056  ret = oss.str();
1057  }
1058  else if( type == ParameterType_Float )
1059  {
1060  std::ostringstream oss;
1061  //oss << std::setprecision(10);
1062  oss << this->GetParameterFloat( paramKey );
1063  ret = oss.str();
1064  }
1065  else if( type == ParameterType_StringList )
1066  {
1067  std::ostringstream oss;
1068  oss << std::setprecision(10);
1069  const std::vector<std::string> strList = this->GetParameterStringList( paramKey );
1070  for (unsigned int i=0; i<strList.size(); i++)
1071  oss << strList[i] << std::endl;
1072  ret = oss.str();
1073  }
1074  else
1075  {
1076  itkExceptionMacro(<<paramKey << " parameter can't be casted to string");
1077  }
1078  return ret;
1079 }
1080 
1082 {
1083  Parameter* param = GetParameterByKey(parameter);
1084  ImagePixelType ret=ImagePixelType_uint8; //by default to avoid warning
1085 
1086  if (dynamic_cast<OutputImageParameter*>(param))
1087  {
1088  OutputImageParameter* paramDown = dynamic_cast<OutputImageParameter*>(param);
1089  ret = paramDown->GetPixelType();
1090  }
1091  else
1092  {
1093  itkExceptionMacro("Unable to find PixelType in parameter "<<parameter<<".");
1094  }
1095 
1096  return ret;
1097 }
1098 
1100 {
1101  Parameter* param = GetParameterByKey(parameter);
1102  ComplexImagePixelType ret=ComplexImagePixelType_float; //by default to avoid warning
1103 
1104  if (dynamic_cast<ComplexOutputImageParameter*>(param))
1105  {
1106  ComplexOutputImageParameter* paramDown = dynamic_cast<ComplexOutputImageParameter*>(param);
1107  ret = paramDown->GetComplexPixelType();
1108  }
1109  else
1110  {
1111  itkExceptionMacro("Unable to find PixelType in parameter "<<parameter<<".");
1112  }
1113 
1114  return ret;
1115 }
1116 
1117 
1118 void
1119 Application::AddChoice(std::string paramKey, std::string paramName)
1120 {
1121  GetParameterList()->AddChoice(paramKey, paramName);
1122 }
1123 
1124 void
1125 Application::ClearChoices(std::string paramKey)
1126 {
1127  GetParameterList()->ClearChoices(paramKey);
1128 }
1129 
1130 std::vector<int>
1132 {
1133  return GetParameterList()->GetSelectedItems(param);
1134 }
1135 
1136 void
1137 Application::AddParameter(ParameterType type, std::string paramKey, std::string paramName)
1138 {
1139  GetParameterList()->AddParameter(type, paramKey, paramName);
1140 }
1141 
1142 void Application::AddRAMParameter(std::string paramKey, std::string paramName, unsigned int defaultValue)
1143 {
1144  GetParameterList()->AddParameter(ParameterType_RAM, paramKey, paramName);
1145  SetDefaultParameterInt(paramKey, defaultValue);
1146  MandatoryOff(paramKey);
1147 }
1148 
1149 // paramKey default value = ram
1150 void Application::AddRAMParameter(std::string paramKey)
1151 {
1152  // Get the RAM Parameter from the configuration file
1153  if (otb::ConfigurationFile::GetInstance()->IsValid() )
1154  {
1155  AddRAMParameter(paramKey,
1156  "Available RAM (Mb)",
1157  otb::ConfigurationFile::GetInstance()->GetAvailableRAMInMBytes());
1158  }
1159  else
1160  {
1161  // TODO check this
1162  AddRAMParameter(paramKey,
1163  "Available RAM (Mb)",
1164  128);
1165  }
1166  MandatoryOff(paramKey);
1167  SetParameterDescription(paramKey, "Available memory for processing (in MB)");
1168 }
1169 
1170 void Application::AddRANDParameter(std::string paramKey, std::string paramName, unsigned int defaultValue)
1171 {
1172  GetParameterList()->AddParameter(ParameterType_Int, paramKey, paramName);
1173  SetDefaultParameterInt(paramKey, defaultValue);
1174  MandatoryOff(paramKey);
1175 }
1176 
1177 // paramKey default value = rand
1178 void Application::AddRANDParameter(std::string paramKey)
1179 {
1180  // Get the RAND Parameter from the configuration file
1181 
1182  GetParameterList()->AddParameter(ParameterType_Int, paramKey, "set user defined seed");
1183  MandatoryOff(paramKey);
1184  SetParameterDescription(paramKey, "Set specific seed. with integer value.");
1185 
1186 }
1187 
1188 
1189 std::vector< std::pair<std::string, std::string> >
1191 {
1192  std::vector< std::pair<std::string, std::string> > res;
1193  std::vector<std::string> paramList = GetParametersKeys(true);
1194  for (std::vector<std::string>::const_iterator it = paramList.begin();
1195  it != paramList.end();
1196  ++it)
1197  {
1198  Parameter* param = GetParameterByKey(*it);
1199  ParameterType type = GetParameterType(*it);
1200 
1201  if ( type != ParameterType_Group )
1202  {
1203  if ( param->GetRole() == Role_Output && IsParameterEnabled(*it) )
1204  {
1205  std::pair<std::string, std::string> keyVal;
1206  keyVal.first = (*it);
1207  if (type == ParameterType_Float)
1208  {
1209  std::ostringstream oss;
1210  oss << std::setprecision(10);
1211  oss << GetParameterFloat(*it);
1212  keyVal.second = oss.str();
1213  }
1214  else
1215  {
1216  keyVal.second = GetParameterAsString(*it);
1217  }
1218  res.push_back( keyVal );
1219  }
1220  }
1221  }
1222  return res;
1223 }
1224 
1225 bool
1227 {
1228  // Check if all the mandatory parameters are set
1229  bool ready = true;
1230 
1231  std::vector<std::string> paramList = GetParametersKeys(true);
1232  for (std::vector<std::string>::const_iterator it = paramList.begin();
1233  it != paramList.end();
1234  ++it)
1235  {
1236  // Check all Input Parameters with Input Role
1237  if (GetParameterByKey(*it)->GetRole() == Role_Input)
1238  {
1239  // When a parameter is mandatory :
1240  // return false when does not have value and:
1241  // - The param is root
1242  // - The param is not root and belonging to a Mandatory Group
1243  // wich is activated
1244  if ( !this->HasValue(*it) && IsMandatory(*it) )
1245  {
1246  if( GetParameterByKey(*it)->IsRoot() )
1247  {
1248  otbDebugMacro("MISSING : "<< (*it).c_str() << " ( Is Root)");
1249  return false;
1250  }
1251  else
1252  {
1253  // check if the parameter is linked to a root parameter with a chain of active parameters
1254  Parameter* currentParam = GetParameterByKey(*it)->GetRoot();
1255  if (currentParam->IsRoot())
1256  {
1257  otbDebugMacro("MISSING : "<< (*it).c_str() << " ( Is Level 1)");
1258  return false;
1259  }
1260 
1261  int level = 1;
1262 
1263  while (!currentParam->IsRoot())
1264  {
1265  if (!currentParam->GetActive())
1266  {
1267  // the missing parameter is not on an active branch : we can ignore it
1268  break;
1269  }
1270  currentParam = currentParam->GetRoot();
1271 
1272  level++;
1273 
1274  if (currentParam->IsRoot())
1275  {
1276  // the missing parameter is on an active branch : we need it
1277  otbDebugMacro("MISSING : "<< (*it).c_str() << " ( Is Level "<< level<<")");
1278  return false;
1279  }
1280  }
1281  }
1282  }
1283  }
1284  }
1285 
1286  return ready;
1287 }
1288 
1289 void
1290 Application::AddProcess(itk::ProcessObject* object, std::string description)
1291 {
1292  m_ProgressSource = object;
1293  m_ProgressSourceDescription = description;
1294 
1295  AddProcessToWatchEvent event;
1296  event.SetProcess(object);
1297  event.SetProcessDescription(description);
1298  this->InvokeEvent(event);
1299 }
1300 
1302 {
1303  return m_ProgressSource;
1304 }
1305 
1307 {
1309 }
1310 
1311 
1312 }
1313 }

Generated at Sun Feb 3 2013 00:59:07 for Orfeo Toolbox with doxygen 1.8.1.1