21 #ifndef otbStatisticsXMLFileWriter_hxx
22 #define otbStatisticsXMLFileWriter_hxx
26 #include "itksys/SystemTools.hxx"
27 #include "otb_tinyxml.h"
34 template <
class TMeasurementVector>
40 template <
class TMeasurementVector>
44 inputData.first = name;
47 for (
unsigned int idx = 0; idx < m_MeasurementVectorContainer.size(); ++idx)
49 if (m_MeasurementVectorContainer[idx].first == name)
51 itkExceptionMacro(<<
"Token selected (" << name <<
") is already added to the XML file");
55 inputData.second = inputVector;
56 m_MeasurementVectorContainer.push_back(inputData);
59 template <
class TMeasurementVector>
63 if (m_MeasurementVectorContainer.size() == 0 && m_GenericMapContainer.size() == 0)
64 itkExceptionMacro(<<
"At least one input is required, please set input using the methods AddInput or AddInputMap");
67 if (m_FileName.empty())
68 itkExceptionMacro(<<
"The XML output FileName is empty, please set the filename via the method SetFileName");
71 const std::string extension = itksys::SystemTools::GetFilenameLastExtension(m_FileName);
72 if (itksys::SystemTools::LowerCase(extension) !=
".xml")
74 itkExceptionMacro(<< extension <<
" is a wrong Extension FileName : Expected .xml");
80 TiXmlDeclaration* decl =
new TiXmlDeclaration(
"1.0",
"",
"");
81 doc.LinkEndChild(decl);
83 TiXmlElement* root =
nullptr;
84 if (m_MeasurementVectorContainer.size())
86 root =
new TiXmlElement(
"FeatureStatistics");
87 doc.LinkEndChild(root);
91 for (
unsigned int i = 0; i < m_MeasurementVectorContainer.size(); ++i)
93 std::string featureName = m_MeasurementVectorContainer[i].first;
97 TiXmlElement* feature =
new TiXmlElement(
"Statistic");
98 feature->SetAttribute(
"name", featureName.c_str());
99 root->LinkEndChild(feature);
102 for (
unsigned int cindex = 0; cindex < currentMeasurementVector.Size(); ++cindex)
105 TiXmlElement* curStatisticVector =
new TiXmlElement(
"StatisticVector");
106 curStatisticVector->SetDoubleAttribute(
"value", currentMeasurementVector.GetElement(cindex));
107 feature->LinkEndChild(curStatisticVector);
112 TiXmlElement* mapRoot =
nullptr;
113 if (m_GenericMapContainer.size())
115 mapRoot =
new TiXmlElement(
"GeneralStatistics");
116 doc.LinkEndChild(mapRoot);
119 GenericMapContainer::const_iterator containerIt;
120 for (containerIt = m_GenericMapContainer.begin(); containerIt != m_GenericMapContainer.end(); ++containerIt)
122 std::string mapName = containerIt->first;
123 GenericMapType::const_iterator mapIter;
126 TiXmlElement* feature =
new TiXmlElement(
"Statistic");
127 feature->SetAttribute(
"name", mapName.c_str());
128 mapRoot->LinkEndChild(feature);
131 for (mapIter = containerIt->second.begin(); mapIter != containerIt->second.end(); ++mapIter)
134 TiXmlElement* curStatisticMap =
new TiXmlElement(
"StatisticMap");
135 curStatisticMap->SetAttribute(
"key", mapIter->first.c_str());
136 curStatisticMap->SetAttribute(
"value", mapIter->second.c_str());
137 feature->LinkEndChild(curStatisticMap);
143 if (!doc.SaveFile(m_FileName.c_str()))
145 itkExceptionMacro(<<
"Unable to write the XML file in " << itksys::SystemTools::GetFilenamePath(m_FileName)
146 <<
" (permission issue? Directory does not exist?).");
150 template <
class TMeasurementVector>
151 template <
typename MapType>
154 std::string token(name);
156 if (m_GenericMapContainer.count(token) > 0)
158 itkExceptionMacro(<<
"Token selected (" << name <<
") is already added to the XML file");
161 typename MapType::const_iterator it;
165 for (it = map.begin(); it != map.end(); ++it)
167 tmpKey = boost::lexical_cast<std::string>(it->first);
168 tmpVal = boost::lexical_cast<std::string>(it->second);
169 insideMap[tmpKey] = tmpVal;
171 m_GenericMapContainer[token] = insideMap;
174 template <
class TMeasurementVector>
178 m_MeasurementVectorContainer.clear();
179 m_GenericMapContainer.clear();
182 template <
class TMeasurementVector>
186 Superclass::PrintSelf(os, indent);
189 os << indent <<
"Input FileName: " << m_FileName << std::endl;
190 os << indent <<
"Vector statistics: ";
191 for (
unsigned int i = 0; i < m_MeasurementVectorContainer.size(); ++i)
195 os << m_MeasurementVectorContainer[i].first;
198 os << indent <<
"Map statistics: ";
199 for (GenericMapContainer::const_iterator it = m_GenericMapContainer.begin(); it != m_GenericMapContainer.end(); ++it)
201 if (it != m_GenericMapContainer.begin())