QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgsmaptopixelgeometrysimplifier.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptopixelgeometrysimplifier.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 <memory>
19
21 #include "qgsapplication.h"
22 #include "qgslogger.h"
23 #include "qgsrectangle.h"
24 #include "qgswkbptr.h"
25 #include "qgsgeometry.h"
26 #include "qgslinestring.h"
27 #include "qgspolygon.h"
28 #include "qgsgeometrycollection.h"
29
30 QgsMapToPixelSimplifier::QgsMapToPixelSimplifier( int simplifyFlags, double tolerance, SimplifyAlgorithm simplifyAlgorithm )
31  : mSimplifyFlags( simplifyFlags )
32  , mSimplifyAlgorithm( simplifyAlgorithm )
33  , mTolerance( tolerance )
34 {
35 }
36
38 // Helper simplification methods
39
40 float QgsMapToPixelSimplifier::calculateLengthSquared2D( double x1, double y1, double x2, double y2 )
41 {
42  float vx = static_cast< float >( x2 - x1 );
43  float vy = static_cast< float >( y2 - y1 );
44
45  return ( vx * vx ) + ( vy * vy );
46 }
47
48 bool QgsMapToPixelSimplifier::equalSnapToGrid( double x1, double y1, double x2, double y2, double gridOriginX, double gridOriginY, float gridInverseSizeXY )
49 {
50  int grid_x1 = std::round( ( x1 - gridOriginX ) * gridInverseSizeXY );
51  int grid_x2 = std::round( ( x2 - gridOriginX ) * gridInverseSizeXY );
52  if ( grid_x1 != grid_x2 ) return false;
53
54  int grid_y1 = std::round( ( y1 - gridOriginY ) * gridInverseSizeXY );
55  int grid_y2 = std::round( ( y2 - gridOriginY ) * gridInverseSizeXY );
56  return grid_y1 == grid_y2;
57 }
58
60 // Helper simplification methods for Visvalingam method
61
62 // It uses a refactored code of the liblwgeom implementation:
63 // https://github.com/postgis/postgis/blob/svn-trunk/liblwgeom/effectivearea.h
64 // https://github.com/postgis/postgis/blob/svn-trunk/liblwgeom/effectivearea.c
65
66 #include "simplify/effectivearea.h"
67
69
71 static std::unique_ptr< QgsAbstractGeometry > generalizeWkbGeometryByBoundingBox(
72  QgsWkbTypes::Type wkbType,
73  const QgsAbstractGeometry &geometry,
74  const QgsRectangle &envelope,
75  bool isRing )
76 {
77  unsigned int geometryType = QgsWkbTypes::singleType( QgsWkbTypes::flatType( wkbType ) );
78
79  // If the geometry is already minimal skip the generalization
80  int minimumSize = geometryType == QgsWkbTypes::LineString ? 2 : 5;
81
82  if ( geometry.nCoordinates() <= minimumSize )
83  {
84  return std::unique_ptr< QgsAbstractGeometry >( geometry.clone() );
85  }
86
87  const double x1 = envelope.xMinimum();
88  const double y1 = envelope.yMinimum();
89  const double x2 = envelope.xMaximum();
90  const double y2 = envelope.yMaximum();
91
92  // Write the generalized geometry
93  if ( geometryType == QgsWkbTypes::LineString && !isRing )
94  {
95  return qgis::make_unique< QgsLineString >( QVector<double>() << x1 << x2, QVector<double>() << y1 << y2 );
96  }
97  else
98  {
99  std::unique_ptr< QgsLineString > ext = qgis::make_unique< QgsLineString >(
100  QVector< double >() << x1
101  << x2
102  << x2
103  << x1
104  << x1,
105  QVector< double >() << y1
106  << y1
107  << y2
108  << y2
109  << y1 );
110  if ( geometryType == QgsWkbTypes::LineString )
111  return std::move( ext );
112  else
113  {
114  std::unique_ptr< QgsPolygon > polygon = qgis::make_unique< QgsPolygon >();
115  polygon->setExteriorRing( ext.release() );
116  return std::move( polygon );
117  }
118  }
119 }
120
121 std::unique_ptr< QgsAbstractGeometry > QgsMapToPixelSimplifier::simplifyGeometry( int simplifyFlags,
123  const QgsAbstractGeometry &geometry, double map2pixelTol,
124  bool isaLinearRing )
125 {
126  bool isGeneralizable = true;
127  QgsWkbTypes::Type wkbType = geometry.wkbType();
128
129  // Can replace the geometry by its BBOX ?
130  QgsRectangle envelope = geometry.boundingBox();
132  isGeneralizableByMapBoundingBox( envelope, map2pixelTol ) )
133  {
134  return generalizeWkbGeometryByBoundingBox( wkbType, geometry, envelope, isaLinearRing );
135  }
136
138  isGeneralizable = false;
139
140  const QgsWkbTypes::Type flatType = QgsWkbTypes::flatType( wkbType );
141
142  // Write the geometry
143  if ( flatType == QgsWkbTypes::LineString || flatType == QgsWkbTypes::CircularString )
144  {
145  const QgsCurve &srcCurve = dynamic_cast<const QgsCurve &>( geometry );
146  const int numPoints = srcCurve.numPoints();
147
148  std::unique_ptr<QgsCurve> output;
149
150  QVector< double > lineStringX;
151  QVector< double > lineStringY;
152  if ( flatType == QgsWkbTypes::LineString )
153  {
154  // if we are making a linestring, we do it in an optimised way by directly constructing
155  // the final x/y vectors, which avoids calling the slower insertVertex method
156  lineStringX.reserve( numPoints );
157  lineStringY.reserve( numPoints );
158  }
159  else
160  {
161  output.reset( qgsgeometry_cast< QgsCurve * >( srcCurve.createEmptyWithSameType() ) );
162  }
163
164  double x = 0.0, y = 0.0, lastX = 0.0, lastY = 0.0;
165
166  if ( numPoints <= ( isaLinearRing ? 4 : 2 ) )
167  isGeneralizable = false;
168
169  bool isLongSegment;
170  bool hasLongSegments = false; //-> To avoid replace the simplified geometry by its BBOX when there are 'long' segments.
171
172  // Check whether the LinearRing is really closed.
173  if ( isaLinearRing )
174  {
175  isaLinearRing = qgsDoubleNear( srcCurve.xAt( 0 ), srcCurve.xAt( numPoints - 1 ) ) &&
176  qgsDoubleNear( srcCurve.yAt( 0 ), srcCurve.yAt( numPoints - 1 ) );
177  }
178
179  // Process each vertex...
180  switch ( simplifyAlgorithm )
181  {
182  case SnapToGrid:
183  {
184  double gridOriginX = envelope.xMinimum();
185  double gridOriginY = envelope.yMinimum();
186
187  // Use a factor for the maximum displacement distance for simplification, similar as GeoServer does
188  float gridInverseSizeXY = map2pixelTol != 0 ? ( float )( 1.0f / ( 0.8 * map2pixelTol ) ) : 0.0f;
189
190  const double *xData = nullptr;
191  const double *yData = nullptr;
192  if ( flatType == QgsWkbTypes::LineString )
193  {
194  xData = qgsgeometry_cast< const QgsLineString * >( &srcCurve )->xData();
195  yData = qgsgeometry_cast< const QgsLineString * >( &srcCurve )->yData();
196  }
197
198  for ( int i = 0; i < numPoints; ++i )
199  {
200  if ( xData && yData )
201  {
202  x = *xData++;
203  y = *yData++;
204  }
205  else
206  {
207  x = srcCurve.xAt( i );
208  y = srcCurve.yAt( i );
209  }
210
211  if ( i == 0 ||
212  !isGeneralizable ||
213  !equalSnapToGrid( x, y, lastX, lastY, gridOriginX, gridOriginY, gridInverseSizeXY ) ||
214  ( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
215  {
216  if ( output )
217  output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( x, y ) );
218  else
219  {
220  lineStringX.append( x );
221  lineStringY.append( y );
222  }
223  lastX = x;
224  lastY = y;
225  }
226  }
227  break;
228  }
229
230  case Visvalingam:
231  {
232  map2pixelTol *= map2pixelTol; //-> Use mappixelTol for 'Area' calculations.
233
234  EFFECTIVE_AREAS ea( srcCurve );
235
236  int set_area = 0;
237  ptarray_calc_areas( &ea, isaLinearRing ? 4 : 2, set_area, map2pixelTol );
238
239  for ( int i = 0; i < numPoints; ++i )
240  {
241  if ( ea.res_arealist[ i ] > map2pixelTol )
242  {
243  if ( output )
244  output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), ea.inpts.at( i ) );
245  else
246  {
247  lineStringX.append( ea.inpts.at( i ).x() );
248  lineStringY.append( ea.inpts.at( i ).y() );
249  }
250  }
251  }
252  break;
253  }
254
255  case Distance:
256  {
257  map2pixelTol *= map2pixelTol; //-> Use mappixelTol for 'LengthSquare' calculations.
258
259  const double *xData = nullptr;
260  const double *yData = nullptr;
261  if ( flatType == QgsWkbTypes::LineString )
262  {
263  xData = qgsgeometry_cast< const QgsLineString * >( &srcCurve )->xData();
264  yData = qgsgeometry_cast< const QgsLineString * >( &srcCurve )->yData();
265  }
266
267  for ( int i = 0; i < numPoints; ++i )
268  {
269  if ( xData && yData )
270  {
271  x = *xData++;
272  y = *yData++;
273  }
274  else
275  {
276  x = srcCurve.xAt( i );
277  y = srcCurve.yAt( i );
278  }
279
280  isLongSegment = false;
281
282  if ( i == 0 ||
283  !isGeneralizable ||
284  ( isLongSegment = ( calculateLengthSquared2D( x, y, lastX, lastY ) > map2pixelTol ) ) ||
285  ( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
286  {
287  if ( output )
288  output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( x, y ) );
289  else
290  {
291  lineStringX.append( x );
292  lineStringY.append( y );
293  }
294  lastX = x;
295  lastY = y;
296
297  hasLongSegments |= isLongSegment;
298  }
299  }
300  }
301  }
302
303  if ( !output )
304  {
305  output = qgis::make_unique< QgsLineString >( lineStringX, lineStringY );
306  }
307  if ( output->numPoints() < ( isaLinearRing ? 4 : 2 ) )
308  {
309  // we simplified the geometry too much!
310  if ( !hasLongSegments )
311  {
312  // approximate the geometry's shape by its bounding box
313  // (rect for linear ring / one segment for line string)
314  return generalizeWkbGeometryByBoundingBox( wkbType, geometry, envelope, isaLinearRing );
315  }
316  else
317  {
318  // Bad luck! The simplified geometry is invalid and approximation by bounding box
319  // would create artifacts due to long segments.
320  // We will return the original geometry
321  return std::unique_ptr< QgsAbstractGeometry >( geometry.clone() );
322  }
323  }
324
325  if ( isaLinearRing )
326  {
327  // make sure we keep the linear ring closed
328  if ( !qgsDoubleNear( lastX, output->xAt( 0 ) ) || !qgsDoubleNear( lastY, output->yAt( 0 ) ) )
329  {
330  output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( output->xAt( 0 ), output->yAt( 0 ) ) );
331  }
332  }
333
334  return std::move( output );
335  }
336  else if ( flatType == QgsWkbTypes::Polygon )
337  {
338  const QgsPolygon &srcPolygon = dynamic_cast<const QgsPolygon &>( geometry );
339  std::unique_ptr<QgsPolygon> polygon( new QgsPolygon() );
340  std::unique_ptr<QgsAbstractGeometry> extRing = simplifyGeometry( simplifyFlags, simplifyAlgorithm, *srcPolygon.exteriorRing(), map2pixelTol, true );
341  polygon->setExteriorRing( qgsgeometry_cast<QgsCurve *>( extRing.release() ) );
342  for ( int i = 0; i < srcPolygon.numInteriorRings(); ++i )
343  {
344  const QgsCurve *sub = srcPolygon.interiorRing( i );
345  std::unique_ptr< QgsAbstractGeometry > ring = simplifyGeometry( simplifyFlags, simplifyAlgorithm, *sub, map2pixelTol, true );
346  polygon->addInteriorRing( qgsgeometry_cast<QgsCurve *>( ring.release() ) );
347  }
348  return std::move( polygon );
349  }
350  else if ( QgsWkbTypes::isMultiType( flatType ) )
351  {
352  const QgsGeometryCollection &srcCollection = dynamic_cast<const QgsGeometryCollection &>( geometry );
353  std::unique_ptr<QgsGeometryCollection> collection( srcCollection.createEmptyWithSameType() );
354  const int numGeoms = srcCollection.numGeometries();
355  for ( int i = 0; i < numGeoms; ++i )
356  {
357  const QgsAbstractGeometry *sub = srcCollection.geometryN( i );
358  std::unique_ptr< QgsAbstractGeometry > part = simplifyGeometry( simplifyFlags, simplifyAlgorithm, *sub, map2pixelTol, false );
359  collection->addGeometry( part.release() );
360  }
361  return std::move( collection );
362  }
363  return std::unique_ptr< QgsAbstractGeometry >( geometry.clone() );
364 }
365
367
368 bool QgsMapToPixelSimplifier::isGeneralizableByMapBoundingBox( const QgsRectangle &envelope, double map2pixelTol )
369 {
370  // Can replace the geometry by its BBOX ?
371  return envelope.width() < map2pixelTol && envelope.height() < map2pixelTol;
372 }
373
375 {
376  if ( geometry.isNull() )
377  {
378  return QgsGeometry();
379  }
381  {
382  return geometry;
383  }
384
385  // Check whether the geometry can be simplified using the map2pixel context
386  const QgsWkbTypes::Type singleType = QgsWkbTypes::singleType( geometry.wkbType() );
387  const QgsWkbTypes::Type flatType = QgsWkbTypes::flatType( singleType );
388  if ( flatType == QgsWkbTypes::Point )
389  {
390  return geometry;
391  }
392
393  const bool isaLinearRing = flatType == QgsWkbTypes::Polygon;
394  const int numPoints = geometry.constGet()->nCoordinates();
395
396  if ( numPoints <= ( isaLinearRing ? 6 : 3 ) )
397  {
398  // No simplify simple geometries
399  return geometry;
400  }
401
402  const QgsRectangle envelope = geometry.boundingBox();
403  if ( std::max( envelope.width(), envelope.height() ) / numPoints > mTolerance * 2.0 )
404  {
405  //points are in average too far apart to lead to any significant simplification
406  return geometry;
407  }
408
409  return QgsGeometry( simplifyGeometry( mSimplifyFlags, mSimplifyAlgorithm, *geometry.constGet(), mTolerance, false ) );
410 }
A rectangle specified with double values.
Definition: qgsrectangle.h:41
static Type singleType(Type type)
Returns the single type for a WKB type.
Definition: qgswkbtypes.h:155
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:560
QgsWkbTypes::Type wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:265
const QgsCurve * interiorRing(int i) const
Retrieves an interior ring from the curve polygon.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
No simplification can be applied.
virtual QgsAbstractGeometry * createEmptyWithSameType() const =0
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
The simplification uses a grid (similar to ST_SnapToGrid) to remove duplicate points.
virtual QgsRectangle boundingBox() const =0
Returns the minimal bounding box for the geometry.
SimplifyAlgorithm
Types of simplification algorithms that can be used.
virtual QgsAbstractGeometry * clone() const =0
Clones the geometry by performing a deep copy.
static bool isGeneralizableByMapBoundingBox(const QgsRectangle &envelope, double map2pixelTol)
Returns whether the envelope can be replaced by its BBOX when is applied the specified map2pixel cont...
int numInteriorRings() const
Returns the number of interior rings contained with the curve polygon.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
static bool equalSnapToGrid(double x1, double y1, double x2, double y2, double gridOriginX, double gridOriginY, float gridInverseSizeXY)
Returns whether the points belong to the same grid.
SimplifyAlgorithm simplifyAlgorithm() const
Gets the local simplification algorithm of the vector layer managed.
Utility class for identifying a unique vertex within a geometry.
Geometry collection.
The geometries can be fully simplified by its BoundingBox.
double width() const
Returns the width of the rectangle.
Definition: qgsrectangle.h:202
QgsGeometryCollection * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
int simplifyFlags() const
Gets the simplification hints of the vector layer managed.
The simplification gives each point in a line an importance weighting, so that least important points...
T qgsgeometry_cast(const QgsAbstractGeometry *geom)
static float calculateLengthSquared2D(double x1, double y1, double x2, double y2)
Returns the squared 2D-distance of the vector defined by the two points specified.
virtual double xAt(int index) const =0
Returns the x-coordinate of the specified node in the line string.
Abstract base class for curved geometry type.
Definition: qgscurve.h:35
Abstract base class for all geometries.
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
double mTolerance
Distance tolerance for the simplification.
int numGeometries() const
Returns the number of geometries within the collection.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
Definition: qgsrectangle.h:177
double xMaximum() const
Returns the x maximum value (right side of rectangle).
Definition: qgsrectangle.h:162
QgsGeometry simplify(const QgsGeometry &geometry) const override
Returns a simplified version the specified geometry.
QgsMapToPixelSimplifier(int simplifyFlags, double tolerance, SimplifyAlgorithm simplifyAlgorithm=Distance)
Constructor.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
QgsRectangle boundingBox() const
Returns the bounding box of the geometry.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
virtual double yAt(int index) const =0
Returns the y-coordinate of the specified node in the line string.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
Definition: qgsrectangle.h:167
double yMaximum() const
Returns the y maximum value (top side of rectangle).
Definition: qgsrectangle.h:172
int mSimplifyFlags
Current simplification flags.
SimplifyAlgorithm mSimplifyAlgorithm
Current algorithm.
The geometries can be simplified using the current map2pixel context state.
Polygon geometry type.
Definition: qgspolygon.h:31
const QgsCurve * exteriorRing() const
Returns the curve polygon&#39;s exterior ring.
virtual int nCoordinates() const
Returns the number of nodes contained in the geometry.
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:430
virtual int numPoints() const =0
Returns the number of points in the curve.
The simplification uses the distance between points to remove duplicate points.
double height() const
Returns the height of the rectangle.
Definition: qgsrectangle.h:209