otbVectorDataToLabelImageFilter.h 6.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
20

21 22
#ifndef otbVectorDataToLabelImageFilter_h
#define otbVectorDataToLabelImageFilter_h
23 24

#include "itkImageToImageFilter.h"
25
#include "itkImageRegionIterator.h"
26 27 28 29 30
#include "otbMacro.h"

#include "otbVectorData.h"

#include "gdal.h"
Julien Michel's avatar
Julien Michel committed
31
#include "ogr_api.h"
32
#include "otbOGRVersionProxy.h"
33
#include <string>
34 35 36 37 38

namespace otb {

/** \class VectorDataToLabelImageFilter
 *  \brief Burn geometries from the specified VectorData into raster
OTB Bot's avatar
OTB Bot committed
39
 *
40 41 42 43
 *  This class handles burning several input VectorDatas into the
 *  output raster.  The burn values are extracted from a field set by
 *  the user.If no burning field is set, the "FID" is used by default.
 *
44
 *  Setting the output raster information can be done in two ways by:
45 46 47
 *    - Setting the Origin/Size/Spacing of the output image
 *    - Using an existing image as support via SetOutputParametersFromImage(ImageBase)
 *
Jonathan Guinet's avatar
Jonathan Guinet committed
48 49
 *  OGRRegisterAll() method must have been called before applying filter.
 *
50 51
 *
 * \ingroup OTBConversion
52 53 54 55 56 57 58 59
 */
template <class TVectorData, class TOutputImage  >
class  ITK_EXPORT VectorDataToLabelImageFilter :
    public itk::ImageSource<TOutputImage>
{
public:
  /** Standard class typedefs */
  typedef VectorDataToLabelImageFilter                           Self;
OTB Bot's avatar
OTB Bot committed
60
  typedef itk::ImageSource<TOutputImage>     Superclass;
61 62
  typedef itk::SmartPointer< Self >                           Pointer;
  typedef itk::SmartPointer<const Self>                       ConstPointer;
63

64 65
  /** Run-time type information (and related methods). */
  itkTypeMacro(VectorDataToLabelImageFilter, itk::ImageSource);
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
  /** Method for creation through the object factory. */
  itkNewMacro(Self);

  typedef TOutputImage                                OutputImageType;
  typedef typename OutputImageType::Pointer           OutputImagePointer;
  typedef typename OutputImageType::SizeType          OutputSizeType;
  typedef typename OutputImageType::IndexType         OutputIndexType;
  typedef typename OutputImageType::SpacingType       OutputSpacingType;
  typedef typename OutputImageType::PointType         OutputOriginType;
  typedef typename OutputImageType::RegionType        OutputImageRegionType;
  typedef typename OutputImageType::PixelType         OutputImagePixelType;
  typedef typename OutputImageType::InternalPixelType OutputImageInternalPixelType;

  /** VectorData typedefs*/
  typedef TVectorData                            VectorDataType;
  typedef typename VectorDataType::DataTreeType  DataTreeType;
  typedef typename DataTreeType::TreeNodeType    InternalTreeNodeType;
  typedef typename DataTreeType::Pointer         DataTreePointerType;
  typedef typename DataTreeType::ConstPointer    DataTreeConstPointerType;

  typedef itk::ImageBase<OutputImageType::ImageDimension>      ImageBaseType;

  /** Get Nth input VectorData */
  const VectorDataType*  GetInput(unsigned int idx);
91

92 93
  /** Method for adding VectorData to rasterize  */
  virtual void AddVectorData(const VectorDataType* vd);
94

95 96 97 98 99 100 101
  /** Set the size of the output image. */
  itkSetMacro(OutputSize, OutputSizeType);

  /** Get the size of the output image. */
  itkGetConstReferenceMacro(OutputSize, OutputSizeType);

  /** Set the origin of the output image.
OTB Bot's avatar
OTB Bot committed
102
   * \sa GetOrigin()
103 104 105 106 107 108 109 110
   */
  itkSetMacro(OutputOrigin, OutputOriginType);
  virtual void SetOutputOrigin(const double origin[2]);
  virtual void SetOutputOrigin(const float origin[2]);

  itkGetConstReferenceMacro(OutputOrigin, OutputOriginType);

  /** Set the spacing (size of a pixel) of the output image.
111
  * \sa GetSignedSpacing()
112 113 114 115 116 117 118 119 120 121 122 123
  */
  virtual void SetOutputSpacing(const OutputSpacingType& spacing);
  virtual void SetOutputSpacing(const double spacing[2]);
  virtual void SetOutputSpacing(const float spacing[2]);

  /** Set/Get Output Projection Ref */
  itkSetStringMacro(OutputProjectionRef);
  itkGetStringMacro(OutputProjectionRef);

  itkSetStringMacro(BurnAttribute);
  itkGetStringMacro(BurnAttribute);

124 125 126 127 128 129 130 131
  /** Set the background value */
  itkSetMacro(BackgroundValue, OutputImageInternalPixelType);
  itkGetMacro(BackgroundValue, OutputImageInternalPixelType);

  /** Set the default burn value */
  itkSetMacro(DefaultBurnValue, OutputImageInternalPixelType);
  itkGetMacro(DefaultBurnValue, OutputImageInternalPixelType);

132 133 134 135 136
  /** Set/Get the AllTouchedMode flag */
  itkSetMacro(AllTouchedMode,bool);
  itkGetConstReferenceMacro(AllTouchedMode,bool);
  itkBooleanMacro(AllTouchedMode);

137 138 139 140
  /** Useful to set the output parameters from an existing image*/
  void SetOutputParametersFromImage(const ImageBaseType * image);

protected:
141
  void GenerateData() override;
142 143

  VectorDataToLabelImageFilter();
144
  ~VectorDataToLabelImageFilter() override
145 146 147 148 149 150
  {
    // Destroy the geometries stored
    for (unsigned int idx = 0; idx < m_SrcDataSetGeometries.size(); ++idx)
      {
      OGR_G_DestroyGeometry(m_SrcDataSetGeometries[idx]);
      }
151

152
    if (m_OGRDataSourcePointer != nullptr)
153
      {
154
      ogr::version_proxy::Close(m_OGRDataSourcePointer);
155 156 157
      }
  }

158
  void GenerateOutputInformation() override;
159

160
  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
161 162

private:
163 164
  VectorDataToLabelImageFilter(const Self&) = delete;
  void operator=(const Self&) = delete;
165

166
  GDALDataset * m_OGRDataSourcePointer;
167 168 169 170 171 172 173

  // Vector Of OGRGeometyH
  std::vector< OGRGeometryH >   m_SrcDataSetGeometries;

  std::vector<double>           m_BurnValues;
  std::vector<double>           m_FullBurnValues;
  std::vector<int>              m_BandsToBurn;
174

175 176 177 178 179 180
  // Field used to extract the burn value
  std::string                   m_BurnAttribute;

  // Default burn value
  double                        m_DefaultBurnValue;

181 182 183
  // Background value
  OutputImageInternalPixelType  m_BackgroundValue;

184 185 186
  // All touched mode
  bool                          m_AllTouchedMode;

187 188 189 190 191 192 193 194 195 196 197 198
  // Output params
  std::string                   m_OutputProjectionRef;
  OutputSpacingType             m_OutputSpacing;
  OutputOriginType              m_OutputOrigin;
  OutputSizeType                m_OutputSize;
  OutputIndexType               m_OutputStartIndex;
}; // end of class VectorDataToLabelImageFilter

} // end of namespace otb


#ifndef  OTB_MANUAL_INSTANTIATION
199
#include "otbVectorDataToLabelImageFilter.hxx"
200 201 202
#endif

#endif