Skip to content

Commit 51f06ed

Browse files
committed
Allow override WkbPtr and define WkbSimplifierPtr class
This commit simplifies the geometry just before to paint it in the current QPainter device. This commit allows to override the '>>' operators of the QgsWkbPtr class. Also, It defines a new QgsWkbSimplifierPtr class to automatically simplify the input point stream.
1 parent c4d7981 commit 51f06ed

19 files changed

+270
-59
lines changed

python/core/qgsclipper.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,5 @@ class QgsClipper
5656
@param wkb pointer to the start of the line wkb
5757
@param clipExtent clipping bounds
5858
@param line out: clipped line coordinates*/
59-
static QgsConstWkbPtr clippedLineWKB( QgsConstWkbPtr wkb, const QgsRectangle& clipExtent, QPolygonF& line );
59+
static QgsConstWkbPtr clippedLineWKB( QgsConstWkbPtr& wkb, const QgsRectangle& clipExtent, QPolygonF& line );
6060
};

python/core/qgsmaptopixelgeometrysimplifier.sip

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class QgsMapToPixelSimplifier : QgsAbstractGeometrySimplifier
2929
//! Simplifies the specified geometry
3030
virtual bool simplifyGeometry( QgsGeometry* geometry ) const;
3131

32+
//! Simplifies the specified WKB-point array
33+
virtual bool simplifyPoints( QgsWKBTypes::Type wkbType, QgsConstWkbPtr& sourceWkbPtr, QPolygonF& targetPoints ) const;
34+
3235
// MapToPixel simplification helper methods
3336
public:
3437

@@ -41,4 +44,7 @@ class QgsMapToPixelSimplifier : QgsAbstractGeometrySimplifier
4144
//! Simplifies the geometry when is applied the specified map2pixel context
4245
static bool simplifyGeometry( QgsGeometry* geometry, int simplifyFlags, double tolerance );
4346

47+
//! Simplifies the WKB-point array when is applied the specified map2pixel context
48+
static bool simplifyPoints( QgsWKBTypes::Type wkbType, QgsConstWkbPtr& sourceWkbPtr, QPolygonF& targetPoints, int simplifyFlags, double tolerance );
49+
4450
};

python/core/symbology-ng/qgsrendererv2.sip

+3-3
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,9 @@ class QgsFeatureRendererV2
403403
//! render editing vertex marker for a polygon
404404
void renderVertexMarkerPolygon( QPolygonF& pts, QList<QPolygonF>* rings, QgsRenderContext& context );
405405

406-
static QgsConstWkbPtr _getPoint( QPointF& pt, QgsRenderContext& context, QgsConstWkbPtr wkb );
407-
static QgsConstWkbPtr _getLineString( QPolygonF& pts, QgsRenderContext& context, QgsConstWkbPtr wkb, bool clipToExtent = true );
408-
static QgsConstWkbPtr _getPolygon( QPolygonF& pts, QList<QPolygonF>& holes, QgsRenderContext& context, QgsConstWkbPtr wkb, bool clipToExtent = true );
406+
static QgsConstWkbPtr _getPoint( QPointF& pt, QgsRenderContext& context, QgsConstWkbPtr& wkb );
407+
static QgsConstWkbPtr _getLineString( QPolygonF& pts, QgsRenderContext& context, QgsConstWkbPtr& wkb, bool clipToExtent = true );
408+
static QgsConstWkbPtr _getPolygon( QPolygonF& pts, QList<QPolygonF>& holes, QgsRenderContext& context, QgsConstWkbPtr& wkb, bool clipToExtent = true );
409409

410410
void setScaleMethodToSymbol( QgsSymbolV2* symbol, int scaleMethod );
411411

python/core/symbology-ng/qgssymbolv2.sip

+3-3
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,19 @@ class QgsSymbolV2
228228
* Creates a point in screen coordinates from a wkb string in map
229229
* coordinates
230230
*/
231-
static QgsConstWkbPtr _getPoint( QPointF& pt, QgsRenderContext& context, QgsConstWkbPtr wkb );
231+
static QgsConstWkbPtr _getPoint( QPointF& pt, QgsRenderContext& context, QgsConstWkbPtr& wkb );
232232

233233
/**
234234
* Creates a line string in screen coordinates from a wkb string in map
235235
* coordinates
236236
*/
237-
static QgsConstWkbPtr _getLineString( QPolygonF& pts, QgsRenderContext& context, QgsConstWkbPtr wkb, bool clipToExtent = true );
237+
static QgsConstWkbPtr _getLineString( QPolygonF& pts, QgsRenderContext& context, QgsConstWkbPtr& wkb, bool clipToExtent = true );
238238

239239
/**
240240
* Creates a polygon in screen coordinates from a wkb string in map
241241
* coordinates
242242
*/
243-
static QgsConstWkbPtr _getPolygon( QPolygonF& pts, QList<QPolygonF>& holes, QgsRenderContext& context, QgsConstWkbPtr wkb, bool clipToExtent = true );
243+
static QgsConstWkbPtr _getPolygon( QPolygonF& pts, QList<QPolygonF>& holes, QgsRenderContext& context, QgsConstWkbPtr& wkb, bool clipToExtent = true );
244244

245245
/**
246246
* Retrieve a cloned list of all layers that make up this symbol.

src/core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ SET(QGIS_CORE_SRCS
352352
geometry/qgspolygonv2.cpp
353353
geometry/qgswkbptr.cpp
354354
geometry/qgswkbtypes.cpp
355+
geometry/qgswkbsimplifierptr.cpp
355356

356357
${CMAKE_CURRENT_BINARY_DIR}/qgscontexthelp_texts.cpp
357358
${CMAKE_CURRENT_BINARY_DIR}/qgsexpression_texts.cpp
@@ -836,6 +837,7 @@ SET(QGIS_CORE_HDRS
836837
geometry/qgspointv2.h
837838
geometry/qgswkbptr.h
838839
geometry/qgswkbtypes.h
840+
geometry/qgswkbsimplifierptr.h
839841
)
840842

841843
IF (QT_MOBILITY_LOCATION_FOUND OR Qt5Positioning_FOUND)

src/core/geometry/qgswkbptr.cpp

+30-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ QgsConstWkbPtr::QgsConstWkbPtr( const unsigned char *p, int size )
3232
mP = const_cast< unsigned char * >( p );
3333
mEnd = mP + size;
3434
mEndianSwap = false;
35+
mWkbType = QgsWKBTypes::Unknown;
3536
}
3637

3738
QgsWKBTypes::Type QgsConstWkbPtr::readHeader() const
@@ -45,12 +46,40 @@ QgsWKBTypes::Type QgsConstWkbPtr::readHeader() const
4546

4647
int wkbType;
4748
*this >> wkbType;
49+
mWkbType = static_cast<QgsWKBTypes::Type>( wkbType );
4850

49-
return static_cast<QgsWKBTypes::Type>( wkbType );
51+
return mWkbType;
5052
}
5153

5254
void QgsConstWkbPtr::verifyBound( int size ) const
5355
{
5456
if ( !mP || mP + size > mEnd )
5557
throw QgsWkbException( "wkb access out of bounds" );
5658
}
59+
60+
const QgsConstWkbPtr &QgsConstWkbPtr::operator>>( QPointF &point ) const
61+
{
62+
read( point.rx() );
63+
read( point.ry() );
64+
return *this;
65+
}
66+
67+
const QgsConstWkbPtr &QgsConstWkbPtr::operator>>( QPolygonF &points ) const
68+
{
69+
int skipZM = ( QgsWKBTypes::coordDimensions( mWkbType ) - 2 ) * sizeof( double );
70+
Q_ASSERT( skipZM >= 0 );
71+
72+
unsigned int nPoints;
73+
read( nPoints );
74+
75+
points.resize( nPoints );
76+
QPointF* ptr = points.data();
77+
78+
for ( unsigned int i = 0; i < nPoints; ++i, ++ptr )
79+
{
80+
read( ptr->rx() );
81+
read( ptr->ry() );
82+
mP += skipZM;
83+
}
84+
return *this;
85+
}

src/core/geometry/qgswkbptr.h

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgsapplication.h"
2020
#include "qgis.h"
2121
#include "qgsexception.h"
22+
#include "qpolygon.h"
2223

2324
/** \ingroup core
2425
* * Custom exception class for Wkb related exceptions.
@@ -89,9 +90,11 @@ class CORE_EXPORT QgsWkbPtr
8990

9091
class CORE_EXPORT QgsConstWkbPtr
9192
{
93+
protected:
9294
mutable unsigned char *mP;
9395
unsigned char *mEnd;
9496
mutable bool mEndianSwap;
97+
mutable QgsWKBTypes::Type mWkbType;
9598

9699
void verifyBound( int size ) const;
97100

@@ -114,6 +117,9 @@ class CORE_EXPORT QgsConstWkbPtr
114117
inline const QgsConstWkbPtr &operator>>( unsigned int &v ) const { read( v ); return *this; }
115118
inline const QgsConstWkbPtr &operator>>( char &v ) const { read( v ); return *this; }
116119

120+
virtual const QgsConstWkbPtr &operator>>( QPointF &point ) const;
121+
virtual const QgsConstWkbPtr &operator>>( QPolygonF &points ) const;
122+
117123
inline void operator+=( int n ) { verifyBound( n ); mP += n; }
118124
inline void operator-=( int n ) { mP -= n; }
119125

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/***************************************************************************
2+
qgswkbsimplifierptr.cpp
3+
---------------------
4+
begin : February 2016
5+
copyright : (C) 2016 by Alvaro Huarte
6+
email : http://wiki.osgeo.org/wiki/Alvaro_Huarte
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#include "qgswkbsimplifierptr.h"
16+
#include "qgsgeometry.h"
17+
#include "qgssimplifymethod.h"
18+
#include "qgsmaptopixelgeometrysimplifier.h"
19+
20+
QgsConstWkbSimplifierPtr::QgsConstWkbSimplifierPtr( const unsigned char *p, int size, const QgsVectorSimplifyMethod &simplifyMethod )
21+
: QgsConstWkbPtr( p, size )
22+
, mSimplifyMethod( simplifyMethod )
23+
{
24+
}
25+
26+
const QgsConstWkbPtr &QgsConstWkbSimplifierPtr::operator>>( QPointF &point ) const
27+
{
28+
return QgsConstWkbPtr::operator>>( point );
29+
}
30+
31+
const QgsConstWkbPtr &QgsConstWkbSimplifierPtr::operator>>( QPolygonF &points ) const
32+
{
33+
if ( mSimplifyMethod.simplifyHints() != QgsVectorSimplifyMethod::NoSimplification && mSimplifyMethod.forceLocalOptimization() )
34+
{
35+
int simplifyHints = mSimplifyMethod.simplifyHints() | QgsMapToPixelSimplifier::SimplifyEnvelope;
36+
37+
QgsConstWkbPtr wkbPtr = *this;
38+
39+
if ( QgsMapToPixelSimplifier::simplifyPoints( mWkbType, wkbPtr, points, simplifyHints, mSimplifyMethod.tolerance() ) )
40+
{
41+
mP = const_cast< unsigned char * >(( const unsigned char * ) wkbPtr );
42+
return *this;
43+
}
44+
}
45+
QgsConstWkbPtr::operator>>( points );
46+
return *this;
47+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/***************************************************************************
2+
qgswkbsimplifierptr.h
3+
---------------------
4+
begin : February 2016
5+
copyright : (C) 2016 by Alvaro Huarte
6+
email : http://wiki.osgeo.org/wiki/Alvaro_Huarte
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#ifndef QGSWKBSIMPLIFIERPTR_H
16+
#define QGSWKBSIMPLIFIERPTR_H
17+
18+
#include "qgswkbptr.h"
19+
#include "qgsvectorsimplifymethod.h"
20+
21+
class QgsGeometry;
22+
23+
/** \class QgsConstWkbSimplifierPtr
24+
* \note not available in Python bindings
25+
*/
26+
27+
class CORE_EXPORT QgsConstWkbSimplifierPtr : public QgsConstWkbPtr
28+
{
29+
/** Simplification object which holds the information about how to simplify the features for fast rendering */
30+
const QgsVectorSimplifyMethod& mSimplifyMethod;
31+
32+
public:
33+
QgsConstWkbSimplifierPtr( const unsigned char *p, int size, const QgsVectorSimplifyMethod &simplifyMethod );
34+
35+
inline const QgsConstWkbPtr &operator>>( double &v ) const { return QgsConstWkbPtr::operator>>( v ); }
36+
inline const QgsConstWkbPtr &operator>>( float &r ) const { return QgsConstWkbPtr::operator>>( r ); }
37+
inline const QgsConstWkbPtr &operator>>( int &v ) const { return QgsConstWkbPtr::operator>>( v ); }
38+
inline const QgsConstWkbPtr &operator>>( unsigned int &v ) const { return QgsConstWkbPtr::operator>>( v ); }
39+
inline const QgsConstWkbPtr &operator>>( char &v ) const { return QgsConstWkbPtr::operator>>( v ); }
40+
41+
virtual const QgsConstWkbPtr &operator>>( QPointF &point ) const override;
42+
virtual const QgsConstWkbPtr &operator>>( QPolygonF &points ) const override;
43+
44+
inline void operator+=( int n ) { QgsConstWkbPtr::operator+=( n ); }
45+
inline void operator-=( int n ) { QgsConstWkbPtr::operator-=( n ); }
46+
47+
inline operator const unsigned char *() const { return mP; }
48+
};
49+
50+
#endif // QGSWKBSIMPLIFIERPTR_H

src/core/qgsclipper.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const double QgsClipper::MIN_Y = -16000;
3838

3939
const double QgsClipper::SMALL_NUM = 1e-12;
4040

41-
QgsConstWkbPtr QgsClipper::clippedLineWKB( QgsConstWkbPtr wkbPtr, const QgsRectangle& clipExtent, QPolygonF& line )
41+
QgsConstWkbPtr QgsClipper::clippedLineWKB( QgsConstWkbPtr& wkbPtr, const QgsRectangle& clipExtent, QPolygonF& line )
4242
{
4343
QgsWKBTypes::Type wkbType = wkbPtr.readHeader();
4444

@@ -57,24 +57,31 @@ QgsConstWkbPtr QgsClipper::clippedLineWKB( QgsConstWkbPtr wkbPtr, const QgsRecta
5757
double p1x_c, p1y_c; //clipped end coordinates
5858
double lastClipX = 0.0, lastClipY = 0.0; //last successfully clipped coords
5959

60+
QPolygonF pts;
61+
wkbPtr -= sizeof( unsigned int );
62+
wkbPtr >> pts;
63+
nPoints = pts.size();
64+
6065
line.clear();
6166
line.reserve( nPoints + 1 );
6267

63-
for ( int i = 0; i < nPoints; ++i )
68+
QPointF *ptr = pts.data();
69+
70+
for ( int i = 0; i < nPoints; ++i, ++ptr )
6471
{
6572
if ( i == 0 )
6673
{
67-
wkbPtr >> p1x >> p1y;
68-
wkbPtr += skipZM;
74+
p1x = ptr->rx();
75+
p1y = ptr->ry();
6976
continue;
7077
}
7178
else
7279
{
7380
p0x = p1x;
7481
p0y = p1y;
7582

76-
wkbPtr >> p1x >> p1y;
77-
wkbPtr += skipZM;
83+
p1x = ptr->rx();
84+
p1y = ptr->ry();
7885

7986
p1x_c = p1x;
8087
p1y_c = p1y;

src/core/qgsclipper.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class CORE_EXPORT QgsClipper
8787
@param wkb pointer to the start of the line wkb
8888
@param clipExtent clipping bounds
8989
@param line out: clipped line coordinates*/
90-
static QgsConstWkbPtr clippedLineWKB( QgsConstWkbPtr wkb, const QgsRectangle& clipExtent, QPolygonF& line );
90+
static QgsConstWkbPtr clippedLineWKB( QgsConstWkbPtr& wkb, const QgsRectangle& clipExtent, QPolygonF& line );
9191

9292
private:
9393

0 commit comments

Comments
 (0)