AggregateAreaTool.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/qt/tools/AggregateAreaTool.cpp
22 
23  \brief This class implements a concrete tool to aggregate polygons.
24 */
25 
26 // TerraLib
27 #include "../../../common/STLUtils.h"
28 #include "../../../dataaccess/dataset/ObjectId.h"
29 #include "../../../dataaccess/dataset/ObjectIdSet.h"
30 #include "../../../dataaccess/utils/Utils.h"
31 #include "../../../geometry/Utils.h"
32 #include "../../../qt/af/events/LayerEvents.h"
33 #include "../../../qt/af/events/MapEvents.h"
34 #include "../../../qt/widgets/canvas/MapDisplay.h"
35 #include "../../Feature.h"
36 #include "../../RepositoryManager.h"
37 #include "../../Utils.h"
38 #include "../Renderer.h"
39 #include "../Utils.h"
40 #include "AggregateAreaTool.h"
41 
42 // Qt
43 #include <QMessageBox>
44 #include <QMouseEvent>
45 #include <QPainter>
46 #include <QPixmap>
47 #include <QDebug>
48 
49 // STL
50 #include <cassert>
51 #include <memory>
52 #include <iostream>
53 
55  : CreateLineTool(display, layer, Qt::ArrowCursor, parent),
56  m_stack(UndoStackManager::getInstance())
57 {
58  setCursor(Qt::ArrowCursor);
59 }
60 
62 
64 {
65  if (e->button() != Qt::LeftButton)
66  return false;
67 
68  if (m_isFinished) // Is Finished?! So, start again...
69  {
71  m_isFinished = false;
72  }
73 
75 
77 }
78 
80 {
82 }
83 
85 {
86  try
87  {
88  if (e->button() != Qt::LeftButton)
89  return false;
90 
91  return editingFinished();
92  }
93  catch (std::exception& e)
94  {
95  QMessageBox::critical(m_display, tr("Error"), QString(tr("Could not join.") + " %1.").arg(e.what()));
96  return false;
97  }
98 }
99 
101 {
102  try
103  {
104  if (e->button() != Qt::RightButton)
105  return false;
106 
107  return editingFinished();
108  }
109  catch (std::exception& e)
110  {
111  QMessageBox::critical(m_display, tr("Error"), QString(tr("Could not join.") + " %1.").arg(e.what()));
112  return false;
113  }
114 }
115 
117 {
118  if (m_coords.size() < 3) // Can not stop yet...
119  return false;
120 
121  if (m_feature == nullptr) // Can not stop yet...
122  {
124  QMessageBox::critical(m_display, tr("Error"), QString(tr("Error aggregating area to the polygon")));
125  return false;
126  }
127 
128  setCursor(Qt::WaitCursor);
129 
130  m_isFinished = true;
131 
132  draw();
133 
134  storeFeature();
135 
137 
138  clear();
139 
140  setCursor(Qt::ArrowCursor);
141 
142  emit geometriesEdited();
143 
144  return true;
145 }
146 
148 {
149  const te::gm::Envelope& env = m_display->getExtent();
150  if (!env.isValid())
151  return;
152 
153  // Clear!
154  QPixmap* draft = m_display->getDraftPixmap();
155  draft->fill(Qt::transparent);
156 
157  // Initialize the renderer
158  Renderer& renderer = Renderer::getInstance();
159  renderer.begin(draft, env, m_display->getSRID());
160 
161  // Draw the layer edited geometries
162  if (!m_coords.empty())
163  {
164  if (m_coords.size() > 3)
165  drawPolygon();
166 
167  if (m_continuousMode == false)
168  m_coords.pop_back();
169  }
170 
171  renderer.end();
172 
173  m_display->repaint();
174 }
175 
177 {
178  // Build the geometry
180 
181  // Draw the current geometry and the vertexes
182  Renderer& renderer = Renderer::getInstance();
183  renderer.draw(m_feature->getGeometry(), true);
184 }
185 
187 {
188  te::gm::Geometry* geom = nullptr;
189 
190  // Build the geometry
192  for (std::size_t i = 0; i < m_coords.size(); ++i)
193  ring->setPoint(i, m_coords[i].x, m_coords[i].y);
194  ring->setPoint(m_coords.size(), m_coords[0].x, m_coords[0].y); // Closing...
195 
197  polygon->setRingN(0, ring);
198  polygon->setSRID(m_display->getSRID());
199 
200  te::gm::Geometry* aggr = te::gm::Validate(polygon);
201 
204 
205  if (!aggr->intersects(m_feature->getGeometry()))
206  {
207  m_isFinished = false;
208  return dynamic_cast<te::gm::Geometry*>(m_feature->getGeometry()->clone());
209  }
210 
211  geom = m_feature->getGeometry()->Union(aggr);
212 
213  return geom;
214 
215 }
216 
218 {
219  QPointF pixelOffset(4.0, 4.0);
220 
221  QRectF rect(pos - pixelOffset, pos + pixelOffset);
222 
223  // Converts rect boundary to world coordinates
224  QPointF ll(rect.left(), rect.bottom());
225  QPointF ur(rect.right(), rect.top());
226  ll = m_display->transform(ll);
227  ur = m_display->transform(ur);
228 
229  te::gm::Envelope env(ll.x(), ll.y(), ur.x(), ur.y());
230 
231  return env;
232 }
233 
235 {
237 }
238 
240 {
241  setCursor(Qt::WaitCursor);
242 
243  te::gm::Envelope env = buildEnvelope(pos);
244 
245  try
246  {
247  if (m_feature == nullptr)
249 
250  setCursor(Qt::ArrowCursor);
251  }
252  catch (std::exception& e)
253  {
254  setCursor(Qt::ArrowCursor);
255  QMessageBox::critical(m_display, tr("Error"), QString(tr("The geometry cannot be selected from the layer. Details:") + " %1.").arg(e.what()));
256  }
257 }
258 
260 {
261  if (m_feature == nullptr)
262  return;
263 
264  if (!m_isFinished)
265  return;
266 
268 
269  QUndoCommand* command = new AddCommand(m_display, m_layer, m_feature->clone()->getId());
270  m_stack.addUndoStack(command);
271 }
272 
274 {
276 
277  m_feature = nullptr;
278 }
virtual const std::string & getId() const
It returns the layer id.
bool mouseDoubleClickEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse double click events...
bool m_isFinished
A flag that indicates if the operations was finished.
Undo/Redo for add one components.
Definition: AddCommand.h:52
bool mouseReleaseEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse release events for ...
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
void pickFeature(const QPointF &pos)
te::da::ObjectId * getId() const
Definition: Feature.cpp:134
This class implements a concrete tool to create lines.
Feature * clone() const
Definition: Feature.cpp:182
void addWatch(Feature *feature)
te::gm::Geometry * getGeometry() const
Definition: Feature.cpp:139
A widget to control the display of a set of layers.
te::gm::Envelope buildEnvelope(const QPointF &pos)
virtual Geometry * Union(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns a geometric object that represents the point set union with another geometry.
virtual bool intersects(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns true if the geometry object spatially intersects rhs geometry.
te::gm::Geometry * buildPolygon()
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
void addUndoStack(QUndoCommand *command)
Method that insert command Undo/Redo of type AddCommand in the Undo/Redo stack.
int getSRID() const _NOEXCEPT_OP(true)
It returns the Spatial Reference System ID associated to this geometric object.
virtual QPointF transform(const QPointF &p)
Transforms the given point, in screen coordinates, to a point in world coordinates.
static T & getInstance()
It returns a reference to the singleton instance.
Definition: Singleton.h:126
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
An Envelope defines a 2D rectangular region.
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
std::vector< te::gm::Coord2D > m_coords
The coord list managed by this tool.
TEEDITEXPORT Feature * PickFeature(const te::map::AbstractLayerPtr &layer, const te::gm::Envelope &env, int srid)
TEEDITQTEXPORT QPointF GetPosition(QMouseEvent *e)
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
virtual const te::gm::Envelope & getExtent() const
It returns the world extent showned by the MapDisplay.
void draw(te::gm::Geometry *geom, bool showVertexes=false)
Definition: Renderer.cpp:260
virtual AbstractData * clone() const =0
It returns a clone of this object.
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
bool mouseMoveEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse move events for the...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
This is a singleton for rendering geometries and features.
Definition: Renderer.h:70
te::map::AbstractLayer * m_layer
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
virtual QPixmap * getDraftPixmap() const
It returns the map display draft pixmap.
AggregateAreaTool(te::qt::widgets::MapDisplay *display, const te::map::AbstractLayerPtr &layer, QObject *parent=0)
void begin(QPaintDevice *device, const te::gm::Envelope &e, int srid)
Definition: Renderer.cpp:59
void setCursor(const QCursor &cursor)
It sets the tool cursor.
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
bool m_continuousMode
A flag that indicates if the tool is working in &#39;continuous mode&#39;. i.e. the coordinates will be acqui...
void setGeometry(te::gm::Geometry *geom)
Definition: Feature.cpp:95
virtual void transform(int srid) _NOEXCEPT_OP(false)=0
It converts the coordinate values of the geometry to the new spatial reference system.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
TEGEOMEXPORT te::gm::Geometry * Validate(te::gm::Geometry *geom)
Get/create a valid version of the geometry given. If the geometry is a polygon or multi polygon...
bool isValid() const
It tells if the rectangle is valid or not.
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
void setRingN(std::size_t i, Curve *r)
It sets the informed position ring to the new one.