21 #ifndef otbVectorDataToLabelImageFilter_hxx
22 #define otbVectorDataToLabelImageFilter_hxx
25 #include "ogr_srs_api.h"
34 template <
class TVectorData,
class TOutputImage>
36 : m_OGRDataSourcePointer(nullptr), m_BandsToBurn(1, 1), m_BurnAttribute(
"FID"), m_DefaultBurnValue(1.), m_BackgroundValue(0.), m_AllTouchedMode(false)
38 this->SetNumberOfRequiredInputs(1);
46 template <
class TVectorData,
class TOutputImage>
49 this->itk::ProcessObject::PushBackInput(vd);
52 template <
class TVectorData,
class TOutputImage>
56 return static_cast<const TVectorData*
>(this->itk::ProcessObject::GetInput(idx));
59 template <
class TVectorData,
class TOutputImage>
62 if (this->m_OutputSpacing != spacing)
64 this->m_OutputSpacing = spacing;
69 template <
class TVectorData,
class TOutputImage>
73 this->SetOutputSpacing(s);
76 template <
class TVectorData,
class TOutputImage>
79 itk::Vector<float, 2> sf(spacing);
82 this->SetOutputSpacing(s);
85 template <
class TVectorData,
class TOutputImage>
89 this->SetOutputOrigin(p);
92 template <
class TVectorData,
class TOutputImage>
95 itk::Point<float, 2> of(origin);
98 this->SetOutputOrigin(p);
101 template <
class TVectorData,
class TOutputImage>
102 template <
class ImagePo
interType>
105 this->SetOutputOrigin(src->GetOrigin());
106 this->SetOutputSpacing(src->GetSignedSpacing());
107 this->SetOutputSize(src->GetLargestPossibleRegion().GetSize());
108 this->SetOutputProjectionRef(src->GetProjectionRef());
111 template <
class TVectorData,
class TOutputImage>
122 typename TOutputImage::RegionType outputLargestPossibleRegion;
123 outputLargestPossibleRegion.SetSize(m_OutputSize);
125 outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
128 outputPtr->SetSignedSpacing(m_OutputSpacing);
129 outputPtr->SetOrigin(m_OutputOrigin);
131 itk::MetaDataDictionary& dict = outputPtr->GetMetaDataDictionary();
136 for (
unsigned int idx = 0; idx < this->GetNumberOfInputs(); ++idx)
141 std::string projectionRefWkt = vd->GetProjectionRef();
142 bool projectionInformationAvailable = !projectionRefWkt.empty();
143 OGRSpatialReference* oSRS =
nullptr;
145 if (projectionInformationAvailable)
147 oSRS =
static_cast<OGRSpatialReference*
>(OSRNewSpatialReference(projectionRefWkt.c_str()));
155 OGRLayer* ogrCurrentLayer =
nullptr;
156 std::vector<OGRLayer*> ogrLayerVector;
162 m_OGRDataSourcePointer =
nullptr;
163 ogrLayerVector = IOConversion->ConvertDataTreeNodeToOGRLayers(vd,vd->GetRoot(), m_OGRDataSourcePointer, ogrCurrentLayer, oSRS);
166 for (
unsigned int idx2 = 0; idx2 < ogrLayerVector.size(); ++idx2)
171 if (!m_BurnAttribute.empty())
173 burnField = OGR_FD_GetFieldIndex(OGR_L_GetLayerDefn((OGRLayerH)(ogrLayerVector[idx2])), m_BurnAttribute.c_str());
177 OGR_L_ResetReading((OGRLayerH)(ogrLayerVector[idx2]));
178 while ((hFeat = OGR_L_GetNextFeature((OGRLayerH)(ogrLayerVector[idx2]))) !=
nullptr)
181 if (OGR_F_GetGeometryRef(hFeat) ==
nullptr)
183 OGR_F_Destroy(hFeat);
187 hGeom = OGR_G_Clone(OGR_F_GetGeometryRef(hFeat));
188 m_SrcDataSetGeometries.push_back(hGeom);
193 m_FullBurnValues.push_back(m_DefaultBurnValue++);
194 itkWarningMacro(<<
"Failed to find attribute " << m_BurnAttribute <<
" in layer "
195 << OGR_FD_GetName(OGR_L_GetLayerDefn((OGRLayerH)(ogrLayerVector[idx2])))
196 <<
" .Setting burn value to default = " << m_DefaultBurnValue);
200 m_FullBurnValues.push_back(OGR_F_GetFieldAsDouble(hFeat, burnField));
203 OGR_F_Destroy(hFeat);
216 template <
class TVectorData,
class TOutputImage>
220 this->AllocateOutputs();
226 this->GetOutput()->FillBuffer(m_BackgroundValue);
229 unsigned int nbBands = this->GetOutput()->GetNumberOfComponentsPerPixel();
234 std::ostringstream stream;
236 <<
"DATAPOINTER=" << (uintptr_t)(this->GetOutput()->GetBufferPointer()) <<
","
237 <<
"PIXELS=" << bufferedRegion.GetSize()[0] <<
","
238 <<
"LINES=" << bufferedRegion.GetSize()[1] <<
","
239 <<
"BANDS=" << nbBands <<
","
240 <<
"DATATYPE=" << GDALGetDataTypeName(GdalDataTypeBridge::GetGDALDataType<OutputImageInternalPixelType>()) <<
","
245 GDALDatasetH dataset = GDALOpen(stream.str().c_str(), GA_Update);
248 GDALSetProjection(dataset, this->GetOutput()->GetProjectionRef().c_str());
251 itk::VariableLengthVector<double> geoTransform(6);
257 this->GetOutput()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
258 geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetOutput()->GetSignedSpacing()[0];
259 geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetOutput()->GetSignedSpacing()[1];
260 geoTransform[1] = this->GetOutput()->GetSignedSpacing()[0];
261 geoTransform[5] = this->GetOutput()->GetSignedSpacing()[1];
264 geoTransform[2] = 0.;
265 geoTransform[4] = 0.;
266 GDALSetGeoTransform(dataset,
const_cast<double*
>(geoTransform.GetDataPointer()));
268 char** options =
nullptr;
269 if (m_AllTouchedMode)
271 options = CSLSetNameValue(options,
"ALL_TOUCHED",
"TRUE");
275 if (dataset !=
nullptr)
277 GDALRasterizeGeometries(dataset, m_BandsToBurn.size(), &(m_BandsToBurn[0]), m_SrcDataSetGeometries.size(), &(m_SrcDataSetGeometries[0]),
nullptr,
nullptr,
278 &(m_FullBurnValues[0]), options, GDALDummyProgress,
nullptr);
287 template <
class TVectorData,
class TOutputImage>
290 Superclass::PrintSelf(os, indent);
itk::SmartPointer< Self > Pointer
OutputImageType::InternalPixelType OutputImageInternalPixelType
virtual void AddVectorData(const VectorDataType *vd)
OutputIndexType m_OutputStartIndex
void PrintSelf(std::ostream &os, itk::Indent indent) const override
virtual void SetOutputSpacing(const OutputSpacingType &spacing)
virtual void SetOutputOrigin(OutputOriginType _arg)
OutputImageType::Pointer OutputImagePointer
OutputImageType::SpacingType OutputSpacingType
VectorDataToLabelImageFilter()
OutputImageType::PointType OutputOriginType
OutputImageType::RegionType OutputImageRegionType
void GenerateOutputInformation() override
void GenerateData() override
OutputSpacingType m_OutputSpacing
void SetOutputParametersFromImage(const ImagePointerType image)
const VectorDataType * GetInput(unsigned int idx)
OutputSizeType m_OutputSize
OutputImageType::IndexType OutputIndexType
TVectorData VectorDataType
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
#define otbMsgDevMacro(x)