KdTreeExamples.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file KdTreeExamples.cpp
22 
23  \brief This file contains several examples on how to use the K-d tree spatial access method.
24  */
25 
26 // TerraLib
27 #include <terralib_buildconfig.h>
28 
29 #include <terralib/common.h>
30 #include <terralib/geometry.h>
31 #include <terralib/sam.h>
32 #include "SAMExamples.h"
33 
34 // STL
35 #include <limits>
36 #include <vector>
37 #include <utility>
38 
39 void KdTree()
40 {
41  // K-d Tree
44 
46 
47  KD_TREE tree(e);
48 
49  std::size_t n = 100;
50 
51  for(std::size_t i = 0; i < n; ++i)
52  {
53  te::gm::Coord2D coord(static_cast<double>(i), static_cast<double>(i));
54  tree.insert(coord, i);
55  }
56 
57  for(std::size_t i = 0; i < n; ++i)
58  {
59  std::vector<KD_NODE*> reports;
60 
61  te::gm::Envelope e(static_cast<double>(i), static_cast<double>(i), static_cast<double>(i), static_cast<double>(i));
62  tree.search(e, reports);
63  }
64 }
65 
67 {
68  // Adaptative K-d Tree
71 
72  std::vector<std::pair<te::gm::Coord2D, te::gm::Point> > dataset;
73 
75 
76  std::size_t n = 100;
77 
78  for(std::size_t i = 0; i < n; ++i)
79  {
80  te::gm::Coord2D coord(static_cast<double>(i), static_cast<double>(i));
81  te::gm::Point point = te::gm::Point(coord.x, coord.y, 4326);
82 
83  e.Union(*point.getMBR());
84 
85  dataset.push_back(std::pair<te::gm::Coord2D, te::gm::Point>(coord, point));
86  }
87 
88  KD_ADAPTATIVE_TREE adaptativeTree(e);
89  adaptativeTree.build(dataset);
90 
91  // Search 1
92  std::vector<KD_ADAPTATIVE_NODE*> reports;
93  adaptativeTree.search(e, reports);
94 
95  // Search 2
96  std::vector<te::gm::Point> reports2;
97  adaptativeTree.search(e, reports2);
98 
99  te::gm::Coord2D coord = te::gm::Coord2D(28.0, 28.0);
100 
101  std::vector<te::gm::Point> points;
102  points.push_back(te::gm::Point(std::numeric_limits<double>::max(), std::numeric_limits<double>::max()));
103  std::vector<double> sqrDists;
104 
105  adaptativeTree.nearestNeighborSearch(coord, points, sqrDists, 1);
106 }
107 
109 {
110  KdTree();
111 
113 }
void insert(const kdKey &key, const kdDataItem &item)
It inserts the data with a given key in tree.
This file contains include headers for TerraLib Spatial Access Methods module.
void AdaptativeKdTree()
double y
y-coordinate.
Definition: Coord2D.h:114
Several examples on how to use Spatial Access Methods in TerraLib.
double x
x-coordinate.
Definition: Coord2D.h:113
void search(const te::gm::Envelope &e, std::vector< KdTreeNode * > &report) const
Range search query.
A class that represents a two dimensional K-d Tree (2-d Tree) that store data-elements into the leafs...
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
te::sam::kdtree::AdaptativeNode< te::gm::Coord2D, std::vector< te::gm::Point >, te::gm::Point > KD_ADAPTATIVE_NODE
void Union(const Envelope &rhs)
It updates the envelop with coordinates of another envelope.
A point with x and y coordinate values.
Definition: Point.h:50
const Envelope * getMBR() const _NOEXCEPT_OP(true)
It returns the minimum bounding rectangle for the geometry in an internal representation.
An Envelope defines a 2D rectangular region.
void build(std::vector< std::pair< kdKey, kdDataItem > > &dataSet)
It inserts the data set into the tree.
te::sam::kdtree::AdaptativeIndex< KD_ADAPTATIVE_NODE > KD_ADAPTATIVE_TREE
void KdTree()
A class that represents a two dimensional K-d Tree (2-d Tree).
te::sam::kdtree::Index< KD_NODE > KD_TREE
Definition: GAP.h:28
A class that represents an Kd-tree node.
Definition: kdtree/Node.h:61
te::sam::kdtree::Node< te::gm::Coord2D, std::pair< int32_t, int32_t >, std::pair< int32_t, int32_t > > KD_NODE
Definition: GAP.h:27
This file contains include headers for the TerraLib Common Runtime module.
This file contains include headers for the Vector Geometry model of TerraLib.
void search(const te::gm::Envelope &e, std::vector< KdTreeNode * > &report) const
Range search query.
A class that represents an Kd-tree node.
Definition: kdtree/Node.h:206
void nearestNeighborSearch(const kdKey &key, std::vector< kdDataItem > &report, std::vector< double > &sqrDists, const std::size_t &k) const
It searches the nearest data in nodes: you must pass an array of kdDataItem of size "k" with coordina...
void IndexPointUsingKdTree()
This example shows how to index a set of points using the K-d tree spatial access method...