OTB  10.0.0
Orfeo Toolbox
otbRasterizeVectorDataFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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 VectorDataType::DataNodePointerType DataNodePointerType;
97  typedef itk::DataObject DataObjectType;
98 
100  itkSetMacro(AllTouchedMode, bool);
101  itkGetConstReferenceMacro(AllTouchedMode, bool);
102  itkBooleanMacro(AllTouchedMode);
104 
106  const DataObjectType* GetInput(unsigned int idx);
107 
109  virtual void AddVectorData(const VectorDataType* vd);
110 
115  void AddColor(const OutputImagePixelType& burnValuesPix)
116  {
117  // checking : If used several times, the user must always set
118  // colors with the same size.
119  unsigned int previousBandVectorSize = m_BandsToBurn.size();
120  if (previousBandVectorSize != 0)
121  {
122  if (burnValuesPix.Size() != previousBandVectorSize)
123  {
124  itkExceptionMacro(<< "The color added does not have the same number of elements than the previous "
125  << "added one."
126  << "( Previous color size : " << previousBandVectorSize << ", new one size :" << burnValuesPix.Size() << ")");
127  }
128  }
130 
131  // Add the value stored in the output image pixel type to
132  // the burn values vector
133  // Add all the bands to be burned and clear previous bands.
134  m_BandsToBurn.clear();
135  for (unsigned int idx = 0; idx < burnValuesPix.Size(); ++idx)
136  {
137  m_BandsToBurn.push_back(idx + 1); // Gdal bands count begins from 1
138  m_BurnValues.push_back(static_cast<double>(burnValuesPix.GetElement(idx)));
139  }
140  }
141 
142 protected:
143  void GenerateData() override;
144 
147  {
148  if (m_OGRDataSourcePointer != nullptr)
149  {
150  GDALClose(m_OGRDataSourcePointer);
151  }
152  }
153 
154  void GenerateOutputInformation() override;
155 
156  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
157 
158 private:
159  RasterizeVectorDataFilter(const Self&) = delete;
160  void operator=(const Self&) = delete;
161 
163 
164  // Vector Of LayersH
165  std::vector<OGRLayerH> m_SrcDataSetLayers;
166 
167  std::vector<double> m_BurnValues;
168  std::vector<double> m_FullBurnValues;
169  std::vector<int> m_BandsToBurn;
171 
172 }; // end of class RasterizeVectorDataFilter
173 
174 } // end of namespace otb
175 
176 
177 #ifndef OTB_MANUAL_INSTANTIATION
179 #endif
180 
181 #endif
Burn geometries from the specified VectorData into raster.
OutputImageType::RegionType OutputImageRegionType
const DataObjectType * GetInput(unsigned int idx)
InputImageType::PixelType InputImagePixelType
OutputImageType::PixelType OutputImagePixelType
InputImageType::ConstPointer InputImagePointer
const InputImageType * GetInput()
itk::CastImageFilter< TInputImage, TOutputImage > Superclass
VectorDataType::DataNodePointerType DataNodePointerType
RasterizeVectorDataFilter(const Self &)=delete
OutputImageType::InternalPixelType OutputImageInternalPixelType
itk::SmartPointer< const Self > ConstPointer
void operator=(const Self &)=delete
InputImageType::RegionType InputImageRegionType
void AddColor(const OutputImagePixelType &burnValuesPix)
VectorDataType::DataTreeType DataTreeType
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.