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()));
161 OGRLayer* ogrCurrentLayer =
nullptr;
162 std::vector<OGRLayer*> ogrLayerVector;
168 m_OGRDataSourcePointer =
nullptr;
169 ogrLayerVector = IOConversion->ConvertDataTreeNodeToOGRLayers(inputRoot, m_OGRDataSourcePointer, ogrCurrentLayer, oSRS);
172 for (
unsigned int idx2 = 0; idx2 < ogrLayerVector.size(); ++idx2)
177 if (!m_BurnAttribute.empty())
179 burnField = OGR_FD_GetFieldIndex(OGR_L_GetLayerDefn((OGRLayerH)(ogrLayerVector[idx2])), m_BurnAttribute.c_str());
183 OGR_L_ResetReading((OGRLayerH)(ogrLayerVector[idx2]));
184 while ((hFeat = OGR_L_GetNextFeature((OGRLayerH)(ogrLayerVector[idx2]))) !=
nullptr)
187 if (OGR_F_GetGeometryRef(hFeat) ==
nullptr)
189 OGR_F_Destroy(hFeat);
193 hGeom = OGR_G_Clone(OGR_F_GetGeometryRef(hFeat));
194 m_SrcDataSetGeometries.push_back(hGeom);
199 m_FullBurnValues.push_back(m_DefaultBurnValue++);
200 itkWarningMacro(<<
"Failed to find attribute " << m_BurnAttribute <<
" in layer "
201 << OGR_FD_GetName(OGR_L_GetLayerDefn((OGRLayerH)(ogrLayerVector[idx2])))
202 <<
" .Setting burn value to default = " << m_DefaultBurnValue);
206 m_FullBurnValues.push_back(OGR_F_GetFieldAsDouble(hFeat, burnField));
209 OGR_F_Destroy(hFeat);
222 template <
class TVectorData,
class TOutputImage>
226 this->AllocateOutputs();
232 this->GetOutput()->FillBuffer(m_BackgroundValue);
235 unsigned int nbBands = this->GetOutput()->GetNumberOfComponentsPerPixel();
240 std::ostringstream stream;
242 <<
"DATAPOINTER=" << (uintptr_t)(this->GetOutput()->GetBufferPointer()) <<
","
243 <<
"PIXELS=" << bufferedRegion.GetSize()[0] <<
","
244 <<
"LINES=" << bufferedRegion.GetSize()[1] <<
","
245 <<
"BANDS=" << nbBands <<
","
246 <<
"DATATYPE=" << GDALGetDataTypeName(GdalDataTypeBridge::GetGDALDataType<OutputImageInternalPixelType>()) <<
","
251 GDALDatasetH dataset = GDALOpen(stream.str().c_str(), GA_Update);
254 GDALSetProjection(dataset, this->GetOutput()->GetProjectionRef().c_str());
257 itk::VariableLengthVector<double> geoTransform(6);
263 this->GetOutput()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
264 geoTransform[0] = bufferOrigin[0] - 0.5 * this->GetOutput()->GetSignedSpacing()[0];
265 geoTransform[3] = bufferOrigin[1] - 0.5 * this->GetOutput()->GetSignedSpacing()[1];
266 geoTransform[1] = this->GetOutput()->GetSignedSpacing()[0];
267 geoTransform[5] = this->GetOutput()->GetSignedSpacing()[1];
270 geoTransform[2] = 0.;
271 geoTransform[4] = 0.;
272 GDALSetGeoTransform(dataset,
const_cast<double*
>(geoTransform.GetDataPointer()));
274 char** options =
nullptr;
275 if (m_AllTouchedMode)
277 options = CSLSetNameValue(options,
"ALL_TOUCHED",
"TRUE");
281 if (dataset !=
nullptr)
283 GDALRasterizeGeometries(dataset, m_BandsToBurn.size(), &(m_BandsToBurn[0]), m_SrcDataSetGeometries.size(), &(m_SrcDataSetGeometries[0]),
nullptr,
nullptr,
284 &(m_FullBurnValues[0]), options, GDALDummyProgress,
nullptr);
293 template <
class TVectorData,
class TOutputImage>
296 Superclass::PrintSelf(os, indent);