21 #ifndef otbLabelImageToVectorDataFilter_hxx
22 #define otbLabelImageToVectorDataFilter_hxx
26 #include "itkImageRegionIterator.h"
31 #include "gdal_priv.h"
38 template <
class TInputImage,
class TPrecision>
41 this->SetNumberOfRequiredInputs(2);
42 this->SetNumberOfRequiredInputs(1);
43 this->SetNumberOfRequiredOutputs(1);
48 template <
class TInputImage,
class TPrecision>
51 this->Superclass::SetNthInput(0,
const_cast<InputImageType*
>(input));
54 template <
class TInputImage,
class TPrecision>
57 if (this->GetNumberOfInputs() < 1)
62 return static_cast<const InputImageType*
>(this->Superclass::GetInput(0));
65 template <
class TInputImage,
class TPrecision>
68 this->Superclass::SetNthInput(1,
const_cast<InputImageType*
>(input));
71 template <
class TInputImage,
class TPrecision>
74 if (this->GetNumberOfInputs() < 2)
79 return static_cast<const InputImageType*
>(this->Superclass::GetInput(1));
82 template <
class TInputImage,
class TPrecision>
86 Superclass::GenerateInputRequestedRegion();
89 typename InputImageType::Pointer input =
const_cast<InputImageType*
>(this->GetInput());
96 input->SetRequestedRegionToLargestPossibleRegion();
98 typename InputImageType::Pointer mask =
const_cast<InputImageType*
>(this->GetInputMask());
104 mask->SetRequestedRegionToLargestPossibleRegion();
108 template <
class TInputImage,
class TPrecision>
111 if (this->GetInput()->GetRequestedRegion() != this->GetInput()->GetLargestPossibleRegion())
113 itkExceptionMacro(<<
"Not streamed filter. ERROR : requested region is not the largest possible region.");
116 typename InputImageType::Pointer inImage =
const_cast<InputImageType*
>(this->GetInput());
118 SizeType size = this->GetInput()->GetLargestPossibleRegion().GetSize();
120 unsigned int nbBands = this->GetInput()->GetNumberOfComponentsPerPixel();
128 std::ostringstream stream;
130 <<
"DATAPOINTER=" << (uintptr_t)(this->GetInput()->GetBufferPointer()) <<
","
131 <<
"PIXELS=" << size[0] <<
","
132 <<
"LINES=" << size[1] <<
","
133 <<
"BANDS=" << nbBands <<
","
134 <<
"DATATYPE=" << GDALGetDataTypeName(GdalDataTypeBridge::GetGDALDataType<InputPixelType>()) <<
","
135 <<
"PIXELOFFSET=" << bytePerPixel * nbBands <<
","
136 <<
"LINEOFFSET=" << bytePerPixel * nbBands * size[0] <<
","
137 <<
"BANDOFFSET=" << bytePerPixel;
140 GDALDataset* dataset =
static_cast<GDALDataset*
>(GDALOpen(stream.str().c_str(), GA_ReadOnly));
143 dataset->SetProjection(this->GetInput()->GetProjectionRef().c_str());
145 unsigned int projSize = this->GetInput()->GetGeoTransform().size();
146 double geoTransform[6];
151 IndexType bufferIndexOrigin = this->GetInput()->GetBufferedRegion().GetIndex();
153 this->GetInput()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
154 geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetInput()->GetSignedSpacing()[0];
155 geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetInput()->GetSignedSpacing()[1];
156 geoTransform[1] = this->GetInput()->GetSignedSpacing()[0];
157 geoTransform[5] = this->GetInput()->GetSignedSpacing()[1];
161 geoTransform[2] = 0.;
162 geoTransform[4] = 0.;
166 geoTransform[2] = this->GetInput()->GetGeoTransform()[2];
167 geoTransform[4] = this->GetInput()->GetGeoTransform()[4];
169 dataset->SetGeoTransform(geoTransform);
174 OGRLayerType outputLayer = ogrDS->CreateLayer(
"layer",
nullptr, wkbPolygon);
176 OGRFieldDefn field(m_FieldName.c_str(), OFTInteger);
183 if (m_Use8Connected ==
true)
185 std::string opt(
"8CONNECTED:8");
186 option[0] =
const_cast<char*
>(opt.c_str());
191 typename InputImageType::ConstPointer inputMask = this->GetInputMask();
192 if (!inputMask.IsNull())
194 size = this->GetInputMask()->GetLargestPossibleRegion().GetSize();
195 nbBands = this->GetInputMask()->GetNumberOfComponentsPerPixel();
201 std::ostringstream maskstream;
202 maskstream <<
"MEM:::"
203 <<
"DATAPOINTER=" << (uintptr_t)(this->GetInputMask()->GetBufferPointer()) <<
","
204 <<
"PIXELS=" << size[0] <<
","
205 <<
"LINES=" << size[1] <<
","
206 <<
"BANDS=" << nbBands <<
","
207 <<
"DATATYPE=" << GDALGetDataTypeName(GdalDataTypeBridge::GetGDALDataType<InputPixelType>()) <<
","
208 <<
"PIXELOFFSET=" << bytePerPixel * nbBands <<
","
209 <<
"LINEOFFSET=" << bytePerPixel * nbBands * size[0] <<
","
210 <<
"BANDOFFSET=" << bytePerPixel;
212 GDALDataset* maskDataset =
static_cast<GDALDataset*
>(GDALOpen(maskstream.str().c_str(), GA_ReadOnly));
215 maskDataset->SetProjection(this->GetInputMask()->GetProjectionRef().c_str());
217 projSize = this->GetInputMask()->GetGeoTransform().size();
222 bufferIndexOrigin = this->GetInputMask()->GetBufferedRegion().GetIndex();
223 this->GetInputMask()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
224 geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetInputMask()->GetSignedSpacing()[0];
225 geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetInputMask()->GetSignedSpacing()[1];
226 geoTransform[1] = this->GetInputMask()->GetSignedSpacing()[0];
227 geoTransform[5] = this->GetInputMask()->GetSignedSpacing()[1];
231 geoTransform[2] = 0.;
232 geoTransform[4] = 0.;
236 geoTransform[2] = this->GetInputMask()->GetGeoTransform()[2];
237 geoTransform[4] = this->GetInputMask()->GetGeoTransform()[4];
239 maskDataset->SetGeoTransform(geoTransform);
241 GDALPolygonize(dataset->GetRasterBand(1), maskDataset->GetRasterBand(1), &outputLayer.
ogr(), 0, options,
nullptr,
nullptr);
242 GDALClose(maskDataset);
246 GDALPolygonize(dataset->GetRasterBand(1),
nullptr, &outputLayer.
ogr(), 0, options,
nullptr,
nullptr);
255 document->SetNodeId(outputLayer.
GetLayerDefn().GetName());
263 tree->Add(document, root);
270 OGRConversion->ConvertOGRLayerToDataTreeNode(&outputLayer.
ogr(), documentPtr);