OTB  9.0.0
Orfeo Toolbox
otbRasterizeVectorDataFilter.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 #ifndef otbRasterizeVectorDataFilter_h
22 #define otbRasterizeVectorDataFilter_h
23 
24 #include "itkUnaryFunctorImageFilter.h"
25 #include "itkImageToImageFilter.h"
26 #include "itkCastImageFilter.h"
27 #include "otbMacro.h"
28 
29 #include "otbVectorData.h"
30 
31 #include "gdal.h"
32 #include "gdal_alg.h"
33 #include "ogr_srs_api.h"
34 
35 namespace otb
36 {
37 
62 template <class TVectorData, class TInputImage, class TOutputImage = TInputImage>
63 class ITK_EXPORT RasterizeVectorDataFilter : public itk::CastImageFilter<TInputImage, TOutputImage>
64 {
65 public:
68  typedef itk::CastImageFilter<TInputImage, TOutputImage> Superclass;
69  typedef itk::SmartPointer<Self> Pointer;
70  typedef itk::SmartPointer<const Self> ConstPointer;
71 
73  // itkTypeMacro(RasterizeVectorDataFilter, itk::InPlaceImageFilter);
74  itkTypeMacro(RasterizeVectorDataFilter, itk::CastImageFilter);
76 
78  itkNewMacro(Self);
79 
80  typedef TInputImage InputImageType;
81  typedef typename InputImageType::ConstPointer InputImagePointer;
82  typedef typename InputImageType::RegionType InputImageRegionType;
83  typedef typename InputImageType::PixelType InputImagePixelType;
84  typedef typename InputImageType::IndexType InputIndexType;
85  typedef typename InputImageType::PointType InputPointType;
86 
87  typedef TOutputImage OutputImageType;
88  typedef typename OutputImageType::Pointer OutputImagePointer;
89  typedef typename OutputImageType::RegionType OutputImageRegionType;
90  typedef typename OutputImageType::PixelType OutputImagePixelType;
91  typedef typename OutputImageType::InternalPixelType OutputImageInternalPixelType;
92 
94  typedef TVectorData VectorDataType;
95  typedef typename VectorDataType::DataTreeType DataTreeType;
96  typedef typename DataTreeType::TreeNodeType InternalTreeNodeType;
97  typedef typename DataTreeType::Pointer DataTreePointerType;
98  typedef typename DataTreeType::ConstPointer DataTreeConstPointerType;
99 
100  typedef itk::DataObject DataObjectType;
101 
103  itkSetMacro(AllTouchedMode, bool);
104  itkGetConstReferenceMacro(AllTouchedMode, bool);
105  itkBooleanMacro(AllTouchedMode);
107 
108  const InputImageType* GetInput();
109  const DataObjectType* GetInput(unsigned int idx);
110 
112  virtual void AddVectorData(const VectorDataType* vd);
113 
118  void AddColor(const OutputImagePixelType& burnValuesPix)
119  {
120  // checking : If used several times, the user must always set
121  // colors with the same size.
122  unsigned int previousBandVectorSize = m_BandsToBurn.size();
123  if (previousBandVectorSize != 0)
124  {
125  if (burnValuesPix.Size() != previousBandVectorSize)
126  {
127  itkExceptionMacro(<< "The color added does not have the same number of elements than the previous "
128  << "added one."
129  << "( Previous color size : " << previousBandVectorSize << ", new one size :" << burnValuesPix.Size() << ")");
130  }
131  }
133 
134  // Add the value stored in the output image pixel type to
135  // the burn values vector
136  // Add all the bands to be burned and clear previous bands.
137  m_BandsToBurn.clear();
138  for (unsigned int idx = 0; idx < burnValuesPix.Size(); ++idx)
139  {
140  m_BandsToBurn.push_back(idx + 1); // Gdal bands count begins from 1
141  m_BurnValues.push_back(static_cast<double>(burnValuesPix.GetElement(idx)));
142  }
143  }
144 
145 protected:
146  void GenerateData() override;
147 
150  {
151  if (m_OGRDataSourcePointer != nullptr)
152  {
153  GDALClose(m_OGRDataSourcePointer);
154  }
155  }
156 
157  void GenerateOutputInformation() override;
158 
159  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
160 
161 private:
162  RasterizeVectorDataFilter(const Self&) = delete;
163  void operator=(const Self&) = delete;
164 
166 
167  // Vector Of LayersH
168  std::vector<OGRLayerH> m_SrcDataSetLayers;
169 
170  std::vector<double> m_BurnValues;
171  std::vector<double> m_FullBurnValues;
172  std::vector<int> m_BandsToBurn;
174 
175 }; // end of class RasterizeVectorDataFilter
176 
177 } // end of namespace otb
178 
179 
180 #ifndef OTB_MANUAL_INSTANTIATION
182 #endif
183 
184 #endif
otb::RasterizeVectorDataFilter::VectorDataType
TVectorData VectorDataType
Definition: otbRasterizeVectorDataFilter.h:94
otb::RasterizeVectorDataFilter::DataTreeConstPointerType
DataTreeType::ConstPointer DataTreeConstPointerType
Definition: otbRasterizeVectorDataFilter.h:98
otb::RasterizeVectorDataFilter::Superclass
itk::CastImageFilter< TInputImage, TOutputImage > Superclass
Definition: otbRasterizeVectorDataFilter.h:68
otbRasterizeVectorDataFilter.hxx
otb::RasterizeVectorDataFilter::OutputImageRegionType
OutputImageType::RegionType OutputImageRegionType
Definition: otbRasterizeVectorDataFilter.h:89
otb::RasterizeVectorDataFilter::OutputImageType
TOutputImage OutputImageType
Definition: otbRasterizeVectorDataFilter.h:87
otb::RasterizeVectorDataFilter::m_FullBurnValues
std::vector< double > m_FullBurnValues
Definition: otbRasterizeVectorDataFilter.h:171
otb::RasterizeVectorDataFilter::InputIndexType
InputImageType::IndexType InputIndexType
Definition: otbRasterizeVectorDataFilter.h:84
otb::RasterizeVectorDataFilter::InputImagePointer
InputImageType::ConstPointer InputImagePointer
Definition: otbRasterizeVectorDataFilter.h:81
otb::RasterizeVectorDataFilter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbRasterizeVectorDataFilter.h:70
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::RasterizeVectorDataFilter::DataTreeType
VectorDataType::DataTreeType DataTreeType
Definition: otbRasterizeVectorDataFilter.h:95
otb::RasterizeVectorDataFilter::m_BurnValues
std::vector< double > m_BurnValues
Definition: otbRasterizeVectorDataFilter.h:170
otbMacro.h
otb::RasterizeVectorDataFilter::OutputImagePixelType
OutputImageType::PixelType OutputImagePixelType
Definition: otbRasterizeVectorDataFilter.h:90
otb::RasterizeVectorDataFilter::InputPointType
InputImageType::PointType InputPointType
Definition: otbRasterizeVectorDataFilter.h:85
otb::RasterizeVectorDataFilter::DataObjectType
itk::DataObject DataObjectType
Definition: otbRasterizeVectorDataFilter.h:100
otb::RasterizeVectorDataFilter::~RasterizeVectorDataFilter
~RasterizeVectorDataFilter() override
Definition: otbRasterizeVectorDataFilter.h:149
otb::RasterizeVectorDataFilter
Burn geometries from the specified VectorData into raster.
Definition: otbRasterizeVectorDataFilter.h:63
otb::RasterizeVectorDataFilter::m_AllTouchedMode
bool m_AllTouchedMode
Definition: otbRasterizeVectorDataFilter.h:173
otb::RasterizeVectorDataFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbRasterizeVectorDataFilter.h:69
otb::RasterizeVectorDataFilter::InputImageType
TInputImage InputImageType
Definition: otbRasterizeVectorDataFilter.h:78
otb::RasterizeVectorDataFilter::InputImageRegionType
InputImageType::RegionType InputImageRegionType
Definition: otbRasterizeVectorDataFilter.h:82
otb::RasterizeVectorDataFilter::m_OGRDataSourcePointer
GDALDataset * m_OGRDataSourcePointer
Definition: otbRasterizeVectorDataFilter.h:165
otbVectorData.h
otb::RasterizeVectorDataFilter::InternalTreeNodeType
DataTreeType::TreeNodeType InternalTreeNodeType
Definition: otbRasterizeVectorDataFilter.h:96
otb::RasterizeVectorDataFilter::Self
RasterizeVectorDataFilter Self
Definition: otbRasterizeVectorDataFilter.h:67
otb::RasterizeVectorDataFilter::m_BandsToBurn
std::vector< int > m_BandsToBurn
Definition: otbRasterizeVectorDataFilter.h:172
otb::RasterizeVectorDataFilter::InputImagePixelType
InputImageType::PixelType InputImagePixelType
Definition: otbRasterizeVectorDataFilter.h:83
otb::RasterizeVectorDataFilter::OutputImagePointer
OutputImageType::Pointer OutputImagePointer
Definition: otbRasterizeVectorDataFilter.h:88
otb::RasterizeVectorDataFilter::DataTreePointerType
DataTreeType::Pointer DataTreePointerType
Definition: otbRasterizeVectorDataFilter.h:97
otb::RasterizeVectorDataFilter::m_SrcDataSetLayers
std::vector< OGRLayerH > m_SrcDataSetLayers
Definition: otbRasterizeVectorDataFilter.h:168
otb::RasterizeVectorDataFilter::OutputImageInternalPixelType
OutputImageType::InternalPixelType OutputImageInternalPixelType
Definition: otbRasterizeVectorDataFilter.h:91
otb::RasterizeVectorDataFilter::AddColor
void AddColor(const OutputImagePixelType &burnValuesPix)
Definition: otbRasterizeVectorDataFilter.h:118