All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Snap.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/Snap.cpp
22 
23  \brief This class implements geometry snap concept.
24 */
25 
26 // TerraLib
27 #include "../common/Exception.h"
28 #include "../common/Translator.h"
29 #include "../dataaccess/dataset/DataSet.h"
30 #include "../dataaccess/utils/Utils.h"
31 #include "../datatype/Enums.h"
32 #include "../maptools/WorldDeviceTransformer.h"
33 #include "Snap.h"
34 #include "Utils.h"
35 
36 // STL
37 #include <cassert>
38 #include <memory>
39 
40 te::edit::Snap::Snap(const std::string& source, int srid)
41  : m_source(source),
42  m_srid(srid),
43  m_nGeometries(0),
44  m_maxGeometries(0),
45  m_tolerance(16),
46  m_transformer(0)
47 {
48 }
49 
51 {
52  delete m_transformer;
53 }
54 
55 std::string te::edit::Snap::getSource() const
56 {
57  return m_source;
58 }
59 
61 {
62  return m_srid;
63 }
64 
65 std::size_t te::edit::Snap::getNGeometries() const
66 {
67  return m_nGeometries;
68 }
69 
71 {
72  return m_maxGeometries;
73 }
74 
76 {
77  return m_tolerance;
78 }
79 
80 void te::edit::Snap::setTolerance(const double& t)
81 {
82  m_tolerance = t;
83 }
84 
85 void te::edit::Snap::setWorld(const double& llx, const double& lly,
86  const double& urx, const double& ury,
87  const std::size_t& width, const std::size_t& height)
88 {
89  delete m_transformer;
90  m_transformer = new te::map::WorldDeviceTransformer(llx, lly, urx, ury, width, height);
91 }
92 
94 {
95  assert(dataset);
96 
97  clear();
98 
99  add(dataset);
100 }
101 
103 {
104  assert(dataset);
105 
106  std::size_t gpos = te::da::GetFirstPropertyPos(dataset, te::dt::GEOMETRY_TYPE);
107 
108  if(gpos == std::string::npos)
109  return;
110 
111  while(dataset->moveNext())
112  {
113  std::auto_ptr<te::gm::Geometry> g(dataset->getGeometry(gpos));
114  add(g.get());
115  }
116 }
117 
119 {
120  te::gm::Envelope e = getSearchEnvelope(coord);
121 
122  return search(e, result);
123 }
124 
126 {
127  if(m_transformer == 0)
128  throw te::common::Exception(TE_TR("World/Device transformer not defined!"));
129 
130  // Device coordinates
131  double dx = 0.0;
132  double dy = 0.0;
133 
134  // Transform the given coordinate to device coordinate
135  m_transformer->world2Device(coord.x, coord.y, dx, dy);
136 
137  // Build the search envelope (device coordinates) using the snap tolerance
138  te::gm::Envelope e(dx - m_tolerance, dy + m_tolerance, dx + m_tolerance, dy - m_tolerance);
139 
140  // Convert the search envelope to world coordinates
141  m_transformer->device2World(e.m_llx, e.m_lly);
142  m_transformer->device2World(e.m_urx, e.m_ury);
143 
144  assert(e.isValid());
145 
146  return e;
147 }
double getTolerance() const
Definition: Snap.cpp:75
void setWorld(const double &llx, const double &lly, const double &urx, const double &ury, const std::size_t &width, const std::size_t &height)
Definition: Snap.cpp:85
This class implements geometry snap concept.
std::size_t getMaxGeometries() const
Definition: Snap.cpp:70
Utility functions for the data access module.
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
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
int getSRID() const
Definition: Snap.cpp:60
std::string getSource() const
Definition: Snap.cpp:55
This class implements the logic for transforming from device coordinate to world coordinate and vice-...
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
void add(te::da::DataSet *dataset)
Definition: Snap.cpp:102
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
void setTolerance(const double &t)
Definition: Snap.cpp:80
void build(te::da::DataSet *dataset)
Definition: Snap.cpp:93
virtual ~Snap()
Definition: Snap.cpp:50
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
virtual bool search(const te::gm::Coord2D &coord, te::gm::Coord2D &result)
Definition: Snap.cpp:118
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:481
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
te::gm::Envelope getSearchEnvelope(const te::gm::Coord2D &coord) const
Definition: Snap.cpp:125
Snap(const std::string &source, int srid=TE_UNKNOWN_SRS)
Definition: Snap.cpp:40
std::size_t getNGeometries() const
Definition: Snap.cpp:65