21 #ifndef otbOGRDataToSamplePositionFilter_hxx
22 #define otbOGRDataToSamplePositionFilter_hxx
30 template <
class TInputImage,
class TMaskImage,
class TSampler>
33 this->SetNumberOfRequiredOutputs(2);
34 m_OriginFieldName = std::string(
"originfid");
35 m_UseOriginField =
true;
38 template <
class TInputImage,
class TMaskImage,
class TSampler>
42 for (
unsigned int i = 0; i < m_Samplers.size(); ++i)
44 for (
typename SamplerMapType::iterator iter = m_Samplers[i].begin(); iter != m_Samplers[i].end(); ++iter)
46 iter->second->Reset();
51 this->ClearAdditionalFields();
52 m_UseOriginField = (this->GetOriginFieldName().size() > 0);
55 this->CreateAdditionalField(this->GetOriginFieldName(), OFTInteger, 12);
59 this->ComputeClassPartition();
63 for (
unsigned int k = 0; k < this->GetNumberOfLevels(); k++)
68 this->InitializeOutputDataSource(inputDS, output);
73 template <
class TInputImage,
class TMaskImage,
class TSampler>
77 if (m_Samplers.size() < (level + 1))
79 itkExceptionMacro(
"The sampler level " << level <<
" doesn't exist !");
81 return m_Samplers[level];
84 template <
class TInputImage,
class TMaskImage,
class TSampler>
89 this->SetNthOutput(1 + level, data);
91 if (m_Samplers.size() < (level + 1))
93 m_Samplers.resize(level + 1);
95 m_Samplers[level].clear();
96 for (SamplingRateCalculator::MapRateType::const_iterator it = map.begin(); it != map.end(); ++it)
99 sampler->SetNumberOfElements(it->second.Required, it->second.Tot);
100 m_Samplers[level][it->first] = sampler;
104 template <
class TInputImage,
class TMaskImage,
class TSampler>
107 if (this->GetNumberOfOutputs() < (level + 2))
114 template <
class TInputImage,
class TMaskImage,
class TSampler>
117 return (this->GetNumberOfOutputs() - 1);
120 template <
class TInputImage,
class TMaskImage,
class TSampler>
124 for (
unsigned int i = (this->GetNumberOfOutputs() - 1); i > 0; --i)
126 this->itk::ProcessObject::RemoveOutput(i);
132 template <
class TInputImage,
class TMaskImage,
class TSampler>
138 return static_cast<itk::DataObject*
>(TInputImage::New().GetPointer());
146 return static_cast<itk::DataObject*
>(TInputImage::New().GetPointer());
149 template <
class TInputImage,
class TMaskImage,
class TSampler>
151 typename TInputImage::PointType& imgPoint,
152 itk::ThreadIdType& threadid)
154 std::string className(feature.
ogr().GetFieldAsString(this->GetFieldIndex()));
155 for (
unsigned int i = 0; i < this->GetNumberOfLevels(); ++i)
157 if (m_Samplers[i][className]->TakeSample())
159 OGRPoint ogrTmpPoint;
160 ogrTmpPoint.setX(imgPoint[0]);
161 ogrTmpPoint.setY(imgPoint[1]);
163 ogr::Layer outputLayer = this->GetInMemoryOutput(threadid, i);
166 if (m_UseOriginField)
168 feat[this->GetOriginFieldName()].SetValue(
static_cast<int>(feature.
GetFID()));
177 template <
class TInputImage,
class TMaskImage,
class TSampler>
180 TInputImage* outputImage = this->GetOutput();
184 const RegionType& requestedRegion = outputImage->GetRequestedRegion();
185 itk::ContinuousIndex<double> startIndex(requestedRegion.GetIndex());
186 itk::ContinuousIndex<double> endIndex(requestedRegion.GetUpperIndex());
187 startIndex[0] += -0.5;
188 startIndex[1] += -0.5;
191 itk::Point<double, 2> startPoint;
192 itk::Point<double, 2> endPoint;
193 outputImage->TransformContinuousIndexToPhysicalPoint(startIndex, startPoint);
194 outputImage->TransformContinuousIndexToPhysicalPoint(endIndex, endPoint);
197 OGRPolygon tmpPolygon;
199 ring.addPoint(startPoint[0], startPoint[1], 0.0);
200 ring.addPoint(startPoint[0], endPoint[1], 0.0);
201 ring.addPoint(endPoint[0], endPoint[1], 0.0);
202 ring.addPoint(endPoint[0], startPoint[1], 0.0);
203 ring.addPoint(startPoint[0], startPoint[1], 0.0);
204 tmpPolygon.addRing(&ring);
208 unsigned int numberOfThreads = this->GetNumberOfThreads();
209 std::vector<ogr::Layer> tmpLayers;
210 for (
unsigned int i = 0; i < numberOfThreads; i++)
212 tmpLayers.push_back(this->GetInMemoryInput(i));
217 std::string className;
218 for (; featIt != inLayer.
end(); ++featIt)
221 dstFeature.
SetFrom(*featIt, TRUE);
222 dstFeature.
SetFID(featIt->GetFID());
223 className = featIt->ogr().GetFieldAsString(this->GetFieldIndex());
224 tmpLayers[m_ClassPartition[className]].CreateFeature(dstFeature);
230 template <
class TInputImage,
class TMaskImage,
class TSampler>
233 m_ClassPartition.clear();
234 typedef std::map<std::string, unsigned long> ClassCountMapType;
235 ClassCountMapType classCounts;
236 for (
typename SamplerMapType::const_iterator it = m_Samplers[0].begin(); it != m_Samplers[0].end(); ++it)
238 classCounts[it->first] = it->second->GetTotalElements();
241 int numberOfThreads = this->GetNumberOfThreads();
242 typedef std::vector<unsigned long> LoadVectorType;
243 LoadVectorType currentLoad;
244 currentLoad.resize(numberOfThreads, 0UL);
246 ClassCountMapType::iterator largestClass;
247 unsigned long minLoad;
248 unsigned int destThread;
249 while (classCounts.size())
252 largestClass = classCounts.begin();
253 for (ClassCountMapType::iterator iter = classCounts.begin(); iter != classCounts.end(); ++iter)
255 if (iter->second > largestClass->second)
262 minLoad = currentLoad[0];
264 for (
unsigned int k = 0; k < currentLoad.size(); ++k)
266 if (currentLoad[k] < minLoad)
268 minLoad = currentLoad[k];
272 m_ClassPartition[largestClass->first] = destThread;
273 currentLoad[destThread] += largestClass->second;
276 classCounts.erase(largestClass);
280 template <
class TInputImage,
class TMaskImage,
class TSampler>
285 OGRErr err = outLayer.
ogr().StartTransaction();
286 if (err != OGRERR_NONE)
288 itkExceptionMacro(<<
"Unable to start transaction for OGR layer " << outLayer.
ogr().GetName() <<
".");
292 for (
auto& label : m_ClassPartition)
294 ogr::Layer inLayer = this->GetInMemoryOutput(label.second, outIdx);
301 for (
auto tmpIt = inLayer.
begin(); tmpIt != inLayer.
end(); ++tmpIt)
303 if (label.first.compare(tmpIt->ogr().GetFieldAsString(this->GetFieldIndex())) != 0)
312 dstFeature.
SetFrom(*tmpIt, TRUE);
318 err = outLayer.
ogr().CommitTransaction();
319 if (err != OGRERR_NONE)
321 itkExceptionMacro(<<
"Unable to commit transaction for OGR layer " << outLayer.
ogr().GetName() <<
".");
327 template <
class TInputImage,
class TMaskImage,
class TSampler>
330 this->GetFilter()->SetInput(image);
333 template <
class TInputImage,
class TMaskImage,
class TSampler>
336 return this->GetFilter()->GetInput();
339 template <
class TInputImage,
class TMaskImage,
class TSampler>
342 this->GetFilter()->SetOGRData(data);
345 template <
class TInputImage,
class TMaskImage,
class TSampler>
348 return this->GetFilter()->GetOGRData();
351 template <
class TInputImage,
class TMaskImage,
class TSampler>
354 this->GetFilter()->SetMask(mask);
357 template <
class TInputImage,
class TMaskImage,
class TSampler>
360 return this->GetFilter()->GetMask();
363 template <
class TInputImage,
class TMaskImage,
class TSampler>
366 this->GetFilter()->SetFieldName(key);
369 template <
class TInputImage,
class TMaskImage,
class TSampler>
372 return this->GetFilter()->GetFieldName();
375 template <
class TInputImage,
class TMaskImage,
class TSampler>
378 this->GetFilter()->SetLayerIndex(index);
381 template <
class TInputImage,
class TMaskImage,
class TSampler>
384 return this->GetFilter()->GetLayerIndex();
387 template <
class TInputImage,
class TMaskImage,
class TSampler>
391 for (
typename SamplerMapType::iterator it = samplers.begin(); it != samplers.end(); ++it)
393 it->second->SetParameters(param);
397 template <
class TInputImage,
class TMaskImage,
class TSampler>
401 return this->GetFilter()->GetSamplers(level);
404 template <
class TInputImage,
class TMaskImage,
class TSampler>
409 this->GetFilter()->SetOutputPositionContainerAndRates(data, map, level);
412 template <
class TInputImage,
class TMaskImage,
class TSampler>
415 return this->GetFilter()->GetOutputPositionContainer(level);
418 template <
class TInputImage,
class TMaskImage,
class TSampler>
421 this->GetFilter()->SetOriginFieldName(key);
424 template <
class TInputImage,
class TMaskImage,
class TSampler>
427 return this->GetFilter()->GetOriginFieldName();