Orfeo Toolbox  4.0
otbRasterizeVectorDataFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 
20 #include "otbOGRIOHelper.h"
21 #include "otbGdalDataTypeBridge.h"
22 #include "otbMacro.h"
23 
24 namespace otb
25 {
26 template<class TVectorData, class TInputImage, class TOutputImage>
29  : m_OGRDataSourcePointer(0)
30 {
31  this->SetNumberOfRequiredInputs(1);
32 }
33 
34 template<class TVectorData, class TInputImage, class TOutputImage>
35 void
38 {
39  // Process object is not const-correct so the const_cast is required
40  // here
41  if (this->GetNumberOfInputs() < 1)
42  {
43  this->itk::ProcessObject::SetNthInput(1, const_cast<VectorDataType *>(vd) );
44  }
45  else
46  {
48  }
49 }
50 
51 
52 template<class TVectorData, class TInputImage, class TOutputImage>
53 void
56 {
57  Superclass::GenerateOutputInformation();
58 
59  // Generate the OGRLayers from the input VectorDatas
60  // iteration begin from 1 cause the 0th input is a image
61  for (unsigned int idx = 1; idx < this->GetNumberOfInputs(); ++idx)
62  {
63  const VectorDataType* vd = dynamic_cast< const VectorDataType*>(this->itk::ProcessObject::GetInput(idx));
64 
65  // Get the projection ref of the current VectorData
66  std::string projectionRefWkt = vd->GetProjectionRef();
67  bool projectionInformationAvailable = !projectionRefWkt.empty();
68  OGRSpatialReference * oSRS = NULL;
69 
70  if (projectionInformationAvailable)
71  {
72  oSRS = static_cast<OGRSpatialReference *>(OSRNewSpatialReference(projectionRefWkt.c_str()));
73  }
74  else
75  {
76  otbMsgDevMacro(<< "Projection information unavailable");
77  }
78 
79  // Retrieving root node
80  DataTreeConstPointerType tree = vd->GetDataTree();
81 
82  // Get the input tree root
83  InternalTreeNodeType * inputRoot = const_cast<InternalTreeNodeType *>(tree->GetRoot());
84 
85  // Iterative method to build the layers from a VectorData
86  OGRRegisterAll();
87  OGRLayer * ogrCurrentLayer = NULL;
88  std::vector<OGRLayer *> ogrLayerVector;
90 
91  // The method ConvertDataTreeNodeToOGRLayers create the
92  // OGRDataSource but don t release it. Destruction is done in the
93  // desctructor
94  m_OGRDataSourcePointer = NULL;
95  ogrLayerVector = IOConversion->ConvertDataTreeNodeToOGRLayers(inputRoot,
96  m_OGRDataSourcePointer,
97  ogrCurrentLayer,
98  oSRS);
99 
100  // Cast OGRLayer* to OGRLayerH
101  for (unsigned int idx = 0; idx < ogrLayerVector.size(); ++idx)
102  {
103  m_SrcDataSetLayers.push_back( (OGRLayerH)(ogrLayerVector[idx]) );
104  }
105 
106  // Destroy the oSRS
107  if (oSRS != NULL)
108  {
109  OSRRelease(oSRS);
110  }
111  }
112 
113  // Some checking : Check the consistency between the band list
114  // to burn and the burn values :
115  // There should be "m_BandsToBurn.size()" burn values for each layer.
116  // If not, burn values vector will be cloned as many time as the number of
117  // OGRLayer we have
118  if (m_BurnValues.size() != m_BandsToBurn.size() * m_SrcDataSetLayers.size())
119  {
120  std::ostringstream oss;
121  oss <<"Inconsistency detected : expected burn vector size to be equal to( bandToBurn * nb layers = "
122  << m_BandsToBurn.size() * m_SrcDataSetLayers.size()
123  << " ), got : "<< m_BurnValues.size()<<std::endl;
124  itkWarningMacro(<<oss.str());
125  }
126 
127  // Clone the burn values to fit the condition
128  for (unsigned int idx =0; idx < m_SrcDataSetLayers.size(); ++idx)
129  {
130  for (unsigned int burnidx = 0; burnidx < m_BurnValues.size(); ++burnidx)
131  {
132  m_FullBurnValues.push_back(m_BurnValues[burnidx]);
133  }
134  }
135 }
136 
137 template<class TVectorData, class TInputImage, class TOutputImage>
138 void
140 {
141  // Call Superclass GenerateData
142  Superclass::GenerateData();
143 
144  // Get the buffered region
145  OutputImageRegionType bufferedRegion = this->GetOutput()->GetBufferedRegion();
146 
147  // nb bands
148  unsigned int nbBands = this->GetOutput()->GetNumberOfComponentsPerPixel();
149 
150  // register drivers
151  GDALAllRegister();
152 
153  std::ostringstream stream;
154  stream << "MEM:::"
155  << "DATAPOINTER=" << (unsigned long)(this->GetOutput()->GetBufferPointer()) << ","
156  << "PIXELS=" << bufferedRegion.GetSize()[0] << ","
157  << "LINES=" << bufferedRegion.GetSize()[1]<< ","
158  << "BANDS=" << nbBands << ","
159  << "DATATYPE=" << GDALGetDataTypeName(GdalDataTypeBridge::GetGDALDataType<OutputImageInternalPixelType>()) << ","
160  << "PIXELOFFSET=" << sizeof(OutputImageInternalPixelType) * nbBands << ","
161  << "LINEOFFSET=" << sizeof(OutputImageInternalPixelType)*nbBands*bufferedRegion.GetSize()[0] << ","
162  << "BANDOFFSET=" << sizeof(OutputImageInternalPixelType);
163 
164  GDALDatasetH dataset = GDALOpen(stream.str().c_str(), GA_Update);
165 
166  // Add the projection ref to the dataset
167  GDALSetProjection (dataset, this->GetOutput()->GetProjectionRef().c_str());
168 
169  // add the geoTransform to the dataset
170  itk::VariableLengthVector<double> geoTransform(6);
171 
172  // Reporting origin and spacing of the buffered region
173  // the spacing is unchanged, the origin is relative to the buffered region
174  InputIndexType bufferIndexOrigin = bufferedRegion.GetIndex();
175  InputPointType bufferOrigin;
176  this->GetOutput()->TransformIndexToPhysicalPoint(bufferIndexOrigin, bufferOrigin);
177  geoTransform[0] = bufferOrigin[0];
178  geoTransform[3] = bufferOrigin[1];
179  geoTransform[1] = this->GetOutput()->GetSpacing()[0];
180  geoTransform[5] = this->GetOutput()->GetSpacing()[1];
181 
182  // FIXME: Here component 1 and 4 should be replaced by the orientation parameters
183  geoTransform[2] = 0.;
184  geoTransform[4] = 0.;
185  GDALSetGeoTransform(dataset,const_cast<double*>(geoTransform.GetDataPointer()));
186 
187  // Burn the geometries into the dataset
188  if (dataset != NULL)
189  {
190  GDALRasterizeLayers( dataset, m_BandsToBurn.size(),
191  &(m_BandsToBurn[0]),
192  m_SrcDataSetLayers.size(),
193  &(m_SrcDataSetLayers[0]),
194  NULL, NULL, &(m_FullBurnValues[0]),
195  NULL,
196  GDALDummyProgress, NULL );
197 
198  // release the dataset
199  GDALClose( dataset );
200  }
201 }
202 
203 template<class TVectorData, class TInputImage, class TOutputImage>
204 void
206 ::PrintSelf(std::ostream& os, itk::Indent indent) const
207 {
208  Superclass::PrintSelf(os, indent);
209 }
210 
211 } // end namespace otb
212 

Generated at Sat Mar 8 2014 16:15:15 for Orfeo Toolbox with doxygen 1.8.3.1