18 #ifndef __otbVectorDataToLabelImageFilter_txx
19 #define __otbVectorDataToLabelImageFilter_txx
27 #include "ogr_srs_api.h"
31 template<
class TVectorData,
class TOutputImage>
34 : m_OGRDataSourcePointer(0),
35 m_BurnAttribute(
"FID")
37 this->SetNumberOfRequiredInputs(1);
40 m_OutputSpacing.Fill(1.0);
42 m_OutputStartIndex.Fill(0);
45 m_BandsToBurn.push_back(1);
48 m_DefaultBurnValue = 1.;
51 template<
class TVectorData,
class TOutputImage>
59 template <
class TVectorData,
class TOutputImage>
64 return static_cast<const TVectorData *
>
68 template <
class TVectorData,
class TOutputImage>
73 if (this->m_OutputSpacing != spacing)
75 this->m_OutputSpacing = spacing;
80 template <
class TVectorData,
class TOutputImage>
86 this->SetOutputSpacing(s);
89 template <
class TVectorData,
class TOutputImage>
97 this->SetOutputSpacing(s);
100 template <
class TVectorData,
class TOutputImage>
106 this->SetOutputOrigin(p);
109 template <
class TVectorData,
class TOutputImage>
117 this->SetOutputOrigin(p);
120 template <
class TVectorData,
class TOutputImage>
125 this->SetOutputOrigin ( src->
GetOrigin() );
129 this->SetOutputProjectionRef(imi->GetProjectionRef());
132 template<
class TVectorData,
class TOutputImage>
145 typename TOutputImage::RegionType outputLargestPossibleRegion;
146 outputLargestPossibleRegion.SetSize(m_OutputSize);
148 outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
151 outputPtr->SetSpacing(m_OutputSpacing);
152 outputPtr->SetOrigin(m_OutputOrigin);
156 static_cast<std::string
>(this->GetOutputProjectionRef()));
160 for (
unsigned int idx = 0; idx < this->GetNumberOfInputs(); ++idx)
165 std::string projectionRefWkt = vd->GetProjectionRef();
166 bool projectionInformationAvailable = !projectionRefWkt.empty();
167 OGRSpatialReference * oSRS =
NULL;
169 if (projectionInformationAvailable)
171 oSRS =
static_cast<OGRSpatialReference *
>(OSRNewSpatialReference(projectionRefWkt.c_str()));
185 OGRLayer * ogrCurrentLayer =
NULL;
186 std::vector<OGRLayer *> ogrLayerVector;
192 m_OGRDataSourcePointer =
NULL;
193 ogrLayerVector = IOConversion->ConvertDataTreeNodeToOGRLayers(inputRoot,
194 m_OGRDataSourcePointer,
199 for (
unsigned int idx = 0; idx < ogrLayerVector.size(); ++idx)
204 if( !m_BurnAttribute.empty() )
206 burnField = OGR_FD_GetFieldIndex( OGR_L_GetLayerDefn( (OGRLayerH)(ogrLayerVector[idx]) ),
207 m_BurnAttribute.c_str() );
211 OGR_L_ResetReading( (OGRLayerH)(ogrLayerVector[idx]) );
212 while( ( hFeat = OGR_L_GetNextFeature( (OGRLayerH)(ogrLayerVector[idx]) )) !=
NULL )
215 if( OGR_F_GetGeometryRef( hFeat ) ==
NULL )
217 OGR_F_Destroy( hFeat );
221 hGeom = OGR_G_Clone( OGR_F_GetGeometryRef( hFeat ) );
222 m_SrcDataSetGeometries.push_back( hGeom );
224 if (burnField == -1 )
227 m_FullBurnValues.push_back(m_DefaultBurnValue++);
228 itkWarningMacro(<<
"Failed to find attribute "<<m_BurnAttribute <<
" in layer "
229 << OGR_FD_GetName( OGR_L_GetLayerDefn( (OGRLayerH)(ogrLayerVector[idx]) ))
230 <<
" .Setting burn value to default = "
231 << m_DefaultBurnValue);
235 m_FullBurnValues.push_back( OGR_F_GetFieldAsDouble( hFeat, burnField ) );
238 OGR_F_Destroy( hFeat );
251 template<
class TVectorData,
class TOutputImage>
256 this->AllocateOutputs();
262 unsigned int nbBands = this->GetOutput()->GetNumberOfComponentsPerPixel();
267 std::ostringstream stream;
269 <<
"DATAPOINTER=" << (
unsigned long)(this->GetOutput()->GetBufferPointer()) <<
","
270 <<
"PIXELS=" << bufferedRegion.GetSize()[0] <<
","
271 <<
"LINES=" << bufferedRegion.GetSize()[1]<<
","
272 <<
"BANDS=" << nbBands <<
","
273 <<
"DATATYPE=" << GDALGetDataTypeName(GdalDataTypeBridge::GetGDALDataType<OutputImageInternalPixelType>()) <<
","
278 GDALDatasetH dataset = GDALOpen(stream.str().c_str(), GA_Update);
281 GDALSetProjection (dataset, this->GetOutput()->GetProjectionRef().c_str());
290 this->GetOutput()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
291 geoTransform[0] = bufferOrigin[0];
292 geoTransform[3] = bufferOrigin[1];
293 geoTransform[1] = this->GetOutput()->GetSpacing()[0];
294 geoTransform[5] = this->GetOutput()->GetSpacing()[1];
297 geoTransform[2] = 0.;
298 geoTransform[4] = 0.;
299 GDALSetGeoTransform(dataset,const_cast<double*>(geoTransform.
GetDataPointer()));
304 GDALRasterizeGeometries( dataset, m_BandsToBurn.size(),
306 m_SrcDataSetGeometries.size(),
307 &(m_SrcDataSetGeometries[0]),
310 GDALDummyProgress,
NULL );
313 GDALClose( dataset );
317 template<
class TVectorData,
class TOutputImage>
322 Superclass::PrintSelf(os, indent);