OTB  6.7.0
Orfeo Toolbox
otbOGRDataToClassStatisticsFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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>
33 {
34  this->SetNumberOfRequiredOutputs(3);
35  this->SetNthOutput(0,TInputImage::New());
36  this->SetNthOutput(1,ClassCountObjectType::New());
37  this->SetNthOutput(2,PolygonSizeObjectType::New());
38 }
39 
40 template<class TInputImage, class TMaskImage>
41 void
44 {
45  otb::ogr::DataSource* vectors = const_cast<otb::ogr::DataSource*>(this->GetOGRData());
46  vectors->GetLayer(this->GetLayerIndex()).SetSpatialFilter(nullptr);
47 
48  ClassCountMapType &classCount = this->GetClassCountOutput()->Get();
49  PolygonSizeMapType &polygonSize = this->GetPolygonSizeOutput()->Get();
50 
51  // Reset outputs
52  classCount.clear();
53  polygonSize.clear();
54  // Copy temporary stats to outputs
55  for (unsigned int k=0 ; k < this->GetNumberOfThreads() ; k++)
56  {
57  ClassCountMapType::iterator itClass = m_ElmtsInClassThread[k].begin();
58  for (; itClass != m_ElmtsInClassThread[k].end() ; ++itClass)
59  {
60  if (classCount.count(itClass->first))
61  {
62  classCount[itClass->first] += itClass->second;
63  }
64  else
65  {
66  classCount[itClass->first] = itClass->second;
67  }
68  }
69  PolygonSizeMapType::iterator itPoly = m_PolygonThread[k].begin();
70  for (; itPoly != m_PolygonThread[k].end() ; ++itPoly)
71  {
72  if (polygonSize.count(itPoly->first))
73  {
74  polygonSize[itPoly->first] += itPoly->second;
75  }
76  else
77  {
78  polygonSize[itPoly->first] = itPoly->second;
79  }
80  }
81  }
82 
83  m_ElmtsInClassThread.clear();
84  m_PolygonThread.clear();
85  m_NbPixelsThread.clear();
86 }
87 
88 template<class TInputImage, class TMaskImage>
89 void
91 ::Reset(void)
92 {
93  m_ElmtsInClassThread.clear();
94  m_PolygonThread.clear();
95  m_NbPixelsThread.clear();
96 
97  m_ElmtsInClassThread.resize(this->GetNumberOfThreads());
98  m_PolygonThread.resize(this->GetNumberOfThreads());
99  m_NbPixelsThread.resize(this->GetNumberOfThreads());
100  m_CurrentClass.resize(this->GetNumberOfThreads());
101  m_CurrentFID.resize(this->GetNumberOfThreads());
102 }
103 
104 template<class TInputImage, class TMaskImage>
108 {
109  if (this->GetNumberOfOutputs()<2)
110  {
111  return nullptr;
112  }
113  return static_cast<const ClassCountObjectType *>(this->itk::ProcessObject::GetOutput(1));
114 }
115 
116 template<class TInputImage, class TMaskImage>
120 {
121  if (this->GetNumberOfOutputs()<2)
122  {
123  return nullptr;
124  }
125  return static_cast<ClassCountObjectType *>(this->itk::ProcessObject::GetOutput(1));
126 }
127 
128 template<class TInputImage, class TMaskImage>
132 {
133  if (this->GetNumberOfOutputs()<3)
134  {
135  return nullptr;
136  }
137  return static_cast<const PolygonSizeObjectType *>(this->itk::ProcessObject::GetOutput(2));
138 }
139 
140 template<class TInputImage, class TMaskImage>
144 {
145  if (this->GetNumberOfOutputs()<3)
146  {
147  return nullptr;
148  }
149  return static_cast<PolygonSizeObjectType *>(this->itk::ProcessObject::GetOutput(2));
150 }
151 
152 template<class TInputImage, class TMaskImage>
156 {
157  switch (idx)
158  {
159  case 0:
160  return static_cast<itk::DataObject*>(TInputImage::New().GetPointer());
161  break;
162  case 1:
163  return static_cast<itk::DataObject*>(ClassCountObjectType::New().GetPointer());
164  break;
165  case 2:
166  return static_cast<itk::DataObject*>(PolygonSizeObjectType::New().GetPointer());
167  break;
168  default:
169  // might as well make an image
170  return static_cast<itk::DataObject*>(TInputImage::New().GetPointer());
171  break;
172  }
173 }
174 
175 template<class TInputImage, class TMaskImage>
176 void
179  const ogr::Feature&,
180  typename TInputImage::IndexType&,
181  typename TInputImage::PointType&,
182  itk::ThreadIdType& threadid)
183 {
184  std::string& className = m_CurrentClass[threadid];
185  unsigned long& fId = m_CurrentFID[threadid];
186 
187  m_ElmtsInClassThread[threadid][className]++;
188  m_PolygonThread[threadid][fId]++;
189  m_NbPixelsThread[threadid]++;
190 }
191 
192 template<class TInputImage, class TMaskImage>
193 void
196  itk::ThreadIdType& threadid)
197 {
198  std::string className(feature.ogr().GetFieldAsString(this->GetFieldIndex()));
199  unsigned long fId = feature.ogr().GetFID();
200  if (!m_ElmtsInClassThread[threadid].count(className))
201  {
202  m_ElmtsInClassThread[threadid][className] = 0;
203  }
204  if (!m_PolygonThread[threadid].count(fId))
205  {
206  m_PolygonThread[threadid][fId] = 0;
207  }
208  m_CurrentClass[threadid] = className;
209  m_CurrentFID[threadid] = fId;
210 }
211 
212 // -------------- otb::OGRDataToClassStatisticsFilter --------------------------
213 
214 template<class TInputImage, class TMaskImage>
215 void
217 ::SetInput(const TInputImage* image)
218 {
219  this->GetFilter()->SetInput(image);
220 }
221 
222 template<class TInputImage, class TMaskImage>
223 const TInputImage*
226 {
227  return this->GetFilter()->GetInput();
228 }
229 
230 template<class TInputImage, class TMaskImage>
231 void
234 {
235  this->GetFilter()->SetOGRData(data);
236 }
237 
238 template<class TInputImage, class TMaskImage>
242 {
243  return this->GetFilter()->GetOGRData();
244 }
245 
246 template<class TInputImage, class TMaskImage>
247 void
249 ::SetMask(const TMaskImage* mask)
250 {
251  this->GetFilter()->SetMask(mask);
252 }
253 
254 template<class TInputImage, class TMaskImage>
255 const TMaskImage*
258 {
259  return this->GetFilter()->GetMask();
260 }
261 
262 template<class TInputImage, class TMaskImage>
263 void
265 ::SetFieldName(std::string &key)
266 {
267  this->GetFilter()->SetFieldName(key);
268 }
269 
270 template<class TInputImage, class TMaskImage>
271 std::string
274 {
275  return this->GetFilter()->GetFieldName();
276 }
277 
278 template<class TInputImage, class TMaskImage>
279 void
281 ::SetLayerIndex(int index)
282 {
283  this->GetFilter()->SetLayerIndex(index);
284 }
285 
286 template<class TInputImage, class TMaskImage>
287 int
290 {
291  return this->GetFilter()->GetLayerIndex();
292 }
293 
294 template<class TInputImage, class TMaskImage>
298 {
299  return this->GetFilter()->GetClassCountOutput();
300 }
301 
302 template<class TInputImage, class TMaskImage>
306 {
307  return this->GetFilter()->GetClassCountOutput();
308 }
309 
310 template<class TInputImage, class TMaskImage>
314 {
315  return this->GetFilter()->GetPolygonSizeOutput();
316 }
317 
318 template<class TInputImage, class TMaskImage>
322 {
323  return this->GetFilter()->GetPolygonSizeOutput();
324 }
325 
326 
327 } // end of namespace otb
328 
329 #endif
Superclass::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
Collection of geometric objects.
Layer GetLayer(vcl_size_t i)
itk::DataObject::Pointer MakeOutput(DataObjectPointerArraySizeType idx) override
const PolygonSizeObjectType * GetPolygonSizeOutput() const
OGRFeature & ogr() const
FilterType::ClassCountObjectType ClassCountObjectType
void PrepareFeature(const ogr::Feature &feature, itk::ThreadIdType &threadid) override
void SetOGRData(const otb::ogr::DataSource *data)
itk::Index< Monteverdi_DIMENSION > IndexType
Definition: mvdTypes.h:133
FilterType::PolygonSizeObjectType PolygonSizeObjectType
virtual void SetInput(const TInputImage *image)
std::map< unsigned long, unsigned long > PolygonSizeMapType
unsigned int ThreadIdType
std::map< std::string, unsigned long > ClassCountMapType
void ProcessSample(const ogr::Feature &feature, typename TInputImage::IndexType &imgIndex, typename TInputImage::PointType &imgPoint, itk::ThreadIdType &threadid) override
Geometric objet with descriptive fields.
VectorImageType::PointType PointType
Definition: mvdTypes.h:189
void SetSpatialFilter(OGRGeometry const *spatialFilter)
const ClassCountObjectType * GetClassCountOutput() const
DataObject * GetOutput(const DataObjectIdentifierType &key)