Skip to content

Commit

Permalink
More efficient simplification of linestring geometries during rendering
Browse files Browse the repository at this point in the history
Refs #17809
  • Loading branch information
nyalldawson committed Feb 11, 2018
1 parent ff7bb9a commit c7e257e
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/core/qgsmaptopixelgeometrysimplifier.cpp
Expand Up @@ -127,14 +127,29 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
// Write the geometry
if ( flatType == QgsWkbTypes::LineString || flatType == QgsWkbTypes::CircularString )
{
const int numPoints = srcCurve.numPoints();

const QgsCurve &srcCurve = dynamic_cast<const QgsCurve &>( geometry );
std::unique_ptr<QgsCurve> output( createEmptySameTypeGeom( srcCurve ) );
std::unique_ptr<QgsCurve> output;

QVector< double > lineStringX;
QVector< double > lineStringY;
if ( flatType == QgsWkbTypes::LineString )
{
// if we are making a linestring, we do it in an optimised way by directly constructing
// the final x/y vectors, which avoids calling the slower insertVertex method
lineStringX.reserve( numPoints );
lineStringY.reserve( numPoints );
}
else
{
output.reset( qgsgeometry_cast< QgsCurve * >( srcCurve.createEmptyWithSameType() ) );
}

double x = 0.0, y = 0.0, lastX = 0.0, lastY = 0.0;
QgsRectangle r;
r.setMinimal();

const int numPoints = srcCurve.numPoints();

if ( numPoints <= ( isaLinearRing ? 4 : 2 ) )
isGeneralizable = false;

Expand Down Expand Up @@ -169,7 +184,13 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
!equalSnapToGrid( x, y, lastX, lastY, gridOriginX, gridOriginY, gridInverseSizeXY ) ||
( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
{
output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( x, y ) );
if ( output )
output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( x, y ) );
else
{
lineStringX.append( x );
lineStringY.append( y );
}
lastX = x;
lastY = y;
}
Expand All @@ -192,7 +213,13 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
{
if ( ea.res_arealist[ i ] > map2pixelTol )
{
output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), ea.inpts.at( i ) );
if ( output )
output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), ea.inpts.at( i ) );
else
{
lineStringX.append( ea.inpts.at( i ).x() );
lineStringY.append( ea.inpts.at( i ).y() );
}
}
}
break;
Expand All @@ -214,7 +241,13 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
( isLongSegment = ( calculateLengthSquared2D( x, y, lastX, lastY ) > map2pixelTol ) ) ||
( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
{
output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( x, y ) );
if ( output )
output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( x, y ) );
else
{
lineStringX.append( x );
lineStringY.append( y );
}
lastX = x;
lastY = y;

Expand All @@ -226,6 +259,10 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
}
}

if ( !output )
{
output = qgis::make_unique< QgsLineString >( lineStringX, lineStringY );
}
if ( output->numPoints() < ( isaLinearRing ? 4 : 2 ) )
{
// we simplified the geometry too much!
Expand Down

0 comments on commit c7e257e

Please sign in to comment.