otbVectorDataToLabelMapWithAttributesFilter.txx 13.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef __otbVectorDataToLabelMapWithAttributesFilter_txx
#define __otbVectorDataToLabelMapWithAttributesFilter_txx

#include "itkBinaryImageToLabelMapFilter.h"
#include "itkNumericTraits.h"

#include "otbVectorDataProperties.h"

namespace otb
{

template <class TVectorData, class TLabelMap>
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::VectorDataToLabelMapWithAttributesFilter()
{
  m_BackgroundValue = itk::NumericTraits<OutputLabelMapPixelType>::max();
  this->SetNumberOfRequiredInputs(1);
  m_Spacing.Fill(1.0);
  m_Origin.Fill(0.0);
  m_Direction.SetIdentity();
  m_Size.Fill(0);
  m_StartIndex.Fill(0);
40
41
42
43
  m_InitialLabel = itk::NumericTraits<LabelType>::Zero;
  
  m_AutomaticSizeComputation=true;
  m_VectorDataProperties = VectorDataPropertiesType::New();
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
}

//----------------------------------------------------------------------------
template <class TVectorData, class TLabelMap>
void
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::SetSpacing(const SpacingType& spacing)
{
  if (this->m_Spacing != spacing)
    {
    this->m_Spacing = spacing;
    this->Modified();
    }
}

//----------------------------------------------------------------------------
template <class TVectorData, class TLabelMap>
void
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::SetSpacing(const double spacing[2])
{
  SpacingType s(spacing);
  this->SetSpacing(s);
}

//----------------------------------------------------------------------------
template <class TVectorData, class TLabelMap>
void
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::SetSpacing(const float spacing[2])
{
  itk::Vector<float, 2> sf(spacing);
  SpacingType s;
  s.CastFrom(sf);
  this->SetSpacing(s);
}

//----------------------------------------------------------------------------
template <class TVectorData, class TLabelMap>
void
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::SetOrigin(const double origin[2])
{
  OriginType p(origin);
  this->SetOrigin(p);
}

//----------------------------------------------------------------------------
template <class TVectorData, class TLabelMap>
void
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::SetOrigin(const float origin[2])
{
  itk::Point<float, 2> of(origin);
  OriginType p;
  p.CastFrom(of);
  this->SetOrigin(p);
}

103

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
   * overloaded because pb during copyinformation
 */

template <class TVectorData, class TLabelMap>
void VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::GenerateOutputInformation()
{
  // we can't call the superclass method here.

  // get pointers to the input and output
  OutputLabelMapType * outputPtr = this->GetOutput();

  if (!outputPtr)
    {
    return;
    }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    
  RegionType outputLargestPossibleRegion;
  if(m_AutomaticSizeComputation==false)
  {
     // Set the size of the output region
     outputLargestPossibleRegion.SetSize(m_Size);
     outputLargestPossibleRegion.SetIndex(m_StartIndex);
     outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
   
     otbGenericMsgDebugMacro(<< "LargestPossibleRegion " << outputPtr->GetLargestPossibleRegion());
   
     // Set spacing and origin
     outputPtr->SetSpacing(m_Spacing);
     outputPtr->SetOrigin(m_Origin);
     outputPtr->SetDirection(m_Direction);
  }
  else
  {
      typename PolygonType::RegionType  region;
      OriginType origin;
      m_VectorDataProperties->SetVectorDataObject(this->GetInput());
      m_VectorDataProperties->SetBoundingRegion(region);
      //Compute the global bounding box of the vectordata
      m_VectorDataProperties->ComputeBoundingRegion();
      origin[0] = m_VectorDataProperties->GetBoundingRegion().GetImageRegion().GetIndex(0);
      origin[1] = m_VectorDataProperties->GetBoundingRegion().GetImageRegion().GetIndex(1);

      // Set spacing and origin
      outputLargestPossibleRegion.SetSize(m_VectorDataProperties->GetBoundingRegion().GetImageRegion().GetSize());
      outputLargestPossibleRegion.SetIndex(m_StartIndex);
      
      outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
      outputPtr->SetSpacing(this->GetInput()->GetSpacing());
      outputPtr->SetOrigin(origin);
      outputPtr->SetDirection(m_Direction);
  }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  return;
}
/*
template <class TVectorData, class TLabelMap >
void
    VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap >
::GenerateInputRequestedRegion()
{

  //call the superclass' implementation of this method
  Superclass::GenerateInputRequestedRegion();

  // We need all the input.
  InputVectorDataPointer input = const_cast<InputVectorDataType *>(this->GetInput());
  if( !input )
  {
    return;
  }
  input->SetRequestedRegionToLargestPossibleRegion ();
}


template <class TVectorData, class TLabelMap >
void
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap >
::EnlargeOutputRequestedRegion(itk::DataObject *)
{
  this->GetOutput()
    ->SetRequestedRegion( this->GetOutput()->GetLargestPossibleRegion() );
}
*/

template<class TVectorData, class TLabelMap>
void
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::SetInput(const InputVectorDataType *input)
{
// Process object is not const-correct so the const_cast is required here
  this->itk::ProcessObject::SetNthInput(0,
                                        const_cast<InputVectorDataType *>(input));
}

template<class TVectorData, class TLabelMap>
void
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::SetInput(unsigned int idx, const InputVectorDataType *input)
{
  // Process object is not const-correct so the const_cast is required here
  this->itk::ProcessObject::SetNthInput(idx,
                                        const_cast<InputVectorDataType *>(input));
}

template<class TVectorData, class TLabelMap>
const typename VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>::InputVectorDataType *
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::GetInput(void)
{
  if (this->GetNumberOfInputs() < 1)
    {
    return 0;
    }

  return static_cast<const TVectorData *>
           (this->itk::ProcessObject::GetInput(0));
}

template<class TVectorData, class TLabelMap>
const typename VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>::InputVectorDataType *
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::GetInput(unsigned int idx)
{
  return static_cast<const TVectorData *>
           (this->itk::ProcessObject::GetInput(idx));
}

template <class TVectorData, class TLabelMap>
void
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::GenerateData()
{
  // Allocate the output
  this->AllocateOutputs();

  OutputLabelMapType * output = this->GetOutput();

  //For each input
  for (unsigned int idx = 0; idx < this->GetNumberOfInputs(); ++idx)
    {
    if (this->GetInput(idx))
      {

      InputVectorDataConstPointer input = this->GetInput(idx);
      InternalTreeNodeType *      inputRoot = const_cast<InternalTreeNodeType *>(input->GetDataTree()->GetRoot());
      //Use our own value for the background
251
      output->SetBackgroundValue(m_BackgroundValue);
252
      //Set the value of the first label
253
      m_lab = m_InitialLabel;
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//       otbGenericMsgDebugMacro(<<"input " <<  idx);

      //The projection information
      output->SetMetaDataDictionary(input->GetMetaDataDictionary());
      ProcessNode(inputRoot);

      }
    }
}

template<class TVectorData, class TLabelMap>
void
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::ProcessNode(InternalTreeNodeType * source)
{

  // Get the children list from the input node
  ChildrenListType children = source->GetChildrenList();

  // For each child
  for (typename ChildrenListType::iterator it = children.begin(); it != children.end(); ++it)
    {
    // Copy input DataNode info
    DataNodePointerType dataNode = (*it)->Get();
    otbGenericMsgDebugMacro(<< "Type of node " << dataNode->GetNodeType() << " id" << dataNode->GetNodeId());
    switch (dataNode->GetNodeType())
      {
      case otb::ROOT:
        {
        ProcessNode((*it));
        break;
        }
      case otb::DOCUMENT:
        {
        ProcessNode((*it));
        break;
        }
      case otb::FOLDER:
        {
        ProcessNode((*it));
        break;
        }
      case FEATURE_POINT:
        {
        otbGenericMsgDebugMacro(<< "Insert Point from vectorData");
        IndexType index;
        this->GetOutput()->TransformPhysicalPointToIndex(dataNode->GetPoint(), index);

        this->GetOutput()->SetPixel(index, m_lab);
303
        m_lab += 1;
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
        break;
        }
      case otb::FEATURE_LINE:
        {
        //TODO Bresenham
        itkExceptionMacro(
          << "This type (FEATURE_LINE) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
        break;
        }
      case FEATURE_POLYGON:
        {

        /** correct polygon exterior ring (simplify and close the pokygon)*/
        CorrectFunctorType correct;
        PolygonPointerType correctPolygonExtRing = correct(dataNode->GetPolygonExteriorRing());

        typedef typename DataNodeType::PolygonType PolygonType;
        typedef typename PolygonType::RegionType   RegionType;
        typedef typename PolygonType::VertexType   VertexType;
        typedef typename IndexType::IndexValueType IndexValueType;
        typedef typename VertexType::ValueType     VertexValueType;
        RegionType polygonExtRingBoundReg = correctPolygonExtRing->GetBoundingRegion();

        VertexType vertex;

Arnaud Jaen's avatar
Arnaud Jaen committed
329
        // For each position in the bounding region of the polygon
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
        for (double i = polygonExtRingBoundReg.GetOrigin(0);
             i < polygonExtRingBoundReg.GetOrigin(0) + polygonExtRingBoundReg.GetSize(0);
             i += this->GetOutput()->GetSpacing()[0])
          {
          vertex[0] = static_cast<VertexValueType>(i);
          for (double j = polygonExtRingBoundReg.GetOrigin(1);
               j < polygonExtRingBoundReg.GetOrigin(1) + polygonExtRingBoundReg.GetSize(1);
               j += this->GetOutput()->GetSpacing()[1])
            {
            vertex[1] = static_cast<VertexValueType>(j);

            if (correctPolygonExtRing->IsInside(vertex) || correctPolygonExtRing->IsOnEdge (vertex))
              {
              IndexType index;
//               index[0] = static_cast<IndexValueType>(vertex[0] - polygonExtRingBoundReg.GetOrigin(0));
//               index[1] = static_cast<IndexValueType>(vertex[1] - polygonExtRingBoundReg.GetOrigin(1));
                 index[0] = static_cast<IndexValueType>(vertex[0]-this->GetOutput()->GetOrigin()[0]);
                 index[1] = static_cast<IndexValueType>(vertex[1]-this->GetOutput()->GetOrigin()[1]);
//               index[0] += this->GetOutput()->GetOrigin()[0];
//               index[1] += this->GetOutput()->GetOrigin()[1];
//               std::cout << "index " << index << std::endl;
              if (this->GetOutput()->HasLabel(m_lab))
                {
                if (!this->GetOutput()->GetLabelObject(m_lab)->HasIndex(index))
                  { //Add a pixel to the current labelObject
                  this->GetOutput()->SetPixel(index, m_lab);
                  }
                }
              else
                {
                //Add a pixel to the current labelObject
                this->GetOutput()->SetPixel(index, m_lab);
                }
                //add attributes
Arnaud Jaen's avatar
Arnaud Jaen committed
364
                AttributesValueType fieldValue;
OTB Bot's avatar
STYLE    
OTB Bot committed
365
                for(unsigned int ii=0; ii<dataNode->GetFieldList().size(); ii++)
Arnaud Jaen's avatar
Arnaud Jaen committed
366
367
368
369
                {
                  fieldValue = static_cast<AttributesValueType>( dataNode->GetFieldAsString(dataNode->GetFieldList()[ii]) );
                  this->GetOutput()->GetLabelObject(m_lab)->SetAttribute( dataNode->GetFieldList()[ii].c_str(), fieldValue );
                }
370
371
372
373
              }
            }
          }
        //Modify the label for the next layer
374
        m_lab += 1;
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
        break;
        }
      case FEATURE_MULTIPOINT:
        {
        itkExceptionMacro(
          <<
          "This type (FEATURE_MULTIPOINT) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
        break;
        }
      case FEATURE_MULTILINE:
        {
        itkExceptionMacro(
          << "This type (FEATURE_MULTILINE) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
        break;
        }
      case FEATURE_MULTIPOLYGON:
        {
        itkExceptionMacro(
          <<
          "This type (FEATURE_MULTIPOLYGON) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
        break;
        }
      case FEATURE_COLLECTION:
        {
        itkExceptionMacro(
          <<
          "This type (FEATURE_COLLECTION) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
        break;
        }
      }
    }
}

template <class TVectorData, class TLabelMap>
void
VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
  Superclass::PrintSelf(os, indent);
  os << indent << "BackgroundValue: "  << static_cast<typename itk::NumericTraits<OutputLabelMapPixelType>::PrintType>(
    m_BackgroundValue) << std::endl;
}

} // end namespace otb

#endif