OTB  9.0.0
Orfeo Toolbox
otbPersistentImageToOGRLayerFilter.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 otbPersistentImageToOGRLayerFilter_hxx
23 #define otbPersistentImageToOGRLayerFilter_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 {
37  m_StreamSize.Fill(0);
38 }
39 
40 template <class TImage>
42 {
43 }
44 
45 template <class TImage>
47 {
48  m_OGRLayer = ogrLayer;
49  this->Modified();
50 }
51 
52 template <class TImage>
54 {
55  return m_OGRLayer;
56 }
57 
58 template <class TImage>
60 {
61  // Nothing that needs to be allocated for the outputs : the output is not meant to be used
62 }
63 
64 template <class TImage>
66 {
67 }
68 
69 template <class TImage>
71 {
72 }
73 
74 template <class TImage>
76 {
77  // Make sure input projection ref is set
78  const_cast<InputImageType*>(this->GetInput())->UpdateOutputInformation();
79 
80  // Ensure that spatial reference of the output layer matches with
81  // the spatial reference of the input image
82  OGRSpatialReference oSRS(this->GetInput()->GetProjectionRef().c_str());
83 
84  // when dealing with .shp OGRSPatialreference is morphed to ESRI WKT,
85  // which results in small difference in WKT with some projection reference
86  // so the comparison must be done with WKT and ESRI WKT Mantis #ref567
87  OGRSpatialReference oSRSESRI(this->GetInput()->GetProjectionRef().c_str());
88 
89  oSRSESRI.morphToESRI();
90  oSRSESRI.morphFromESRI();
91 
92 #if GDAL_VERSION_NUM >= 3000000 // importFromWkt is const-correct in GDAL 3
93  // Use the same mapping strategy as the one in the datasource.
94  auto mappingStrategy = m_OGRLayer.GetSpatialRef()->GetAxisMappingStrategy();
95  oSRS.SetAxisMappingStrategy(mappingStrategy);
96  oSRSESRI.SetAxisMappingStrategy(mappingStrategy);
97 #endif
98 
99  if (m_OGRLayer.GetSpatialRef() && (!oSRS.IsSame(m_OGRLayer.GetSpatialRef()) && !oSRSESRI.IsSame(m_OGRLayer.GetSpatialRef())))
100  {
101  if ((oSRS.Validate() != OGRERR_NONE) && (oSRSESRI.Validate() != OGRERR_NONE))
102  {
103  itkExceptionMacro(<< "Input projection ref is not valid");
104  }
105  itkExceptionMacro(<< "Spatial reference of input image and target layer do not match! " << std::endl
106  << "Input image : " << this->GetInput()->GetProjectionRef() << std::endl
107  << "Target layer : " << m_OGRLayer.GetProjectionRef());
108  }
109 }
110 
111 template <class TImage>
113 {
114  if (!m_OGRLayer)
115  {
116  itkExceptionMacro(<< "Output OGRLayer is null.");
117  }
118 
119  if (this->GetStreamSize()[0] == 0 && this->GetStreamSize()[1] == 0)
120  {
121  this->m_StreamSize = this->GetInput()->GetRequestedRegion().GetSize();
122  }
123 
124  // call the processing function for this tile
125  OGRDataSourcePointerType currentTileVD = this->ProcessTile();
126  OGRLayerType srcLayer = currentTileVD->GetLayerChecked(0);
127 
128 
129  // Check spatial reference matches
130  if (srcLayer.GetSpatialRef() && m_OGRLayer.GetSpatialRef() && !srcLayer.GetSpatialRef()->IsSame(m_OGRLayer.GetSpatialRef()))
131  {
132  itkExceptionMacro(<< "Spatial reference of internal and target layers do not match!");
133  }
134 
135  // Copy features contained in the memory layer (srcLayer) in the output layer
137 
138  OGRErr err = m_OGRLayer.ogr().StartTransaction();
139  if (err != OGRERR_NONE)
140  {
141  itkExceptionMacro(<< "Unable to start transaction for OGR layer " << m_OGRLayer.ogr().GetName() << ".");
142  }
143 
144  OGRLayerType::const_iterator featIt = srcLayer.begin();
145  for (; featIt != srcLayer.end(); ++featIt)
146  {
147  OGRFeatureType dstFeature(m_OGRLayer.GetLayerDefn());
148  dstFeature.SetFrom(*featIt, TRUE);
149  m_OGRLayer.CreateFeature(dstFeature);
150  }
151 
152  err = m_OGRLayer.ogr().CommitTransaction();
153 
154  if (err != OGRERR_NONE)
155  {
156  itkExceptionMacro(<< "Unable to commit transaction for OGR layer " << m_OGRLayer.ogr().GetName() << ".");
157  }
158 
159  chrono.Stop();
160  otbMsgDebugMacro(<< "Writing OGR tile took " << chrono.GetElapsedMilliseconds() << " ms");
161 }
162 
163 template <class TImage>
164 void PersistentImageToOGRLayerFilter<TImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
165 {
166  Superclass::PrintSelf(os, indent);
167 }
168 
169 } // end namespace otb
170 #endif
otb::ogr::Layer::GetSpatialRef
OGRSpatialReference const * GetSpatialRef() const
otb::PersistentImageToOGRLayerFilter< TImageType >::OGRDataSourcePointerType
OGRDataSourceType::Pointer OGRDataSourcePointerType
Definition: otbPersistentImageToOGRLayerFilter.h:72
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
otbMacro.h
otb::PersistentImageToOGRLayerFilter::PersistentImageToOGRLayerFilter
PersistentImageToOGRLayerFilter()
Definition: otbPersistentImageToOGRLayerFilter.hxx:35
otbPersistentImageToOGRLayerFilter.h
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::Stopwatch
Stopwatch timer.
Definition: otbStopwatch.h:41
otb::ogr::Layer::feature_iter
Implementation class for Feature iterator. This iterator is a single pass iterator....
Definition: otbOGRLayerWrapper.h:348
otb::Stopwatch::StartNew
static Stopwatch StartNew()
otb::PersistentImageFilter< TImageType, TImageType >::InputImageType
TImageType InputImageType
Definition: otbPersistentImageFilter.h:57
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::ogr::Layer
Layer of geometric objects.
Definition: otbOGRLayerWrapper.h:80
otb::PersistentImageToOGRLayerFilter
Perform vectorization in a persistent way.
Definition: otbPersistentImageToOGRLayerFilter.h:51
otbStopwatch.h