21 #ifndef otbStatisticsXMLFileReader_hxx
22 #define otbStatisticsXMLFileReader_hxx
26 #include "itksys/SystemTools.hxx"
27 #include "otb_tinyxml.h"
34 template <
class TMeasurementVector>
39 template <
class TMeasurementVector>
42 return m_MeasurementVectorContainer.size() + m_GenericMapContainer.size();
45 template <
class TMeasurementVector>
54 std::vector<std::string> output;
55 for (
unsigned int i = 0; i < m_MeasurementVectorContainer.size(); ++i)
57 output.push_back(m_MeasurementVectorContainer[i].first);
62 template <
class TMeasurementVector>
71 std::vector<std::string> output;
72 for (GenericMapContainer::iterator it = m_GenericMapContainer.begin(); it != m_GenericMapContainer.end(); ++it)
74 output.push_back(it->first);
79 template <
class TMeasurementVector>
91 unsigned int index = 0;
92 for (
unsigned int idx = 0; idx < m_MeasurementVectorContainer.size(); ++idx)
94 if (m_MeasurementVectorContainer[idx].first == statisticName)
103 itkExceptionMacro(<<
"No entry corresponding to the token selected (" << statisticName <<
") in the XML file");
105 return m_MeasurementVectorContainer[index].second;
108 template <
class TMeasurementVector>
109 template <
typename MapType>
119 std::string statName(statisticName);
120 if (m_GenericMapContainer.count(statName) == 0)
122 itkExceptionMacro(<<
"No entry corresponding to the token selected (" << statName <<
") in the XML file");
126 typename MapType::key_type tmpKey;
127 typename MapType::mapped_type tmpVal;
128 for (GenericMapType::iterator it = m_GenericMapContainer[statName].begin(); it != m_GenericMapContainer[statName].end(); ++it)
130 tmpKey = boost::lexical_cast<typename MapType::key_type>(it->first);
131 tmpVal = boost::lexical_cast<typename MapType::mapped_type>(it->second);
132 outputMap[tmpKey] = tmpVal;
137 template <
class TMeasurementVector>
141 if (m_FileName.empty())
142 itkExceptionMacro(<<
"The XML output FileName is empty, please set the filename via the method SetFileName");
145 std::string extension = itksys::SystemTools::GetFilenameLastExtension(m_FileName);
146 if (itksys::SystemTools::LowerCase(extension) !=
".xml")
148 itkExceptionMacro(<< extension <<
" is a wrong Extension FileName : Expected .xml");
152 m_MeasurementVectorContainer.clear();
153 m_GenericMapContainer.clear();
156 TiXmlDocument doc(m_FileName.c_str());
159 itkExceptionMacro(<<
"Can't open file " << m_FileName);
162 TiXmlHandle hDoc(&doc);
163 TiXmlElement* root = hDoc.FirstChildElement(
"FeatureStatistics").ToElement();
168 for (TiXmlElement* currentStat = root->FirstChildElement(); currentStat !=
nullptr; currentStat = currentStat->NextSiblingElement())
173 currentStatisticVector.first = currentStat->Attribute(
"name");
178 std::vector<double> tempMeasurementVector;
180 for (TiXmlElement* sample = currentStat->FirstChildElement(
"StatisticVector"); sample !=
nullptr; sample = sample->NextSiblingElement())
184 sample->QueryDoubleAttribute(
"value", &value);
186 tempMeasurementVector.push_back(value);
190 currentStatisticVector.second.SetSize(tempMeasurementVector.size());
191 for (
unsigned int i = 0; i < tempMeasurementVector.size(); ++i)
192 currentStatisticVector.second.SetElement(i, (
static_cast<InputValueType>(tempMeasurementVector[i])));
193 m_MeasurementVectorContainer.push_back(currentStatisticVector);
200 root = hDoc.FirstChildElement(
"GeneralStatistics").ToElement();
204 for (TiXmlElement* currentStat = root->FirstChildElement(); currentStat !=
nullptr; currentStat = currentStat->NextSiblingElement())
207 std::string currentName(currentStat->Attribute(
"name"));
209 for (TiXmlElement* sample = currentStat->FirstChildElement(
"StatisticMap"); sample !=
nullptr; sample = sample->NextSiblingElement())
212 const char* c_key = sample->Attribute(
"key");
213 const char* c_value = sample->Attribute(
"value");
214 if (c_key ==
nullptr)
216 itkExceptionMacro(
"'key' attribute not found in StatisticMap !");
218 if (c_value ==
nullptr)
220 itkExceptionMacro(
"'value' attribute not found in StatisticMap !");
222 key = std::string(c_key);
223 value = std::string(c_value);
225 currentMap[key] = value;
227 m_GenericMapContainer[currentName] = currentMap;
235 template <
class TMeasurementVector>
239 Superclass::PrintSelf(os, indent);
242 os << indent <<
"Input FileName: " << m_FileName << std::endl;
243 os << indent <<
"Vector statistics: ";
244 for (
unsigned int i = 0; i < m_MeasurementVectorContainer.size(); ++i)
248 os << m_MeasurementVectorContainer[i].first;
251 os << indent <<
"Map statistics: ";
252 for (GenericMapContainer::const_iterator it = m_GenericMapContainer.begin(); it != m_GenericMapContainer.end(); ++it)
254 if (it != m_GenericMapContainer.begin())