@@ -127,14 +127,29 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
127
127
// Write the geometry
128
128
if ( flatType == QgsWkbTypes::LineString || flatType == QgsWkbTypes::CircularString )
129
129
{
130
+ const int numPoints = srcCurve.numPoints ();
131
+
130
132
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
+
132
149
double x = 0.0 , y = 0.0 , lastX = 0.0 , lastY = 0.0 ;
133
150
QgsRectangle r;
134
151
r.setMinimal ();
135
152
136
- const int numPoints = srcCurve.numPoints ();
137
-
138
153
if ( numPoints <= ( isaLinearRing ? 4 : 2 ) )
139
154
isGeneralizable = false ;
140
155
@@ -169,7 +184,13 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
169
184
!equalSnapToGrid ( x, y, lastX, lastY, gridOriginX, gridOriginY, gridInverseSizeXY ) ||
170
185
( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
171
186
{
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
+ }
173
194
lastX = x;
174
195
lastY = y;
175
196
}
@@ -192,7 +213,13 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
192
213
{
193
214
if ( ea.res_arealist [ i ] > map2pixelTol )
194
215
{
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
+ }
196
223
}
197
224
}
198
225
break ;
@@ -214,7 +241,13 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
214
241
( isLongSegment = ( calculateLengthSquared2D ( x, y, lastX, lastY ) > map2pixelTol ) ) ||
215
242
( !isaLinearRing && ( i == 1 || i >= numPoints - 2 ) ) )
216
243
{
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
+ }
218
251
lastX = x;
219
252
lastY = y;
220
253
@@ -226,6 +259,10 @@ QgsGeometry QgsMapToPixelSimplifier::simplifyGeometry(
226
259
}
227
260
}
228
261
262
+ if ( !output )
263
+ {
264
+ output = qgis::make_unique< QgsLineString >( lineStringX, lineStringY );
265
+ }
229
266
if ( output->numPoints () < ( isaLinearRing ? 4 : 2 ) )
230
267
{
231
268
// we simplified the geometry too much!
0 commit comments