All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 {
40 }
41 
43 {
44  assert(geom);
45 
46  if(m_maxGeometries > 0 && m_nGeometries >= m_maxGeometries)
47  return;
48 
49  std::size_t lastPos = m_coords.size();
50 
51  // Adding the new coordinates
52  GetCoordinates(geom, m_coords);
53 
54  // Indexing...
55  for(std::size_t i = lastPos; i < m_coords.size(); ++i)
56  {
57  te::gm::Envelope e(m_coords[i].x, m_coords[i].y, m_coords[i].x, m_coords[i].y);
58  m_rtree.insert(e, i);
59  }
60 
61  ++m_nGeometries;
62 }
63 
65 {
66  m_nGeometries = 0;
67  m_coords.clear();
68  m_rtree.clear();
69 }
70 
71 std::string te::edit::SnapVertex::getName() const
72 {
73  return "SnapVertex";
74 }
75 
77 {
78  return "Implements vertex search snap.";
79 }
80 
81 te::edit::Snap* te::edit::SnapVertex::Builder(const std::string& source, int srid)
82 {
83  return new SnapVertex(source, srid);
84 }
85 
87 {
88  assert(e.isValid());
89 
90  std::vector<std::size_t> report;
91  m_rtree.search(e, report);
92 
93  if(report.empty())
94  return false;
95 
96  std::size_t snappedPos = report[0];
97 
98  if(report.size() > 1)
99  {
100  // Finds the nearest coordinate
101  te::gm::Coord2D center = e.getCenter();
102  double minDistance = std::numeric_limits<double>::max();
103  for(std::size_t i = 1; i < report.size(); ++i)
104  {
105  const te::gm::Coord2D& foundCoord = m_coords[report[i]];
106  double distance = GetDistance(center, foundCoord);
107  if(distance < minDistance)
108  {
109  minDistance = distance;
110  snappedPos = report[i];
111  }
112  }
113  }
114 
115  assert(snappedPos < m_coords.size());
116 
117  // Gets the snapped coordinate
118  const te::gm::Coord2D& snapped = m_coords[snappedPos];
119 
120  // Informs the output
121  result.x = snapped.x;
122  result.y = snapped.y;
123 
124  return true;
125 }
126 
This class implements geometry snap concept.
Definition: Snap.h:63
SnapVertex(const std::string &source, int srid=TE_UNKNOWN_SRS)
Definition: SnapVertex.cpp:33
double y
y-coordinate.
Definition: Coord2D.h:114
This class implements a vertex search snap.
Definition: SnapVertex.h:45
double x
x-coordinate.
Definition: Coord2D.h:113
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)
Definition: Utils.cpp:300
void add(te::gm::Geometry *geom)
Definition: SnapVertex.cpp:42
std::string getName() const
Definition: SnapVertex.cpp:71
Coord2D getCenter() const
It returns the rectangle's center coordinate.
Definition: Envelope.cpp:51
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
bool search(const te::gm::Envelope &e, te::gm::Coord2D &result)
Definition: SnapVertex.cpp:86
TEEDITEXPORT double GetDistance(const te::gm::Coord2D &c1, const te::gm::Coord2D &c2)
Definition: Utils.cpp:295
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
std::string getDescription() const
Definition: SnapVertex.cpp:76
Utility functions for TerraLib Edit module.
static Snap * Builder(const std::string &source, int srid)
Definition: SnapVertex.cpp:81
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438