OTB  9.0.0
Orfeo Toolbox
otbReadDataFile.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #include "otbStringUtils.h"
22 #include "otbStringUtilities.h"
24 
25 #include "itkListSample.h"
26 #include <string>
27 #include <algorithm>
28 
29 namespace otb
30 {
35 template <typename TInput, typename TTarget>
36 bool ReadDataFile(const std::string& infname, itk::SmartPointer<itk::Statistics::ListSample<TInput>> samples,
37  itk::SmartPointer<itk::Statistics::ListSample<TTarget>> labels)
38 {
39  typedef typename itk::Statistics::ListSample<TInput>::MeasurementType IValueType;
40  typedef typename itk::Statistics::ListSample<TTarget>::MeasurementType TValueType;
42 
43  std::ifstream ifs;
44  ifs.open(infname);
45 
46  if (!ifs)
47  {
48  std::cout << "Could not read file " << infname << std::endl;
49  return false;
50  }
51 
52  labels->SetMeasurementVectorSize(1);
53  unsigned int nbfeatures = 0;
54 
55  for (std::string line; std::getline(ifs, line) ;)
56  {
57  boost::algorithm::trim(line);
58 
59  if (nbfeatures == 0)
60  {
61  nbfeatures = std::count(line.begin(), line.end(), ' ');
62  samples->SetMeasurementVectorSize(nbfeatures);
63  }
64 
65  if (line.empty())
66  continue;
67 
68  TInput sample;
69  itk::NumericTraits<TInput>::SetLength(sample, nbfeatures);
70 
71  auto const parts = otb::split_on(line, ' ');
72  auto it_parts = parts.begin();
73  auto const end_parts = parts.end();
74 
75  if (it_parts == end_parts) {
76  throw std::runtime_error("Line is not correctly formatted.");
77  }
78 
79  // Parse label
80  TTarget label;
81  itk::NumericTraits<TTarget>::SetLength(label, 1);
82  label[0] = to<TValueType>(*it_parts, "extracting label");
83 
84  unsigned int id = 0;
85 
86  IValueType value;
87  for (++it_parts ; it_parts != end_parts ; ++it_parts)
88  {
89  auto semicolon_pos = std::find(it_parts->begin(), it_parts->end(), ':');
90  if (semicolon_pos == it_parts->end())
91  {
92  id++;
93  value = to<IValueType>(*it_parts, "extracting field");
94  }
95  else
96  {
97  id = to<unsigned int>(string_view(it_parts->begin(), semicolon_pos), "extracting field id");
98  value = to<IValueType>(string_view(semicolon_pos + 1, it_parts->end()), "extracting field");
99  }
100  if (id > nbfeatures)
101  {
102  throw std::runtime_error("id is higher than the number of features.");
103  }
104  sample[id - 1] = value;
105  }
106  samples->PushBack(sample);
107  labels->PushBack(label);
108  }
109 
110  ifs.close();
111  return true;
112 }
113 
114 } // end of namespace otb
otb_boost_string_header.h
otb::split_on
part_range< splitter_on_delim > split_on(String const &str, char delim)
Definition: otbStringUtilities.h:489
otb::find
string_view find(string_view const &haystack, string_view const &needle)
Definition: otbStringUtilities.h:305
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ReadDataFile
bool ReadDataFile(const std::string &infname, itk::SmartPointer< itk::Statistics::ListSample< TInput >> samples, itk::SmartPointer< itk::Statistics::ListSample< TTarget >> labels)
Definition: otbReadDataFile.h:36
otbStringUtilities.h
otbStringUtils.h
otb::string_view
Definition: otbStringUtilities.h:58