OTB  9.0.0
Orfeo Toolbox
otbRasterizeVectorDataFilter.hxx
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_hxx
22 #define otbRasterizeVectorDataFilter_hxx
23 
25 #include "otbOGRIOHelper.h"
26 #include "otbGdalDataTypeBridge.h"
27 
28 namespace otb
29 {
30 template <class TVectorData, class TInputImage, class TOutputImage>
32 {
33  this->SetNumberOfRequiredInputs(1);
34 }
35 
36 template <class TVectorData, class TInputImage, class TOutputImage>
38 {
39  // Process object is not const-correct so the const_cast is required
40  // here
41  if (this->GetNumberOfInputs() < 1)
42  {
43  this->itk::ProcessObject::SetNthInput(1, const_cast<VectorDataType*>(vd));
44  }
45  else
46  {
47  this->itk::ProcessObject::PushBackInput(vd);
48  }
49 }
50 
51 
52 template <class TVectorData, class TInputImage, class TOutputImage>
54 {
55  Superclass::GenerateOutputInformation();
56 
57  // Generate the OGRLayers from the input VectorDatas
58  // iteration begin from 1 cause the 0th input is a image
59  for (unsigned int idx = 1; idx < this->GetNumberOfInputs(); ++idx)
60  {
61  const VectorDataType* vd = dynamic_cast<const VectorDataType*>(this->itk::ProcessObject::GetInput(idx));
62 
63  // Get the projection ref of the current VectorData
64  std::string projectionRefWkt = vd->GetProjectionRef();
65  bool projectionInformationAvailable = !projectionRefWkt.empty();
66  OGRSpatialReference* oSRS = nullptr;
67 
68  if (projectionInformationAvailable)
69  {
70  oSRS = static_cast<OGRSpatialReference*>(OSRNewSpatialReference(projectionRefWkt.c_str()));
71  }
72  else
73  {
74  otbMsgDevMacro(<< "Projection information unavailable");
75  }
76 
77  // Retrieving root node
78  DataTreeConstPointerType tree = vd->GetDataTree();
79 
80  // Get the input tree root
81  InternalTreeNodeType* inputRoot = const_cast<InternalTreeNodeType*>(tree->GetRoot());
82 
83  // Iterative method to build the layers from a VectorData
84  OGRRegisterAll();
85  OGRLayer* ogrCurrentLayer = nullptr;
86  std::vector<OGRLayer*> ogrLayerVector;
88 
89  // The method ConvertDataTreeNodeToOGRLayers create the
90  // OGRDataSource but don t release it. Destruction is done in the
91  // destructor
92  m_OGRDataSourcePointer = nullptr;
93  ogrLayerVector = IOConversion->ConvertDataTreeNodeToOGRLayers(inputRoot, m_OGRDataSourcePointer, ogrCurrentLayer, oSRS);
94 
95  // Cast OGRLayer* to OGRLayerH
96  for (unsigned int idx2 = 0; idx2 < ogrLayerVector.size(); ++idx2)
97  {
98  m_SrcDataSetLayers.push_back((OGRLayerH)(ogrLayerVector[idx2]));
99  }
100 
101  // Destroy the oSRS
102  if (oSRS != nullptr)
103  {
104  OSRRelease(oSRS);
105  }
106  }
107 
108  // Some checking : Check the consistency between the band list
109  // to burn and the burn values :
110  // There should be "m_BandsToBurn.size()" burn values for each layer.
111  // If not, burn values vector will be cloned as many time as the number of
112  // OGRLayer we have
113  if (m_BurnValues.size() != m_BandsToBurn.size() * m_SrcDataSetLayers.size())
114  {
115  std::ostringstream oss;
116  oss << "Inconsistency detected : expected burn vector size to be equal to( bandToBurn * nb layers = " << m_BandsToBurn.size() * m_SrcDataSetLayers.size()
117  << " ), got : " << m_BurnValues.size() << std::endl;
118  itkWarningMacro(<< oss.str());
119  }
120 
121  // Clone the burn values to fit the condition
122  for (unsigned int idx = 0; idx < m_SrcDataSetLayers.size(); ++idx)
123  {
124  for (unsigned int burnidx = 0; burnidx < m_BurnValues.size(); ++burnidx)
125  {
126  m_FullBurnValues.push_back(m_BurnValues[burnidx]);
127  }
128  }
129 }
130 
131 template <class TVectorData, class TInputImage, class TOutputImage>
133 {
134  // Call Superclass GenerateData
135  Superclass::GenerateData();
136 
137  // Get the buffered region
138  OutputImageRegionType bufferedRegion = this->GetOutput()->GetBufferedRegion();
139 
140  // nb bands
141  unsigned int nbBands = this->GetOutput()->GetNumberOfComponentsPerPixel();
142 
143  // register drivers
144  GDALAllRegister();
145 
146  std::ostringstream stream;
147  stream << "MEM:::"
148  << "DATAPOINTER=" << (uintptr_t)(this->GetOutput()->GetBufferPointer()) << ","
149  << "PIXELS=" << bufferedRegion.GetSize()[0] << ","
150  << "LINES=" << bufferedRegion.GetSize()[1] << ","
151  << "BANDS=" << nbBands << ","
152  << "DATATYPE=" << GDALGetDataTypeName(GdalDataTypeBridge::GetGDALDataType<OutputImageInternalPixelType>()) << ","
153  << "PIXELOFFSET=" << sizeof(OutputImageInternalPixelType) * nbBands << ","
154  << "LINEOFFSET=" << sizeof(OutputImageInternalPixelType) * nbBands * bufferedRegion.GetSize()[0] << ","
155  << "BANDOFFSET=" << sizeof(OutputImageInternalPixelType);
156 
157  GDALDatasetH dataset = GDALOpen(stream.str().c_str(), GA_Update);
158 
159  // Add the projection ref to the dataset
160  GDALSetProjection(dataset, this->GetOutput()->GetProjectionRef().c_str());
161 
162  // add the geoTransform to the dataset
163  itk::VariableLengthVector<double> geoTransform(6);
164 
165  // Reporting origin and spacing of the buffered region
166  // the spacing is unchanged, the origin is relative to the buffered region
167  InputIndexType bufferIndexOrigin = bufferedRegion.GetIndex();
168  InputPointType bufferOrigin;
169  this->GetOutput()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
170  geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetOutput()->GetSignedSpacing()[0];
171  geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetOutput()->GetSignedSpacing()[1];
172  geoTransform[1] = this->GetOutput()->GetSignedSpacing()[0];
173  geoTransform[5] = this->GetOutput()->GetSignedSpacing()[1];
174 
175  // FIXME: Here component 1 and 4 should be replaced by the orientation parameters
176  geoTransform[2] = 0.;
177  geoTransform[4] = 0.;
178  GDALSetGeoTransform(dataset, const_cast<double*>(geoTransform.GetDataPointer()));
179 
180  char** options = nullptr;
181  if (m_AllTouchedMode)
182  {
183  options = CSLSetNameValue(options, "ALL_TOUCHED", "TRUE");
184  }
185 
186  // Burn the geometries into the dataset
187  if (dataset != nullptr)
188  {
189  GDALRasterizeLayers(dataset, m_BandsToBurn.size(), &(m_BandsToBurn[0]), m_SrcDataSetLayers.size(), &(m_SrcDataSetLayers[0]), nullptr, nullptr,
190  &(m_FullBurnValues[0]), options, GDALDummyProgress, nullptr);
191 
192  CSLDestroy(options);
193 
194  // release the dataset
195  GDALClose(dataset);
196  }
197 }
198 
199 template <class TVectorData, class TInputImage, class TOutputImage>
201 {
202  Superclass::PrintSelf(os, indent);
203 }
204 
205 } // end namespace otb
206 
207 #endif
otb::RasterizeVectorDataFilter::VectorDataType
TVectorData VectorDataType
Definition: otbRasterizeVectorDataFilter.h:94
otb::RasterizeVectorDataFilter::DataTreeConstPointerType
DataTreeType::ConstPointer DataTreeConstPointerType
Definition: otbRasterizeVectorDataFilter.h:98
otb::RasterizeVectorDataFilter::AddVectorData
virtual void AddVectorData(const VectorDataType *vd)
Definition: otbRasterizeVectorDataFilter.hxx:37
otbRasterizeVectorDataFilter.h
otb::RasterizeVectorDataFilter::OutputImageRegionType
OutputImageType::RegionType OutputImageRegionType
Definition: otbRasterizeVectorDataFilter.h:89
otbOGRIOHelper.h
otb::RasterizeVectorDataFilter::InputIndexType
InputImageType::IndexType InputIndexType
Definition: otbRasterizeVectorDataFilter.h:84
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::RasterizeVectorDataFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbRasterizeVectorDataFilter.hxx:200
otb::OGRIOHelper::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbOGRIOHelper.h:53
otb::OGRIOHelper::New
static Pointer New()
otb::RasterizeVectorDataFilter::InputPointType
InputImageType::PointType InputPointType
Definition: otbRasterizeVectorDataFilter.h:85
otbGdalDataTypeBridge.h
otb::RasterizeVectorDataFilter::InternalTreeNodeType
DataTreeType::TreeNodeType InternalTreeNodeType
Definition: otbRasterizeVectorDataFilter.h:96
otb::RasterizeVectorDataFilter::RasterizeVectorDataFilter
RasterizeVectorDataFilter()
Definition: otbRasterizeVectorDataFilter.hxx:31
otbMsgDevMacro
#define otbMsgDevMacro(x)
Definition: otbMacro.h:64
otb::RasterizeVectorDataFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbRasterizeVectorDataFilter.hxx:53
otb::RasterizeVectorDataFilter::OutputImageInternalPixelType
OutputImageType::InternalPixelType OutputImageInternalPixelType
Definition: otbRasterizeVectorDataFilter.h:91
otb::RasterizeVectorDataFilter::GenerateData
void GenerateData() override
Definition: otbRasterizeVectorDataFilter.hxx:132