All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SnapManager.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/SnapManager.cpp
22 
23  \brief This is a singleton for managing geometries snap.
24 */
25 
26 // TerraLib
27 #include "../common/Exception.h"
28 #include "../common/STLUtils.h"
29 #include "../common/Translator.h"
30 #include "../dataaccess/dataset/DataSet.h"
31 #include "Snap.h"
32 #include "SnapManager.h"
33 
34 // Boost
35 #include <boost/format.hpp>
36 
37 // STL
38 #include <cassert>
39 
40 bool te::edit::SnapManager::hasSnap(const std::string& source) const
41 {
42  return m_snaps.find(source) != m_snaps.end();
43 }
44 
45 void te::edit::SnapManager::createSnap(const std::string& source, int srid)
46 {
47  if(hasSnap(source))
48  return;
49 
50  SnapStrategies::iterator it = m_snapStrategies.find("vertex");
51 
52  assert(it != m_snapStrategies.end());
53 
54  Snap* snap = it->second(source, srid);
55 
56  // Store!
57  m_snaps[source] = snap;
58 }
59 
60 void te::edit::SnapManager::buildSnap(const std::string& source, int srid, te::da::DataSet* dataset)
61 {
62  Snap* snap = getSnap(source);
63 
64  if(snap == 0)
65  {
66  // Not found! Create a new snap associated with the given source
67  SnapStrategies::iterator it = m_snapStrategies.find("vertex");
68 
69  assert(it != m_snapStrategies.end());
70 
71  snap = it->second(source, srid);
72 
73  // Build the snap
74  snap->build(dataset);
75 
76  // Store!
77  m_snaps[source] = snap;
78  }
79  else
80  snap->build(dataset);
81 }
82 
83 void te::edit::SnapManager::removeSnap(const std::string& source)
84 {
85  std::map<std::string, Snap*>::iterator it = m_snaps.find(source);
86 
87  if(it == m_snaps.end())
88  return;
89 
90  delete it->second;
91  m_snaps.erase(it);
92 }
93 
94 const std::map<std::string, te::edit::Snap*>& te::edit::SnapManager::getSnaps() const
95 {
96  return m_snaps;
97 }
98 
99 te::edit::Snap* te::edit::SnapManager::getSnap(const std::string& source) const
100 {
101  std::map<std::string, Snap*>::const_iterator it = m_snaps.find(source);
102 
103  if(it == m_snaps.end())
104  return 0;
105 
106  return it->second;
107 }
108 
109 void te::edit::SnapManager::clear(const std::string& source)
110 {
111  Snap* snap = getSnap(source);
112 
113  if(snap)
114  snap->clear();
115 }
116 
118 {
119  te::common::FreeContents(m_snaps);
120  m_snaps.clear();
121 }
122 
123 bool te::edit::SnapManager::search(const te::gm::Coord2D& coord, int /*srid*/, te::gm::Coord2D& result)
124 {
125  std::map<std::string, Snap*>::const_iterator it;
126  for(it = m_snaps.begin(); it != m_snaps.end(); ++it)
127  {
128  if(it->second->search(coord, result))
129  return true;
130  }
131 
132  return false;
133 }
134 
135 void te::edit::SnapManager::setWorld(const double& llx, const double& lly,
136  const double& urx, const double& ury,
137  const std::size_t& width, const std::size_t& height)
138 {
139  std::map<std::string, Snap*>::const_iterator it;
140  for(it = m_snaps.begin(); it != m_snaps.end(); ++it)
141  it->second->setWorld(llx, lly, urx, ury, width, height);
142 }
143 
144 void te::edit::SnapManager::reg(const std::string& name, const SnapStrategyFnctType& builder)
145 {
146  SnapStrategies::iterator it = m_snapStrategies.find(name);
147 
148  if(it != m_snapStrategies.end())
149  throw te::common::Exception((boost::format(TE_TR("There is already a snap strategy registered with the given key %1%.")) % name).str());
150 
151  m_snapStrategies[name] = builder;
152 }
153 
155 {
156 }
157 
159 {
160 }
This class implements geometry snap concept.
Definition: Snap.h:63
This class implements geometry snap concept.
bool hasSnap(const std::string &source) const
Definition: SnapManager.cpp:40
std::map< std::string, Snap * > m_snaps
Definition: SnapManager.h:109
void buildSnap(const std::string &source, int srid, te::da::DataSet *dataset)
Definition: SnapManager.cpp:60
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
void setWorld(const double &llx, const double &lly, const double &urx, const double &ury, const std::size_t &width, const std::size_t &height)
void removeSnap(const std::string &source)
Definition: SnapManager.cpp:83
virtual void clear()=0
bool search(const te::gm::Coord2D &coord, int srid, te::gm::Coord2D &result)
void createSnap(const std::string &source, int srid)
Definition: SnapManager.cpp:45
void build(te::da::DataSet *dataset)
Definition: Snap.cpp:93
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
This is a singleton for managing geometries snap.
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
void reg(const std::string &name, const SnapStrategyFnctType &strategy)
~SnapManager()
Singleton destructor.
Snap * getSnap(const std::string &source) const
Definition: SnapManager.cpp:99
SnapManager()
It initializes the singleton instance of the snap manager.
void clear(const std::string &source)
boost::function< Snap *(const std::string &source, int srid)> SnapStrategyFnctType
Definition: SnapManager.h:71
void FreeContents(boost::unordered_map< K, V * > &m)
This function can be applied to a map of pointers. It will delete each pointer in the map...
Definition: BoostUtils.h:55
const std::map< std::string, Snap * > & getSnaps() const
Definition: SnapManager.cpp:94