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

Generated at Sun May 19 2013 01:01:43 for Orfeo Toolbox with doxygen 1.8.3.1