21 #ifndef otbLabelImageToVectorDataFilter_hxx
22 #define otbLabelImageToVectorDataFilter_hxx
26 #include "itkImageRegionIterator.h"
32 #include "gdal_priv.h"
39 template <
class TInputImage,
class TPrecision>
42 this->SetNumberOfRequiredInputs(2);
43 this->SetNumberOfRequiredInputs(1);
44 this->SetNumberOfRequiredOutputs(1);
47 template <
class TInputImage,
class TPrecision>
50 this->Superclass::SetNthInput(0,
const_cast<InputImageType*
>(input));
53 template <
class TInputImage,
class TPrecision>
56 if (this->GetNumberOfInputs() < 1)
61 return static_cast<const InputImageType*
>(this->Superclass::GetInput(0));
64 template <
class TInputImage,
class TPrecision>
67 this->Superclass::SetNthInput(1,
const_cast<InputImageType*
>(input));
70 template <
class TInputImage,
class TPrecision>
73 if (this->GetNumberOfInputs() < 2)
78 return static_cast<const InputImageType*
>(this->Superclass::GetInput(1));
81 template <
class TInputImage,
class TPrecision>
85 Superclass::GenerateInputRequestedRegion();
88 typename InputImageType::Pointer input =
const_cast<InputImageType*
>(this->GetInput());
95 input->SetRequestedRegionToLargestPossibleRegion();
97 typename InputImageType::Pointer mask =
const_cast<InputImageType*
>(this->GetInputMask());
103 mask->SetRequestedRegionToLargestPossibleRegion();
107 template <
class TInputImage,
class TPrecision>
110 if (this->GetInput()->GetRequestedRegion() != this->GetInput()->GetLargestPossibleRegion())
112 itkExceptionMacro(<<
"Not streamed filter. ERROR : requested region is not the largest possible region.");
115 typename InputImageType::Pointer inImage =
const_cast<InputImageType*
>(this->GetInput());
117 unsigned int nbBands = this->GetInput()->GetNumberOfComponentsPerPixel();
123 const_cast<InputPixelType*
>(this->GetInput()->GetBufferPointer()),
124 this->GetInput()->GetLargestPossibleRegion().GetSize()[0],
125 this->GetInput()->GetLargestPossibleRegion().GetSize()[1],
126 GdalDataTypeBridge::GetGDALDataType<InputPixelType>(), bytePerPixel, nbBands, bytePerPixel);
129 dataset->GetDataSet()->SetProjection(this->GetInput()->GetProjectionRef().c_str());
131 unsigned int projSize = this->GetInput()->GetGeoTransform().size();
132 double geoTransform[6];
137 IndexType bufferIndexOrigin = this->GetInput()->GetBufferedRegion().GetIndex();
139 this->GetInput()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
140 geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetInput()->GetSignedSpacing()[0];
141 geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetInput()->GetSignedSpacing()[1];
142 geoTransform[1] = this->GetInput()->GetSignedSpacing()[0];
143 geoTransform[5] = this->GetInput()->GetSignedSpacing()[1];
147 geoTransform[2] = 0.;
148 geoTransform[4] = 0.;
152 geoTransform[2] = this->GetInput()->GetGeoTransform()[2];
153 geoTransform[4] = this->GetInput()->GetGeoTransform()[4];
155 dataset->GetDataSet()->SetGeoTransform(geoTransform);
160 OGRLayerType outputLayer = ogrDS->CreateLayer(
"layer",
nullptr, wkbPolygon);
162 OGRFieldDefn field(m_FieldName.c_str(), OFTInteger);
169 if (m_Use8Connected ==
true)
171 std::string opt(
"8CONNECTED:8");
172 option[0] =
const_cast<char*
>(opt.c_str());
177 typename InputImageType::ConstPointer inputMask = this->GetInputMask();
178 if (!inputMask.IsNull())
180 nbBands = this->GetInputMask()->GetNumberOfComponentsPerPixel();
185 const_cast<InputPixelType*
>(this->GetInputMask()->GetBufferPointer()),
186 this->GetInputMask()->GetLargestPossibleRegion().GetSize()[0],
187 this->GetInputMask()->GetLargestPossibleRegion().GetSize()[1],
188 GdalDataTypeBridge::GetGDALDataType<InputPixelType>(), bytePerPixel, nbBands, bytePerPixel);
191 maskDataset->GetDataSet()->SetProjection(this->GetInputMask()
192 ->GetProjectionRef().c_str());
194 projSize = this->GetInputMask()->GetGeoTransform().size();
199 bufferIndexOrigin = this->GetInputMask()->GetBufferedRegion().GetIndex();
200 this->GetInputMask()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
201 geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetInputMask()->GetSignedSpacing()[0];
202 geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetInputMask()->GetSignedSpacing()[1];
203 geoTransform[1] = this->GetInputMask()->GetSignedSpacing()[0];
204 geoTransform[5] = this->GetInputMask()->GetSignedSpacing()[1];
208 geoTransform[2] = 0.;
209 geoTransform[4] = 0.;
213 geoTransform[2] = this->GetInputMask()->GetGeoTransform()[2];
214 geoTransform[4] = this->GetInputMask()->GetGeoTransform()[4];
216 maskDataset->GetDataSet()->SetGeoTransform(geoTransform);
218 GDALPolygonize(dataset->GetDataSet()->GetRasterBand(1),
219 maskDataset->GetDataSet()->GetRasterBand(1),
220 &outputLayer.
ogr(), 0, options,
nullptr,
nullptr);
224 GDALPolygonize(dataset->GetDataSet()->GetRasterBand(1),
nullptr,
225 &outputLayer.
ogr(), 0, options,
nullptr,
nullptr);
234 document->SetNodeId(outputLayer.
GetLayerDefn().GetName());
239 data->Add(document, data->GetRoot());
242 OGRConversion->ConvertOGRLayerToDataTreeNode(data,&outputLayer.
ogr(), document);
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
VectorDataType::Pointer VectorDataPointerType
void GenerateInputRequestedRegion() override
virtual void SetInputMask(const InputImageType *input)
TInputImage InputImageType
virtual void SetInput(const InputImageType *input)
virtual const InputImageType * GetInput(void)
InputImageType::PointType OriginType
InputImageType::PixelType InputPixelType
DataNodeType::Pointer DataNodePointerType
virtual const InputImageType * GetInputMask(void)
LabelImageToVectorDataFilter()
void GenerateData() override
InputImageType::IndexType IndexType
itk::SmartPointer< Self > Pointer
This class represents a hierarchy of vector data.
void SetProjectionRef(const std::string &projectionRef)
itk::SmartPointer< Self > Pointer
Layer of geometric objects.
void CreateField(FieldDefn const &field, bool bApproxOK=true)
OGRFeatureDefn & GetLayerDefn() const
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.