Orfeo Toolbox  4.2
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  dstLayer.ogr().StartTransaction();
177  OGRLayerType::const_iterator featIt = srcLayer.begin();
178  for(; featIt!=srcLayer.end(); ++featIt)
179  {
180  OGRFeatureType dstFeature(dstLayer.GetLayerDefn());
181  dstFeature.SetFrom( *featIt, TRUE );
182  dstLayer.CreateFeature( dstFeature );
183  }
184 
185  dstLayer.ogr().CommitTransaction();
186  chrono.Stop();
187  otbMsgDebugMacro(<< "write ogr tile took " << chrono.GetTotal() << " sec");
188 
189 }
190 
191 template<class TImage>
192 void
194 ::PrintSelf(std::ostream& os, itk::Indent indent) const
195 {
196  Superclass::PrintSelf(os, indent);
197 }
198 
199 } // end namespace otb
200 #endif

Generated at Sat Aug 16 2014 16:13:59 for Orfeo Toolbox with doxygen 1.8.3.1