@@ -230,9 +230,9 @@ void QgsRubberBand::setToGeometry( const QgsGeometry &geom, QgsVectorLayer *laye
230
230
addGeometry ( geom, layer );
231
231
}
232
232
233
- void QgsRubberBand::addGeometry ( const QgsGeometry &geom , QgsVectorLayer *layer )
233
+ void QgsRubberBand::addGeometry ( const QgsGeometry &geometry , QgsVectorLayer *layer )
234
234
{
235
- if ( geom. isNull () )
235
+ if ( geometry. isEmpty () )
236
236
{
237
237
return ;
238
238
}
@@ -242,21 +242,20 @@ void QgsRubberBand::addGeometry( const QgsGeometry &geom, QgsVectorLayer *layer
242
242
243
243
int idx = mPoints .size ();
244
244
245
+ QgsGeometry geom = geometry;
246
+ if ( layer )
247
+ {
248
+ QgsCoordinateTransform ct = ms.layerTransform ( layer );
249
+ geom.transform ( ct );
250
+ }
251
+
245
252
switch ( geom.wkbType () )
246
253
{
247
254
248
255
case QgsWkbTypes::Point:
249
256
case QgsWkbTypes::Point25D:
250
257
{
251
- QgsPointXY pt;
252
- if ( layer )
253
- {
254
- pt = ms.layerToMapCoordinates ( layer, geom.asPoint () );
255
- }
256
- else
257
- {
258
- pt = geom.asPoint ();
259
- }
258
+ QgsPointXY pt = geom.asPoint ();
260
259
addPoint ( pt, false , idx );
261
260
removeLastPoint ( idx, false );
262
261
}
@@ -265,38 +264,23 @@ void QgsRubberBand::addGeometry( const QgsGeometry &geom, QgsVectorLayer *layer
265
264
case QgsWkbTypes::MultiPoint:
266
265
case QgsWkbTypes::MultiPoint25D:
267
266
{
268
- QgsMultiPoint mpt = geom.asMultiPoint ();
269
- for ( int i = 0 ; i < mpt. size (); ++i, ++idx )
267
+ const QgsMultiPoint mpt = geom.asMultiPoint ();
268
+ for ( QgsPointXY pt : mpt )
270
269
{
271
- QgsPointXY pt = mpt[i];
272
- if ( layer )
273
- {
274
- addPoint ( ms.layerToMapCoordinates ( layer, pt ), false , idx );
275
- removeLastPoint ( idx, false );
276
- }
277
- else
278
- {
279
- addPoint ( pt, false , idx );
280
- removeLastPoint ( idx, false );
281
- }
270
+ addPoint ( pt, false , idx );
271
+ removeLastPoint ( idx, false );
272
+ idx++;
282
273
}
283
274
}
284
275
break ;
285
276
286
277
case QgsWkbTypes::LineString:
287
278
case QgsWkbTypes::LineString25D:
288
279
{
289
- QgsPolyline line = geom.asPolyline ();
290
- for ( int i = 0 ; i < line. count (); i++ )
280
+ const QgsPolyline line = geom.asPolyline ();
281
+ for ( QgsPointXY pt : line )
291
282
{
292
- if ( layer )
293
- {
294
- addPoint ( ms.layerToMapCoordinates ( layer, line[i] ), false , idx );
295
- }
296
- else
297
- {
298
- addPoint ( line[i], false , idx );
299
- }
283
+ addPoint ( pt, false , idx );
300
284
}
301
285
}
302
286
break ;
@@ -305,46 +289,31 @@ void QgsRubberBand::addGeometry( const QgsGeometry &geom, QgsVectorLayer *layer
305
289
case QgsWkbTypes::MultiLineString25D:
306
290
{
307
291
308
- QgsMultiPolyline mline = geom.asMultiPolyline ();
309
- for ( int i = 0 ; i < mline. size (); ++i, ++idx )
292
+ const QgsMultiPolyline mline = geom.asMultiPolyline ();
293
+ for ( const QgsPolyline &line : mline )
310
294
{
311
- QgsPolyline line = mline[i];
312
-
313
295
if ( line.isEmpty () )
314
296
{
315
- --idx ;
297
+ continue ;
316
298
}
317
299
318
- for ( int j = 0 ; j < line. size (); ++j )
300
+ for ( QgsPointXY pt : line )
319
301
{
320
- if ( layer )
321
- {
322
- addPoint ( ms.layerToMapCoordinates ( layer, line[j] ), false , idx );
323
- }
324
- else
325
- {
326
- addPoint ( line[j], false , idx );
327
- }
302
+ addPoint ( pt, false , idx );
328
303
}
304
+ idx++;
329
305
}
330
306
}
331
307
break ;
332
308
333
309
case QgsWkbTypes::Polygon:
334
310
case QgsWkbTypes::Polygon25D:
335
311
{
336
- QgsPolygon poly = geom.asPolygon ();
337
- QgsPolyline line = poly[ 0 ] ;
338
- for ( int i = 0 ; i < line. count (); i++ )
312
+ const QgsPolygon poly = geom.asPolygon ();
313
+ const QgsPolyline line = poly. at ( 0 ) ;
314
+ for ( QgsPointXY pt : line )
339
315
{
340
- if ( layer )
341
- {
342
- addPoint ( ms.layerToMapCoordinates ( layer, line[i] ), false , idx );
343
- }
344
- else
345
- {
346
- addPoint ( line[i], false , idx );
347
- }
316
+ addPoint ( pt, false , idx );
348
317
}
349
318
}
350
319
break ;
@@ -353,22 +322,18 @@ void QgsRubberBand::addGeometry( const QgsGeometry &geom, QgsVectorLayer *layer
353
322
case QgsWkbTypes::MultiPolygon25D:
354
323
{
355
324
356
- QgsMultiPolygon multipoly = geom.asMultiPolygon ();
357
- for ( int i = 0 ; i < multipoly. size (); ++i, ++idx )
325
+ const QgsMultiPolygon multipoly = geom.asMultiPolygon ();
326
+ for ( const QgsPolygon &poly : multipoly )
358
327
{
359
- QgsPolygon poly = multipoly[i];
360
- QgsPolyline line = poly[0 ];
361
- for ( int j = 0 ; j < line.count (); ++j )
328
+ if ( poly.empty () )
329
+ continue ;
330
+
331
+ const QgsPolyline line = poly.at ( 0 );
332
+ for ( QgsPointXY pt : line )
362
333
{
363
- if ( layer )
364
- {
365
- addPoint ( ms.layerToMapCoordinates ( layer, line[j] ), false , idx );
366
- }
367
- else
368
- {
369
- addPoint ( line[j], false , idx );
370
- }
334
+ addPoint ( pt, false , idx );
371
335
}
336
+ idx++;
372
337
}
373
338
}
374
339
break ;
0 commit comments