Orfeo Toolbox  4.0
otbPersistentImageToOGRLayerFilter.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 __otbPersistentImageToOGRLayerFilter_txx
22 #define __otbPersistentImageToOGRLayerFilter_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 {
38  m_StreamSize.Fill(0);
39 }
40 
41 template<class TImage>
44 {
45 }
46 
47 template<class TImage>
48 void
50 ::SetOGRLayer(const OGRLayerType & ogrLayer)
51 {
52  m_OGRLayer = ogrLayer;
53  this->Modified();
54 }
55 
56 template<class TImage>
59 ::GetOGRLayer( void ) const
60 {
61  return m_OGRLayer;
62 }
63 
64 template<class TImage>
65 void
68 {
69  // Nothing that needs to be allocated for the outputs : the output is not meant to be used
70 }
71 
72 template<class TImage>
73 void
76 {
77 }
78 
79 template<class TImage>
80 void
83 {
84 }
85 
86 template<class TImage>
87 void
90 {
91  // Make sure input projection ref is set
92  const_cast<InputImageType*> (this->GetInput())->UpdateOutputInformation();
93 
94  // Ensure that spatial reference of the output layer matches with
95  // the spatial reference of the input image
96  OGRSpatialReference oSRS(this->GetInput()->GetProjectionRef().c_str());
97 
98  // when dealing with .shp OGRSPatialreference is morphed to ESRI WKT,
99  // which results in small difference in WKT with some projection reference
100  // so the comparison must be done with WKT and ESRI WKT Mantis #ref567
101  OGRSpatialReference oSRSESRI(this->GetInput()->GetProjectionRef().c_str());
102 
103  oSRSESRI.morphToESRI();
104  oSRSESRI.morphFromESRI();
105  if (m_OGRLayer.GetSpatialRef() && (!oSRS.IsSame(m_OGRLayer.GetSpatialRef())
106  && !oSRSESRI.IsSame(m_OGRLayer.GetSpatialRef())))
107  {
108  if ((oSRS.Validate() != OGRERR_NONE) && (oSRSESRI.Validate() != OGRERR_NONE))
109  {
110  itkExceptionMacro(<<"Input projection ref is not valid");
111  }
112  itkExceptionMacro(<<"Spatial reference of input image and target layer do not match! "<< std::endl
113  << "Input image : "<< this->GetInput()->GetProjectionRef()<< std::endl
114  << "Target layer : "<<m_OGRLayer.GetProjectionRef());
115  }
116 }
117 
118 template<class TImage>
119 void
122 {
123  if(!m_OGRLayer)
124  {
125  itkExceptionMacro(<<"Output OGRLayer is null.");
126  }
127 
128  if (this->GetStreamSize()[0]==0 && this->GetStreamSize()[1]==0)
129  {
130  this->m_StreamSize = this->GetInput()->GetRequestedRegion().GetSize();
131  }
132 
133  // call the processing function for this tile
134  OGRDataSourcePointerType currentTileVD = this->ProcessTile();
135  OGRLayerType srcLayer = currentTileVD->GetLayerChecked(0);
136 
137 
138  // Check spatial reference matches
139  if(srcLayer.GetSpatialRef() && m_OGRLayer.GetSpatialRef() && !srcLayer.GetSpatialRef()->IsSame(m_OGRLayer.GetSpatialRef()))
140  {
141  itkExceptionMacro(<<"Spatial reference of internal and target layers do not match!");
142  }
143 
144  //Copy features contained in the memory layer (srcLayer) in the output layer
145  itk::TimeProbe chrono;
146  chrono.Start();
147  m_OGRLayer.ogr().StartTransaction();
148  OGRLayerType::const_iterator featIt = srcLayer.begin();
149  for(; featIt!=srcLayer.end(); ++featIt)
150  {
151  OGRFeatureType dstFeature(m_OGRLayer.GetLayerDefn());
152  dstFeature.SetFrom( *featIt, TRUE );
153  m_OGRLayer.CreateFeature( dstFeature );
154  }
155 
156  m_OGRLayer.ogr().CommitTransaction();
157  chrono.Stop();
158  otbMsgDebugMacro(<< "write ogr tile took " << chrono.GetTotal() << " sec");
159 
160 }
161 
162 template<class TImage>
163 void
165 ::PrintSelf(std::ostream& os, itk::Indent indent) const
166 {
167  Superclass::PrintSelf(os, indent);
168 }
169 
170 } // end namespace otb
171 #endif

Generated at Sat Mar 8 2014 16:12:55 for Orfeo Toolbox with doxygen 1.8.3.1