Skip to content

Commit c7e257e

Browse files
committedFeb 11, 2018
More efficient simplification of linestring geometries during rendering
Refs #17809
1 parent ff7bb9a commit c7e257e

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed
 

‎src/core/qgsmaptopixelgeometrysimplifier.cpp

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,29 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
127127
// Write the geometry
128128
if ( flatType == QgsWkbTypes::LineString || flatType == QgsWkbTypes::CircularString )
129129
{
130+
const int numPoints = srcCurve.numPoints();
131+
130132
const QgsCurve &srcCurve = dynamic_cast<const QgsCurve &>( geometry );
131-
std::unique_ptr<QgsCurve> output( createEmptySameTypeGeom( srcCurve ) );
133+
std::unique_ptr<QgsCurve> output;
134+
135+
QVector< double > lineStringX;
136+
QVector< double > lineStringY;
137+
if ( flatType == QgsWkbTypes::LineString )
138+
{
139+
// if we are making a linestring, we do it in an optimised way by directly constructing
140+
// the final x/y vectors, which avoids calling the slower insertVertex method
141+
lineStringX.reserve( numPoints );
142+
lineStringY.reserve( numPoints );
143+
}
144+
else
145+
{
146+
output.reset( qgsgeometry_cast< QgsCurve * >( srcCurve.createEmptyWithSameType() ) );
147+
}
148+
132149
double x = 0.0, y = 0.0, lastX = 0.0, lastY = 0.0;
133150
QgsRectangle r;
134151
r.setMinimal();
135152

136-
const int numPoints = srcCurve.numPoints();
137-
138153
if ( numPoints <= ( isaLinearRing ? 4 : 2 ) )
139154
isGeneralizable = false;
140155

@@ -169,7 +184,13 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
169184
!equalSnapToGrid( x, y, lastX, lastY, gridOriginX, gridOriginY, gridInverseSizeXY ) ||
170185
( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
171186
{
172-
output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( x, y ) );
187+
if ( output )
188+
output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( x, y ) );
189+
else
190+
{
191+
lineStringX.append( x );
192+
lineStringY.append( y );
193+
}
173194
lastX = x;
174195
lastY = y;
175196
}
@@ -192,7 +213,13 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
192213
{
193214
if ( ea.res_arealist[ i ] > map2pixelTol )
194215
{
195-
output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), ea.inpts.at( i ) );
216+
if ( output )
217+
output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), ea.inpts.at( i ) );
218+
else
219+
{
220+
lineStringX.append( ea.inpts.at( i ).x() );
221+
lineStringY.append( ea.inpts.at( i ).y() );
222+
}
196223
}
197224
}
198225
break;
@@ -214,7 +241,13 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
214241
( isLongSegment = ( calculateLengthSquared2D( x, y, lastX, lastY ) > map2pixelTol ) ) ||
215242
( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
216243
{
217-
output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( x, y ) );
244+
if ( output )
245+
output->insertVertex( QgsVertexId( 0, 0, output->numPoints() ), QgsPoint( x, y ) );
246+
else
247+
{
248+
lineStringX.append( x );
249+
lineStringY.append( y );
250+
}
218251
lastX = x;
219252
lastY = y;
220253

@@ -226,6 +259,10 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
226259
}
227260
}
228261

262+
if ( !output )
263+
{
264+
output = qgis::make_unique< QgsLineString >( lineStringX, lineStringY );
265+
}
229266
if ( output->numPoints() < ( isaLinearRing ? 4 : 2 ) )
230267
{
231268
// we simplified the geometry too much!

0 commit comments

Comments
 (0)
Please sign in to comment.