SnapVertex.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 terralib/edit/SnapVertex.cpp
22 
23  \brief This class implements a vertex search snap.
24 */
25 
26 // TerraLib
27 #include "SnapVertex.h"
28 #include "Utils.h"
29 
30 // STL
31 #include <cassert>
32 
33 te::edit::SnapVertex::SnapVertex(const std::string& source, int srid)
34  : Snap(source, srid)
35 {
36 }
37 
39 
41 {
42  assert(geom);
43 
45  return;
46 
47  std::size_t lastPos = m_coords.size();
48 
49  // Adding the new coordinates
50  GetCoordinates(geom, m_coords);
51 
52  // Indexing...
53  for(std::size_t i = lastPos; i < m_coords.size(); ++i)
54  {
55  te::gm::Envelope e(m_coords[i].x, m_coords[i].y, m_coords[i].x, m_coords[i].y);
56  m_rtree.insert(e, i);
57  }
58 
59  ++m_nGeometries;
60 }
61 
63 {
64  m_nGeometries = 0;
65  m_coords.clear();
66  m_rtree.clear();
67 }
68 
69 std::string te::edit::SnapVertex::getName() const
70 {
71  return "SnapVertex";
72 }
73 
75 {
76  return "Implements vertex search snap.";
77 }
78 
79 te::edit::Snap* te::edit::SnapVertex::Builder(const std::string& source, int srid)
80 {
81  return new SnapVertex(source, srid);
82 }
83 
85 {
86  assert(e.isValid());
87 
88  std::vector<std::size_t> report;
89  m_rtree.search(e, report);
90 
91  if(report.empty())
92  return false;
93 
94  std::size_t snappedPos = report[0];
95 
96  if(report.size() > 1)
97  {
98  // Finds the nearest coordinate
99  te::gm::Coord2D center = e.getCenter();
100  double minDistance = std::numeric_limits<double>::max();
101  for(std::size_t i = 1; i < report.size(); ++i)
102  {
103  const te::gm::Coord2D& foundCoord = m_coords[report[i]];
104  double distance = GetDistance(center, foundCoord);
105  if(distance < minDistance)
106  {
107  minDistance = distance;
108  snappedPos = report[i];
109  }
110  }
111  }
112 
113  assert(snappedPos < m_coords.size());
114 
115  // Gets the snapped coordinate
116  const te::gm::Coord2D& snapped = m_coords[snappedPos];
117 
118  // Informs the output
119  result.x = snapped.x;
120  result.y = snapped.y;
121 
122  return true;
123 }
124 
This class implements geometry snap concept.
Definition: Snap.h:63
Utility functions for TerraLib Edit module.
SnapVertex(const std::string &source, int srid=TE_UNKNOWN_SRS)
Definition: SnapVertex.cpp:33
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
te::sam::rtree::Index< std::size_t, 8 > m_rtree
Internal index used to retrieve geometries spatially.
Definition: SnapVertex.h:70
This class implements a vertex search snap.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
TEEDITEXPORT void GetCoordinates(te::gm::Geometry *geom, std::vector< te::gm::Coord2D > &coords)
void add(te::gm::Geometry *geom)
Definition: SnapVertex.cpp:40
std::string getName() const
Definition: SnapVertex.cpp:69
Coord2D getCenter() const
It returns the rectangle&#39;s center coordinate.
std::size_t m_nGeometries
The current number of geometries added to the snap.
Definition: Snap.h:111
An Envelope defines a 2D rectangular region.
bool search(const te::gm::Envelope &e, te::gm::Coord2D &result)
Definition: SnapVertex.cpp:84
TEEDITEXPORT double GetDistance(const te::gm::Coord2D &c1, const te::gm::Coord2D &c2)
int search(const te::gm::Envelope &mbr, std::vector< DATATYPE > &report) const
Range search query.
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
std::string getDescription() const
Definition: SnapVertex.cpp:74
void insert(const te::gm::Envelope &mbr, const DATATYPE &data)
It inserts an item into the tree.
static Snap * Builder(const std::string &source, int srid)
Definition: SnapVertex.cpp:79
bool isValid() const
It tells if the rectangle is valid or not.
std::vector< te::gm::Coord2D > m_coords
The snap coordinates.
Definition: SnapVertex.h:69
std::size_t m_maxGeometries
The maximum number of geometries that can be added to the snap. If 0, there will be not limit...
Definition: Snap.h:112