OTB  6.7.0
Orfeo Toolbox
otbRasterizeVectorDataFilter.h
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 otbRasterizeVectorDataFilter_h
22 #define otbRasterizeVectorDataFilter_h
23 
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 
61 template <class TVectorData, class TInputImage, class TOutputImage = TInputImage >
62 class ITK_EXPORT RasterizeVectorDataFilter :
63  public itk::CastImageFilter< TInputImage, TOutputImage>
64 {
65 public:
71 
73  //itkTypeMacro(RasterizeVectorDataFilter, itk::InPlaceImageFilter);
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;
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 
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." << "( Previous color size : "<< previousBandVectorSize
129  << ", 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 
165  GDALDataset * m_OGRDataSourcePointer;
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
OutputImageType::RegionType OutputImageRegionType
itk::SmartPointer< const Self > ConstPointer
DataTreeType::TreeNodeType InternalTreeNodeType
void AddColor(const OutputImagePixelType &burnValuesPix)
DataTreeType::ConstPointer DataTreeConstPointerType
OutputImageType::InternalPixelType OutputImageInternalPixelType
Burn geometries from the specified VectorData into raster.
itk::Index< Monteverdi_DIMENSION > IndexType
Definition: mvdTypes.h:133
OutputImageType::PixelType OutputImagePixelType
TInputImage InputImageType
itk::CastImageFilter< TInputImage, TOutputImage > Superclass
OutputImageType::PixelType OutputImagePixelType
InputImageType::ConstPointer InputImagePointer
InputImageType::PixelType InputImagePixelType
VectorDataType::DataTreeType DataTreeType
VectorImageType::PointType PointType
Definition: mvdTypes.h:189
InputImageType::RegionType InputImageRegionType