OTB  9.0.0
Orfeo Toolbox
otbPersistentImageToOGRDataFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2011 Insight Software Consortium
3  * Copyright (C) 2005-2022 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_hxx
23 #define otbPersistentImageToOGRDataFilter_hxx
24 
26 #include "otbStopwatch.h"
27 #include <stdio.h>
28 #include "otbMacro.h"
29 #include "otbOGRHelpers.h"
30 
31 namespace otb
32 {
33 
34 template <class TImage>
36  : m_FieldName("DN"), m_LayerName("Layer"), m_GeometryType(wkbMultiPolygon), m_FieldType(OFTInteger)
37 {
38  this->SetNumberOfRequiredInputs(2);
39  this->SetNumberOfRequiredInputs(2);
40  m_StreamSize.Fill(0);
41 }
42 
43 template <class TImage>
45 {
46 }
47 
48 template <class TImage>
50 {
51  this->itk::ProcessObject::SetNthInput(1, ogrDS);
52 }
53 
54 template <class TImage>
56 {
57  return static_cast<OGRDataSourceType*>(this->itk::ProcessObject::GetInput(1));
58 }
59 
60 template <class TImage>
62 {
63  m_OGRLayerCreationOptions.push_back(option);
64  this->Modified();
65 }
66 
67 template <class TImage>
69 {
70  m_OGRLayerCreationOptions.clear();
71  this->Modified();
72 }
73 
74 template <class TImage>
75 void PersistentImageToOGRDataFilter<TImage>::SetOGRLayerCreationOptions(const std::vector<std::string>& options)
76 {
77  m_OGRLayerCreationOptions = options;
78  this->Modified();
79 }
80 
81 template <class TImage>
83 {
84  return m_OGRLayerCreationOptions;
85 }
86 
87 template <class TImage>
89 {
90  // Nothing that needs to be allocated for the outputs : the output is not meant to be used
91 }
92 
93 template <class TImage>
95 {
96 }
97 
98 template <class TImage>
100 {
101 }
102 
103 template <class TImage>
105 {
106  std::string projectionRefWkt = this->GetInput()->GetProjectionRef();
107  bool projectionInformationAvailable = !projectionRefWkt.empty();
108  OGRSpatialReference* oSRS = NULL;
109  if (projectionInformationAvailable)
110  {
111  oSRS = static_cast<OGRSpatialReference*>(OSRNewSpatialReference(projectionRefWkt.c_str()));
112  }
113 
114  OGRDataSourcePointerType ogrDS = this->GetOGRDataSource();
115  OGRLayerType outLayer = ogrDS->CreateLayer(m_LayerName, oSRS, m_GeometryType, m_OGRLayerCreationOptions);
116  OGRFieldDefn field(m_FieldName.c_str(), m_FieldType);
117  outLayer.CreateField(field, true);
118 }
119 
120 
121 template <class TImage>
123 {
124 
125  if (this->GetStreamSize()[0] == 0 && this->GetStreamSize()[1] == 0)
126  {
127  this->m_StreamSize = this->GetInput()->GetRequestedRegion().GetSize();
128  }
129 
130  // call the processing function for this tile
131  OGRDataSourcePointerType currentTileVD = this->ProcessTile();
132  OGRLayerType srcLayer = currentTileVD->GetLayerChecked(0);
133 
134  OGRDataSourcePointerType ogrDS = this->GetOGRDataSource();
135  OGRLayerType dstLayer = ogrDS->GetLayersCount() == 1 ? ogrDS->GetLayer(0) : ogrDS->GetLayer(m_LayerName);
136 
137 
138  // Copy features contained in the memory layer (srcLayer) in the output layer
140 
141  OGRErr err = dstLayer.ogr().StartTransaction();
142  if (err != OGRERR_NONE)
143  {
144  itkExceptionMacro(<< "Unable to start transaction for OGR layer " << dstLayer.ogr().GetName() << ".");
145  }
146 
147  OGRLayerType::const_iterator featIt = srcLayer.begin();
148  for (; featIt != srcLayer.end(); ++featIt)
149  {
150  OGRFeatureType dstFeature(dstLayer.GetLayerDefn());
151  dstFeature.SetFrom(*featIt, TRUE);
152  dstLayer.CreateFeature(dstFeature);
153  }
154 
155  err = dstLayer.ogr().CommitTransaction();
156 
157  if (err != OGRERR_NONE)
158  {
159  itkExceptionMacro(<< "Unable to commit transaction for OGR layer " << dstLayer.ogr().GetName() << ".");
160  }
161 
162  chrono.Stop();
163  otbMsgDebugMacro(<< "Writing OGR tile took " << chrono.GetElapsedMilliseconds() << " ms");
164 }
165 
166 template <class TImage>
167 void PersistentImageToOGRDataFilter<TImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
168 {
169  Superclass::PrintSelf(os, indent);
170 }
171 
172 } // end namespace otb
173 #endif
otb::PersistentImageToOGRDataFilter::Synthetize
virtual void Synthetize(void)
Definition: otbPersistentImageToOGRDataFilter.hxx:99
otb::PersistentImageToOGRDataFilter::Reset
virtual void Reset(void)
Definition: otbPersistentImageToOGRDataFilter.hxx:94
otb::PersistentImageToOGRDataFilter::GetOGRDataSource
OGRDataSourceType * GetOGRDataSource(void)
Definition: otbPersistentImageToOGRDataFilter.hxx:55
otb::PersistentImageToOGRDataFilter::PersistentImageToOGRDataFilter
PersistentImageToOGRDataFilter()
Definition: otbPersistentImageToOGRDataFilter.hxx:35
otb::PersistentImageToOGRDataFilter::Initialize
virtual void Initialize(void)
Definition: otbPersistentImageToOGRDataFilter.hxx:104
otb::PersistentImageToOGRDataFilter::ClearOGRLayerCreationOptions
void ClearOGRLayerCreationOptions()
Definition: otbPersistentImageToOGRDataFilter.hxx:68
otb::ogr::Feature::SetFrom
void SetFrom(Feature const &rhs, int *map, bool mustForgive=true)
Definition: otbOGRFeatureWrapper.hxx:51
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ogr::DataSource
Collection of geometric objects.
Definition: otbOGRDataSourceWrapper.h:83
otb::PersistentImageToOGRDataFilter::GenerateData
virtual void GenerateData()
Definition: otbPersistentImageToOGRDataFilter.hxx:122
otbMacro.h
otb::ogr::Layer::CreateFeature
void CreateFeature(Feature feature)
otb::PersistentImageToOGRDataFilter::~PersistentImageToOGRDataFilter
virtual ~PersistentImageToOGRDataFilter()
Definition: otbPersistentImageToOGRDataFilter.hxx:44
otb::PersistentImageToOGRDataFilter::m_StreamSize
SizeType m_StreamSize
Definition: otbPersistentImageToOGRDataFilter.h:157
otb::ogr::Layer::begin
const_iterator begin() const
Definition: otbOGRLayerWrapper.h:412
otb::ogr::Layer::end
const_iterator end() const
Definition: otbOGRLayerWrapper.h:418
otb::PersistentImageToOGRDataFilter::AllocateOutputs
void AllocateOutputs()
Definition: otbPersistentImageToOGRDataFilter.hxx:88
otb::Stopwatch
Stopwatch timer.
Definition: otbStopwatch.h:41
otb::PersistentImageToOGRDataFilter::GetOGRLayerCreationOptions
const std::vector< std::string > & GetOGRLayerCreationOptions(void)
Definition: otbPersistentImageToOGRDataFilter.hxx:82
otb::ogr::Layer::ogr
OGRLayer & ogr()
otb::ogr::Layer::feature_iter
Implementation class for Feature iterator. This iterator is a single pass iterator....
Definition: otbOGRLayerWrapper.h:348
otb::PersistentImageToOGRDataFilter::SetOGRLayerCreationOptions
void SetOGRLayerCreationOptions(const std::vector< std::string > &options)
Definition: otbPersistentImageToOGRDataFilter.hxx:75
otb::Stopwatch::StartNew
static Stopwatch StartNew()
otb::ogr::Layer::CreateField
void CreateField(FieldDefn const &field, bool bApproxOK=true)
otbMsgDebugMacro
#define otbMsgDebugMacro(x)
Definition: otbMacro.h:62
otb::Stopwatch::Stop
void Stop()
otbOGRHelpers.h
otb::ogr::Feature
Geometric object with descriptive fields.
Definition: otbOGRFeatureWrapper.h:63
otb::Stopwatch::GetElapsedMilliseconds
DurationType GetElapsedMilliseconds() const
otb::PersistentImageToOGRDataFilter::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const
Definition: otbPersistentImageToOGRDataFilter.hxx:167
otb::PersistentImageToOGRDataFilter::SetOGRDataSource
void SetOGRDataSource(OGRDataSourcePointerType ogrDS)
Definition: otbPersistentImageToOGRDataFilter.hxx:49
otb::ogr::Layer
Layer of geometric objects.
Definition: otbOGRLayerWrapper.h:80
otbStopwatch.h
otb::PersistentImageToOGRDataFilter::OGRDataSourcePointerType
OGRDataSourceType::Pointer OGRDataSourcePointerType
Definition: otbPersistentImageToOGRDataFilter.h:73
otb::PersistentImageToOGRDataFilter::AddOGRLayerCreationOption
void AddOGRLayerCreationOption(const std::string &option)
Definition: otbPersistentImageToOGRDataFilter.hxx:61
otb::ogr::Layer::GetLayerDefn
OGRFeatureDefn & GetLayerDefn() const
otbPersistentImageToOGRDataFilter.h