OTB  9.0.0
Orfeo Toolbox
otbOGRDataSourceToLabelImageFilter.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 otbOGRDataSourceToLabelImageFilter_hxx
22 #define otbOGRDataSourceToLabelImageFilter_hxx
23 
25 #include "otbOGRIOHelper.h"
26 #include "otbGdalDataTypeBridge.h"
28 #include "itkMetaDataObject.h"
29 #include "otbMetaDataKey.h"
30 #include "otbImage.h"
31 #include "otbNoDataHelper.h"
32 
33 #include "gdal_alg.h"
34 #include "stdint.h" //needed for uintptr_t
35 
36 namespace otb
37 {
38 template <class TOutputImage>
40  : m_BurnAttribute("DN"), m_BackgroundValue(0), m_ForegroundValue(255), m_BurnAttributeMode(true), m_AllTouchedMode(false)
41 {
42  this->SetNumberOfRequiredInputs(1);
43 
44  // Output parameters initialization
45  m_OutputSpacing.Fill(1.0);
46  m_OutputSize.Fill(0);
47  m_OutputStartIndex.Fill(0);
48  m_BandsToBurn.clear();
49  m_BandsToBurn.push_back(1);
50 }
51 
52 template <class TOutputImage>
54 {
55  this->itk::ProcessObject::PushBackInput(ds);
56 }
57 
58 template <class TOutputImage>
60 {
61  return static_cast<const OGRDataSourceType*>(this->itk::ProcessObject::GetInput(idx));
62 }
63 
64 template <class TOutputImage>
66 {
67  if (this->m_OutputSpacing != spacing)
68  {
69  this->m_OutputSpacing = spacing;
70  this->Modified();
71  }
72 }
73 
74 template <class TOutputImage>
76 {
77  OutputSpacingType s(spacing);
78  this->SetOutputSpacing(s);
79 }
80 
81 template <class TOutputImage>
83 {
84  itk::Vector<float, 2> sf(spacing);
86  s.CastFrom(sf);
87  this->SetOutputSpacing(s);
88 }
89 
90 template <class TOutputImage>
92 {
93  OutputOriginType p(origin);
94  this->SetOutputOrigin(p);
95 }
96 
97 template <class TOutputImage>
99 {
100  itk::Point<float, 2> of(origin);
102  p.CastFrom(of);
103  this->SetOutputOrigin(p);
104 }
105 
106 template <class TOutputImage>
107 template <class ImagePointerType>
109 {
110  this->SetOutputOrigin(src->GetOrigin());
111  this->SetOutputSpacing(src->GetSignedSpacing());
112  this->SetOutputSize(src->GetLargestPossibleRegion().GetSize());
113  this->SetOutputProjectionRef(src->GetProjectionRef());
114 }
115 
116 template <class TOutputImage>
118 {
119  // get pointer to the output
120  OutputImagePointer outputPtr = this->GetOutput();
121  if (!outputPtr)
122  {
123  return;
124  }
125 
126  // Set the size of the output region
127  typename TOutputImage::RegionType outputLargestPossibleRegion;
128  outputLargestPossibleRegion.SetSize(m_OutputSize);
129  // outputLargestPossibleRegion.SetIndex(m_OutputStartIndex);
130  outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
131 
132  // Set spacing and origin
133  outputPtr->SetSignedSpacing(m_OutputSpacing);
134  outputPtr->SetOrigin(m_OutputOrigin);
135  outputPtr->SetProjectionRef(this->GetOutputProjectionRef());
136 
137  // Generate the OGRLayers from the input OGRDataSource
138  for (unsigned int idx = 0; idx < this->GetNumberOfInputs(); ++idx)
139  {
140  OGRDataSourcePointerType ogrDS = dynamic_cast<OGRDataSourceType*>(this->itk::ProcessObject::GetInput(idx));
141  const unsigned int nbLayers = ogrDS->GetLayersCount();
142 
143  for (unsigned int layer = 0; layer < nbLayers; ++layer)
144  {
145  m_SrcDataSetLayers.push_back(&(ogrDS->GetLayer(layer).ogr()));
146  }
147  }
148 
149  // Set the NoData value using the background
150  const unsigned int& nbBands = outputPtr->GetNumberOfComponentsPerPixel();
151  std::vector<bool> noDataValueAvailable;
152  noDataValueAvailable.resize(nbBands, true);
153  std::vector<double> noDataValue;
154  noDataValue.resize(nbBands, static_cast<double>(m_BackgroundValue));
155 
156  WriteNoDataFlags(noDataValueAvailable, noDataValue, outputPtr->GetImageMetadata());
157 }
158 
159 template <class TOutputImage>
161 {
162  // Call Superclass GenerateData
163  this->AllocateOutputs();
164 
165  // Get the buffered region
166  OutputImageRegionType bufferedRegion = this->GetOutput()->GetBufferedRegion();
167 
168  // nb bands
169  const unsigned int& nbBands = this->GetOutput()->GetNumberOfComponentsPerPixel();
170 
171  // register drivers
172  GDALAllRegister();
173 
174  std::ostringstream stream;
175  stream << "MEM:::"
176  << "DATAPOINTER=" << (uintptr_t)(this->GetOutput()->GetBufferPointer()) << ","
177  << "PIXELS=" << bufferedRegion.GetSize()[0] << ","
178  << "LINES=" << bufferedRegion.GetSize()[1] << ","
179  << "BANDS=" << nbBands << ","
180  << "DATATYPE=" << GDALGetDataTypeName(GdalDataTypeBridge::GetGDALDataType<OutputImageInternalPixelType>()) << ","
181  << "PIXELOFFSET=" << sizeof(OutputImageInternalPixelType) * nbBands << ","
182  << "LINEOFFSET=" << sizeof(OutputImageInternalPixelType) * nbBands * bufferedRegion.GetSize()[0] << ","
183  << "BANDOFFSET=" << sizeof(OutputImageInternalPixelType);
184 
185  GDALDatasetH dataset = GDALOpen(stream.str().c_str(), GA_Update);
186 
187  // Add the projection ref to the dataset
188  GDALSetProjection(dataset, this->GetOutput()->GetProjectionRef().c_str());
189 
190  // Set the nodata value
191  for (unsigned int band = 0; band < nbBands; ++band)
192  {
193  GDALRasterBandH hBand = GDALGetRasterBand(dataset, band + 1);
194  GDALFillRaster(hBand, m_BackgroundValue, 0);
195  }
196 
197  // add the geoTransform to the dataset
198  itk::VariableLengthVector<double> geoTransform(6);
199 
200  // Reporting origin and spacing of the buffered region
201  // the spacing is unchanged, the origin is relative to the buffered region
202  OutputIndexType bufferIndexOrigin = bufferedRegion.GetIndex();
203  OutputOriginType bufferOrigin;
204  this->GetOutput()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
205  geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetOutput()->GetSignedSpacing()[0];
206  geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetOutput()->GetSignedSpacing()[1];
207  geoTransform[1] = this->GetOutput()->GetSignedSpacing()[0];
208  geoTransform[5] = this->GetOutput()->GetSignedSpacing()[1];
209 
210  // FIXME: Here component 1 and 4 should be replaced by the orientation parameters
211  geoTransform[2] = 0.;
212  geoTransform[4] = 0.;
213  GDALSetGeoTransform(dataset, const_cast<double*>(geoTransform.GetDataPointer()));
214 
215  // Burn the geometries into the dataset
216  if (dataset != nullptr)
217  {
218  std::vector<std::string> options;
219 
220  std::vector<double> foreground(nbBands * m_SrcDataSetLayers.size(), m_ForegroundValue);
221 
222  if (m_BurnAttributeMode)
223  {
224  options.push_back("ATTRIBUTE=" + m_BurnAttribute);
225  }
226  if (m_AllTouchedMode)
227  {
228  options.push_back("ALL_TOUCHED=TRUE");
229  }
230 
231  GDALRasterizeLayers(dataset, nbBands, &m_BandsToBurn[0], m_SrcDataSetLayers.size(), &(m_SrcDataSetLayers[0]), nullptr, nullptr, &foreground[0],
232  ogr::StringListConverter(options).to_ogr(), nullptr, nullptr);
233  // release the dataset
234  GDALClose(dataset);
235  }
236 }
237 
238 template <class TOutputImage>
239 void OGRDataSourceToLabelImageFilter<TOutputImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
240 {
241  Superclass::PrintSelf(os, indent);
242 }
243 
244 } // end namespace otb
245 
246 #endif
otbImageMetadataInterfaceFactory.h
otb::OGRDataSourceToLabelImageFilter::OGRDataSourcePointerType
OGRDataSourceType::Pointer OGRDataSourcePointerType
Definition: otbOGRDataSourceToLabelImageFilter.h:86
otb::OGRDataSourceToLabelImageFilter::SetOutputParametersFromImage
void SetOutputParametersFromImage(const ImagePointerType image)
Definition: otbOGRDataSourceToLabelImageFilter.hxx:108
otb::ogr::StringListConverter
Helper class to convert a set of standard C++ string into char** as OGR API expects.
Definition: otbOGRHelpers.h:73
otb::WriteNoDataFlags
void OTBMetadata_EXPORT WriteNoDataFlags(const std::vector< bool > &flags, const std::vector< double > &values, ImageMetadata &imd)
otbImage.h
otb::OGRDataSourceToLabelImageFilter::OGRDataSourceToLabelImageFilter
OGRDataSourceToLabelImageFilter()
Definition: otbOGRDataSourceToLabelImageFilter.hxx:39
otbOGRIOHelper.h
otb::OGRDataSourceToLabelImageFilter::OutputImageRegionType
OutputImageType::RegionType OutputImageRegionType
Definition: otbOGRDataSourceToLabelImageFilter.h:80
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::OGRDataSourceToLabelImageFilter::m_OutputSize
OutputSizeType m_OutputSize
Definition: otbOGRDataSourceToLabelImageFilter.h:181
otb::ogr::DataSource
Collection of geometric objects.
Definition: otbOGRDataSourceWrapper.h:83
otb::OGRDataSourceToLabelImageFilter::m_BandsToBurn
std::vector< int > m_BandsToBurn
Definition: otbOGRDataSourceToLabelImageFilter.h:172
otb::OGRDataSourceToLabelImageFilter::m_OutputSpacing
OutputSpacingType m_OutputSpacing
Definition: otbOGRDataSourceToLabelImageFilter.h:179
otb::OGRDataSourceToLabelImageFilter::SetOutputSpacing
virtual void SetOutputSpacing(const OutputSpacingType &spacing)
Definition: otbOGRDataSourceToLabelImageFilter.hxx:65
otb::OGRDataSourceToLabelImageFilter::OutputIndexType
OutputImageType::IndexType OutputIndexType
Definition: otbOGRDataSourceToLabelImageFilter.h:77
otb::OGRDataSourceToLabelImageFilter::OutputSpacingType
OutputImageType::SpacingType OutputSpacingType
Definition: otbOGRDataSourceToLabelImageFilter.h:78
otb::OGRDataSourceToLabelImageFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbOGRDataSourceToLabelImageFilter.hxx:117
otbOGRDataSourceToLabelImageFilter.h
otb::OGRDataSourceToLabelImageFilter::OutputImageInternalPixelType
OutputImageType::InternalPixelType OutputImageInternalPixelType
Definition: otbOGRDataSourceToLabelImageFilter.h:82
otb::OGRDataSourceToLabelImageFilter::AddOGRDataSource
virtual void AddOGRDataSource(const OGRDataSourceType *ds)
Definition: otbOGRDataSourceToLabelImageFilter.hxx:53
otb::ogr::DataSource::GetLayersCount
int GetLayersCount() const
otbNoDataHelper.h
otb::OGRDataSourceToLabelImageFilter::OutputOriginType
OutputImageType::PointType OutputOriginType
Definition: otbOGRDataSourceToLabelImageFilter.h:79
otb::ogr::StringListConverter::to_ogr
char ** to_ogr() const
Definition: otbOGRHelpers.h:103
otb::OGRDataSourceToLabelImageFilter::SetOutputOrigin
virtual void SetOutputOrigin(OutputOriginType _arg)
otbGdalDataTypeBridge.h
otbMetaDataKey.h
otb::OGRDataSourceToLabelImageFilter::OutputImagePointer
OutputImageType::Pointer OutputImagePointer
Definition: otbOGRDataSourceToLabelImageFilter.h:75
otb::OGRDataSourceToLabelImageFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbOGRDataSourceToLabelImageFilter.hxx:239
otb::OGRDataSourceToLabelImageFilter::GetInput
const OGRDataSourceType * GetInput(unsigned int idx)
Definition: otbOGRDataSourceToLabelImageFilter.hxx:59
otb::OGRDataSourceToLabelImageFilter::GenerateData
void GenerateData() override
Definition: otbOGRDataSourceToLabelImageFilter.hxx:160
otb::OGRDataSourceToLabelImageFilter::m_OutputStartIndex
OutputIndexType m_OutputStartIndex
Definition: otbOGRDataSourceToLabelImageFilter.h:182