Orfeo Toolbox  4.0
otbOGRIOHelper.cxx
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 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #include "otbOGRIOHelper.h"
19 
20 #include "ogrsf_frmts.h"
21 #include "otbOGR.h"
22 #include "itkTimeProbe.h"
23 
24 namespace otb
25 {
26 
27 void
29 ::ConvertGeometryToPointNode(const OGRGeometry * ogrGeometry, DataNodePointerType node) const
30 {
31  OGRPoint * ogrPoint = (OGRPoint *) ogrGeometry;
32 
33  if (ogrPoint == NULL)
34  {
35  itkGenericExceptionMacro(<< "Failed to convert OGRGeometry to OGRPoint");
36  }
37 
38  PointType otbPoint;
39  otbPoint.Fill(0);
40  otbPoint[0] = static_cast<DataNodeType::PrecisionType>(ogrPoint->getX());
41  otbPoint[1] = static_cast<DataNodeType::PrecisionType>(ogrPoint->getY());
42 
43  if (DataNodeType::Dimension > 2)
44  {
45  if (PointType::PointDimension != 3)
46  {
47  itkGenericExceptionMacro(<< "OTB vector data can't contain the OGR information (2D instead of 2.5D)");
48  }
49  otbPoint[2] = static_cast<DataNodeType::PrecisionType>(ogrPoint->getZ());
50  }
51 
52  node->SetPoint(otbPoint);
53 }
54 
55 
56 void
58 ::ConvertGeometryToLineNode(const OGRGeometry * ogrGeometry, DataNodePointerType node) const
59 {
60  OGRLineString * ogrLine = (OGRLineString *) ogrGeometry;
61 
62  if (ogrLine == NULL)
63  {
64  itkGenericExceptionMacro(<< "Failed to convert OGRGeometry to OGRLine");
65  }
66 
67  LinePointerType line = LineType::New();
68 
69  OGRPoint * ogrTmpPoint = (OGRPoint *) OGRGeometryFactory::createGeometry(wkbPoint);
70 
71  for (int pIndex = 0; pIndex < ogrLine->getNumPoints(); ++pIndex)
72  {
73 
74  ogrLine->getPoint(pIndex, ogrTmpPoint);
75 
76  LineType::VertexType vertex;
77 
78  vertex[0] = ogrTmpPoint->getX();
79  vertex[1] = ogrTmpPoint->getY();
80 
81  if (DataNodeType::Dimension > 2)
82  {
83  if (LineType::VertexType::PointDimension != 3)
84  {
85  itkGenericExceptionMacro(<< "OTB vector data can't contain the OGR information (2D instead of 2.5D)");
86  }
87  vertex[2] = ogrTmpPoint->getZ();
88  }
89 
90  line->AddVertex(vertex);
91  }
92  OGRGeometryFactory::destroyGeometry(ogrTmpPoint);
93 
94  node->SetLine(line);
95 }
96 
97 
98 inline void
100 ::ConvertGeometryToPolygonNode(const OGRGeometry * ogrGeometry, DataNodePointerType node) const
101 {
102  OGRPolygon * ogrPolygon = (OGRPolygon *) ogrGeometry;
103 
104  if (ogrPolygon == NULL)
105  {
106  itkGenericExceptionMacro(<< "Failed to convert OGRGeometry to OGRPolygon");
107  }
108 
109  OGRPoint * ogrTmpPoint = (OGRPoint *) OGRGeometryFactory::createGeometry(wkbPoint);
110  OGRLinearRing * ogrRing = ogrPolygon->getExteriorRing();
111 
112  PolygonPointerType extRing = PolygonType::New();
113 
114  for (int pIndex = 0; pIndex < ogrRing->getNumPoints(); ++pIndex)
115  {
116  ogrRing->getPoint(pIndex, ogrTmpPoint);
118  vertex[0] = ogrTmpPoint->getX();
119  vertex[1] = ogrTmpPoint->getY();
120 
121  if (DataNodeType::Dimension > 2)
122  {
123  if (PolygonType::VertexType::PointDimension != 3)
124  {
125  itkGenericExceptionMacro(<< "OTB vector data can't contain the OGR information (2D instead of 2.5D)");
126  }
127  vertex[2] = ogrTmpPoint->getZ();
128  }
129 
130  extRing->AddVertex(vertex);
131  }
132 
133  PolygonListPointerType intRings = PolygonListType::New();
134 
135  for (int intRingIndex = 0; intRingIndex < ogrPolygon->getNumInteriorRings(); ++intRingIndex)
136  {
137  PolygonPointerType ring = PolygonType::New();
138  ogrRing = ogrPolygon->getInteriorRing(intRingIndex);
139  for (int pIndex = 0; pIndex < ogrRing->getNumPoints(); ++pIndex)
140  {
141  ogrRing->getPoint(pIndex, ogrTmpPoint);
143 
144  vertex[0] = ogrTmpPoint->getX();
145  vertex[1] = ogrTmpPoint->getY();
146  if (DataNodeType::Dimension > 2)
147  {
148  if (PolygonType::VertexType::PointDimension != 3)
149  {
150  itkGenericExceptionMacro(<< "OTB vector data can't contain the OGR information (2D instead of 2.5D)");
151  }
152  vertex[2] = ogrTmpPoint->getZ();
153  }
154  ring->AddVertex(vertex);
155  }
156  intRings->PushBack(ring);
157  }
158 
159  OGRGeometryFactory::destroyGeometry(ogrTmpPoint);
160 
161  node->SetPolygonExteriorRing(extRing);
162  node->SetPolygonInteriorRings(intRings);
163 }
164 
165 
168 {
170 }
171 
172 
175 {}
176 
177 
178 void OGRIOHelper
179 ::ConvertOGRLayerToDataTreeNode(OGRLayer * layer, InternalTreeNodeType * documentPtr) const
180 {
182  OGRFeature * feature;
183 
184  layer->ResetReading();
185 
186  unsigned int counter = 0;
187  itk::TimeProbe chrono;
188 
189  while ((feature = layer->GetNextFeature()) != NULL)
190  {
191  chrono.Start();
192 
193  // A pointer to the current multi-geometry
195 
197  OGRGeometry * geometry = feature->GetGeometryRef();
198 
199  if (geometry == NULL)
200  {
201  OGRFeature::DestroyFeature(feature);
202  chrono.Stop();
203  ++counter;
204  continue;
205  }
206 
208  for (int fieldNum = 0; fieldNum < feature->GetFieldCount(); ++fieldNum)
209  {
210  if (feature->IsFieldSet(fieldNum))
211  {
212  kwl.AddField(feature->GetFieldDefnRef(fieldNum), feature->GetRawFieldRef(fieldNum));
213  }
214  }
215 
216  switch (geometry->getGeometryType())
217  {
218  case wkbPoint:
219  {
220  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
221  DataNodePointerType dataNode = DataNodeType::New();
222  ConvertGeometryToPointNode(geometry, dataNode);
223  newNode->Set(dataNode);
224  //Reach the DataNode inside the tree node
225  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
226  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
228  kwl);
229  documentPtr->AddChild(newNode);
230  break;
231  }
232  case wkbPoint25D:
233  {
234  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
235  DataNodePointerType dataNode = DataNodeType::New();
236  ConvertGeometryToPointNode(geometry, dataNode);
237  newNode->Set(dataNode);
238  //Reach the DataNode inside the tree node
239  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
240  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
242  kwl);
243  documentPtr->AddChild(newNode);
244  break;
245  }
246  case wkbLineString:
247  {
248  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
249  DataNodePointerType dataNode = DataNodeType::New();
250  ConvertGeometryToLineNode(geometry, dataNode);
251  newNode->Set(dataNode);
252  //Reach the DataNode inside the tree node
253  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
254  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
256  kwl);
257  documentPtr->AddChild(newNode);
258  break;
259  }
260  case wkbLineString25D:
261  {
262  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
263  DataNodePointerType dataNode = DataNodeType::New();
264  ConvertGeometryToLineNode(geometry, dataNode);
265  newNode->Set(dataNode);
266  //Reach the DataNode inside the tree node
267  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
268  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
270  kwl);
271  documentPtr->AddChild(newNode);
272  break;
273  }
274  case wkbPolygon:
275  {
276  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
277  DataNodePointerType dataNode = DataNodeType::New();
278  ConvertGeometryToPolygonNode(geometry, dataNode);
279  newNode->Set(dataNode);
280  //Reach the DataNode inside the tree node
281  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
282  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
284  kwl);
285  documentPtr->AddChild(newNode);
286  break;
287  }
288  case wkbPolygon25D:
289  {
290  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
291  DataNodePointerType dataNode = DataNodeType::New();
292  ConvertGeometryToPolygonNode(geometry, dataNode);
293  newNode->Set(dataNode);
294  //Reach the DataNode inside the tree node
295  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
296  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
298  kwl);
299  documentPtr->AddChild(newNode);
300  break;
301  }
302  case wkbMultiPoint:
303  {
304 
305  DataNodePointerType multi = DataNodeType::New();
306  multi->SetNodeType(FEATURE_MULTIPOINT);
307  multiPtr = InternalTreeNodeType::New();
308  multiPtr->Set(multi);
309  documentPtr->AddChild(multiPtr);
310 
311  OGRMultiPoint * ogrMulti = (OGRMultiPoint *) geometry;
312 
313  for (int geoIndex = 0; geoIndex < ogrMulti->getNumGeometries(); ++geoIndex)
314  {
315  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
316  DataNodePointerType dataNode = DataNodeType::New();
317  ConvertGeometryToPointNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
318  newNode->Set(dataNode);
319  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
320  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
322  kwl);
323  multiPtr->AddChild(newNode);
324  }
325  break;
326  }
327  case wkbMultiPoint25D:
328  {
329  DataNodePointerType multi = DataNodeType::New();
330  multi->SetNodeType(FEATURE_MULTIPOINT);
331 
332  multiPtr = InternalTreeNodeType::New();
333  multiPtr->Set(multi);
334  documentPtr->AddChild(multiPtr);
335 
336  OGRMultiPoint * ogrMulti = (OGRMultiPoint *) geometry;
337 
338  for (int geoIndex = 0; geoIndex < ogrMulti->getNumGeometries(); ++geoIndex)
339  {
340  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
341  DataNodePointerType dataNode = DataNodeType::New();
342  ConvertGeometryToPointNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
343  newNode->Set(dataNode);
344  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
345  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
347  kwl);
348  multiPtr->AddChild(newNode);
349  }
350  break;
351  }
352  case wkbMultiLineString:
353  {
354  DataNodePointerType multi = DataNodeType::New();
355  multi->SetNodeType(FEATURE_MULTILINE);
356 
357  multiPtr = InternalTreeNodeType::New();
358  multiPtr->Set(multi);
359  documentPtr->AddChild(multiPtr);
360 
361  OGRMultiLineString * ogrMulti = (OGRMultiLineString *) geometry;
362 
363  for (int geoIndex = 0; geoIndex < ogrMulti->getNumGeometries(); ++geoIndex)
364  {
365  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
366  DataNodePointerType dataNode = DataNodeType::New();
367  ConvertGeometryToLineNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
368  newNode->Set(dataNode);
369  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
370  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
372  kwl);
373  multiPtr->AddChild(newNode);
374  }
375  break;
376  }
377  case wkbMultiLineString25D:
378  {
379  DataNodePointerType multi = DataNodeType::New();
380  multi->SetNodeType(FEATURE_MULTILINE);
381 
382  multiPtr = InternalTreeNodeType::New();
383  multiPtr->Set(multi);
384  documentPtr->AddChild(multiPtr);
385 
386  OGRMultiLineString * ogrMulti = (OGRMultiLineString *) geometry;
387 
388  for (int geoIndex = 0; geoIndex < ogrMulti->getNumGeometries(); ++geoIndex)
389  {
390  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
391  DataNodePointerType dataNode = DataNodeType::New();
392  ConvertGeometryToLineNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
393  newNode->Set(dataNode);
394  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
395  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
397  kwl);
398  multiPtr->AddChild(newNode);
399  }
400  break;
401  }
402  case wkbMultiPolygon:
403  {
404  DataNodePointerType multi = DataNodeType::New();
405  multi->SetNodeType(FEATURE_MULTIPOLYGON);
406 
407  multiPtr = InternalTreeNodeType::New();
408  multiPtr->Set(multi);
409  documentPtr->AddChild(multiPtr);
410 
411  OGRMultiPolygon * ogrMulti = (OGRMultiPolygon *) geometry;
412 
413  for (int geoIndex = 0; geoIndex < ogrMulti->getNumGeometries(); ++geoIndex)
414  {
415  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
416  DataNodePointerType dataNode = DataNodeType::New();
417  ConvertGeometryToPolygonNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
418  newNode->Set(dataNode);
419  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
420  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
422  kwl);
423  multiPtr->AddChild(newNode);
424  }
425  break;
426  }
427  case wkbMultiPolygon25D:
428  {
429  DataNodePointerType multi = DataNodeType::New();
430  multi->SetNodeType(FEATURE_MULTIPOLYGON);
431 
432  multiPtr = InternalTreeNodeType::New();
433  multiPtr->Set(multi);
434  documentPtr->AddChild(multiPtr);
435 
436  OGRMultiPolygon * ogrMulti = (OGRMultiPolygon *) geometry;
437 
438  for (int geoIndex = 0; geoIndex < ogrMulti->getNumGeometries(); ++geoIndex)
439  {
440  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
441  DataNodePointerType dataNode = DataNodeType::New();
442  ConvertGeometryToPolygonNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
443  newNode->Set(dataNode);
444  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
445  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
447  kwl);
448  multiPtr->AddChild(newNode);
449  }
450  break;
451  }
452  case wkbGeometryCollection:
453  {
454  DataNodePointerType multi = DataNodeType::New();
455  multi->SetNodeType(FEATURE_COLLECTION);
456 
457  multiPtr = InternalTreeNodeType::New();
458  multiPtr->Set(multi);
459  documentPtr->AddChild(multiPtr);
460 
461  OGRGeometryCollection * ogrMulti = (OGRGeometryCollection *) geometry;
462 
463  for (int geoIndex = 0; geoIndex < ogrMulti->getNumGeometries(); ++geoIndex)
464  {
465  switch (ogrMulti->getGeometryRef(geoIndex)->getGeometryType())
466  {
467  case wkbPoint:
468  {
469  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
470  DataNodePointerType dataNode = DataNodeType::New();
471  ConvertGeometryToPointNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
472  newNode->Set(dataNode);
473  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
474  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
476  kwl);
477  multiPtr->AddChild(newNode);
478  break;
479  }
480  case wkbPoint25D:
481  {
482  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
483  DataNodePointerType dataNode = DataNodeType::New();
484  ConvertGeometryToPointNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
485  newNode->Set(dataNode);
486  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
487  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
489  kwl);
490  multiPtr->AddChild(newNode);
491  break;
492  }
493  case wkbLineString:
494  {
495  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
496  DataNodePointerType dataNode = DataNodeType::New();
497  ConvertGeometryToLineNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
498  newNode->Set(dataNode);
499  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
500  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
502  kwl);
503  multiPtr->AddChild(newNode);
504  break;
505  }
506  case wkbLineString25D:
507  {
508  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
509  DataNodePointerType dataNode = DataNodeType::New();
510  ConvertGeometryToLineNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
511  newNode->Set(dataNode);
512  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
513  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
515  kwl);
516  multiPtr->AddChild(newNode);
517  break;
518  }
519  case wkbPolygon:
520  {
521  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
522  DataNodePointerType dataNode = DataNodeType::New();
523  ConvertGeometryToPolygonNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
524  newNode->Set(dataNode);
525  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
526  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
528  kwl);
529  multiPtr->AddChild(newNode);
530  break;
531  }
532  case wkbPolygon25D:
533  {
534  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
535  DataNodePointerType dataNode = DataNodeType::New();
536  ConvertGeometryToPolygonNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
537  newNode->Set(dataNode);
538  itk::MetaDataDictionary& dict = newNode->Get()->GetMetaDataDictionary();
539  itk::EncapsulateMetaData<VectorDataKeywordlist>(dict,
541  kwl);
542  multiPtr->AddChild(newNode);
543  break;
544  }
545  default:
546  {
547  otbWarningMacro( << "Geometry type not found: " << ogrMulti->getGeometryRef(geoIndex)->getGeometryType());
548  break;
549  }
550  }
551  }
552  break;
553  }
554  case wkbGeometryCollection25D:
555  {
556  DataNodePointerType multi = DataNodeType::New();
557  multi->SetNodeType(FEATURE_COLLECTION);
558 
559  multiPtr = InternalTreeNodeType::New();
560  multiPtr->Set(multi);
561  documentPtr->AddChild(multiPtr);
562 
563  OGRGeometryCollection * ogrMulti = (OGRGeometryCollection *) geometry;
564 
565  for (int geoIndex = 0; geoIndex < ogrMulti->getNumGeometries(); ++geoIndex)
566  {
567  switch (ogrMulti->getGeometryRef(geoIndex)->getGeometryType())
568  {
569  case wkbPoint:
570  {
571  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
572  DataNodePointerType dataNode = DataNodeType::New();
573  ConvertGeometryToPointNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
574  newNode->Set(dataNode);
575  multiPtr->AddChild(newNode);
576  break;
577  }
578  case wkbPoint25D:
579  {
580  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
581  DataNodePointerType dataNode = DataNodeType::New();
582  ConvertGeometryToPointNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
583  newNode->Set(dataNode);
584  multiPtr->AddChild(newNode);
585  break;
586  }
587  case wkbLineString:
588  {
589  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
590  DataNodePointerType dataNode = DataNodeType::New();
591  ConvertGeometryToLineNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
592  newNode->Set(dataNode);
593  multiPtr->AddChild(newNode);
594  break;
595  }
596  case wkbLineString25D:
597  {
598  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
599  DataNodePointerType dataNode = DataNodeType::New();
600  ConvertGeometryToLineNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
601  newNode->Set(dataNode);
602  multiPtr->AddChild(newNode);
603  break;
604  }
605  case wkbPolygon:
606  {
607  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
608  DataNodePointerType dataNode = DataNodeType::New();
609  ConvertGeometryToPolygonNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
610  newNode->Set(dataNode);
611  multiPtr->AddChild(newNode);
612  break;
613  }
614  case wkbPolygon25D:
615  {
616  InternalTreeNodeType::Pointer newNode = InternalTreeNodeType::New();
617  DataNodePointerType dataNode = DataNodeType::New();
618  ConvertGeometryToPolygonNode(ogrMulti->getGeometryRef(geoIndex), dataNode);
619  newNode->Set(dataNode);
620  multiPtr->AddChild(newNode);
621  break;
622  }
623  default:
624  {
625  otbWarningMacro( << "Geometry type not found: " << ogrMulti->getGeometryRef(geoIndex)->getGeometryType());
626  break;
627  }
628  }
629  }
630  break;
631  }
632  default:
633  {
634  otbWarningMacro("Geometry not handled: " << geometry->getGeometryName());
635  break;
636  }
637  }
638 
639 
640  OGRFeature::DestroyFeature(feature);
641  chrono.Stop();
642  ++counter;
643  } //end While feature
645  << layer->GetFeatureCount() << " features read, average insertion time " << chrono.GetMean() << " s");
646 }
647 
648 
649 unsigned int OGRIOHelper
651  OGRDataSource * m_DataSource,
652  OGRGeometryCollection * ogrCollection,
653  OGRLayer * ogrCurrentLayer,
654  OGRSpatialReference * oSRS)
655 {
656  unsigned int kept = 0;
657  bool fieldsAddedToOGRLayer = false;
658  // Get the children list from the input node
659  typedef InternalTreeNodeType::ChildrenListType ChildrenListType;
660  ChildrenListType children = source->GetChildrenList();
661 
662  // For each child
663  for (ChildrenListType::iterator it = children.begin(); it != children.end(); ++it)
664  {
665  DataNodePointerType dataNode = (*it)->Get();
666  //otbMsgDevMacro(<< "Type of node " << dataNode->GetNodeType() << " id " << dataNode->GetNodeId());
667  ++kept;
668 
669  // Get the kwl
671  itk::ExposeMetaData<VectorDataKeywordlist>(dataNode->GetMetaDataDictionary(),
673  kwl);
674 
675  // Create the field once
676  if (ogrCurrentLayer != NULL && !fieldsAddedToOGRLayer)
677  {
678  // Take into account the fields stored in the
679  // vectordatakeywordlist
680  for (unsigned int fieldIdx = 0; fieldIdx < kwl.GetNumberOfFields(); fieldIdx++)
681  {
682  if ( std::string(kwl.GetNthField(fieldIdx).first->GetNameRef()) != "FID" )
683  {
684  otbMsgDevMacro(<< " CreateField '" << kwl.GetNthField(fieldIdx).first->GetNameRef() << "'");
685  if (ogrCurrentLayer->CreateField(kwl.GetNthField(fieldIdx).first) != OGRERR_NONE )
686  {
687  itkExceptionMacro(<< "Failed to create Field "<<kwl.GetNthField(fieldIdx).first->GetNameRef());
688  }
689  }
690  else
691  {
692  otbMsgDevMacro(<< "WARNING: Skipping OGR field 'FID'");
693  }
694  }
695  // While no feature are added to the layer (in case of multiple
696  // folders added to the document) continue test for adding
697  // fields to the ogrCurrentLayer
698  if ( kwl.GetNumberOfFields() > 0 || ogrCurrentLayer->GetFeatureCount() > 0)
699  {
700  fieldsAddedToOGRLayer = true;
701  }
702  }
703 
704  switch (dataNode->GetNodeType())
705  {
706  case ROOT:
707  {
708  break;
709  }
710  case DOCUMENT:
711  {
712  ogrCurrentLayer = m_DataSource->CreateLayer(dataNode->GetNodeId(), oSRS, wkbUnknown, NULL);
713  if (ogrCurrentLayer == NULL)
714  {
715  //itkExceptionMacro(<<"Failed to create layer "<<dataNode->GetNodeId());
716  std::cout << "Failed to create layer " << dataNode->GetNodeId() << std::endl;
717  }
718  else
719  {
720  // New OGRLayer, set the flag to false
721  fieldsAddedToOGRLayer = false;
722  }
723  ProcessNodeWrite(*it, m_DataSource, ogrCollection, ogrCurrentLayer, oSRS);
724  break;
725  }
726  case FOLDER:
727  {
728  ProcessNodeWrite(*it, m_DataSource, ogrCollection, ogrCurrentLayer, oSRS);
729  break;
730  }
731  case FEATURE_POINT:
732  {
733  //Build the ogrObject
734  OGRPoint ogrPoint;
735  ogrPoint.setX(dataNode->GetPoint()[0]);
736  ogrPoint.setY(dataNode->GetPoint()[1]);
737 
738  if (DataNodeType::Dimension > 2)
739  {
740  ogrPoint.setZ(dataNode->GetPoint()[2]);
741  }
742 
743  //Save it in the structure
744  if (ogrCollection == NULL)
745  {
746  OGRFeature *ogrFeature;
747  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
748 
749  // Add the fields to the features
750  for (unsigned int i = 0; i < kwl.GetNumberOfFields(); ++i)
751  {
752  // Get the key of the Nth OGRFieldRefn
753  const char * key = kwl.GetNthField(i).first->GetNameRef();
754 
755  if (std::string(key) != "FID")
756  {
757  // Edit the value of the field and add it to the current feature
758  ogrFeature->SetField(ogrFeature->GetFieldIndex(key) , kwl.GetFieldAsString(key).c_str());
759  }
760  }
761 
762 // ogrFeature->SetField("Name", dataNode->GetNodeId());
763  ogrFeature->SetGeometry(&ogrPoint);
764 
765  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
766  {
767  itkExceptionMacro(<< "Failed to create feature in shapefile.");
768  //std::cout << "Failed to create feature in shapefile."
769  }
770 
771  OGRFeature::DestroyFeature(ogrFeature);
772  }
773  else
774  {
775  ogrCollection->addGeometry(&ogrPoint);
776  }
777 
778  break;
779  }
780  case FEATURE_LINE:
781  {
782  //Build the ogrObject
783  OGRLineString ogrLine;
784  VertexListConstPointerType vertexList = dataNode->GetLine()->GetVertexList();
785 
786  VertexListType::ConstIterator vIt = vertexList->Begin();
787 
788  while (vIt != vertexList->End())
789  {
790  OGRPoint ogrPoint;
791  ogrPoint.setX(vIt.Value()[0]);
792  ogrPoint.setY(vIt.Value()[1]);
793  if (DataNodeType::Dimension > 2)
794  {
795  ogrPoint.setZ(vIt.Value()[2]);
796  }
797  ogrLine.addPoint(&ogrPoint);
798  ++vIt;
799  }
800 
801  //Save it in the structure
802  if (ogrCollection == NULL)
803  {
804  OGRFeature *ogrFeature;
805  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
806 
807  // Add the fields to the features
808  for (unsigned int i = 0; i < kwl.GetNumberOfFields(); ++i)
809  {
810  // Get the key of the Nth OGRFieldRefn
811  const char * key = kwl.GetNthField(i).first->GetNameRef();
812  // Edit the value of the field and add it to the current feature
813  ogrFeature->SetField(ogrFeature->GetFieldIndex(key) , kwl.GetFieldAsString(key).c_str());
814  }
815 
816 // ogrFeature->SetField("Name", dataNode->GetNodeId());
817  ogrFeature->SetGeometry(&ogrLine);
818 
819  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
820  {
821  itkExceptionMacro(<< "Failed to create feature in shapefile.");
822  }
823 
824  OGRFeature::DestroyFeature(ogrFeature);
825 
826  }
827  else
828  {
829  ogrCollection->addGeometry(&ogrLine);
830  }
831 
832  break;
833  }
834  case FEATURE_POLYGON:
835  {
836 
837  //Build the ogrObject
838  OGRPolygon * ogrPolygon = (OGRPolygon *) OGRGeometryFactory::createGeometry(wkbPolygon);
839  OGRLinearRing * ogrExternalRing = (OGRLinearRing *) OGRGeometryFactory::createGeometry(wkbLinearRing);
840  VertexListConstPointerType vertexList = dataNode->GetPolygonExteriorRing()->GetVertexList();
841 
842  VertexListType::ConstIterator vIt = vertexList->Begin();
843 
844  while (vIt != vertexList->End())
845  {
846  OGRPoint ogrPoint;
847  ogrPoint.setX(vIt.Value()[0]);
848  ogrPoint.setY(vIt.Value()[1]);
849  if (DataNodeType::Dimension > 2)
850  {
851  ogrPoint.setZ(vIt.Value()[2]);
852  }
853 
854  ogrExternalRing->addPoint(&ogrPoint);
855  ++vIt;
856  }
857  ogrPolygon->addRing(ogrExternalRing);
858  // Close the polygon
859  ogrPolygon->closeRings();
860  OGRGeometryFactory::destroyGeometry(ogrExternalRing);
861 
862  // Retrieving internal rings as well
863  for (PolygonListType::Iterator pIt = dataNode->GetPolygonInteriorRings()->Begin();
864  pIt != dataNode->GetPolygonInteriorRings()->End(); ++pIt)
865  {
866  OGRLinearRing * ogrInternalRing = (OGRLinearRing *) OGRGeometryFactory::createGeometry(wkbLinearRing);
867  vertexList = pIt.Get()->GetVertexList();
868  vIt = vertexList->Begin();
869 
870  while (vIt != vertexList->End())
871  {
872  OGRPoint ogrPoint;
873  ogrPoint.setX(vIt.Value()[0]);
874  ogrPoint.setY(vIt.Value()[1]);
875  if (DataNodeType::Dimension > 2)
876  {
877  ogrPoint.setZ(vIt.Value()[2]);
878  }
879  ogrInternalRing->addPoint(&ogrPoint);
880  ++vIt;
881  }
882  ogrPolygon->addRing(ogrInternalRing);
883  OGRGeometryFactory::destroyGeometry(ogrInternalRing);
884  }
885 
886  //Save it in the structure
887  if (ogrCollection == NULL)
888  {
889  OGRFeature *ogrFeature;
890  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
891 
892  // Add the fields to the features
893  for (unsigned int i = 0; i < kwl.GetNumberOfFields(); ++i)
894  {
895  // Get the key of the Nth OGRFieldRefn
896  const char * key = kwl.GetNthField(i).first->GetNameRef();
897  // Edit the value of the field and add it to the current feature
898  ogrFeature->SetField(ogrFeature->GetFieldIndex(key) , kwl.GetFieldAsString(key).c_str());
899  }
900 
901  ogrFeature->SetGeometry(ogrPolygon);
902 
903  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
904  {
905  itkExceptionMacro(<< "Failed to create feature in shapefile.");
906  }
907 
908  OGRFeature::DestroyFeature(ogrFeature);
909  }
910  else
911  {
912  ogrCollection->addGeometry(ogrPolygon);
913  }
914 
915  OGRGeometryFactory::destroyGeometry(ogrPolygon);
916  break;
917  }
918  case FEATURE_MULTIPOINT:
919  {
920  if (ogrCollection != NULL)
921  {
922  itkExceptionMacro(<< "Problem while creating multipoint.");
923  }
924 
925  OGRMultiPoint* ogrMultiPoint = (OGRMultiPoint*) OGRGeometryFactory::createGeometry(wkbMultiPoint);
926  OGRFeature * ogrFeature;
927 
928  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
929 // ogrFeature->SetField("Name", dataNode->GetNodeId());
930  ogrFeature->GetDefnRef()->SetGeomType(wkbMultiPoint);
931  ogrFeature->SetGeometry(ogrMultiPoint);
932 
933  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
934  {
935  itkExceptionMacro(<< "Failed to create feature in shapefile.");
936  }
937 
938  ProcessNodeWrite(*it, m_DataSource, ogrCollection, ogrCurrentLayer, oSRS);
939  break;
940  }
941  case FEATURE_MULTILINE:
942  {
943  if (ogrCollection != NULL)
944  {
945  itkExceptionMacro(<< "Problem while creating multiline.");
946  }
947 
948  // Instanciate a new ogrMultiLineString feature
949  OGRMultiLineString* ogrMultiLineString = (OGRMultiLineString*) OGRGeometryFactory::createGeometry(
950  wkbMultiLineString);
951 
952  OGRFeature *ogrFeature;
953 
954  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
955 // ogrFeature->SetField("Name", dataNode->GetNodeId());
956  ogrFeature->GetDefnRef()->SetGeomType(wkbMultiLineString);
957  ogrFeature->SetGeometry(ogrMultiLineString);
958 
959  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
960  {
961  itkExceptionMacro(<< "Failed to create feature in shapefile.");
962  }
963  ProcessNodeWrite(*it, m_DataSource, ogrCollection, ogrCurrentLayer, oSRS);
964  break;
965  }
967  {
968  if (ogrCollection != NULL)
969  {
970  itkExceptionMacro(<< "Problem while creating multipolygon.");
971  }
972 
973  // Instanciate a new multipolygon feature
974  OGRMultiPolygon* ogrMultiPolygon = (OGRMultiPolygon*) OGRGeometryFactory::createGeometry(wkbMultiPolygon);
975  OGRFeature * ogrFeature;
976 
977  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
978 // ogrFeature->SetField("Name", dataNode->GetNodeId());
979  ogrFeature->GetDefnRef()->SetGeomType(wkbMultiPolygon);
980  ogrFeature->SetGeometry(ogrMultiPolygon);
981 
982  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
983  {
984  itkExceptionMacro(<< "Failed to create feature in shapefile.");
985  }
986  ProcessNodeWrite(*it, m_DataSource, ogrCollection, ogrCurrentLayer, oSRS);
987  break;
988  }
989  case FEATURE_COLLECTION:
990  {
991  if (ogrCollection != NULL)
992  {
993  itkExceptionMacro(<< "Problem while creating collection.");
994  }
995 
996  OGRGeometryCollection* ogrCollectionGeometry = (OGRGeometryCollection*) OGRGeometryFactory::createGeometry(
997  wkbGeometryCollection);
998 
999  OGRFeature *ogrFeature;
1000 
1001  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
1002 // ogrFeature->SetField("Name", dataNode->GetNodeId());
1003  ogrFeature->GetDefnRef()->SetGeomType(wkbGeometryCollection);
1004  ogrFeature->SetGeometry(ogrCollectionGeometry);
1005 
1006  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
1007  {
1008  itkExceptionMacro(<< "Failed to create feature in shapefile.");
1009  }
1010 
1011  ProcessNodeWrite(*it, m_DataSource, ogrCollection, ogrCurrentLayer, oSRS);
1012  break;
1013  }
1014  }
1015  }
1016 
1017  return kept;
1018 
1019 }
1020 
1025 std::vector<OGRLayer*> OGRIOHelper
1027  OGRDataSource * inMemoryDataSource,
1028  OGRLayer* ogrCurrentLayer,
1029  OGRSpatialReference * oSRS)
1030 {
1031 
1032  // Create the in memory datasource if NULL
1033  if (inMemoryDataSource == NULL)
1034  {
1035  const char * driverName = "Memory";
1036  OGRSFDriver * ogrDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driverName);
1037  inMemoryDataSource = ogrDriver->CreateDataSource("tempDataSource",NULL);
1038  }
1039 
1040  std::vector<OGRLayer*> ogrLayerVector;
1041  //unsigned int kept = 0;
1042  bool fieldsAddedToOGRLayer = false;
1043  // Get the children list from the input node
1044  typedef InternalTreeNodeType::ChildrenListType ChildrenListType;
1045  ChildrenListType children = source->GetChildrenList();
1046 
1047  // For each child
1048  for (ChildrenListType::iterator it = children.begin(); it != children.end(); ++it)
1049  {
1050  DataNodePointerType dataNode = (*it)->Get();
1051 
1052  // Get the kwl
1054  itk::ExposeMetaData<VectorDataKeywordlist>(dataNode->GetMetaDataDictionary(),
1056  kwl);
1057 
1058  // Create the field once
1059  if (ogrCurrentLayer != NULL && !fieldsAddedToOGRLayer)
1060  {
1061  // Take into account the fields stored in the
1062  // vectordatakeywordlist
1063  for (unsigned int fieldIdx = 0; fieldIdx < kwl.GetNumberOfFields(); fieldIdx++)
1064  {
1065  if ( std::string(kwl.GetNthField(fieldIdx).first->GetNameRef()) != "FID" )
1066  {
1067  otbMsgDevMacro(<< " CreateField '" << kwl.GetNthField(fieldIdx).first->GetNameRef() << "'");
1068  if (ogrCurrentLayer->CreateField(kwl.GetNthField(fieldIdx).first) != OGRERR_NONE )
1069  {
1070  itkExceptionMacro(<< "Failed to create Field "<<kwl.GetNthField(fieldIdx).first->GetNameRef());
1071  }
1072  }
1073  else
1074  {
1075  otbMsgDevMacro(<< "WARNING: Skipping OGR field 'FID'");
1076  }
1077  }
1078  fieldsAddedToOGRLayer = true;
1079  }
1080 
1081  switch (dataNode->GetNodeType())
1082  {
1083  case ROOT:
1084  {
1085  break;
1086  }
1087  case DOCUMENT:
1088  {
1089  ogrCurrentLayer = inMemoryDataSource->CreateLayer(dataNode->GetNodeId(), oSRS,
1090  wkbUnknown, NULL);
1091  if (ogrCurrentLayer == NULL)
1092  {
1093  std::cout << "Failed to create layer " << dataNode->GetNodeId() << std::endl;
1094  }
1095  else
1096  {
1097  // New OGRLayer, set the flag to false
1098  fieldsAddedToOGRLayer = false;
1099  }
1100  ogrLayerVector.push_back(ogrCurrentLayer);
1101  ConvertDataTreeNodeToOGRLayers(*it, inMemoryDataSource, ogrCurrentLayer, oSRS);
1102  break;
1103  }
1104  case FOLDER:
1105  {
1106  ConvertDataTreeNodeToOGRLayers(*it, inMemoryDataSource, ogrCurrentLayer, oSRS);
1107  break;
1108  }
1109  case FEATURE_POINT:
1110  {
1111  //Build the ogrObject
1112  OGRPoint ogrPoint;
1113  ogrPoint.setX(dataNode->GetPoint()[0]);
1114  ogrPoint.setY(dataNode->GetPoint()[1]);
1115 
1116  if (DataNodeType::Dimension > 2)
1117  {
1118  ogrPoint.setZ(dataNode->GetPoint()[2]);
1119  }
1120 
1121  //Save it in the structure
1122  OGRFeature *ogrFeature;
1123  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
1124 
1125  // Add the fields to the features
1126  for (unsigned int i = 0; i < kwl.GetNumberOfFields(); ++i)
1127  {
1128  // Get the key of the Nth OGRFieldRefn
1129  const char * key = kwl.GetNthField(i).first->GetNameRef();
1130 
1131  if (std::string(key) != "FID")
1132  {
1133  // Edit the value of the field and add it to the current feature
1134  ogrFeature->SetField(ogrFeature->GetFieldIndex(key) , kwl.GetFieldAsString(key).c_str());
1135  }
1136  }
1137  ogrFeature->SetGeometry(&ogrPoint);
1138 
1139  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
1140  {
1141  itkExceptionMacro(<< "Failed to create feature in shapefile.");
1142  }
1143 
1144  OGRFeature::DestroyFeature(ogrFeature);
1145  break;
1146  }
1147  case FEATURE_LINE:
1148  {
1149  //Build the ogrObject
1150  OGRLineString ogrLine;
1151  VertexListConstPointerType vertexList = dataNode->GetLine()->GetVertexList();
1152 
1153  VertexListType::ConstIterator vIt = vertexList->Begin();
1154 
1155  while (vIt != vertexList->End())
1156  {
1157  OGRPoint ogrPoint;
1158  ogrPoint.setX(vIt.Value()[0]);
1159  ogrPoint.setY(vIt.Value()[1]);
1160  if (DataNodeType::Dimension > 2)
1161  {
1162  ogrPoint.setZ(vIt.Value()[2]);
1163  }
1164  ogrLine.addPoint(&ogrPoint);
1165  ++vIt;
1166  }
1167 
1168  //Save it in the structure
1169  OGRFeature *ogrFeature;
1170  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
1171 
1172  // Add the fields to the features
1173  for (unsigned int i = 0; i < kwl.GetNumberOfFields(); ++i)
1174  {
1175  // Get the key of the Nth OGRFieldRefn
1176  const char * key = kwl.GetNthField(i).first->GetNameRef();
1177  // Edit the value of the field and add it to the current feature
1178  ogrFeature->SetField(ogrFeature->GetFieldIndex(key) , kwl.GetFieldAsString(key).c_str());
1179  }
1180  ogrFeature->SetGeometry(&ogrLine);
1181 
1182  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
1183  {
1184  itkExceptionMacro(<< "Failed to create feature in shapefile.");
1185  }
1186 
1187  OGRFeature::DestroyFeature(ogrFeature);
1188  break;
1189  }
1190  case FEATURE_POLYGON:
1191  {
1192  //Build the ogrObject
1193  OGRPolygon * ogrPolygon = (OGRPolygon *) OGRGeometryFactory::createGeometry(wkbPolygon);
1194  OGRLinearRing * ogrExternalRing = (OGRLinearRing *) OGRGeometryFactory::createGeometry(wkbLinearRing);
1195  VertexListConstPointerType vertexList = dataNode->GetPolygonExteriorRing()->GetVertexList();
1196 
1197  VertexListType::ConstIterator vIt = vertexList->Begin();
1198 
1199  while (vIt != vertexList->End())
1200  {
1201  OGRPoint ogrPoint;
1202  ogrPoint.setX(vIt.Value()[0]);
1203  ogrPoint.setY(vIt.Value()[1]);
1204  if (DataNodeType::Dimension > 2)
1205  {
1206  ogrPoint.setZ(vIt.Value()[2]);
1207  }
1208 
1209  ogrExternalRing->addPoint(&ogrPoint);
1210  ++vIt;
1211  }
1212  ogrPolygon->addRing(ogrExternalRing);
1213  // Close the polygon
1214  ogrPolygon->closeRings();
1215  OGRGeometryFactory::destroyGeometry(ogrExternalRing);
1216 
1217  // Retrieving internal rings as well
1218  for (PolygonListType::Iterator pIt = dataNode->GetPolygonInteriorRings()->Begin();
1219  pIt != dataNode->GetPolygonInteriorRings()->End(); ++pIt)
1220  {
1221  OGRLinearRing * ogrInternalRing = (OGRLinearRing *) OGRGeometryFactory::createGeometry(wkbLinearRing);
1222  vertexList = pIt.Get()->GetVertexList();
1223  vIt = vertexList->Begin();
1224 
1225  while (vIt != vertexList->End())
1226  {
1227  OGRPoint ogrPoint;
1228  ogrPoint.setX(vIt.Value()[0]);
1229  ogrPoint.setY(vIt.Value()[1]);
1230  if (DataNodeType::Dimension > 2)
1231  {
1232  ogrPoint.setZ(vIt.Value()[2]);
1233  }
1234  ogrInternalRing->addPoint(&ogrPoint);
1235  ++vIt;
1236  }
1237  ogrPolygon->addRing(ogrInternalRing);
1238  OGRGeometryFactory::destroyGeometry(ogrInternalRing);
1239  }
1240 
1241  OGRFeature *ogrFeature;
1242  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
1243 
1244  // Add the fields to the features
1245  for (unsigned int i = 0; i < kwl.GetNumberOfFields(); ++i)
1246  {
1247  // Get the key of the Nth OGRFieldRefn
1248  const char * key = kwl.GetNthField(i).first->GetNameRef();
1249  // Edit the value of the field and add it to the current feature
1250  ogrFeature->SetField(ogrFeature->GetFieldIndex(key) , kwl.GetFieldAsString(key).c_str());
1251  }
1252 
1253  ogrFeature->SetGeometry(ogrPolygon);
1254 
1255  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
1256  {
1257  itkExceptionMacro(<< "Failed to create feature in shapefile.");
1258  }
1259 
1260  OGRFeature::DestroyFeature(ogrFeature);
1261  OGRGeometryFactory::destroyGeometry(ogrPolygon);
1262  break;
1263  }
1264  case FEATURE_MULTIPOINT:
1265  {
1266  OGRMultiPoint* ogrMultiPoint = (OGRMultiPoint*) OGRGeometryFactory::createGeometry(wkbMultiPoint);
1267  OGRFeature * ogrFeature;
1268 
1269  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
1270  ogrFeature->GetDefnRef()->SetGeomType(wkbMultiPoint);
1271  ogrFeature->SetGeometry(ogrMultiPoint);
1272 
1273  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
1274  {
1275  itkExceptionMacro(<< "Failed to create feature in shapefile.");
1276  }
1277  ConvertDataTreeNodeToOGRLayers(*it, inMemoryDataSource, ogrCurrentLayer, oSRS);
1278  break;
1279  }
1280  case FEATURE_MULTILINE:
1281  {
1282  // Instanciate a new ogrMultiLineString feature
1283  OGRMultiLineString* ogrMultiLineString = (OGRMultiLineString*) OGRGeometryFactory::createGeometry(
1284  wkbMultiLineString);
1285 
1286  OGRFeature *ogrFeature;
1287 
1288  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
1289  ogrFeature->GetDefnRef()->SetGeomType(wkbMultiLineString);
1290  ogrFeature->SetGeometry(ogrMultiLineString);
1291 
1292  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
1293  {
1294  itkExceptionMacro(<< "Failed to create feature in shapefile.");
1295  }
1296  ConvertDataTreeNodeToOGRLayers(*it, inMemoryDataSource, ogrCurrentLayer, oSRS);
1297  break;
1298  }
1299  case FEATURE_MULTIPOLYGON:
1300  {
1301  // Instanciate a new multipolygon feature
1302  OGRMultiPolygon* ogrMultiPolygon = (OGRMultiPolygon*) OGRGeometryFactory::createGeometry(wkbMultiPolygon);
1303  OGRFeature * ogrFeature;
1304 
1305  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
1306  ogrFeature->GetDefnRef()->SetGeomType(wkbMultiPolygon);
1307  ogrFeature->SetGeometry(ogrMultiPolygon);
1308 
1309  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
1310  {
1311  itkExceptionMacro(<< "Failed to create feature in shapefile.");
1312  }
1313  ConvertDataTreeNodeToOGRLayers(*it, inMemoryDataSource, ogrCurrentLayer, oSRS);
1314  break;
1315  }
1316  case FEATURE_COLLECTION:
1317  {
1318  OGRGeometryCollection* ogrCollectionGeometry = (OGRGeometryCollection*) OGRGeometryFactory::createGeometry(
1319  wkbGeometryCollection);
1320 
1321  OGRFeature *ogrFeature;
1322 
1323  ogrFeature = OGRFeature::CreateFeature(ogrCurrentLayer->GetLayerDefn());
1324  ogrFeature->GetDefnRef()->SetGeomType(wkbGeometryCollection);
1325  ogrFeature->SetGeometry(ogrCollectionGeometry);
1326 
1327  if (ogrCurrentLayer->CreateFeature(ogrFeature) != OGRERR_NONE)
1328  {
1329  itkExceptionMacro(<< "Failed to create feature in shapefile.");
1330  }
1331  ConvertDataTreeNodeToOGRLayers(*it, inMemoryDataSource, ogrCurrentLayer, oSRS);
1332  break;
1333  }
1334  }
1335  }
1336 
1337  return ogrLayerVector;
1338 }
1339 
1340 
1341 } // end namespace otb

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