QGIS API Documentation  2.4.0-Chugiak
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsgeometrysimplifier.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometrysimplifier.cpp
3  ---------------------
4  begin : December 2013
5  copyright : (C) 2013 by Alvaro Huarte
6  email : http://wiki.osgeo.org/wiki/Alvaro_Huarte
7 
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include <limits>
18 #include "qgsgeometrysimplifier.h"
19 
21 {
22 }
23 
26 {
27  return ( envelope.xMaximum() - envelope.xMinimum() ) < mapToPixelTol && ( envelope.yMaximum() - envelope.yMinimum() ) < mapToPixelTol;
28 }
29 
31 bool QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( const QVector<QPointF>& points, float mapToPixelTol )
32 {
33  double xmin = std::numeric_limits<double>::max(), x, y;
34  double ymin = std::numeric_limits<double>::max();
35  double xmax = -std::numeric_limits<double>::max();
36  double ymax = -std::numeric_limits<double>::max();
37 
38  for ( int i = 0, numPoints = points.size(); i < numPoints; ++i )
39  {
40  x = points[i].x();
41  y = points[i].y();
42 
43  if ( xmin > x ) xmin = x;
44  if ( ymin > y ) ymin = y;
45  if ( xmax < x ) xmax = x;
46  if ( ymax < y ) ymax = y;
47  }
48  return canbeGeneralizedByDeviceBoundingBox( QgsRectangle( xmin, ymin, xmax, ymax ), mapToPixelTol );
49 }
50 
51 /***************************************************************************/
55 QgsTopologyPreservingSimplifier::QgsTopologyPreservingSimplifier( double tolerance ) : mTolerance( tolerance )
56 {
57 }
59 {
60 }
61 
64 {
65  return geometry->simplify( mTolerance );
66 }
67 
70 {
71  QgsGeometry* g = geometry->simplify( mTolerance );
72 
73  if ( g )
74  {
75  size_t wkbSize = g->wkbSize();
76  unsigned char* wkb = ( unsigned char* )malloc( wkbSize );
77  memcpy( wkb, g->asWkb(), wkbSize );
78  geometry->fromWkb( wkb, wkbSize );
79  delete g;
80 
81  return true;
82  }
83  return false;
84 }
A rectangle specified with double values.
Definition: qgsrectangle.h:35
size_t wkbSize() const
Returns the size of the WKB in asWkb().
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:194
double ANALYSIS_EXPORT max(double x, double y)
returns the maximum of two doubles or the first argument if both are equal
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:199
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:184
double mTolerance
Distance tolerance for the simplification.
QgsGeometry * simplify(double tolerance)
Returns a simplified version of this geometry using a specified tolerance value.
virtual bool simplifyGeometry(QgsGeometry *geometry) const
Simplifies the specified geometry.
void fromWkb(unsigned char *wkb, size_t length)
Set the geometry, feeding in the buffer containing OGC Well-Known Binary and the buffer's length...
QgsTopologyPreservingSimplifier(double tolerance)
Implementation of GeometrySimplifier using the Douglas-Peucker algorithm.
const unsigned char * asWkb() const
Returns the buffer containing this geometry in WKB format.
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:189
static bool canbeGeneralizedByDeviceBoundingBox(const QgsRectangle &envelope, float mapToPixelTol=1.0f)
Returns whether the device-envelope can be replaced by its BBOX when is applied the specified toleran...
virtual QgsGeometry * simplify(QgsGeometry *geometry) const
Returns a simplified version the specified geometry.