Commit e33e2200 authored by Rashad Kanavath's avatar Rashad Kanavath

reverting to 113b4bc due to error merge from otbmain

parent 4504abd4
......@@ -24,6 +24,7 @@
#include "otbWrapperInputFilenameParameter.h"
#include "otbWrapperInputFilenameListParameter.h"
#include "otbWrapperOutputFilenameParameter.h"
#include "otbWrapperOutputProcessXMLParameter.h"
#include "otbWrapperInputVectorDataParameter.h"
#include "otbWrapperInputVectorDataListParameter.h"
#include "otbWrapperNumericalParameter.h"
......@@ -32,6 +33,7 @@
#include "otbWrapperStringParameter.h"
#include "otbWrapperStringListParameter.h"
#include "otbWrapperInputImageListParameter.h"
#include "otbWrapperInputProcessXMLParameter.h"
#include "otbWrapperRAMParameter.h"
#include "otbWrapperParameterGroup.h"
......@@ -58,6 +60,8 @@ Application::Application()
m_DocAuthors(""),
m_DocLimitations(""),
m_DocSeeAlso(""),
m_HaveInXML(true),
m_HaveOutXML(true),
m_DocTags()
{
// Don't call Init from the constructor, since it calls a virtual method !
......@@ -114,8 +118,54 @@ void Application::Init()
m_DocExample = DocExampleStructure::New();
m_ParameterList = ParameterGroup::New();
this->DoInit();
//rashad: global parameters. now used only for save xml
if (this->GetHaveInXML())
{
AddInXMLParameter();
}
if (this->GetHaveOutXML())
{
AddOutXMLParameter();
}
}
void Application::AddOutXMLParameter()
{
Parameter::Pointer tmpParam;
tmpParam = OutputProcessXMLParameter::New();
const std::string key = tmpParam->GetKey();
const std::string descr = tmpParam->GetDescription();
const std::string defaultXMLFileName = std::string(GetName()) + ".xml";
tmpParam = NULL;
AddParameter(ParameterType_OutputProcessXML, key, descr);
SetParameterDescription(key, descr);
MandatoryOff(key);
//SetParameterString(key, defaultXMLFileName);
DisableParameter(key);
}
void Application::AddInXMLParameter()
{
Parameter::Pointer tmpParam;
tmpParam = InputProcessXMLParameter::New();
const std::string key = tmpParam->GetKey();
const std::string descr = tmpParam->GetDescription();
const std::string defaultXMLFileName = std::string(GetName()) + ".xml";
tmpParam = NULL;
AddParameter(ParameterType_InputProcessXML, key, descr);
SetParameterDescription(key, descr);
MandatoryOff(key);
//SetParameterString(key, defaultXMLFileName);
DisableParameter(key);
}
void Application::UpdateParameters()
{
this->DoUpdateParameters();
......@@ -124,6 +174,18 @@ void Application::UpdateParameters()
int Application::Execute()
{
int ret = 0;
//read application from xml
std::string inXMLKey = "inxml";
if (GetParameterType(inXMLKey) == ParameterType_InputProcessXML
&& IsParameterEnabled(inXMLKey) )
{
Parameter* param = GetParameterByKey(inXMLKey);
InputProcessXMLParameter* inXMLParam = dynamic_cast<InputProcessXMLParameter*>(param);
inXMLParam->Read(this);
this->UpdateParameters();
}
// before execute we set the seed of mersenne twister
std::vector<std::string> paramList = GetParametersKeys(true);
bool UseSpecificSeed = false;
......@@ -131,7 +193,6 @@ int Application::Execute()
for (std::vector<std::string>::const_iterator it = paramList.begin(); it != paramList.end(); ++it)
{
std::string key = *it;
if ((key.compare(0, 4, "rand") == 0) && HasValue("rand"))
{
UseSpecificSeed = true;
......@@ -140,9 +201,7 @@ int Application::Execute()
int seed = randParam->GetValue();
itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->SetSeed(seed);
}
}
if (!UseSpecificSeed)
{
itk::Statistics::MersenneTwisterRandomVariateGenerator::GetInstance()->Initialize();
......@@ -226,6 +285,15 @@ int Application::ExecuteAndWriteOutput()
AddProcess(outputParam->GetWriter(), progressId.str());
outputParam->Write();
}
//xml writer parameter
else if (GetParameterType(key) == ParameterType_OutputProcessXML
&& IsParameterEnabled(key) && HasValue(key) )
{
Parameter* param = GetParameterByKey(key);
OutputProcessXMLParameter* outXMLParam = dynamic_cast<OutputProcessXMLParameter*>(param);
outXMLParam->Write(this);
}
}
}
......@@ -329,6 +397,7 @@ void Application::SetParameterRole(std::string paramKey, Role role)
ParameterType Application::GetParameterType(std::string paramKey) const
{
const Parameter* param = GetParameterByKey(paramKey);
ParameterType type;
if (dynamic_cast<const ChoiceParameter*>(param))
......@@ -419,6 +488,14 @@ ParameterType Application::GetParameterType(std::string paramKey) const
{
type = ParameterType_Group;
}
else if (dynamic_cast<const OutputProcessXMLParameter*>(param))
{
type = ParameterType_OutputProcessXML;
}
else if (dynamic_cast<const InputProcessXMLParameter*>(param))
{
type = ParameterType_InputProcessXML;
}
else
{
itkExceptionMacro(<< "Unknown parameter : " << paramKey);
......@@ -680,6 +757,16 @@ void Application::SetParameterString(std::string parameter, std::string value)
RAMParameter* paramDown = dynamic_cast<RAMParameter*>(param);
paramDown->SetValue(value);
}
else if (dynamic_cast<OutputProcessXMLParameter*>(param))
{
OutputProcessXMLParameter* paramDown = dynamic_cast<OutputProcessXMLParameter*>(param);
paramDown->SetValue(value);
}
else if (dynamic_cast<InputProcessXMLParameter*>(param))
{
InputProcessXMLParameter* paramDown = dynamic_cast<InputProcessXMLParameter*>(param);
paramDown->SetValue(value);
}
}
void Application::SetParameterStringList(std::string parameter, std::vector<std::string> value)
......@@ -923,9 +1010,14 @@ std::string Application::GetParameterString(std::string parameter)
OutputVectorDataParameter* paramDown = dynamic_cast<OutputVectorDataParameter*>(param);
ret = paramDown->GetFileName();
}
else if (dynamic_cast<OutputProcessXMLParameter*>(param))
{
OutputProcessXMLParameter* paramDown = dynamic_cast<OutputProcessXMLParameter*>(param);
ret = paramDown->GetFileName();
}
else
{
itkExceptionMacro(<<parameter << "parameter can't be casted to string");
itkExceptionMacro(<<parameter << " : parameter can't be casted to string");
}
return ret;
......@@ -1063,7 +1155,8 @@ std::string Application::GetParameterAsString(std::string paramKey)
|| type == ParameterType_Directory || type == ParameterType_InputImage
|| type == ParameterType_ComplexInputImage || type == ParameterType_InputVectorData
|| type == ParameterType_OutputImage || type == ParameterType_OutputVectorData
|| type == ParameterType_ListView || type == ParameterType_Choice)
|| type == ParameterType_ListView || type == ParameterType_Choice
|| type == ParameterType_OutputProcessXML )
{
ret = this->GetParameterString( paramKey );
}
......
......@@ -33,7 +33,6 @@
#include "otbWrapperOutputImageParameter.h"
#include "otbWrapperComplexInputImageParameter.h"
#include "otbWrapperComplexOutputImageParameter.h"
#include "otbWrapperDocExampleStructure.h"
#include "itkMersenneTwisterRandomVariateGenerator.h"
......@@ -83,6 +82,31 @@ public:
/** Check if the application has been initialized */
bool IsInitialized() const;
/** rashad: Add xml parameters eg: -xml */
void AddInXMLParameter();
void AddOutXMLParameter();
/** Set the parameter xml flag */
itkSetMacro(HaveInXML, bool);
/** Get the parameter xml flag */
itkGetConstMacro(HaveInXML, bool);
/** Toogle the parameter xml flag */
itkBooleanMacro(HaveInXML);
/** Set the parameter xml flag */
itkSetMacro(HaveOutXML, bool);
/** Get the parameter xml flag */
itkGetConstMacro(HaveOutXML, bool);
/** Toogle the parameter xml flag */
itkBooleanMacro(HaveOutXML);
/** Update the value of parameters for which no user value has been provided */
void UpdateParameters();
......@@ -724,6 +748,10 @@ private:
std::string m_Description;
ParameterGroup::Pointer m_ParameterList;
//rashad:: controls adding of -xml parameter. set to true by default
bool m_HaveInXML;
bool m_HaveOutXML;
itk::Logger::Pointer m_Logger;
itk::ProcessObject::Pointer m_ProgressSource;
......
#include "otbWrapperInputProcessXMLParameter.h"
#include "otbWrapperChoiceParameter.h"
#include "otbWrapperListViewParameter.h"
#include "otbWrapperDirectoryParameter.h"
#include "otbWrapperEmptyParameter.h"
#include "otbWrapperInputFilenameParameter.h"
#include "otbWrapperInputFilenameListParameter.h"
#include "otbWrapperOutputFilenameParameter.h"
#include "otbWrapperInputVectorDataParameter.h"
#include "otbWrapperInputVectorDataListParameter.h"
#include "otbWrapperNumericalParameter.h"
#include "otbWrapperOutputVectorDataParameter.h"
#include "otbWrapperRadiusParameter.h"
#include "otbWrapperStringParameter.h"
#include "otbWrapperStringListParameter.h"
#include "otbWrapperInputImageParameter.h"
#include "otbWrapperInputImageListParameter.h"
#include "otbWrapperComplexInputImageParameter.h"
#include "otbWrapperOutputImageParameter.h"
#include "otbWrapperComplexOutputImageParameter.h"
#include "otbWrapperRAMParameter.h"
#include "itksys/SystemTools.hxx"
namespace otb
{
namespace Wrapper
{
InputProcessXMLParameter::InputProcessXMLParameter()
{
this->SetKey("inxml");
this->SetName("Load otb application from xml file");
this->SetDescription("Load otb application from xml file");
this->SetMandatory(false);
this->SetActive(false);
this->SetRole(Role_Input);
}
InputProcessXMLParameter::~InputProcessXMLParameter()
{
}
std::string
InputProcessXMLParameter::pixelTypeToString(ImagePixelType pixType)
{
std::string type;
switch( pixType )
{
case ImagePixelType_uint8:
{
type = "uint8";
break;
}
case ImagePixelType_int16:
{
type = "int16";
break;
}
case ImagePixelType_uint16:
{
type = "uint16";
break;
}
case ImagePixelType_int32:
{
type = "int32";
break;
}
case ImagePixelType_uint32:
{
type = "uint32";
break;
}
case ImagePixelType_float:
{
type = "float";
break;
}
case ImagePixelType_double:
{
type = "double";
break;
}
}
return type;
}
const std::string InputProcessXMLParameter::GetChildNodeTextOf(TiXmlElement *parentElement, std::string key)
{
std::string value="";
if(parentElement)
{
TiXmlElement* childElement = 0;
childElement = parentElement->FirstChildElement(key.c_str());
if(childElement)
{
value = childElement->GetText();
}
// value = parent->FirstChildElement(key)->GetText();
}
return value;
}
void
InputProcessXMLParameter::Read(Application::Pointer app)
{
// Check if the filename is not empty
if(m_FileName.empty())
itkExceptionMacro(<<"The XML input FileName is empty, please set the filename via the method SetFileName");
// Check that the right extension is given : expected .xml */
if (itksys::SystemTools::GetFilenameLastExtension(m_FileName) != ".xml")
{
itkExceptionMacro(<<itksys::SystemTools::GetFilenameLastExtension(m_FileName)
<<" is a wrong Extension FileName : Expected .xml");
}
// Open the xml file
TiXmlDocument doc(m_FileName.c_str());
if (!doc.LoadFile())
{
itkExceptionMacro(<<"Can't open file "<<m_FileName);
}
TiXmlHandle handle(&doc);
TiXmlElement *n_OTB;
n_OTB = handle.FirstChild("OTB").Element();
if(!n_OTB)
itkExceptionMacro(<< "Input XML file " << this->GetFileName() << " is invalid.");
std::string otb_Version, otb_Build, otb_Platform;
otb_Version = GetChildNodeTextOf(n_OTB,"version");
otb_Build = GetChildNodeTextOf(n_OTB, "build");
otb_Platform = GetChildNodeTextOf(n_OTB, "platform");
TiXmlElement* n_App = n_OTB->FirstChildElement("application");
std::string app_Name, app_Descr;
app_Name = GetChildNodeTextOf(n_App, "name");
app_Descr = GetChildNodeTextOf(n_App, "descr");
TiXmlElement* n_Doc = n_App->FirstChildElement("doc");
std::string doc_Name, doc_Descr, doc_Author, doc_Limitation, doc_Related;
doc_Name = GetChildNodeTextOf(n_Doc, "name");
doc_Descr = GetChildNodeTextOf(n_Doc, "descr");
doc_Author = GetChildNodeTextOf(n_Doc, "author");
doc_Limitation = GetChildNodeTextOf(n_Doc, "limitation");
doc_Related = GetChildNodeTextOf(n_Doc, "related");
TiXmlElement* n_Tags = n_Doc->FirstChildElement("tags");
std::vector<std::string> doc_TagList;
// GetChildNodeTextOf(n_Tags, "tag");
std::cerr << "Application : " << app_Name << std::endl;
std::cerr << "Description : " << app_Descr << std::endl;
std::cerr << "OTB Version : " << otb_Version << std::endl;
std::cerr << "OS : " << otb_Platform << std::endl;
// Iterate through the tree to get all the stats
for( TiXmlElement* n_Parameter = n_App->FirstChildElement("parameter"); n_Parameter != NULL;
n_Parameter = n_Parameter->NextSiblingElement() )
{
std::string key, type, value;
std::vector<std::string> values;
key = GetChildNodeTextOf(n_Parameter, "key");
type = GetChildNodeTextOf(n_Parameter, "type");
value = GetChildNodeTextOf(n_Parameter, "value");
TiXmlElement* n_Values = NULL;
n_Values = n_Parameter->FirstChildElement("values");
if(n_Values)
{
for(TiXmlElement* n_Value = n_Values->FirstChildElement("value"); n_Value != NULL;
n_Value = n_Value->NextSiblingElement())
{
values.push_back(n_Value->GetText());
}
}
if ( type == "InputFilename" || type == "OutputFilename" ||
type == "Directory" || type == "InputImage" ||
type == "ComplexInputImage" || type == "InputVectorData" ||
type == "OutputImage" || type == "ComplexOutputImage" ||
type == "OutputVectorData" || type == "String" ||
type == "Choice")
{
std::string userValue = app->GetParameterString(key);
if(userValue.empty())
app->SetParameterString(key, value);
}
else if (type == "Radius" || type == "Int" || type == "RAM" )
{
int intValue;
std::stringstream(value) >> intValue;
int userValue = 0;
userValue = app->GetParameterInt(key);
if(userValue == 0)
app->SetParameterInt(key,intValue);
}
else if (type == "Float")
{
int floatValue;
std::stringstream(value) >> floatValue;
float userValue = 0;
userValue = app->GetParameterFloat(key);
if( userValue == 0 )
app->SetParameterFloat(key, floatValue);
}
else if (type == "InputFilenameList" || type == "InputImageList" ||
type =="InputVectorDataList" || type == "StringList" ||
type == "ListView")
{
if(values.empty())
itkExceptionMacro(<< key << " has null values");
std::vector<std::string> userValues;
userValues = app->GetParameterStringList(key);
if( userValues.size() == 0 )
app->SetParameterStringList(key,values);
}
//choice also comes as setint and setstring why??
}
}
} //end namespace wrapper
} //end namespace otb
#ifndef __otbWrapperInputProcessXMLParameter_h
#define __otbWrapperInputProcessXMLParameter_h
#include "otbWrapperApplication.h"
#include "otbTinyXML.h"
namespace otb
{
namespace Wrapper
{
/** \class InputProcessXMLParameter
* \brief This class represent a xml filename parameter for the wrapper framework to load otb application.
*/
class InputProcessXMLParameter
: public Parameter
{
public:
/** Standard class typedef */
typedef InputProcessXMLParameter Self;
typedef Parameter Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Defining ::New() static method */
itkNewMacro(Self);
/** RTTI support */
itkTypeMacro(InputProcessXMLParameter, Parameter);
itkGetStringMacro(FileName);
bool HasValue() const
{
if(m_FileName.empty())
return false;
else
return true;
}
// Get Value
//TODO otbGetObjectMemberMacro(StringParam, Value , std::string);
void SetFileName(std::string value)
{
this->SetValue(value);
}
// Set Value
virtual void SetValue(const std::string value)
{
itkDebugMacro("setting member m_FileName to " << value);
this->m_FileName = value;
SetActive(true);
this->Modified();
}
const std::string GetChildNodeTextOf(TiXmlElement *parentElement, std::string key);
std::string pixelTypeToString(ImagePixelType pixType);
ParameterType GetParameterType(const Parameter* param) const;
void Read(Application::Pointer application);
protected:
InputProcessXMLParameter();
/** Destructor */
virtual ~InputProcessXMLParameter();
private:
std::string m_FileName;
InputProcessXMLParameter(const InputProcessXMLParameter &); //purposely not implemented
void operator =(const InputProcessXMLParameter&); //purposely not implemented
}; // End class Parameter
} // End namespace Wrapper
} // End namespace otb
#endif
#include "otbWrapperOutputProcessXMLParameter.h"
#include "otbWrapperChoiceParameter.h"
#include "otbWrapperListViewParameter.h"
#include "otbWrapperDirectoryParameter.h"
#include "otbWrapperEmptyParameter.h"
#include "otbWrapperInputFilenameParameter.h"
#include "otbWrapperInputFilenameListParameter.h"
#include "otbWrapperOutputFilenameParameter.h"
#include "otbWrapperInputVectorDataParameter.h"
#include "otbWrapperInputVectorDataListParameter.h"
#include "otbWrapperNumericalParameter.h"
#include "otbWrapperOutputVectorDataParameter.h"
#include "otbWrapperRadiusParameter.h"
#include "otbWrapperStringParameter.h"
#include "otbWrapperStringListParameter.h"
#include "otbWrapperInputImageParameter.h"
#include "otbWrapperInputImageListParameter.h"
#include "otbWrapperComplexInputImageParameter.h"
#include "otbWrapperOutputImageParameter.h"
#include "otbWrapperComplexOutputImageParameter.h"
#include "otbWrapperRAMParameter.h"
#include "itksys/SystemTools.hxx"
namespace otb
{
namespace Wrapper
{
OutputProcessXMLParameter::OutputProcessXMLParameter()
{
this->SetKey("outxml");
this->SetName("Save otb application to xml file");
this->SetDescription("Save otb application to xml file");
this->SetMandatory(false);
this->SetActive(false);
this->SetRole(Role_Output);
}
OutputProcessXMLParameter::~OutputProcessXMLParameter()
{
}
std::string
OutputProcessXMLParameter::pixelTypeToString(ImagePixelType pixType)
{
std::string type;
switch( pixType )
{
case ImagePixelType_uint8:
{
type = "uint8";
break;
}
case ImagePixelType_int16:
{
type = "int16";
break;
}
case ImagePixelType_uint16:
{
type = "uint16";
break;
}
case ImagePixelType_int32:
{
type = "int32";
break;
}
case ImagePixelType_uint32:
{
type = "uint32";
break;
}
case ImagePixelType_float:
{
type = "float";
break;
}
case ImagePixelType_double:
{
type = "double";
break;
}
}
return type;
}
TiXmlElement* OutputProcessXMLParameter::AddChildNodeTo(TiXmlElement *parent, std::string name, std::string value)
{
TiXmlElement * n_Node = new TiXmlElement( name.c_str() );
parent->LinkEndChild( n_Node );
if(!value.empty())
{
TiXmlText * nv_NodeValue = new TiXmlText( value.c_str() );
n_Node->LinkEndChild( nv_NodeValue );
}
return n_Node;
}
void
OutputProcessXMLParameter::Write(Application::Pointer app)
{
// Check if the filename is not empty
if(m_FileName.empty())
itkExceptionMacro("The XML output FileName is empty, please set the filename via the method SetFileName");
// Check that the right extension is given : expected .xml */
if (itksys::SystemTools::GetFilenameLastExtension(m_FileName) != ".xml")
{
itkExceptionMacro(<<itksys::SystemTools::GetFilenameLastExtension(m_FileName)
<<" is a wrong Extension FileName : Expected .xml");
}
// Write the XML file
TiXmlDocument doc;
TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
doc.LinkEndChild( decl );
TiXmlElement * n_OTB = new TiXmlElement( "OTB");
doc.LinkEndChild( n_OTB );
std::string version = "3.18";
std::string platform = "Linux";
std::string build = "18-05-2013";
AddChildNodeTo(n_OTB, "version", version);
AddChildNodeTo(n_OTB, "build", build);
AddChildNodeTo(n_OTB, "platform", platform);
TiXmlElement *n_App;
n_App = AddChildNodeTo(n_OTB, "application");
AddChildNodeTo(n_App, "name", app->GetName());
AddChildNodeTo(n_App, "descr", app->GetDescription());
TiXmlElement *n_AppDoc;
n_AppDoc = AddChildNodeTo(n_App, "doc");
AddChildNodeTo(n_AppDoc, "name", app->GetDocName());
AddChildNodeTo(n_AppDoc, "descr", app->GetDocLongDescription());
AddChildNodeTo(n_AppDoc, "author", app->GetDocAuthors());
AddChildNodeTo(n_AppDoc, "limitation", app->GetDocLimitations());