OTB  6.1.0
Orfeo Toolbox
otbPersistentImageToOGRDataFilter.txx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbPersistentImageToOGRDataFilter_txx
23 #define otbPersistentImageToOGRDataFilter_txx
24 
26 #include "itkTimeProbe.h"
27 #include <boost/foreach.hpp>
28 #include <stdio.h>
29 #include "otbMacro.h"
30 #include "otbOGRHelpers.h"
31 
32 namespace otb
33 {
34 
35 template<class TImage>
38  : m_FieldName("DN")
39  , m_LayerName("Layer")
40  , m_GeometryType(wkbMultiPolygon)
41  , m_FieldType(OFTInteger)
42 {
43  this->SetNumberOfRequiredInputs(2);
44  this->SetNumberOfRequiredInputs(2);
45  m_StreamSize.Fill(0);
46 }
47 
48 template<class TImage>
51 {
52 }
53 
54 template<class TImage>
55 void
58 {
59  this->itk::ProcessObject::SetNthInput(1, ogrDS);
60 }
61 
62 template<class TImage>
66 {
67  return static_cast<OGRDataSourceType *> (this->itk::ProcessObject::GetInput(1));
68 }
69 
70 template<class TImage>
71 void
73 ::AddOGRLayerCreationOption(const std::string& option)
74 {
75  m_OGRLayerCreationOptions.push_back(option);
76  this->Modified();
77 }
78 
79 template<class TImage>
80 void
83 {
84  m_OGRLayerCreationOptions.clear();
85  this->Modified();
86 }
87 
88 template<class TImage>
89 void
91 ::SetOGRLayerCreationOptions(const std::vector<std::string>& options)
92 {
93  m_OGRLayerCreationOptions = options;
94  this->Modified();
95 }
96 
97 template<class TImage>
98 const std::vector<std::string>&
101 {
102  return m_OGRLayerCreationOptions;
103 }
104 
105 template<class TImage>
106 void
109 {
110  // Nothing that needs to be allocated for the outputs : the output is not meant to be used
111 }
112 
113 template<class TImage>
114 void
117 {
118 }
119 
120 template<class TImage>
121 void
124 {
125 }
126 
127 template<class TImage>
128 void
131 {
132  std::string projectionRefWkt = this->GetInput()->GetProjectionRef();
133  bool projectionInformationAvailable = !projectionRefWkt.empty();
134  OGRSpatialReference * oSRS = NULL;
135  if(projectionInformationAvailable)
136  {
137  oSRS = static_cast<OGRSpatialReference *>(OSRNewSpatialReference(projectionRefWkt.c_str()));
138  }
139 
140  OGRDataSourcePointerType ogrDS = this->GetOGRDataSource();
141  OGRLayerType outLayer = ogrDS->CreateLayer(m_LayerName, oSRS ,m_GeometryType, m_OGRLayerCreationOptions);
142  OGRFieldDefn field(m_FieldName.c_str(),m_FieldType);
143  outLayer.CreateField(field, true);
144 }
145 
146 
147 template<class TImage>
148 void
151 {
152 
153  if (this->GetStreamSize()[0]==0 && this->GetStreamSize()[1]==0)
154  {
155  this->m_StreamSize = this->GetInput()->GetRequestedRegion().GetSize();
156  }
157 
158  // call the processing function for this tile
159  OGRDataSourcePointerType currentTileVD = this->ProcessTile();
160  OGRLayerType srcLayer = currentTileVD->GetLayerChecked(0);
161 
162  OGRDataSourcePointerType ogrDS = this->GetOGRDataSource();
163  OGRLayerType dstLayer = ogrDS->GetLayersCount() == 1
164  ? ogrDS->GetLayer(0)
165  : ogrDS->GetLayer(m_LayerName);
166 
167 
168  //Copy features contained in the memory layer (srcLayer) in the output layer
169  itk::TimeProbe chrono;
170  chrono.Start();
171 
172  OGRErr err = dstLayer.ogr().StartTransaction();
173 
174  if (err != OGRERR_NONE)
175  {
176  itkExceptionMacro(<< "Unable to start transaction for OGR layer " << dstLayer.ogr().GetName() << ".");
177  }
178 
179  OGRLayerType::const_iterator featIt = srcLayer.begin();
180  for(; featIt!=srcLayer.end(); ++featIt)
181  {
182  OGRFeatureType dstFeature(dstLayer.GetLayerDefn());
183  dstFeature.SetFrom( *featIt, TRUE );
184  dstLayer.CreateFeature( dstFeature );
185  }
186 
187  err = dstLayer.ogr().CommitTransaction();
188 
189  if (err != OGRERR_NONE)
190  {
191  itkExceptionMacro(<< "Unable to commit transaction for OGR layer " << dstLayer.ogr().GetName() << ".");
192  }
193 
194  chrono.Stop();
195  otbMsgDebugMacro(<< "write ogr tile took " << chrono.GetTotal() << " sec");
196 }
197 
198 template<class TImage>
199 void
201 ::PrintSelf(std::ostream& os, itk::Indent indent) const
202 {
203  Superclass::PrintSelf(os, indent);
204 }
205 
206 } // end namespace otb
207 #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:58
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