OTB  5.7.0
Orfeo Toolbox
otbPersistentImageToOGRDataFilter.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  Some parts of this code are derived from ITK. See ITKCopyright.txt
13  for details.
14 
15 
16  This software is distributed WITHOUT ANY WARRANTY; without even
17  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18  PURPOSE. See the above copyright notices for more information.
19 
20 =========================================================================*/
21 #ifndef otbPersistentImageToOGRDataFilter_txx
22 #define otbPersistentImageToOGRDataFilter_txx
23 
25 #include "itkTimeProbe.h"
26 #include <boost/foreach.hpp>
27 #include <stdio.h>
28 #include "otbMacro.h"
29 #include "otbOGRHelpers.h"
30 
31 namespace otb
32 {
33 
34 template<class TImage>
37  : m_FieldName("DN")
38  , m_LayerName("Layer")
39  , m_GeometryType(wkbMultiPolygon)
40  , m_FieldType(OFTInteger)
41 {
42  this->SetNumberOfRequiredInputs(2);
43  this->SetNumberOfRequiredInputs(2);
44  m_StreamSize.Fill(0);
45 }
46 
47 template<class TImage>
50 {
51 }
52 
53 template<class TImage>
54 void
57 {
58  this->itk::ProcessObject::SetNthInput(1, ogrDS);
59 }
60 
61 template<class TImage>
65 {
66  return static_cast<OGRDataSourceType *> (this->itk::ProcessObject::GetInput(1));
67 }
68 
69 template<class TImage>
70 void
72 ::AddOGRLayerCreationOption(const std::string& option)
73 {
74  m_OGRLayerCreationOptions.push_back(option);
75  this->Modified();
76 }
77 
78 template<class TImage>
79 void
82 {
83  m_OGRLayerCreationOptions.clear();
84  this->Modified();
85 }
86 
87 template<class TImage>
88 void
90 ::SetOGRLayerCreationOptions(const std::vector<std::string>& options)
91 {
92  m_OGRLayerCreationOptions = options;
93  this->Modified();
94 }
95 
96 template<class TImage>
97 const std::vector<std::string>&
100 {
101  return m_OGRLayerCreationOptions;
102 }
103 
104 template<class TImage>
105 void
108 {
109  // Nothing that needs to be allocated for the outputs : the output is not meant to be used
110 }
111 
112 template<class TImage>
113 void
116 {
117 }
118 
119 template<class TImage>
120 void
123 {
124 }
125 
126 template<class TImage>
127 void
130 {
131  std::string projectionRefWkt = this->GetInput()->GetProjectionRef();
132  bool projectionInformationAvailable = !projectionRefWkt.empty();
133  OGRSpatialReference * oSRS = NULL;
134  if(projectionInformationAvailable)
135  {
136  oSRS = static_cast<OGRSpatialReference *>(OSRNewSpatialReference(projectionRefWkt.c_str()));
137  }
138 
139  OGRDataSourcePointerType ogrDS = this->GetOGRDataSource();
140  OGRLayerType outLayer = ogrDS->CreateLayer(m_LayerName, oSRS ,m_GeometryType, m_OGRLayerCreationOptions);
141  OGRFieldDefn field(m_FieldName.c_str(),m_FieldType);
142  outLayer.CreateField(field, true);
143 }
144 
145 
146 template<class TImage>
147 void
150 {
151 
152  if (this->GetStreamSize()[0]==0 && this->GetStreamSize()[1]==0)
153  {
154  this->m_StreamSize = this->GetInput()->GetRequestedRegion().GetSize();
155  }
156 
157  // call the processing function for this tile
158  OGRDataSourcePointerType currentTileVD = this->ProcessTile();
159  OGRLayerType srcLayer = currentTileVD->GetLayerChecked(0);
160 
161  OGRDataSourcePointerType ogrDS = this->GetOGRDataSource();
162  OGRLayerType dstLayer = ogrDS->GetLayersCount() == 1
163  ? ogrDS->GetLayer(0)
164  : ogrDS->GetLayer(m_LayerName);
165 
166 
167  //Copy features contained in the memory layer (srcLayer) in the output layer
168  itk::TimeProbe chrono;
169  chrono.Start();
170 
171  OGRErr err = dstLayer.ogr().StartTransaction();
172 
173  if (err != OGRERR_NONE)
174  {
175  itkExceptionMacro(<< "Unable to start transaction for OGR layer " << dstLayer.ogr().GetName() << ".");
176  }
177 
178  OGRLayerType::const_iterator featIt = srcLayer.begin();
179  for(; featIt!=srcLayer.end(); ++featIt)
180  {
181  OGRFeatureType dstFeature(dstLayer.GetLayerDefn());
182  dstFeature.SetFrom( *featIt, TRUE );
183  dstLayer.CreateFeature( dstFeature );
184  }
185 
186  err = dstLayer.ogr().CommitTransaction();
187 
188  if (err != OGRERR_NONE)
189  {
190  itkExceptionMacro(<< "Unable to commit transaction for OGR layer " << dstLayer.ogr().GetName() << ".");
191  }
192 
193  chrono.Stop();
194  otbMsgDebugMacro(<< "write ogr tile took " << chrono.GetTotal() << " sec");
195 }
196 
197 template<class TImage>
198 void
200 ::PrintSelf(std::ostream& os, itk::Indent indent) const
201 {
202  Superclass::PrintSelf(os, indent);
203 }
204 
205 } // end namespace otb
206 #endif
void SetOGRLayerCreationOptions(const std::vector< std::string > &options)
Collection of geometric objects.
void CreateField(FieldDefn const &field, bool bApproxOK=true)
void AddOGRLayerCreationOption(const std::string &option)
OGRFeatureDefn & GetLayerDefn() const
Layer of geometric objets.
const std::vector< std::string > & GetOGRLayerCreationOptions(void)
Implementation class for Feature iterator. This iterator is a single pass iterator. We may fetch the Feature referenced by an iterator previously stored, but never resume the iteration after a call to Layer::begin(), Layer::start_at(), Layer::CreateFeature(), Layer::DeleteFeature(), Layer::GetFeature(), Layer::SetFeature(), nor fork the iteration.
const_iterator begin() const
void CreateFeature(Feature feature)
#define otbMsgDebugMacro(x)
Definition: otbMacro.h:55
void SetOGRDataSource(OGRDataSourcePointerType ogrDS)
void PrintSelf(std::ostream &os, itk::Indent indent) const
virtual void Stop()
virtual void Start()
DataObject * GetInput(const DataObjectIdentifierType &key)
OGRLayer & ogr()
void SetFrom(Feature const &rhs, int *map, bool mustForgive=true)
Geometric objet with descriptive fields.
virtual void SetNthInput(DataObjectPointerArraySizeType num, DataObject *input)
virtual ValueType GetTotal() const
const_iterator end() const