21 #ifndef otbVectorDataToLabelImageFilter_hxx
22 #define otbVectorDataToLabelImageFilter_hxx
25 #include "ogr_srs_api.h"
35 template <
class TVectorData,
class TOutputImage>
37 : m_OGRDataSourcePointer(nullptr), m_BandsToBurn(1, 1), m_BurnAttribute(
"FID"), m_DefaultBurnValue(1.), m_BackgroundValue(0.), m_AllTouchedMode(false)
39 this->SetNumberOfRequiredInputs(1);
47 template <
class TVectorData,
class TOutputImage>
50 this->itk::ProcessObject::PushBackInput(vd);
53 template <
class TVectorData,
class TOutputImage>
57 return static_cast<const TVectorData*
>(this->itk::ProcessObject::GetInput(idx));
60 template <
class TVectorData,
class TOutputImage>
63 if (this->m_OutputSpacing != spacing)
65 this->m_OutputSpacing = spacing;
70 template <
class TVectorData,
class TOutputImage>
74 this->SetOutputSpacing(s);
77 template <
class TVectorData,
class TOutputImage>
80 itk::Vector<float, 2> sf(spacing);
83 this->SetOutputSpacing(s);
86 template <
class TVectorData,
class TOutputImage>
90 this->SetOutputOrigin(p);
93 template <
class TVectorData,
class TOutputImage>
96 itk::Point<float, 2> of(origin);
99 this->SetOutputOrigin(p);
102 template <
class TVectorData,
class TOutputImage>
103 template <
class ImagePo
interType>
106 this->SetOutputOrigin(src->GetOrigin());
107 this->SetOutputSpacing(src->GetSignedSpacing());
108 this->SetOutputSize(src->GetLargestPossibleRegion().GetSize());
109 this->SetOutputProjectionRef(src->GetProjectionRef());
112 template <
class TVectorData,
class TOutputImage>
123 typename TOutputImage::RegionType outputLargestPossibleRegion;
124 outputLargestPossibleRegion.SetSize(m_OutputSize);
126 outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
129 outputPtr->SetSignedSpacing(m_OutputSpacing);
130 outputPtr->SetOrigin(m_OutputOrigin);
132 itk::MetaDataDictionary& dict = outputPtr->GetMetaDataDictionary();
137 for (
unsigned int idx = 0; idx < this->GetNumberOfInputs(); ++idx)
142 std::string projectionRefWkt = vd->GetProjectionRef();
143 bool projectionInformationAvailable = !projectionRefWkt.empty();
144 OGRSpatialReference* oSRS =
nullptr;
146 if (projectionInformationAvailable)
148 oSRS =
static_cast<OGRSpatialReference*
>(OSRNewSpatialReference(projectionRefWkt.c_str()));
156 OGRLayer* ogrCurrentLayer =
nullptr;
157 std::vector<OGRLayer*> ogrLayerVector;
163 m_OGRDataSourcePointer =
nullptr;
164 ogrLayerVector = IOConversion->ConvertDataTreeNodeToOGRLayers(vd,vd->GetRoot(), m_OGRDataSourcePointer, ogrCurrentLayer, oSRS);
167 for (
unsigned int idx2 = 0; idx2 < ogrLayerVector.size(); ++idx2)
172 if (!m_BurnAttribute.empty())
174 burnField = OGR_FD_GetFieldIndex(OGR_L_GetLayerDefn((OGRLayerH)(ogrLayerVector[idx2])), m_BurnAttribute.c_str());
178 OGR_L_ResetReading((OGRLayerH)(ogrLayerVector[idx2]));
179 while ((hFeat = OGR_L_GetNextFeature((OGRLayerH)(ogrLayerVector[idx2]))) !=
nullptr)
182 if (OGR_F_GetGeometryRef(hFeat) ==
nullptr)
184 OGR_F_Destroy(hFeat);
188 hGeom = OGR_G_Clone(OGR_F_GetGeometryRef(hFeat));
189 m_SrcDataSetGeometries.push_back(hGeom);
194 m_FullBurnValues.push_back(m_DefaultBurnValue++);
195 itkWarningMacro(<<
"Failed to find attribute " << m_BurnAttribute <<
" in layer "
196 << OGR_FD_GetName(OGR_L_GetLayerDefn((OGRLayerH)(ogrLayerVector[idx2])))
197 <<
" .Setting burn value to default = " << m_DefaultBurnValue);
201 m_FullBurnValues.push_back(OGR_F_GetFieldAsDouble(hFeat, burnField));
204 OGR_F_Destroy(hFeat);
217 template <
class TVectorData,
class TOutputImage>
221 this->AllocateOutputs();
227 this->GetOutput()->FillBuffer(m_BackgroundValue);
230 unsigned int nbBands = this->GetOutput()->GetNumberOfComponentsPerPixel();
234 this->GetOutput()->GetBufferPointer(),
235 bufferedRegion.GetSize()[0],
236 bufferedRegion.GetSize()[1],
242 GDALSetProjection(dataset->GetDataSet(), this->GetOutput()->GetProjectionRef().c_str());
245 itk::VariableLengthVector<double> geoTransform(6);
251 this->GetOutput()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
252 geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetOutput()->GetSignedSpacing()[0];
253 geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetOutput()->GetSignedSpacing()[1];
254 geoTransform[1] = this->GetOutput()->GetSignedSpacing()[0];
255 geoTransform[5] = this->GetOutput()->GetSignedSpacing()[1];
258 geoTransform[2] = 0.;
259 geoTransform[4] = 0.;
260 GDALSetGeoTransform(dataset->GetDataSet(),
261 const_cast<double*
>(geoTransform.GetDataPointer()));
263 char** options =
nullptr;
264 if (m_AllTouchedMode)
266 options = CSLSetNameValue(options,
"ALL_TOUCHED",
"TRUE");
270 if (dataset->GetDataSet() !=
nullptr)
272 GDALRasterizeGeometries(dataset->GetDataSet(), m_BandsToBurn.size(),
273 &(m_BandsToBurn[0]), m_SrcDataSetGeometries.size(),
274 &(m_SrcDataSetGeometries[0]),
nullptr,
nullptr,
275 &(m_FullBurnValues[0]), options, GDALDummyProgress,
282 template <
class TVectorData,
class TOutputImage>
285 Superclass::PrintSelf(os, indent);
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
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)