21 #ifndef otbLabelImageToOGRDataSourceFilter_hxx
22 #define otbLabelImageToOGRDataSourceFilter_hxx
29 #include "gdal_priv.h"
37 template <
class TInputImage>
40 this->SetNumberOfRequiredInputs(2);
41 this->SetNumberOfRequiredInputs(1);
42 this->SetNumberOfRequiredOutputs(1);
46 this->ProcessObject::SetNthOutput(0, this->
MakeOutput(0));
50 template <
class TInputImage>
57 template <
class TInputImage>
63 template <
class TInputImage>
66 this->Superclass::SetNthInput(0,
const_cast<InputImageType*
>(input));
69 template <
class TInputImage>
72 if (this->GetNumberOfInputs() < 1)
77 return static_cast<const InputImageType*
>(this->Superclass::GetInput(0));
80 template <
class TInputImage>
83 this->Superclass::SetNthInput(1,
const_cast<InputImageType*
>(input));
86 template <
class TInputImage>
89 if (this->GetNumberOfInputs() < 2)
94 return static_cast<const InputImageType*
>(this->Superclass::GetInput(1));
97 template <
class TInputImage>
101 Superclass::GenerateInputRequestedRegion();
104 typename InputImageType::Pointer input =
const_cast<InputImageType*
>(this->GetInput());
111 input->SetRequestedRegionToLargestPossibleRegion();
113 typename InputImageType::Pointer mask =
const_cast<InputImageType*
>(this->GetInputMask());
119 mask->SetRequestedRegionToLargestPossibleRegion();
123 template <
class TInputImage>
126 if (this->GetInput()->GetRequestedRegion() != this->GetInput()->GetLargestPossibleRegion())
128 itkExceptionMacro(<<
"Not streamed filter. ERROR : requested region is not the largest possible region.");
132 unsigned int nbBands = 0;
133 unsigned int bytePerPixel = 0;
136 size = this->GetInput()->GetLargestPossibleRegion().GetSize();
137 nbBands = this->GetInput()->GetNumberOfComponentsPerPixel();
144 std::ostringstream stream;
146 <<
"DATAPOINTER=" << (uintptr_t)(this->GetInput()->GetBufferPointer()) <<
","
147 <<
"PIXELS=" << size[0] <<
","
148 <<
"LINES=" << size[1] <<
","
149 <<
"BANDS=" << nbBands <<
","
150 <<
"DATATYPE=" << GDALGetDataTypeName(GdalDataTypeBridge::GetGDALDataType<InputPixelType>()) <<
","
151 <<
"PIXELOFFSET=" << bytePerPixel * nbBands <<
","
152 <<
"LINEOFFSET=" << bytePerPixel * nbBands * size[0] <<
","
153 <<
"BANDOFFSET=" << bytePerPixel;
155 GDALDataset* dataset =
static_cast<GDALDataset*
>(GDALOpen(stream.str().c_str(), GA_ReadOnly));
158 dataset->SetProjection(this->GetInput()->GetProjectionRef().c_str());
160 unsigned int projSize = this->GetInput()->GetGeoTransform().size();
161 double geoTransform[6];
166 IndexType bufferIndexOrigin = this->GetInput()->GetBufferedRegion().GetIndex();
168 this->GetInput()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
169 geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetInput()->GetSignedSpacing()[0];
170 geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetInput()->GetSignedSpacing()[1];
171 geoTransform[1] = this->GetInput()->GetSignedSpacing()[0];
172 geoTransform[5] = this->GetInput()->GetSignedSpacing()[1];
176 geoTransform[2] = 0.;
177 geoTransform[4] = 0.;
181 geoTransform[2] = this->GetInput()->GetGeoTransform()[2];
182 geoTransform[4] = this->GetInput()->GetGeoTransform()[4];
184 dataset->SetGeoTransform(geoTransform);
189 OGRLayerType outputLayer = ogrDS->CreateLayer(
"layer",
nullptr, wkbPolygon);
191 OGRFieldDefn field(m_FieldName.c_str(), OFTInteger);
197 char* option[2] = {
nullptr,
nullptr};
198 if (m_Use8Connected ==
true)
200 std::string opt(
"8CONNECTED:8");
201 option[0] =
const_cast<char*
>(opt.c_str());
206 typename InputImageType::ConstPointer inputMask = this->GetInputMask();
207 if (!inputMask.IsNull())
209 size = this->GetInputMask()->GetLargestPossibleRegion().GetSize();
210 nbBands = this->GetInputMask()->GetNumberOfComponentsPerPixel();
216 std::ostringstream maskstream;
217 maskstream <<
"MEM:::"
218 <<
"DATAPOINTER=" << (uintptr_t)(this->GetInputMask()->GetBufferPointer()) <<
","
219 <<
"PIXELS=" << size[0] <<
","
220 <<
"LINES=" << size[1] <<
","
221 <<
"BANDS=" << nbBands <<
","
222 <<
"DATATYPE=" << GDALGetDataTypeName(GdalDataTypeBridge::GetGDALDataType<InputPixelType>()) <<
","
223 <<
"PIXELOFFSET=" << bytePerPixel * nbBands <<
","
224 <<
"LINEOFFSET=" << bytePerPixel * nbBands * size[0] <<
","
225 <<
"BANDOFFSET=" << bytePerPixel;
227 GDALDataset* maskDataset =
static_cast<GDALDataset*
>(GDALOpen(maskstream.str().c_str(), GA_ReadOnly));
230 maskDataset->SetProjection(this->GetInputMask()->GetProjectionRef().c_str());
232 projSize = this->GetInputMask()->GetGeoTransform().size();
237 bufferIndexOrigin = this->GetInputMask()->GetBufferedRegion().GetIndex();
238 this->GetInputMask()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
239 geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetInputMask()->GetSignedSpacing()[0];
240 geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetInputMask()->GetSignedSpacing()[1];
241 geoTransform[1] = this->GetInputMask()->GetSignedSpacing()[0];
242 geoTransform[5] = this->GetInputMask()->GetSignedSpacing()[1];
246 geoTransform[2] = 0.;
247 geoTransform[4] = 0.;
251 geoTransform[2] = this->GetInputMask()->GetGeoTransform()[2];
252 geoTransform[4] = this->GetInputMask()->GetGeoTransform()[4];
254 maskDataset->SetGeoTransform(geoTransform);
256 GDALPolygonize(dataset->GetRasterBand(1), maskDataset->GetRasterBand(1), &outputLayer.
ogr(), 0, options,
nullptr,
nullptr);
257 GDALClose(maskDataset);
261 GDALPolygonize(dataset->GetRasterBand(1),
nullptr, &outputLayer.
ogr(), 0, options,
nullptr,
nullptr);
264 this->SetNthOutput(0, ogrDS);