Skip to content

Commit

Permalink
Merge pull request #1094 from ahuarte47/Issue_8725R-maxscale
Browse files Browse the repository at this point in the history
Feature #8725R: support for maximum scale at which the layer should be simplified
  • Loading branch information
timlinux committed Jan 22, 2014
2 parents 52d114f + 031596c commit f2d0100
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 7 deletions.
2 changes: 1 addition & 1 deletion python/core/qgsvectorlayer.sip
Expand Up @@ -1013,7 +1013,7 @@ class QgsVectorLayer : QgsMapLayer
const QgsVectorSimplifyMethod& simplifyMethod() const;

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

public slots:
/**
Expand Down
5 changes: 5 additions & 0 deletions python/core/qgsvectorsimplifymethod.sip
Expand Up @@ -26,4 +26,9 @@ class QgsVectorSimplifyMethod
void setForceLocalOptimization( bool localOptimization );
/** Gets where the simplification executes, after fetch the geometries from provider, or when supported, in provider before fetch the geometries */
bool forceLocalOptimization();

/** Sets the maximum scale at which the layer should be simplified */
void setMaximumScale( float maximumScale );
/** Gets the maximum scale at which the layer should be simplified */
float maximumScale() const;
};
6 changes: 6 additions & 0 deletions src/app/qgsoptions.cpp
Expand Up @@ -570,6 +570,11 @@ QgsOptions::QgsOptions( QWidget *parent, Qt::WFlags fl ) :
mSimplifyDrawingSpinBox->setValue( settings.value( "/qgis/simplifyDrawingTol", QGis::DEFAULT_MAPTOPIXEL_THRESHOLD ).toFloat() );
mSimplifyDrawingAtProvider->setChecked( !settings.value( "/qgis/simplifyLocal", true ).toBool() );

QStringList myScalesList = PROJECT_SCALES.split( "," );
myScalesList.append( "1:1" );
mSimplifyMaximumScaleComboBox->updateScales( myScalesList );
mSimplifyMaximumScaleComboBox->setScale( 1.0 / settings.value( "/qgis/simplifyMaxScale", 1 ).toFloat() );

// Slightly awkard here at the settings value is true to use QImage,
// but the checkbox is true to use QPixmap
chkUseQPixmap->setChecked( !( settings.value( "/qgis/use_qimage_to_render", true ).toBool() ) );
Expand Down Expand Up @@ -1111,6 +1116,7 @@ void QgsOptions::saveOptions()
settings.setValue( "/qgis/simplifyDrawingHints", simplifyHints );
settings.setValue( "/qgis/simplifyDrawingTol", mSimplifyDrawingSpinBox->value() );
settings.setValue( "/qgis/simplifyLocal", !mSimplifyDrawingAtProvider->isChecked() );
settings.setValue( "/qgis/simplifyMaxScale", 1.0 / mSimplifyMaximumScaleComboBox->scale() );

// project
settings.setValue( "/qgis/projOpenAtLaunch", mProjectOnLaunchCmbBx->currentIndex() );
Expand Down
6 changes: 6 additions & 0 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -416,6 +416,11 @@ void QgsVectorLayerProperties::syncToLayer( void )
mSimplifyDrawingGroupBox->setChecked( false );
}

QStringList myScalesList = PROJECT_SCALES.split( "," );
myScalesList.append( "1:1" );
mSimplifyMaximumScaleComboBox->updateScales( myScalesList );
mSimplifyMaximumScaleComboBox->setScale( 1.0 / simplifyMethod.maximumScale() );

// load appropriate symbology page (V1 or V2)
updateSymbologyPage();

Expand Down Expand Up @@ -564,6 +569,7 @@ void QgsVectorLayerProperties::apply()
simplifyMethod.setSimplifyHints( simplifyHints );
simplifyMethod.setThreshold( mSimplifyDrawingSpinBox->value() );
simplifyMethod.setForceLocalOptimization( !mSimplifyDrawingAtProvider->isChecked() );
simplifyMethod.setMaximumScale( 1.0 / mSimplifyMaximumScaleComboBox->scale() );
layer->setSimplifyMethod( simplifyMethod );

// update symbology
Expand Down
19 changes: 16 additions & 3 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -187,6 +187,7 @@ QgsVectorLayer::QgsVectorLayer( QString vectorLayerPath,
mSimplifyMethod.setSimplifyHints( settings.value( "/qgis/simplifyDrawingHints", mSimplifyMethod.simplifyHints() ).toInt() );
mSimplifyMethod.setThreshold( settings.value( "/qgis/simplifyDrawingTol", mSimplifyMethod.threshold() ).toFloat() );
mSimplifyMethod.setForceLocalOptimization( settings.value( "/qgis/simplifyLocal", mSimplifyMethod.forceLocalOptimization() ).toBool() );
mSimplifyMethod.setMaximumScale( settings.value( "/qgis/simplifyMaxScale", mSimplifyMethod.maximumScale() ).toFloat() );

} // QgsVectorLayer ctor

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

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

bool QgsVectorLayer::simplifyDrawingCanbeApplied( int simplifyHint ) const
bool QgsVectorLayer::simplifyDrawingCanbeApplied( const QgsRenderContext& renderContext, int simplifyHint ) const
{
return mDataProvider && !mEditBuffer && ( hasGeometryType() && geometryType() != QGis::Point ) && ( mSimplifyMethod.simplifyHints() & simplifyHint ) && ( !mCurrentRendererContext || mCurrentRendererContext->useRenderingOptimization() );
if ( mDataProvider && !mEditBuffer && ( hasGeometryType() && geometryType() != QGis::Point ) && ( mSimplifyMethod.simplifyHints() & simplifyHint ) && renderContext.useRenderingOptimization() )
{
double maximumSimplificationScale = mSimplifyMethod.maximumScale();

// check maximum scale at which generalisation should be carried out
if ( maximumSimplificationScale > 1 && renderContext.rendererScale() <= maximumSimplificationScale )
return false;

return true;
}
return false;
}

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

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

//save customproperties (for labeling ng)
writeCustomProperties( node, doc );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsvectorlayer.h
Expand Up @@ -1387,7 +1387,7 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
inline const QgsVectorSimplifyMethod& simplifyMethod() const { return mSimplifyMethod; }

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

public slots:
/**
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsvectorsimplifymethod.cpp
Expand Up @@ -21,6 +21,7 @@ QgsVectorSimplifyMethod::QgsVectorSimplifyMethod()
: mSimplifyHints( QGis::DEFAULT_MAPTOPIXEL_THRESHOLD > 1 ? QgsVectorLayer::FullSimplification : QgsVectorLayer::GeometrySimplification )
, mThreshold( QGis::DEFAULT_MAPTOPIXEL_THRESHOLD )
, mLocalOptimization( true )
, mMaximumScale( 1 )
{
}

Expand All @@ -34,5 +35,6 @@ QgsVectorSimplifyMethod& QgsVectorSimplifyMethod::operator=( const QgsVectorSimp
mSimplifyHints = rh.mSimplifyHints;
mThreshold = rh.mThreshold;
mLocalOptimization = rh.mLocalOptimization;
mMaximumScale = rh.mMaximumScale;
return *this;
}
7 changes: 7 additions & 0 deletions src/core/qgsvectorsimplifymethod.h
Expand Up @@ -42,13 +42,20 @@ class CORE_EXPORT QgsVectorSimplifyMethod
/** Gets where the simplification executes, after fetch the geometries from provider, or when supported, in provider before fetch the geometries */
inline bool forceLocalOptimization() const { return mLocalOptimization; }

/** Sets the maximum scale at which the layer should be simplified */
void setMaximumScale( float maximumScale ) { mMaximumScale = maximumScale; }
/** Gets the maximum scale at which the layer should be simplified */
inline float maximumScale() const { return mMaximumScale; }

private:
/** Simplification hints for fast rendering of features of the vector layer managed */
int mSimplifyHints;
/** Simplification threshold */
float mThreshold;
/** Simplification executes after fetch the geometries from provider, otherwise it executes, when supported, in provider before fetch the geometries */
bool mLocalOptimization;
/** Maximum scale at which the layer should be simplified (Maximum scale at which generalisation should be carried out) */
float mMaximumScale;
};

#endif // QGSVECTORSIMPLIFYMETHOD_H
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgslinesymbollayerv2.cpp
Expand Up @@ -188,7 +188,7 @@ void QgsSimpleLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSym
p->setPen( context.selected() ? mSelPen : mPen );

// Disable 'Antialiasing' if the geometry was generalized in the current RenderContext (We known that it must have least #2 points).
if ( points.size() <= 2 && context.layer() && context.layer()->simplifyDrawingCanbeApplied( QgsVectorLayer::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( points, context.layer()->simplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) )
if ( points.size() <= 2 && context.layer() && context.layer()->simplifyDrawingCanbeApplied( context.renderContext(), QgsVectorLayer::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( points, context.layer()->simplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) )
{
p->setRenderHint( QPainter::Antialiasing, false );
p->drawPolyline( points );
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgssymbollayerv2.cpp
Expand Up @@ -388,7 +388,7 @@ void QgsFillSymbolLayerV2::_renderPolygon( QPainter* p, const QPolygonF& points,
}

// Disable 'Antialiasing' if the geometry was generalized in the current RenderContext (We known that it must have least #5 points).
if ( points.size() <= 5 && context.layer() && context.layer()->simplifyDrawingCanbeApplied( QgsVectorLayer::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( points, context.layer()->simplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) )
if ( points.size() <= 5 && context.layer() && context.layer()->simplifyDrawingCanbeApplied( context.renderContext(), QgsVectorLayer::AntialiasingSimplification ) && QgsAbstractGeometrySimplifier::canbeGeneralizedByDeviceBoundingBox( points, context.layer()->simplifyMethod().threshold() ) && ( p->renderHints() & QPainter::Antialiasing ) )
{
p->setRenderHint( QPainter::Antialiasing, false );
p->drawRect( points.boundingRect() );
Expand Down
20 changes: 20 additions & 0 deletions src/ui/qgsoptionsbase.ui
Expand Up @@ -1710,6 +1710,26 @@
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="mSimplifyMaxScaleLabel">
<property name="text">
<string>Maximum scale at which the layer should be simplified (1:1 always simplifies): </string>
</property>
<property name="margin">
<number>2</number>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QgsScaleComboBox" name="mSimplifyMaximumScaleComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down
20 changes: 20 additions & 0 deletions src/ui/qgsvectorlayerpropertiesbase.ui
Expand Up @@ -958,6 +958,26 @@
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="mSimplifyMaxScaleLabel">
<property name="text">
<string>Maximum scale at which the layer should be simplified (1:1 always simplifies): </string>
</property>
<property name="margin">
<number>2</number>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QgsScaleComboBox" name="mSimplifyMaximumScaleComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit f2d0100

Please sign in to comment.