21 #ifndef otbLabelImageToOGRDataSourceFilter_hxx
22 #define otbLabelImageToOGRDataSourceFilter_hxx
30 #include "gdal_priv.h"
38 template <
class TInputImage>
41 this->SetNumberOfRequiredInputs(2);
42 this->SetNumberOfRequiredInputs(1);
43 this->SetNumberOfRequiredOutputs(1);
45 this->ProcessObject::SetNthOutput(0, this->
MakeOutput(0));
49 template <
class TInputImage>
56 template <
class TInputImage>
62 template <
class TInputImage>
65 this->Superclass::SetNthInput(0,
const_cast<InputImageType*
>(input));
68 template <
class TInputImage>
71 if (this->GetNumberOfInputs() < 1)
76 return static_cast<const InputImageType*
>(this->Superclass::GetInput(0));
79 template <
class TInputImage>
82 this->Superclass::SetNthInput(1,
const_cast<InputImageType*
>(input));
85 template <
class TInputImage>
88 if (this->GetNumberOfInputs() < 2)
93 return static_cast<const InputImageType*
>(this->Superclass::GetInput(1));
96 template <
class TInputImage>
100 Superclass::GenerateInputRequestedRegion();
103 typename InputImageType::Pointer input =
const_cast<InputImageType*
>(this->GetInput());
110 input->SetRequestedRegionToLargestPossibleRegion();
112 typename InputImageType::Pointer mask =
const_cast<InputImageType*
>(this->GetInputMask());
118 mask->SetRequestedRegionToLargestPossibleRegion();
122 template <
class TInputImage>
125 if (this->GetInput()->GetRequestedRegion() != this->GetInput()->GetLargestPossibleRegion())
127 itkExceptionMacro(<<
"Not streamed filter. ERROR : requested region is not the largest possible region.");
131 unsigned int nbBands = 0;
132 unsigned int bytePerPixel = 0;
135 size = this->GetInput()->GetLargestPossibleRegion().GetSize();
136 nbBands = this->GetInput()->GetNumberOfComponentsPerPixel();
141 const_cast<InputPixelType*
>(this->GetInput()->GetBufferPointer()),
142 this->GetInput()->GetLargestPossibleRegion().GetSize()[0],
143 this->GetInput()->GetLargestPossibleRegion().GetSize()[1],
144 GdalDataTypeBridge::GetGDALDataType<InputPixelType>(), bytePerPixel, nbBands, bytePerPixel);
147 dataset->GetDataSet()->SetProjection(this->GetInput()->GetProjectionRef().c_str());
149 unsigned int projSize = this->GetInput()->GetGeoTransform().size();
150 double geoTransform[6];
155 IndexType bufferIndexOrigin = this->GetInput()->GetBufferedRegion().GetIndex();
157 this->GetInput()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
158 geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetInput()->GetSignedSpacing()[0];
159 geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetInput()->GetSignedSpacing()[1];
160 geoTransform[1] = this->GetInput()->GetSignedSpacing()[0];
161 geoTransform[5] = this->GetInput()->GetSignedSpacing()[1];
165 geoTransform[2] = 0.;
166 geoTransform[4] = 0.;
170 geoTransform[2] = this->GetInput()->GetGeoTransform()[2];
171 geoTransform[4] = this->GetInput()->GetGeoTransform()[4];
173 dataset->GetDataSet()->SetGeoTransform(geoTransform);
178 OGRLayerType outputLayer = ogrDS->CreateLayer(
"layer",
nullptr, wkbPolygon);
180 OGRFieldDefn field(m_FieldName.c_str(), OFTInteger);
186 char* option[2] = {
nullptr,
nullptr};
187 if (m_Use8Connected ==
true)
189 std::string opt(
"8CONNECTED:8");
190 option[0] =
const_cast<char*
>(opt.c_str());
195 typename InputImageType::ConstPointer inputMask = this->GetInputMask();
196 if (!inputMask.IsNull())
198 nbBands = this->GetInputMask()->GetNumberOfComponentsPerPixel();
203 const_cast<InputPixelType*
>(this->GetInputMask()->GetBufferPointer()),
204 this->GetInput()->GetLargestPossibleRegion().GetSize()[0],
205 this->GetInput()->GetLargestPossibleRegion().GetSize()[1],
206 GdalDataTypeBridge::GetGDALDataType<InputPixelType>(), bytePerPixel, nbBands, bytePerPixel);
209 maskDataset->GetDataSet()->SetProjection(this->GetInputMask()->GetProjectionRef().c_str());
211 projSize = this->GetInputMask()->GetGeoTransform().size();
216 bufferIndexOrigin = this->GetInputMask()->GetBufferedRegion().GetIndex();
217 this->GetInputMask()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
218 geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetInputMask()->GetSignedSpacing()[0];
219 geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetInputMask()->GetSignedSpacing()[1];
220 geoTransform[1] = this->GetInputMask()->GetSignedSpacing()[0];
221 geoTransform[5] = this->GetInputMask()->GetSignedSpacing()[1];
225 geoTransform[2] = 0.;
226 geoTransform[4] = 0.;
230 geoTransform[2] = this->GetInputMask()->GetGeoTransform()[2];
231 geoTransform[4] = this->GetInputMask()->GetGeoTransform()[4];
233 maskDataset->GetDataSet()->SetGeoTransform(geoTransform);
235 GDALPolygonize(dataset->GetDataSet()->GetRasterBand(1),
236 maskDataset->GetDataSet()->GetRasterBand(1),
237 &outputLayer.
ogr(), 0,
238 options,
nullptr,
nullptr);
242 GDALPolygonize(dataset->GetDataSet()->GetRasterBand(1),
nullptr,
243 &outputLayer.
ogr(), 0, options,
nullptr,
nullptr);
246 this->SetNthOutput(0, ogrDS);
itk::SmartPointer< Self > Pointer
static GDALDriverManagerWrapper & GetInstance()
GDALDatasetWrapper::Pointer OpenFromMemory(void *mem_ptr, const uint64_t &width, const uint64_t &height, const GDALDataType pix_type, const uint32_t byte_per_pixel, const uint16_t nb_bands=1, const uint64_t &band_offset=1) const
InputImageType::PointType OriginType
InputImageType::IndexType IndexType
InputImageType::PixelType InputPixelType
void GenerateInputRequestedRegion() override
InputImageType::SizeType SizeType
void GenerateData() override
virtual const InputImageType * GetInputMask(void)
virtual void SetInput(const InputImageType *input)
const OGRDataSourceType * GetOutput()
DataObjectPointer MakeOutput(DataObjectPointerArraySizeType idx) override
itk::ProcessObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType
TInputImage InputImageType
virtual const InputImageType * GetInput(void)
LabelImageToOGRDataSourceFilter()
virtual void SetInputMask(const InputImageType *input)
itk::DataObject::Pointer DataObjectPointer
Collection of geometric objects.
itk::SmartPointer< Self > Pointer
Layer of geometric objects.
void CreateField(FieldDefn const &field, bool bApproxOK=true)
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.