Use a more appropriate, global snap to grid algorithm when simplifying

geometries for layout exports

This avoids all chance of slivers or overlaps between previously
adjacent features.
This commit is contained in:
Nyall Dawson 2019-08-07 11:27:54 +10:00
parent d8d23b56ea
commit 0419f182d8
6 changed files with 13 additions and 2 deletions

View File

@ -28,6 +28,7 @@ This class enables simplify the geometries to be rendered in a MapCanvas target
Distance,
SnapToGrid,
Visvalingam,
SnappedToGridGlobal,
};
QgsMapToPixelSimplifier( int simplifyFlags, double tolerance, SimplifyAlgorithm simplifyAlgorithm = Distance );

View File

@ -53,6 +53,7 @@ Gets the simplification hints of the vector layer managed
Distance,
SnapToGrid,
Visvalingam,
SnappedToGridGlobal,
};
void setSimplifyAlgorithm( SimplifyAlgorithm simplifyAlgorithm );

View File

@ -1471,8 +1471,9 @@ QgsVectorSimplifyMethod QgsLayoutExporter::createExportSimplifyMethod()
QgsVectorSimplifyMethod simplifyMethod;
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::GeometrySimplification );
simplifyMethod.setForceLocalOptimization( true );
simplifyMethod.setSimplifyAlgorithm( QgsVectorSimplifyMethod::SnapToGrid );
simplifyMethod.setThreshold( 0.5 ); // pixels
// we use SnappedToGridGlobal, because it avoids gaps and slivers between previously adjacent polygons
simplifyMethod.setSimplifyAlgorithm( QgsVectorSimplifyMethod::SnappedToGridGlobal );
simplifyMethod.setThreshold( 0.1f ); // (pixels). We are quite conservative here. This could possibly be bumped all the way up to 1. But let's play it safe.
return simplifyMethod;
}

View File

@ -227,6 +227,12 @@ std::unique_ptr< QgsAbstractGeometry > QgsMapToPixelSimplifier::simplifyGeometry
break;
}
case SnappedToGridGlobal:
{
output.reset( qgsgeometry_cast< QgsCurve * >( srcCurve.snappedToGrid( map2pixelTol, map2pixelTol ) ) );
break;
}
case Visvalingam:
{
map2pixelTol *= map2pixelTol; //-> Use mappixelTol for 'Area' calculations.

View File

@ -44,6 +44,7 @@ class CORE_EXPORT QgsMapToPixelSimplifier : public QgsAbstractGeometrySimplifier
Distance = 0, //!< The simplification uses the distance between points to remove duplicate points
SnapToGrid = 1, //!< The simplification uses a grid (similar to ST_SnapToGrid) to remove duplicate points
Visvalingam = 2, //!< The simplification gives each point in a line an importance weighting, so that least important points are removed first
SnappedToGridGlobal = 3, //!< Snap to a global grid based on the tolerance. Good for consistent results for incoming vertices, regardless of their feature
};
//! Constructor

View File

@ -56,6 +56,7 @@ class CORE_EXPORT QgsVectorSimplifyMethod
Distance = 0, //!< The simplification uses the distance between points to remove duplicate points
SnapToGrid = 1, //!< The simplification uses a grid (similar to ST_SnapToGrid) to remove duplicate points
Visvalingam = 2, //!< The simplification gives each point in a line an importance weighting, so that least important points are removed first
SnappedToGridGlobal = 3, //!< Snap to a global grid based on the tolerance. Good for consistent results for incoming vertices, regardless of their feature
};
Q_ENUM( SimplifyAlgorithm )