21 #ifndef otbOGRDataSourceToLabelImageFilter_hxx
22 #define otbOGRDataSourceToLabelImageFilter_hxx
28 #include "itkMetaDataObject.h"
38 template <
class TOutputImage>
40 : m_BurnAttribute(
"DN"), m_BackgroundValue(0), m_ForegroundValue(255), m_BurnAttributeMode(true), m_AllTouchedMode(false)
42 this->SetNumberOfRequiredInputs(1);
52 template <
class TOutputImage>
55 this->itk::ProcessObject::PushBackInput(ds);
58 template <
class TOutputImage>
61 return static_cast<const OGRDataSourceType*
>(this->itk::ProcessObject::GetInput(idx));
64 template <
class TOutputImage>
67 if (this->m_OutputSpacing != spacing)
69 this->m_OutputSpacing = spacing;
74 template <
class TOutputImage>
78 this->SetOutputSpacing(s);
81 template <
class TOutputImage>
84 itk::Vector<float, 2> sf(spacing);
87 this->SetOutputSpacing(s);
90 template <
class TOutputImage>
94 this->SetOutputOrigin(p);
97 template <
class TOutputImage>
100 itk::Point<float, 2> of(origin);
103 this->SetOutputOrigin(p);
106 template <
class TOutputImage>
107 template <
class ImagePo
interType>
110 this->SetOutputOrigin(src->GetOrigin());
111 this->SetOutputSpacing(src->GetSignedSpacing());
112 this->SetOutputSize(src->GetLargestPossibleRegion().GetSize());
113 this->SetOutputProjectionRef(src->GetProjectionRef());
116 template <
class TOutputImage>
127 typename TOutputImage::RegionType outputLargestPossibleRegion;
128 outputLargestPossibleRegion.SetSize(m_OutputSize);
130 outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
133 outputPtr->SetSignedSpacing(m_OutputSpacing);
134 outputPtr->SetOrigin(m_OutputOrigin);
135 outputPtr->SetProjectionRef(this->GetOutputProjectionRef());
138 for (
unsigned int idx = 0; idx < this->GetNumberOfInputs(); ++idx)
143 for (
unsigned int layer = 0; layer < nbLayers; ++layer)
145 m_SrcDataSetLayers.push_back(&(ogrDS->GetLayer(layer).ogr()));
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));
156 WriteNoDataFlags(noDataValueAvailable, noDataValue, outputPtr->GetImageMetadata());
159 template <
class TOutputImage>
163 this->AllocateOutputs();
169 const unsigned int& nbBands = this->GetOutput()->GetNumberOfComponentsPerPixel();
174 std::ostringstream stream;
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>()) <<
","
185 GDALDatasetH dataset = GDALOpen(stream.str().c_str(), GA_Update);
188 GDALSetProjection(dataset, this->GetOutput()->GetProjectionRef().c_str());
191 for (
unsigned int band = 0; band < nbBands; ++band)
193 GDALRasterBandH hBand = GDALGetRasterBand(dataset, band + 1);
194 GDALFillRaster(hBand, m_BackgroundValue, 0);
198 itk::VariableLengthVector<double> geoTransform(6);
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];
211 geoTransform[2] = 0.;
212 geoTransform[4] = 0.;
213 GDALSetGeoTransform(dataset,
const_cast<double*
>(geoTransform.GetDataPointer()));
216 if (dataset !=
nullptr)
218 std::vector<std::string> options;
220 std::vector<double> foreground(nbBands * m_SrcDataSetLayers.size(), m_ForegroundValue);
222 if (m_BurnAttributeMode)
224 options.push_back(
"ATTRIBUTE=" + m_BurnAttribute);
226 if (m_AllTouchedMode)
228 options.push_back(
"ALL_TOUCHED=TRUE");
231 GDALRasterizeLayers(dataset, nbBands, &m_BandsToBurn[0], m_SrcDataSetLayers.size(), &(m_SrcDataSetLayers[0]),
nullptr,
nullptr, &foreground[0],
238 template <
class TOutputImage>
241 Superclass::PrintSelf(os, indent);