Skip to content

Commit f2d0100

Browse files
committed
Merge pull request #1094 from ahuarte47/Issue_8725R-maxscale
Feature #8725R: support for maximum scale at which the layer should be simplified
2 parents 52d114f + 031596c commit f2d0100

12 files changed

+86
-7
lines changed

python/core/qgsvectorlayer.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ class QgsVectorLayer : QgsMapLayer
10131013
const QgsVectorSimplifyMethod& simplifyMethod() const;
10141014

10151015
/** Returns whether the VectorLayer can apply the specified simplification hint */
1016-
bool simplifyDrawingCanbeApplied( int simplifyHint ) const;
1016+
bool simplifyDrawingCanbeApplied( const QgsRenderContext& renderContext, int simplifyHint ) const;
10171017

10181018
public slots:
10191019
/**

python/core/qgsvectorsimplifymethod.sip

+5
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ class QgsVectorSimplifyMethod
2626
void setForceLocalOptimization( bool localOptimization );
2727
/** Gets where the simplification executes, after fetch the geometries from provider, or when supported, in provider before fetch the geometries */
2828
bool forceLocalOptimization();
29+
30+
/** Sets the maximum scale at which the layer should be simplified */
31+
void setMaximumScale( float maximumScale );
32+
/** Gets the maximum scale at which the layer should be simplified */
33+
float maximumScale() const;
2934
};

src/app/qgsoptions.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,11 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
570570
mSimplifyDrawingSpinBox->setValue( settings.value( "/qgis/simplifyDrawingTol", QGis::DEFAULT_MAPTOPIXEL_THRESHOLD ).toFloat() );
571571
mSimplifyDrawingAtProvider->setChecked( !settings.value( "/qgis/simplifyLocal", true ).toBool() );
572572

573+
QStringList myScalesList = PROJECT_SCALES.split( "," );
574+
myScalesList.append( "1:1" );
575+
mSimplifyMaximumScaleComboBox->updateScales( myScalesList );
576+
mSimplifyMaximumScaleComboBox->setScale( 1.0 / settings.value( "/qgis/simplifyMaxScale", 1 ).toFloat() );
577+
573578
// Slightly awkard here at the settings value is true to use QImage,
574579
// but the checkbox is true to use QPixmap
575580
chkUseQPixmap->setChecked( !( settings.value( "/qgis/use_qimage_to_render", true ).toBool() ) );
@@ -1111,6 +1116,7 @@ void QgsOptions::saveOptions()
11111116
settings.setValue( "/qgis/simplifyDrawingHints", simplifyHints );
11121117
settings.setValue( "/qgis/simplifyDrawingTol", mSimplifyDrawingSpinBox->value() );
11131118
settings.setValue( "/qgis/simplifyLocal", !mSimplifyDrawingAtProvider->isChecked() );
1119+
settings.setValue( "/qgis/simplifyMaxScale", 1.0 / mSimplifyMaximumScaleComboBox->scale() );
11141120

11151121
// project
11161122
settings.setValue( "/qgis/projOpenAtLaunch", mProjectOnLaunchCmbBx->currentIndex() );

src/app/qgsvectorlayerproperties.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ void QgsVectorLayerProperties::syncToLayer( void )
416416
mSimplifyDrawingGroupBox->setChecked( false );
417417
}
418418

419+
QStringList myScalesList = PROJECT_SCALES.split( "," );
420+
myScalesList.append( "1:1" );
421+
mSimplifyMaximumScaleComboBox->updateScales( myScalesList );
422+
mSimplifyMaximumScaleComboBox->setScale( 1.0 / simplifyMethod.maximumScale() );
423+
419424
// load appropriate symbology page (V1 or V2)
420425
updateSymbologyPage();
421426

@@ -564,6 +569,7 @@ void QgsVectorLayerProperties::apply()
564569
simplifyMethod.setSimplifyHints( simplifyHints );
565570
simplifyMethod.setThreshold( mSimplifyDrawingSpinBox->value() );
566571
simplifyMethod.setForceLocalOptimization( !mSimplifyDrawingAtProvider->isChecked() );
572+
simplifyMethod.setMaximumScale( 1.0 / mSimplifyMaximumScaleComboBox->scale() );
567573
layer->setSimplifyMethod( simplifyMethod );
568574

569575
// update symbology

src/core/qgsvectorlayer.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
187187
mSimplifyMethod.setSimplifyHints( settings.value( "/qgis/simplifyDrawingHints", mSimplifyMethod.simplifyHints() ).toInt() );
188188
mSimplifyMethod.setThreshold( settings.value( "/qgis/simplifyDrawingTol", mSimplifyMethod.threshold() ).toFloat() );
189189
mSimplifyMethod.setForceLocalOptimization( settings.value( "/qgis/simplifyLocal", mSimplifyMethod.forceLocalOptimization() ).toBool() );
190+
mSimplifyMethod.setMaximumScale( settings.value( "/qgis/simplifyMaxScale", mSimplifyMethod.maximumScale() ).toFloat() );
190191

191192
} // QgsVectorLayer ctor
192193

@@ -701,7 +702,7 @@ bool QgsVectorLayer::draw( QgsRenderContext& rendererContext )
701702
.setSubsetOfAttributes( attributes );
702703

703704
// enable the simplification of the geometries (Using the current map2pixel context) before send it to renderer engine.
704-
if ( simplifyDrawingCanbeApplied( QgsVectorLayer::GeometrySimplification ) )
705+
if ( simplifyDrawingCanbeApplied( rendererContext, QgsVectorLayer::GeometrySimplification ) )
705706
{
706707
QPainter* p = rendererContext.painter();
707708
double dpi = ( p->device()->logicalDpiX() + p->device()->logicalDpiY() ) / 2;
@@ -1256,9 +1257,19 @@ bool QgsVectorLayer::setSubsetString( QString subset )
12561257
return res;
12571258
}
12581259

1259-
bool QgsVectorLayer::simplifyDrawingCanbeApplied( int simplifyHint ) const
1260+
bool QgsVectorLayer::simplifyDrawingCanbeApplied( const QgsRenderContext& renderContext, int simplifyHint ) const
12601261
{
1261-
return mDataProvider && !mEditBuffer && ( hasGeometryType() && geometryType() != QGis::Point ) && ( mSimplifyMethod.simplifyHints() & simplifyHint ) && ( !mCurrentRendererContext || mCurrentRendererContext->useRenderingOptimization() );
1262+
if ( mDataProvider && !mEditBuffer && ( hasGeometryType() && geometryType() != QGis::Point ) && ( mSimplifyMethod.simplifyHints() & simplifyHint ) && renderContext.useRenderingOptimization() )
1263+
{
1264+
double maximumSimplificationScale = mSimplifyMethod.maximumScale();
1265+
1266+
// check maximum scale at which generalisation should be carried out
1267+
if ( maximumSimplificationScale > 1 && renderContext.rendererScale() <= maximumSimplificationScale )
1268+
return false;
1269+
1270+
return true;
1271+
}
1272+
return false;
12621273
}
12631274

12641275
QgsFeatureIterator QgsVectorLayer::getFeatures( const QgsFeatureRequest& request )
@@ -1873,6 +1884,7 @@ bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage
18731884
mSimplifyMethod.setSimplifyHints( e.attribute( "simplifyDrawingHints", "1" ).toInt() );
18741885
mSimplifyMethod.setThreshold( e.attribute( "simplifyDrawingTol", "1" ).toFloat() );
18751886
mSimplifyMethod.setForceLocalOptimization( e.attribute( "simplifyLocal", "1" ).toInt() );
1887+
mSimplifyMethod.setMaximumScale( e.attribute( "simplifyMaxScale", "1" ).toFloat() );
18761888

18771889
//also restore custom properties (for labeling-ng)
18781890
readCustomProperties( node, "labeling" );
@@ -2211,6 +2223,7 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
22112223
mapLayerNode.setAttribute( "simplifyDrawingHints", QString::number( mSimplifyMethod.simplifyHints() ) );
22122224
mapLayerNode.setAttribute( "simplifyDrawingTol", QString::number( mSimplifyMethod.threshold() ) );
22132225
mapLayerNode.setAttribute( "simplifyLocal", mSimplifyMethod.forceLocalOptimization() ? 1 : 0 );
2226+
mapLayerNode.setAttribute( "simplifyMaxScale", QString::number( mSimplifyMethod.maximumScale() ) );
22142227

22152228
//save customproperties (for labeling ng)
22162229
writeCustomProperties( node, doc );

src/core/qgsvectorlayer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
13871387
inline const QgsVectorSimplifyMethod& simplifyMethod() const { return mSimplifyMethod; }
13881388

13891389
/** Returns whether the VectorLayer can apply the specified simplification hint */
1390-
bool simplifyDrawingCanbeApplied( int simplifyHint ) const;
1390+
bool simplifyDrawingCanbeApplied( const QgsRenderContext& renderContext, int simplifyHint ) const;
13911391

13921392
public slots:
13931393
/**

src/core/qgsvectorsimplifymethod.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ QgsVectorSimplifyMethod::QgsVectorSimplifyMethod()
2121
: mSimplifyHints( QGis::DEFAULT_MAPTOPIXEL_THRESHOLD > 1 ? QgsVectorLayer::FullSimplification : QgsVectorLayer::GeometrySimplification )
2222
, mThreshold( QGis::DEFAULT_MAPTOPIXEL_THRESHOLD )
2323
, mLocalOptimization( true )
24+
, mMaximumScale( 1 )
2425
{
2526
}
2627

@@ -34,5 +35,6 @@ QgsVectorSimplifyMethod& QgsVectorSimplifyMethod::operator=( const QgsVectorSimp
3435
mSimplifyHints = rh.mSimplifyHints;
3536
mThreshold = rh.mThreshold;
3637
mLocalOptimization = rh.mLocalOptimization;
38+
mMaximumScale = rh.mMaximumScale;
3739
return *this;
3840
}

src/core/qgsvectorsimplifymethod.h

+7
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,20 @@ class CORE_EXPORT QgsVectorSimplifyMethod
4242
/** Gets where the simplification executes, after fetch the geometries from provider, or when supported, in provider before fetch the geometries */
4343
inline bool forceLocalOptimization() const { return mLocalOptimization; }
4444

45+
/** Sets the maximum scale at which the layer should be simplified */
46+
void setMaximumScale( float maximumScale ) { mMaximumScale = maximumScale; }
47+
/** Gets the maximum scale at which the layer should be simplified */
48+
inline float maximumScale() const { return mMaximumScale; }
49+
4550
private:
4651
/** Simplification hints for fast rendering of features of the vector layer managed */
4752
int mSimplifyHints;
4853
/** Simplification threshold */
4954
float mThreshold;
5055
/** Simplification executes after fetch the geometries from provider, otherwise it executes, when supported, in provider before fetch the geometries */
5156
bool mLocalOptimization;
57+
/** Maximum scale at which the layer should be simplified (Maximum scale at which generalisation should be carried out) */
58+
float mMaximumScale;
5259
};
5360

5461
#endif // QGSVECTORSIMPLIFYMETHOD_H

src/core/symbology-ng/qgslinesymbollayerv2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ void QgsSimpleLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSym
188188
p->setPen( context.selected() ? mSelPen : mPen );
189189

190190
// Disable 'Antialiasing' if the geometry was generalized in the current RenderContext (We known that it must have least #2 points).
191-
if ( points.size() <= 2 && context.layer() && context.layer()->simplifyDrawingCanbeApplied( QgsVectorLayer::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( points, context.layer()->simplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) )
191+
if ( points.size() <= 2 && context.layer() && context.layer()->simplifyDrawingCanbeApplied( context.renderContext(), QgsVectorLayer::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( points, context.layer()->simplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) )
192192
{
193193
p->setRenderHint( QPainter::Antialiasing, false );
194194
p->drawPolyline( points );

src/core/symbology-ng/qgssymbollayerv2.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ void QgsFillSymbolLayerV2::_renderPolygon( QPainter* p, const QPolygonF& points,
388388
}
389389

390390
// Disable 'Antialiasing' if the geometry was generalized in the current RenderContext (We known that it must have least #5 points).
391-
if ( points.size() <= 5 && context.layer() && context.layer()->simplifyDrawingCanbeApplied( QgsVectorLayer::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( points, context.layer()->simplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) )
391+
if ( points.size() <= 5 && context.layer() && context.layer()->simplifyDrawingCanbeApplied( context.renderContext(), QgsVectorLayer::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( points, context.layer()->simplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) )
392392
{
393393
p->setRenderHint( QPainter::Antialiasing, false );
394394
p->drawRect( points.boundingRect() );

src/ui/qgsoptionsbase.ui

+20
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,26 @@
17101710
</property>
17111711
</widget>
17121712
</item>
1713+
<item row="3" column="1">
1714+
<widget class="QLabel" name="mSimplifyMaxScaleLabel">
1715+
<property name="text">
1716+
<string>Maximum scale at which the layer should be simplified (1:1 always simplifies): </string>
1717+
</property>
1718+
<property name="margin">
1719+
<number>2</number>
1720+
</property>
1721+
</widget>
1722+
</item>
1723+
<item row="3" column="2">
1724+
<widget class="QgsScaleComboBox" name="mSimplifyMaximumScaleComboBox">
1725+
<property name="sizePolicy">
1726+
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
1727+
<horstretch>0</horstretch>
1728+
<verstretch>0</verstretch>
1729+
</sizepolicy>
1730+
</property>
1731+
</widget>
1732+
</item>
17131733
</layout>
17141734
</widget>
17151735
</item>

src/ui/qgsvectorlayerpropertiesbase.ui

+20
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,26 @@
958958
</property>
959959
</widget>
960960
</item>
961+
<item row="3" column="1">
962+
<widget class="QLabel" name="mSimplifyMaxScaleLabel">
963+
<property name="text">
964+
<string>Maximum scale at which the layer should be simplified (1:1 always simplifies): </string>
965+
</property>
966+
<property name="margin">
967+
<number>2</number>
968+
</property>
969+
</widget>
970+
</item>
971+
<item row="3" column="2">
972+
<widget class="QgsScaleComboBox" name="mSimplifyMaximumScaleComboBox">
973+
<property name="sizePolicy">
974+
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
975+
<horstretch>0</horstretch>
976+
<verstretch>0</verstretch>
977+
</sizepolicy>
978+
</property>
979+
</widget>
980+
</item>
961981
</layout>
962982
</widget>
963983
</item>

0 commit comments

Comments
 (0)