DeletePartTool.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 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/DeletePartTool.cpp
22 
23  \brief This class implements a concrete tool to delete parts of multi features (deleting polygons of a multi feature polygons for example).
24 */
25 
26 // TerraLib
27 #include "../../../geometry/GeometryProperty.h"
28 #include "../../../geometry/MultiLineString.h"
29 #include "../../../geometry/MultiPoint.h"
30 #include "../../../geometry/MultiPolygon.h"
31 #include "../../../geometry/Utils.h"
32 #include "../../../dataaccess/dataset/ObjectIdSet.h"
33 #include "../../../dataaccess/utils/Utils.h"
34 #include "../../../qt/widgets/canvas/MapDisplay.h"
35 #include "../../../qt/widgets/Utils.h"
36 #include "../../Feature.h"
37 #include "../../RepositoryManager.h"
38 #include "../../Utils.h"
39 #include "../Renderer.h"
40 #include "../Utils.h"
41 #include "DeletePartTool.h"
42 
43 // Qt
44 #include <QMessageBox>
45 #include <QMouseEvent>
46 
47 // STL
48 #include <cassert>
49 #include <memory>
50 
52  : GeometriesUpdateTool(display, layer.get(), parent)
53 {
54  updateCursor();
55 }
56 
58 
60 {
61  if (e->button() != Qt::LeftButton)
62  return false;
63 
64  m_mbr = buildEnvelope(e->pos());
65  if (!m_mbr.isValid())
66  return false;
67 
69  if (m_feature)
70  {
71  storeFeature();
72  }
73  else
74  {
75  QMessageBox::critical(m_display, tr("Error"), QString(tr("The geometry cannot be selected from the layer.")));
76  return false;
77  }
78 
79  return true;
80 }
81 
83 {
84  QPointF pixelOffset(4.0, 4.0);
85 
86  QRectF rect(pos - pixelOffset, pos + pixelOffset);
87 
88  // Converts rect boundary to world coordinates
89  QPointF ll(rect.left(), rect.bottom());
90  QPointF ur(rect.right(), rect.top());
91  ll = m_display->transform(ll);
92  ur = m_display->transform(ur);
93 
94  te::gm::Envelope env(ll.x(), ll.y(), ur.x(), ur.y());
95 
96  return env;
97 }
98 
100 {
102 
103  // Generates a geometry from the given extent. It will be used to refine the results
104  std::unique_ptr<te::gm::Geometry> geometryFromEnvelope(te::gm::GetGeomFromEnvelope(&m_mbr, m_layer->getSRID()));
105 
106  // The restriction point. It will be used to refine the results
107  te::gm::Coord2D center = m_mbr.getCenter();
108  te::gm::Point point(center.x, center.y, m_layer->getSRID());
109 
110  switch (m_feature->getGeometry()->getGeomTypeId())
111  {
113  {
115 
116  std::size_t i = 0;
117  while (i < mp->getNumGeometries() && mp->getNumGeometries() > 1)
118  {
119  te::gm::Polygon* p = dynamic_cast<te::gm::Polygon*>(mp->getGeometryN(i));
120  if (p->contains(&point) || p->crosses(geometryFromEnvelope.get()) || geometryFromEnvelope->contains(p))
121  {
122  mp->removeGeometryN(i);
123  i--;
124  }
125  else
126  i++;
127  }
128 
129  m_feature->clone()->setGeometry(mp);
130  }
131  break;
132 
134  {
136 
137  std::size_t i = 0;
138  while (i < ml->getNumGeometries() && ml->getNumGeometries() > 1)
139  {
140  te::gm::LineString* l = dynamic_cast<te::gm::LineString*>(ml->getGeometryN(i));
141  if (l->contains(&point) || l->crosses(geometryFromEnvelope.get()) || geometryFromEnvelope->contains(l))
142  {
143  ml->removeGeometryN(i);
144  i--;
145  }
146  else
147  i++;
148  }
149 
150  m_feature->clone()->setGeometry(ml);
151  }
152  break;
153 
155  {
157 
158  std::size_t i = 0;
159  while (i < mpt->getNumGeometries() && mpt->getNumGeometries() > 1)
160  {
161  te::gm::Point* pt = dynamic_cast<te::gm::Point*>(mpt->getGeometryN(i));
162  if (pt->contains(&point) || pt->crosses(geometryFromEnvelope.get()) || geometryFromEnvelope->contains(pt))
163  {
164  mpt->removeGeometryN(i);
165  i--;
166  }
167  else
168  i++;
169  }
170 
171  m_feature->clone()->setGeometry(mpt);
172  }
173  break;
174 
175  default:
176  oType = DELETE;
177  break;
178  }
179 
181 
183 
184  emit geometriesEdited();
185 }
186 
188 {
189  setCursor(Qt::ArrowCursor);
190 }
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
virtual const std::string & getId() const
It returns the layer id.
MultiPolygon is a MultiSurface whose elements are Polygons.
Definition: MultiPolygon.h:50
double y
y-coordinate.
Definition: Coord2D.h:114
double x
x-coordinate.
Definition: Coord2D.h:113
GeomType getGeomTypeId() const _NOEXCEPT_OP(true)
It returns the geometry subclass type identifier.
Feature * clone() const
Definition: Feature.cpp:182
te::gm::Geometry * getGeometry() const
Definition: Feature.cpp:139
A widget to control the display of a set of layers.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
void removeGeometryN(std::size_t i)
It removes the n-th geometry in this geometry collection.
Coord2D getCenter() const
It returns the rectangle&#39;s center coordinate.
bool mousePressEvent(QMouseEvent *e)
This event handler can be reimplemented in a concrete tool class to receive mouse press events for th...
~DeletePartTool()
Destructor.
MultiPoint is a GeometryCollection whose elements are restricted to points.
Definition: MultiPoint.h:50
virtual QPointF transform(const QPointF &p)
Transforms the given point, in screen coordinates, to a point in world coordinates.
LineString is a curve with linear interpolation between points.
Definition: LineString.h:62
void setOperationTypeId(const te::edit::OperationType &currentOperationType)
Definition: Feature.cpp:124
static RepositoryManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
Definition: Point.h:50
An Envelope defines a 2D rectangular region.
virtual int getSRID() const
It return the Spatial Reference System used by the Map Display.
TEEDITEXPORT Feature * PickFeature(const te::map::AbstractLayerPtr &layer, const te::gm::Envelope &env, int srid)
This class implements a concrete tool to delete parts of multi features (deleting polygons of a multi...
te::gm::Polygon * p
virtual bool contains(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns true if this geometry object spatially contains rhs geometry.
te::gm::Envelope buildEnvelope(const QPointF &pos)
te::map::AbstractLayer * m_layer
Geometry * getGeometryN(std::size_t i) const
It returns the n-th geometry in this GeometryCollection.
MultiLineString is a MultiCurve whose elements are LineStrings.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
DeletePartTool(te::qt::widgets::MapDisplay *display, const te::map::AbstractLayerPtr &layer, QObject *parent=0)
It constructs a move geometry tool associated with the given map display.
void setCursor(const QCursor &cursor)
It sets the tool cursor.
MapDisplay * m_display
The map display associated with the tool.
Definition: AbstractTool.h:171
virtual int getSRID() const
It returns the Spatial Reference System ID associated to the Layer.
void setGeometry(te::gm::Geometry *geom)
Definition: Feature.cpp:95
virtual bool crosses(const Geometry *const rhs) const _NOEXCEPT_OP(false)
It returns true if the geometry object spatially crosses rhs geometry.
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
bool isValid() const
It tells if the rectangle is valid or not.
te::gm::Envelope m_mbr
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.