OTB  5.5.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>
36 ::PersistentImageToOGRDataFilter() : m_FieldName("DN"), m_LayerName("Layer"), m_GeometryType(wkbMultiPolygon)
37 {
38  this->SetNumberOfRequiredInputs(2);
39  this->SetNumberOfRequiredInputs(2);
40  m_StreamSize.Fill(0);
41 }
42 
43 template<class TImage>
46 {
47 }
48 
49 template<class TImage>
50 void
53 {
54  this->itk::ProcessObject::SetNthInput(1, ogrDS);
55 }
56 
57 template<class TImage>
61 {
62  return static_cast<OGRDataSourceType *> (this->itk::ProcessObject::GetInput(1));
63 }
64 
65 template<class TImage>
66 void
68 ::AddOGRLayerCreationOption(const std::string& option)
69 {
70  m_OGRLayerCreationOptions.push_back(option);
71  this->Modified();
72 }
73 
74 template<class TImage>
75 void
78 {
79  m_OGRLayerCreationOptions.clear();
80  this->Modified();
81 }
82 
83 template<class TImage>
84 void
86 ::SetOGRLayerCreationOptions(const std::vector<std::string>& options)
87 {
88  m_OGRLayerCreationOptions = options;
89  this->Modified();
90 }
91 
92 
93 template<class TImage>
94 void
97 {
98  // Nothing that needs to be allocated for the outputs : the output is not meant to be used
99 }
100 
101 template<class TImage>
102 void
105 {
106 }
107 
108 template<class TImage>
109 void
112 {
113 }
114 
115 template<class TImage>
116 void
119 {
120 
121  std::string projectionRefWkt = this->GetInput()->GetProjectionRef();
122  bool projectionInformationAvailable = !projectionRefWkt.empty();
123  OGRSpatialReference * oSRS = NULL;
124  if(projectionInformationAvailable)
125  {
126  oSRS = static_cast<OGRSpatialReference *>(OSRNewSpatialReference(projectionRefWkt.c_str()));
127  }
128 
129 
130  OGRDataSourcePointerType ogrDS = this->GetOGRDataSource();
131 
132  ogrDS->CreateLayer(m_LayerName, oSRS ,m_GeometryType, m_OGRLayerCreationOptions);
133  OGRFieldDefn field(m_FieldName.c_str(),OFTInteger);
134 
135  //Handle the case of shapefile. A shapefile is a layer and not a datasource.
136  //The layer name in a shapefile is the shapefile's name.
137  //This is not the case for a database as sqlite or PG.
138  if (ogrDS->GetLayersCount() == 1)
139  {
140  ogrDS->GetLayer(0).CreateField(field, true);
141  }
142  else
143  {
144  ogrDS->GetLayer(m_LayerName).CreateField(field, true);
145  }
146 
147  //CSLDestroy( options );
148 
149 }
150 
151 
152 template<class TImage>
153 void
156 {
157 
158  if (this->GetStreamSize()[0]==0 && this->GetStreamSize()[1]==0)
159  {
160  this->m_StreamSize = this->GetInput()->GetRequestedRegion().GetSize();
161  }
162 
163  // call the processing function for this tile
164  OGRDataSourcePointerType currentTileVD = this->ProcessTile();
165  OGRLayerType srcLayer = currentTileVD->GetLayerChecked(0);
166 
167  OGRDataSourcePointerType ogrDS = this->GetOGRDataSource();
168  OGRLayerType dstLayer = ogrDS->GetLayersCount() == 1
169  ? ogrDS->GetLayer(0)
170  : ogrDS->GetLayer(m_LayerName);
171 
172 
173  //Copy features contained in the memory layer (srcLayer) in the output layer
174  itk::TimeProbe chrono;
175  chrono.Start();
176 
177  OGRErr err = dstLayer.ogr().StartTransaction();
178 
179  if (err != OGRERR_NONE)
180  {
181  itkExceptionMacro(<< "Unable to start transaction for OGR layer " << dstLayer.ogr().GetName() << ".");
182  }
183 
184  OGRLayerType::const_iterator featIt = srcLayer.begin();
185  for(; featIt!=srcLayer.end(); ++featIt)
186  {
187  OGRFeatureType dstFeature(dstLayer.GetLayerDefn());
188  dstFeature.SetFrom( *featIt, TRUE );
189  dstLayer.CreateFeature( dstFeature );
190  }
191 
192  err = dstLayer.ogr().CommitTransaction();
193 
194  if (err != OGRERR_NONE)
195  {
196  itkExceptionMacro(<< "Unable to commit transaction for OGR layer " << dstLayer.ogr().GetName() << ".");
197  }
198 
199  chrono.Stop();
200  otbMsgDebugMacro(<< "write ogr tile took " << chrono.GetTotal() << " sec");
201 }
202 
203 template<class TImage>
204 void
206 ::PrintSelf(std::ostream& os, itk::Indent indent) const
207 {
208  Superclass::PrintSelf(os, indent);
209 }
210 
211 } // end namespace otb
212 #endif
void SetOGRLayerCreationOptions(const std::vector< std::string > &options)
Collection of geometric objects.
void AddOGRLayerCreationOption(const std::string &option)
OGRFeatureDefn & GetLayerDefn() const
Layer of geometric objets.
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