OTB  9.0.0
Orfeo Toolbox
otbOGRDataToClassStatisticsFilter.hxx
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 #ifndef otbOGRDataToClassStatisticsFilter_hxx
22 #define otbOGRDataToClassStatisticsFilter_hxx
23 
25 
26 namespace otb
27 {
28 // --------- otb::PersistentOGRDataToClassStatisticsFilter ---------------------
29 
30 template <class TInputImage, class TMaskImage>
32 {
33  this->SetNumberOfRequiredOutputs(3);
34  this->SetNthOutput(0, TInputImage::New());
35  this->SetNthOutput(1, ClassCountObjectType::New());
36  this->SetNthOutput(2, PolygonSizeObjectType::New());
37 }
38 
39 template <class TInputImage, class TMaskImage>
41 {
42  otb::ogr::DataSource* vectors = const_cast<otb::ogr::DataSource*>(this->GetOGRData());
43  vectors->GetLayer(this->GetLayerIndex()).SetSpatialFilter(nullptr);
44 
45  ClassCountMapType& classCount = this->GetClassCountOutput()->Get();
46  PolygonSizeMapType& polygonSize = this->GetPolygonSizeOutput()->Get();
47 
48  // Reset outputs
49  classCount.clear();
50  polygonSize.clear();
51  // Copy temporary stats to outputs
52  for (unsigned int k = 0; k < this->GetNumberOfThreads(); k++)
53  {
54  ClassCountMapType::iterator itClass = m_ElmtsInClassThread[k].begin();
55  for (; itClass != m_ElmtsInClassThread[k].end(); ++itClass)
56  {
57  if (classCount.count(itClass->first))
58  {
59  classCount[itClass->first] += itClass->second;
60  }
61  else
62  {
63  classCount[itClass->first] = itClass->second;
64  }
65  }
66  PolygonSizeMapType::iterator itPoly = m_PolygonThread[k].begin();
67  for (; itPoly != m_PolygonThread[k].end(); ++itPoly)
68  {
69  if (polygonSize.count(itPoly->first))
70  {
71  polygonSize[itPoly->first] += itPoly->second;
72  }
73  else
74  {
75  polygonSize[itPoly->first] = itPoly->second;
76  }
77  }
78  }
79 
80  m_ElmtsInClassThread.clear();
81  m_PolygonThread.clear();
82  m_NbPixelsThread.clear();
83 }
84 
85 template <class TInputImage, class TMaskImage>
87 {
88  m_ElmtsInClassThread.clear();
89  m_PolygonThread.clear();
90  m_NbPixelsThread.clear();
91 
92  m_ElmtsInClassThread.resize(this->GetNumberOfThreads());
93  m_PolygonThread.resize(this->GetNumberOfThreads());
94  m_NbPixelsThread.resize(this->GetNumberOfThreads());
95  m_CurrentClass.resize(this->GetNumberOfThreads());
96  m_CurrentFID.resize(this->GetNumberOfThreads());
97 }
98 
99 template <class TInputImage, class TMaskImage>
102 {
103  if (this->GetNumberOfOutputs() < 2)
104  {
105  return nullptr;
106  }
107  return static_cast<const ClassCountObjectType*>(this->itk::ProcessObject::GetOutput(1));
108 }
109 
110 template <class TInputImage, class TMaskImage>
113 {
114  if (this->GetNumberOfOutputs() < 2)
115  {
116  return nullptr;
117  }
118  return static_cast<ClassCountObjectType*>(this->itk::ProcessObject::GetOutput(1));
119 }
120 
121 template <class TInputImage, class TMaskImage>
124 {
125  if (this->GetNumberOfOutputs() < 3)
126  {
127  return nullptr;
128  }
129  return static_cast<const PolygonSizeObjectType*>(this->itk::ProcessObject::GetOutput(2));
130 }
131 
132 template <class TInputImage, class TMaskImage>
135 {
136  if (this->GetNumberOfOutputs() < 3)
137  {
138  return nullptr;
139  }
140  return static_cast<PolygonSizeObjectType*>(this->itk::ProcessObject::GetOutput(2));
141 }
142 
143 template <class TInputImage, class TMaskImage>
145 {
146  switch (idx)
147  {
148  case 0:
149  return static_cast<itk::DataObject*>(TInputImage::New().GetPointer());
150  break;
151  case 1:
152  return static_cast<itk::DataObject*>(ClassCountObjectType::New().GetPointer());
153  break;
154  case 2:
155  return static_cast<itk::DataObject*>(PolygonSizeObjectType::New().GetPointer());
156  break;
157  default:
158  // might as well make an image
159  return static_cast<itk::DataObject*>(TInputImage::New().GetPointer());
160  break;
161  }
162 }
163 
164 template <class TInputImage, class TMaskImage>
166  typename TInputImage::PointType&, itk::ThreadIdType& threadid)
167 {
168  std::string& className = m_CurrentClass[threadid];
169  unsigned long& fId = m_CurrentFID[threadid];
170 
171  m_ElmtsInClassThread[threadid][className]++;
172  m_PolygonThread[threadid][fId]++;
173  m_NbPixelsThread[threadid]++;
174 }
175 
176 template <class TInputImage, class TMaskImage>
178 {
179  std::string className(feature.ogr().GetFieldAsString(this->GetFieldIndex()));
180  unsigned long fId = feature.ogr().GetFID();
181  if (!m_ElmtsInClassThread[threadid].count(className))
182  {
183  m_ElmtsInClassThread[threadid][className] = 0;
184  }
185  if (!m_PolygonThread[threadid].count(fId))
186  {
187  m_PolygonThread[threadid][fId] = 0;
188  }
189  m_CurrentClass[threadid] = className;
190  m_CurrentFID[threadid] = fId;
191 }
192 
193 // -------------- otb::OGRDataToClassStatisticsFilter --------------------------
194 
195 template <class TInputImage, class TMaskImage>
197 {
198  this->GetFilter()->SetInput(image);
199 }
200 
201 template <class TInputImage, class TMaskImage>
203 {
204  return this->GetFilter()->GetInput();
205 }
206 
207 template <class TInputImage, class TMaskImage>
209 {
210  this->GetFilter()->SetOGRData(data);
211 }
212 
213 template <class TInputImage, class TMaskImage>
215 {
216  return this->GetFilter()->GetOGRData();
217 }
218 
219 template <class TInputImage, class TMaskImage>
221 {
222  this->GetFilter()->SetMask(mask);
223 }
224 
225 template <class TInputImage, class TMaskImage>
227 {
228  return this->GetFilter()->GetMask();
229 }
230 
231 template <class TInputImage, class TMaskImage>
233 {
234  this->GetFilter()->SetFieldName(key);
235 }
236 
237 template <class TInputImage, class TMaskImage>
239 {
240  return this->GetFilter()->GetFieldName();
241 }
242 
243 template <class TInputImage, class TMaskImage>
245 {
246  this->GetFilter()->SetLayerIndex(index);
247 }
248 
249 template <class TInputImage, class TMaskImage>
251 {
252  return this->GetFilter()->GetLayerIndex();
253 }
254 
255 template <class TInputImage, class TMaskImage>
258 {
259  return this->GetFilter()->GetClassCountOutput();
260 }
261 
262 template <class TInputImage, class TMaskImage>
265 {
266  return this->GetFilter()->GetClassCountOutput();
267 }
268 
269 template <class TInputImage, class TMaskImage>
272 {
273  return this->GetFilter()->GetPolygonSizeOutput();
274 }
275 
276 template <class TInputImage, class TMaskImage>
279 {
280  return this->GetFilter()->GetPolygonSizeOutput();
281 }
282 
283 
284 } // end of namespace otb
285 
286 #endif
otb::PersistentOGRDataToClassStatisticsFilter::MakeOutput
itk::DataObject::Pointer MakeOutput(DataObjectPointerArraySizeType idx) override
Definition: otbOGRDataToClassStatisticsFilter.hxx:144
otb::OGRDataToClassStatisticsFilter::GetMask
const TMaskImage * GetMask()
Definition: otbOGRDataToClassStatisticsFilter.hxx:226
otb::PersistentOGRDataToClassStatisticsFilter::GetClassCountOutput
const ClassCountObjectType * GetClassCountOutput() const
Definition: otbOGRDataToClassStatisticsFilter.hxx:101
otb::OGRDataToClassStatisticsFilter::SetMask
void SetMask(const TMaskImage *mask)
Definition: otbOGRDataToClassStatisticsFilter.hxx:220
otb::OGRDataToClassStatisticsFilter::SetLayerIndex
void SetLayerIndex(int index)
Definition: otbOGRDataToClassStatisticsFilter.hxx:244
otb::PersistentOGRDataToClassStatisticsFilter::DataObjectPointerArraySizeType
itk::DataObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
Definition: otbOGRDataToClassStatisticsFilter.h:61
otb::OGRDataToClassStatisticsFilter::SetFieldName
void SetFieldName(std::string &key)
Definition: otbOGRDataToClassStatisticsFilter.hxx:232
otb::OGRDataToClassStatisticsFilter::GetLayerIndex
int GetLayerIndex()
Definition: otbOGRDataToClassStatisticsFilter.hxx:250
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::PersistentOGRDataToClassStatisticsFilter::ClassCountObjectType
itk::SimpleDataObjectDecorator< ClassCountMapType > ClassCountObjectType
Definition: otbOGRDataToClassStatisticsFilter.h:58
otb::ogr::DataSource
Collection of geometric objects.
Definition: otbOGRDataSourceWrapper.h:83
otb::OGRDataToClassStatisticsFilter::SetInput
virtual void SetInput(const TInputImage *image)
Definition: otbOGRDataToClassStatisticsFilter.hxx:196
otb::PersistentOGRDataToClassStatisticsFilter::PrepareFeature
void PrepareFeature(const ogr::Feature &feature, itk::ThreadIdType &threadid) override
Definition: otbOGRDataToClassStatisticsFilter.hxx:177
otb::OGRDataToClassStatisticsFilter::ClassCountObjectType
FilterType::ClassCountObjectType ClassCountObjectType
Definition: otbOGRDataToClassStatisticsFilter.h:152
otb::PersistentOGRDataToClassStatisticsFilter::ClassCountMapType
std::map< std::string, unsigned long > ClassCountMapType
Definition: otbOGRDataToClassStatisticsFilter.h:56
otb::OGRDataToClassStatisticsFilter::PolygonSizeObjectType
FilterType::PolygonSizeObjectType PolygonSizeObjectType
Definition: otbOGRDataToClassStatisticsFilter.h:153
otb::PersistentOGRDataToClassStatisticsFilter::Reset
void Reset(void) override
Definition: otbOGRDataToClassStatisticsFilter.hxx:86
otb::ogr::Layer::begin
const_iterator begin() const
Definition: otbOGRLayerWrapper.h:412
otb::OGRDataToClassStatisticsFilter::GetPolygonSizeOutput
const PolygonSizeObjectType * GetPolygonSizeOutput() const
Definition: otbOGRDataToClassStatisticsFilter.hxx:271
otb::OGRDataToClassStatisticsFilter::GetInput
const TInputImage * GetInput()
Definition: otbOGRDataToClassStatisticsFilter.hxx:202
otb::PersistentOGRDataToClassStatisticsFilter::PersistentOGRDataToClassStatisticsFilter
PersistentOGRDataToClassStatisticsFilter()
Definition: otbOGRDataToClassStatisticsFilter.hxx:31
otb::PersistentOGRDataToClassStatisticsFilter::Synthetize
void Synthetize(void) override
Definition: otbOGRDataToClassStatisticsFilter.hxx:40
otb::ogr::DataSource::GetLayer
Layer GetLayer(vcl_size_t i)
Definition: otbOGRDataSourceWrapper.hxx:51
otb::OGRDataToClassStatisticsFilter::GetFieldName
std::string GetFieldName()
Definition: otbOGRDataToClassStatisticsFilter.hxx:238
otbOGRDataToClassStatisticsFilter.h
otb::PersistentOGRDataToClassStatisticsFilter::PolygonSizeObjectType
itk::SimpleDataObjectDecorator< PolygonSizeMapType > PolygonSizeObjectType
Definition: otbOGRDataToClassStatisticsFilter.h:59
otb::OGRDataToClassStatisticsFilter::SetOGRData
void SetOGRData(const otb::ogr::DataSource *data)
Definition: otbOGRDataToClassStatisticsFilter.hxx:208
otb::OGRDataToClassStatisticsFilter::GetClassCountOutput
const ClassCountObjectType * GetClassCountOutput() const
Definition: otbOGRDataToClassStatisticsFilter.hxx:257
otb::PersistentOGRDataToClassStatisticsFilter::PolygonSizeMapType
std::map< unsigned long, unsigned long > PolygonSizeMapType
Definition: otbOGRDataToClassStatisticsFilter.h:57
otb::PersistentOGRDataToClassStatisticsFilter::GetPolygonSizeOutput
const PolygonSizeObjectType * GetPolygonSizeOutput() const
Definition: otbOGRDataToClassStatisticsFilter.hxx:123
otb::ogr::Feature
Geometric object with descriptive fields.
Definition: otbOGRFeatureWrapper.h:63
otb::PersistentOGRDataToClassStatisticsFilter::ProcessSample
void ProcessSample(const ogr::Feature &feature, typename TInputImage::IndexType &imgIndex, typename TInputImage::PointType &imgPoint, itk::ThreadIdType &threadid) override
Definition: otbOGRDataToClassStatisticsFilter.hxx:165
otb::OGRDataToClassStatisticsFilter::GetOGRData
const otb::ogr::DataSource * GetOGRData()
Definition: otbOGRDataToClassStatisticsFilter.hxx:214
otb::ogr::Feature::ogr
OGRFeature & ogr() const
Definition: otbOGRFeatureWrapper.hxx:158