Orfeo Toolbox  4.0
otbDataNode.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 
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 #ifndef __otbDataNode_txx
19 #define __otbDataNode_txx
20 
21 #include "otbDataNode.h"
22 #include "otbMetaDataKey.h"
24 
25 namespace otb
26 {
27 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
30 {
31  m_NodeType = ROOT;
32  m_NodeId = "";
33  m_Data.valid = false;
34 }
35 
36 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
37 void
40 {
41  m_NodeType = type;
42  m_Data.valid = false;
43 }
44 
45 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
46 void
49 {
50  m_NodeType = FEATURE_POINT;
51  m_Data.point = point;
52  m_Data.valid = true;
53 }
54 
55 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
58 {
59  m_NodeType = FEATURE_LINE;
60  m_Data.line = line;
61  m_Data.valid = true;
62 }
63 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
64 void
67 {
68  m_NodeType = FEATURE_POLYGON;
69  m_Data.exteriorRing = polygon;
70  if (!m_Data.interiorRings)
71  {
72  m_Data.interiorRings = PolygonListType::New();
73  }
74  m_Data.valid = true;
75 }
76 
77 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
78 void
81 {
82  m_NodeType = FEATURE_POLYGON;
83  m_Data.interiorRings = polygonList;
84  if (!m_Data.exteriorRing)
85  {
86  m_Data.exteriorRing = PolygonType::New();
87  }
88  m_Data.valid = true;
89 }
90 
91 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
93 ::PointType
95 ::GetPoint() const
96 {
97  if (!IsPointFeature())
98  {
99  itkGenericExceptionMacro(<< "Node " << m_NodeId << " is not a point.");
100  }
101  if (!m_Data.valid)
102  {
103  itkGenericExceptionMacro(<< "Invalid point node.");
104  }
105  return m_Data.point;
106 }
107 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
109 ::LinePointerType
111 ::GetLine() const
112 {
113  if (!IsLineFeature())
114  {
115  itkGenericExceptionMacro(<< "Node " << m_NodeId << " is not a line.");
116  }
117  if (!m_Data.valid)
118  {
119  itkGenericExceptionMacro(<< "Invalid line node.");
120  }
121  return m_Data.line;
122 }
123 
124 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
126 ::PolygonPointerType
129 {
130  if (!IsPolygonFeature())
131  {
132  itkGenericExceptionMacro(<< "Node " << m_NodeId << " is not a polygon.");
133  }
134  if (!m_Data.valid || !m_Data.exteriorRing)
135  {
136  itkGenericExceptionMacro(<< "Invalid polygon node.");
137  }
138  return m_Data.exteriorRing;
139 }
140 
141 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
143 ::PolygonListPointerType
146 {
147  if (!IsPolygonFeature())
148  {
149  itkGenericExceptionMacro(<< "Node " << m_NodeId << " is not a polygon.");
150  }
151  if (!m_Data.valid || !m_Data.interiorRings)
152  {
153  itkGenericExceptionMacro(<< "Invalid polygon node.");
154  }
155  return m_Data.interiorRings;
156 }
157 
158 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
159 void
161 ::PrintSelf(std::ostream& os, itk::Indent indent) const
162 {
163  os << indent << this->GetNodeTypeAsString();
164 }
165 
166 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
167 std::string
170 {
171  std::ostringstream oss;
172  switch (m_NodeType)
173  {
174  case ROOT:
175  {
176  oss << "Root (" << m_NodeId << ")";
177  break;
178  }
179  case DOCUMENT:
180  {
181  oss << "Document (" << m_NodeId << ")";
182  break;
183  }
184  case FOLDER:
185  {
186  oss << "Folder (" << m_NodeId << ")";
187  break;
188  }
189  case FEATURE_POINT:
190  {
191  oss << "Point (" << m_NodeId << ") " << m_Data.point;
192  break;
193  }
194  case FEATURE_LINE:
195  {
196  oss << "Line (" << m_NodeId << ") " << m_Data.line->GetVertexList()->Size() << " points";
197  break;
198  }
199  case FEATURE_POLYGON:
200  {
201  oss << "Polygon (" << m_NodeId << ") " << this->GetPolygonExteriorRing()->GetVertexList()->Size() <<
202  " points, " <<
203  this->GetPolygonInteriorRings()->Size() << " interior rings";
204  break;
205  }
206  case FEATURE_MULTIPOINT:
207  {
208  oss << "MultiPoint (" << m_NodeId << ")";
209  break;
210  }
211  case FEATURE_MULTILINE:
212  {
213  oss << "MultiLine (" << m_NodeId << ")";
214  break;
215  }
217  {
218  oss << "MultiPolygon (" << m_NodeId << ")";
219  break;
220  }
221  case FEATURE_COLLECTION:
222  {
223  oss << "Collection (" << m_NodeId << ")";
224  break;
225  }
226  }
227  if (GetMetaDataDictionary().HasKey(MetaDataKey::VectorDataKeywordlistKey))
228  {
230  itk::ExposeMetaData<VectorDataKeywordlist>(GetMetaDataDictionary(), MetaDataKey::VectorDataKeywordlistKey, kwl);
231  oss << "\n -> Metadata: " << kwl;
232  }
233  return oss.str();
234 }
235 /*
236 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
237 void
238 DataNode<TPrecision, VDimension, TValuePrecision>
239 ::SetField(const std::string& key, const std::string& value)
240 {
241  m_FieldMap[key] = value;
242 }
243 */
244 
245 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
246 void
248 ::SetFieldAsString(const std::string& key, const std::string& value)
249 {
251  itk::ExposeMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
253  kwl);
254  kwl.SetFieldAsString(key, value);
255  itk::EncapsulateMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
257  kwl);
258 }
259 
260 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
261 void
263 ::SetFieldAsInt(const std::string& key, int value)
264 {
266  itk::ExposeMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
268  kwl);
269  kwl.SetFieldAsInt(key, value);
270  itk::EncapsulateMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
272  kwl);
273 }
274 
275 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
276 void
278 ::SetFieldAsDouble(const std::string& key, double value)
279 {
281  itk::ExposeMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
283  kwl);
284 
285  kwl.SetFieldAsDouble(key, value);
286  itk::EncapsulateMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
288  kwl);
289 }
290 
291 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
292 double
294 ::GetFieldAsDouble(const std::string& key) const
295 {
296  VectorDataKeywordlist keywordlist;
297  if (HasField(key))
298  {
299  itk::ExposeMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
301 
302  return keywordlist.GetFieldAsDouble(key);
303  }
304  return 0;
305 }
306 
307 /*
308 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
309 std::string
310 DataNode<TPrecision, VDimension, TValuePrecision>
311 ::GetField(const std::string& key) const
312 {
313  if (HasField(key))
314  {
315  return (*m_FieldMap.find(key)).second;
316  }
317  else
318  {
319  return "Unknown Key";
320  }
321 }*/
322 
323 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
324 std::string
326 ::GetFieldAsString(const std::string& key) const
327 {
328  VectorDataKeywordlist keywordlist;
329  if (HasField(key))
330  {
331  itk::ExposeMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
333  return keywordlist.GetFieldAsString(key);
334  }
335  return "";
336 }
337 
338 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
339 int
341 ::GetFieldAsInt(const std::string& key) const
342 {
343  VectorDataKeywordlist keywordlist;
344  if (HasField(key))
345  {
346  itk::ExposeMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
348 
349  return keywordlist.GetFieldAsInt(key);
350  }
351  return 0;
352 }
353 
354 /*
355 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
356 void
357 DataNode<TPrecision, VDimension, TValuePrecision>
358 ::RemoveField(const std::string& key)
359 {
360  m_FieldMap.erase(key);
361 }
362 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
363 bool
364 DataNode<TPrecision, VDimension, TValuePrecision>
365 ::HasField(const std::string& key) const
366 {
367  return (m_FieldMap.find(key)!=m_FieldMap.end());
368 }
369 */
370 
371 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
372 bool
374 ::HasField(const std::string& key) const
375 {
376  VectorDataKeywordlist keywordlist;
377  if (this->GetMetaDataDictionary().HasKey(MetaDataKey::VectorDataKeywordlistKey))
378  {
379  itk::ExposeMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
381  return keywordlist.HasField(key);
382  }
383  return false;
384 }
385 
386 /*
387 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
388 typename DataNode<TPrecision, VDimension, TValuePrecision>
389 ::FieldType
390 DataNode<TPrecision, VDimension, TValuePrecision>
391 ::GetNthField(unsigned int index) const
392 {
393  if (index<GetNumberOfFields())
394  {
395  FieldMapType::iterator it = m_FieldMap.begin();
396 
397  for (unsigned int i = 0; i<index; ++i)
398  {
399  ++it;
400  }
401  return (*it);
402  }
403  return FieldType("No key","No value");
404 }
405 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
406 unsigned int
407 DataNode<TPrecision, VDimension, TValuePrecision>
408 ::GetNumberOfFields() const
409 {
410  return m_FieldMap.size();
411 }
412 */
413 
414 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
415 void
417 ::CopyFieldList(const DataNode * dataNode)
418 {
419  // The source keywordlist where to get the feature list to copy
421  itk::ExposeMetaData<VectorDataKeywordlist>(dataNode->GetMetaDataDictionary(),
423  srcKwl);
424 
426  itk::ExposeMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
428  kwl);
429 
430  kwl.CopyFieldList(srcKwl);
431  itk::EncapsulateMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
433  kwl);
434 }
435 
436 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
437 std::vector<std::string>
440 {
441  VectorDataKeywordlist keywordlist;
442  if (this->GetMetaDataDictionary().HasKey(MetaDataKey::VectorDataKeywordlistKey))
443  {
444  itk::ExposeMetaData<VectorDataKeywordlist>(this->GetMetaDataDictionary(),
446  return keywordlist.GetFieldList();
447  }
448  std::vector<std::string> empty;
449  return empty;
450 }
451 
452 /*
453 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
454 void
455 DataNode<TPrecision, VDimension, TValuePrecision>
456 ::ClearFields()
457 {
458  m_FieldMap.clear();
459 }*/
460 
461 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
462 bool
465 {
466  return m_NodeType == DOCUMENT;
467 }
468 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
469 bool
471 ::IsRoot() const
472 {
473  return m_NodeType == ROOT;
474 }
475 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
476 bool
478 ::IsFolder() const
479 {
480  return m_NodeType == FOLDER;
481 }
482 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
483 bool
486 {
487  return m_NodeType == FEATURE_POINT;
488 }
489 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
490 bool
493 {
494  return m_NodeType == FEATURE_LINE;
495 }
496 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
497 bool
500 {
501  return m_NodeType == FEATURE_POLYGON;
502 }
503 
504 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
505 bool
508 {
509  return m_NodeType == FEATURE_MULTIPOINT;
510 }
511 
512 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
513 bool
516 {
517  return m_NodeType == FEATURE_MULTILINE;
518 }
519 
520 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
521 bool
524 {
525  return m_NodeType == FEATURE_MULTIPOLYGON;
526 }
527 
528 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
529 bool
532 {
533  return m_NodeType == FEATURE_COLLECTION;
534 }
535 
536 
537 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
538 OGRGeometry *
541 {
542  switch(dataNode->GetNodeType())
543  {
544  case FEATURE_POINT:
545  {
546  OGRPoint *ogrPoint = (OGRPoint *) OGRGeometryFactory::createGeometry(wkbPoint);
547  ogrPoint->setX(dataNode->GetPoint()[0]);
548  ogrPoint->setY(dataNode->GetPoint()[1]);
549  return ogrPoint;
550  }
551  break;
552  case FEATURE_LINE:
553  {
554  //Build the ogrObject
555  OGRLineString * ogrLine = (OGRLineString *) OGRGeometryFactory::createGeometry(wkbLineString);
556  VertexListConstPointerType vertexList = dataNode->GetLine()->GetVertexList();
557  typename VertexListType::ConstIterator vIt = vertexList->Begin();
558 
559  while (vIt != vertexList->End())
560  {
561  OGRPoint ogrPoint;
562  ogrPoint.setX(vIt.Value()[0]);
563  ogrPoint.setY(vIt.Value()[1]);
564  if (Dimension > 2)
565  {
566  ogrPoint.setZ(vIt.Value()[2]);
567  }
568  ogrLine->addPoint(&ogrPoint);
569  ++vIt;
570  }
571  return ogrLine;
572  }
573  break;
574  case FEATURE_POLYGON:
575  {
576  OGRPolygon * polygon = (OGRPolygon *) OGRGeometryFactory::createGeometry(wkbPolygon);
577  OGRLinearRing * ogrExternalRing = (OGRLinearRing *) OGRGeometryFactory::createGeometry(wkbLinearRing);
578 
579  VertexListConstPointerType vertexList = dataNode->GetPolygonExteriorRing()->GetVertexList();
580 
581  typename VertexListType::ConstIterator vIt = vertexList->Begin();
582 
583  while (vIt != vertexList->End())
584  {
585  OGRPoint ogrPoint;
586  ogrPoint.setX(vIt.Value()[0]);
587  ogrPoint.setY(vIt.Value()[1]);
588  if ( Dimension > 2)
589  {
590  ogrPoint.setZ(vIt.Value()[2]);
591  }
592 
593  ogrExternalRing->addPoint(&ogrPoint);
594  ++vIt;
595  }
596  polygon->addRing(ogrExternalRing);
597  // Close the polygon
598  polygon->closeRings();
599  OGRGeometryFactory::destroyGeometry(ogrExternalRing);
600  return polygon;
601  }
602  break;
603  default: break;
604  }
605 
606  return NULL;
607 }
608 
609 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
610 double
613 {
614  // Convert the nodes to OGRGeometries
615  OGRGeometry * dstGeomtery = this->ConvertDataNodeToOGRGeometry(node);
616  OGRGeometry * currentGeometry = this->ConvertDataNodeToOGRGeometry(this);
617 
618  // Compute the distance
619  return currentGeometry->Distance(dstGeomtery);
620 }
621 
622 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
623 double
626 {
627  // Convert Point to point to ogrPoint
628  OGRPoint ogrPointSrc;
629  ogrPointSrc.setX(point[0]);
630  ogrPointSrc.setY(point[1]);
631 
632  // Convert the current datanode to an OGRGeometry
633  OGRGeometry * currentGeometry = this->ConvertDataNodeToOGRGeometry(this);
634 
635  // return the distance
636  return currentGeometry->Distance(&ogrPointSrc);
637 }
638 
639 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
640 bool
642 ::Intersects(const DataNode* node)
643 {
644  // Convert the nodes to OGRGeometries
645  OGRGeometry * dstGeomtery = this->ConvertDataNodeToOGRGeometry(node);
646  OGRGeometry * currentGeometry = this->ConvertDataNodeToOGRGeometry(this);
647 
648  //
649  return currentGeometry->Intersects(dstGeomtery);
650 }
651 
652 template <class TPrecision, unsigned int VDimension, class TValuePrecision>
653 bool
655 ::Within(const DataNode* node)
656 {
657  // Convert the nodes to OGRGeometries
658  OGRGeometry * dstGeomtery = this->ConvertDataNodeToOGRGeometry(node);
659  OGRGeometry * currentGeometry = this->ConvertDataNodeToOGRGeometry(this);
660 
661  //
662  return currentGeometry->Within(dstGeomtery);
663 }
664 
665 
666 } // end namespace otb
667 
668 #endif

Generated at Sat Mar 8 2014 15:53:15 for Orfeo Toolbox with doxygen 1.8.3.1