-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgscompoundcurve.cpp
990 lines (881 loc) · 26.1 KB
/
qgscompoundcurve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
/***************************************************************************
qgscompoundcurve.cpp
----------------------
begin : September 2014
copyright : (C) 2014 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgscompoundcurve.h"
#include "qgsapplication.h"
#include "qgscircularstring.h"
#include "qgsgeometryutils.h"
#include "qgslinestring.h"
#include "qgswkbptr.h"
#include <QPainter>
#include <QPainterPath>
#include <memory>
QgsCompoundCurve::QgsCompoundCurve()
{
mWkbType = QgsWkbTypes::CompoundCurve;
}
QgsCompoundCurve::~QgsCompoundCurve()
{
clear();
}
bool QgsCompoundCurve::equals( const QgsCurve &other ) const
{
const QgsCompoundCurve *otherCurve = qgsgeometry_cast< const QgsCompoundCurve * >( &other );
if ( !otherCurve )
return false;
if ( mWkbType != otherCurve->mWkbType )
return false;
if ( mCurves.size() != otherCurve->mCurves.size() )
return false;
for ( int i = 0; i < mCurves.size(); ++i )
{
if ( *mCurves.at( i ) != *otherCurve->mCurves.at( i ) )
return false;
}
return true;
}
QgsCompoundCurve *QgsCompoundCurve::createEmptyWithSameType() const
{
auto result = qgis::make_unique< QgsCompoundCurve >();
result->mWkbType = mWkbType;
return result.release();
}
QString QgsCompoundCurve::geometryType() const
{
return QStringLiteral( "CompoundCurve" );
}
int QgsCompoundCurve::dimension() const
{
return 1;
}
QgsCompoundCurve::QgsCompoundCurve( const QgsCompoundCurve &curve ): QgsCurve( curve )
{
mWkbType = curve.wkbType();
mCurves.reserve( curve.mCurves.size() );
for ( const QgsCurve *c : curve.mCurves )
{
mCurves.append( c->clone() );
}
}
QgsCompoundCurve &QgsCompoundCurve::operator=( const QgsCompoundCurve &curve )
{
if ( &curve != this )
{
clearCache();
QgsCurve::operator=( curve );
for ( const QgsCurve *c : curve.mCurves )
{
mCurves.append( c->clone() );
}
}
return *this;
}
QgsCompoundCurve *QgsCompoundCurve::clone() const
{
return new QgsCompoundCurve( *this );
}
void QgsCompoundCurve::clear()
{
mWkbType = QgsWkbTypes::CompoundCurve;
qDeleteAll( mCurves );
mCurves.clear();
clearCache();
}
QgsRectangle QgsCompoundCurve::calculateBoundingBox() const
{
if ( mCurves.empty() )
{
return QgsRectangle();
}
QgsRectangle bbox = mCurves.at( 0 )->boundingBox();
for ( int i = 1; i < mCurves.size(); ++i )
{
QgsRectangle curveBox = mCurves.at( i )->boundingBox();
bbox.combineExtentWith( curveBox );
}
return bbox;
}
bool QgsCompoundCurve::fromWkb( QgsConstWkbPtr &wkbPtr )
{
clear();
if ( !wkbPtr )
{
return false;
}
QgsWkbTypes::Type type = wkbPtr.readHeader();
if ( QgsWkbTypes::flatType( type ) != QgsWkbTypes::CompoundCurve )
{
return false;
}
mWkbType = type;
int nCurves;
wkbPtr >> nCurves;
QgsCurve *currentCurve = nullptr;
for ( int i = 0; i < nCurves; ++i )
{
QgsWkbTypes::Type curveType = wkbPtr.readHeader();
wkbPtr -= 1 + sizeof( int );
if ( QgsWkbTypes::flatType( curveType ) == QgsWkbTypes::LineString )
{
currentCurve = new QgsLineString();
}
else if ( QgsWkbTypes::flatType( curveType ) == QgsWkbTypes::CircularString )
{
currentCurve = new QgsCircularString();
}
else
{
return false;
}
currentCurve->fromWkb( wkbPtr ); // also updates wkbPtr
mCurves.append( currentCurve );
}
return true;
}
bool QgsCompoundCurve::fromWkt( const QString &wkt )
{
clear();
QPair<QgsWkbTypes::Type, QString> parts = QgsGeometryUtils::wktReadBlock( wkt );
if ( QgsWkbTypes::flatType( parts.first ) != QgsWkbTypes::CompoundCurve )
return false;
mWkbType = parts.first;
QString defaultChildWkbType = QStringLiteral( "LineString%1%2" ).arg( is3D() ? QStringLiteral( "Z" ) : QString(), isMeasure() ? QStringLiteral( "M" ) : QString() );
const QStringList blocks = QgsGeometryUtils::wktGetChildBlocks( parts.second, defaultChildWkbType );
for ( const QString &childWkt : blocks )
{
QPair<QgsWkbTypes::Type, QString> childParts = QgsGeometryUtils::wktReadBlock( childWkt );
if ( QgsWkbTypes::flatType( childParts.first ) == QgsWkbTypes::LineString )
mCurves.append( new QgsLineString() );
else if ( QgsWkbTypes::flatType( childParts.first ) == QgsWkbTypes::CircularString )
mCurves.append( new QgsCircularString() );
else
{
clear();
return false;
}
if ( !mCurves.back()->fromWkt( childWkt ) )
{
clear();
return false;
}
}
//scan through curves and check if dimensionality of curves is different to compound curve.
//if so, update the type dimensionality of the compound curve to match
bool hasZ = false;
bool hasM = false;
for ( const QgsCurve *curve : qgis::as_const( mCurves ) )
{
hasZ = hasZ || curve->is3D();
hasM = hasM || curve->isMeasure();
if ( hasZ && hasM )
break;
}
if ( hasZ )
addZValue( 0 );
if ( hasM )
addMValue( 0 );
return true;
}
QByteArray QgsCompoundCurve::asWkb() const
{
int binarySize = sizeof( char ) + sizeof( quint32 ) + sizeof( quint32 );
QVector<QByteArray> wkbForCurves;
wkbForCurves.reserve( mCurves.size() );
for ( const QgsCurve *curve : mCurves )
{
QByteArray wkbForCurve = curve->asWkb();
binarySize += wkbForCurve.length();
wkbForCurves << wkbForCurve;
}
QByteArray wkbArray;
wkbArray.resize( binarySize );
QgsWkbPtr wkb( wkbArray );
wkb << static_cast<char>( QgsApplication::endian() );
wkb << static_cast<quint32>( wkbType() );
wkb << static_cast<quint32>( mCurves.size() );
for ( const QByteArray &wkbForCurve : qgis::as_const( wkbForCurves ) )
{
wkb << wkbForCurve;
}
return wkbArray;
}
QString QgsCompoundCurve::asWkt( int precision ) const
{
QString wkt = wktTypeStr() + " (";
for ( const QgsCurve *curve : mCurves )
{
QString childWkt = curve->asWkt( precision );
if ( qgsgeometry_cast<const QgsLineString *>( curve ) )
{
// Type names of linear geometries are omitted
childWkt = childWkt.mid( childWkt.indexOf( '(' ) );
}
wkt += childWkt + ',';
}
if ( wkt.endsWith( ',' ) )
{
wkt.chop( 1 );
}
wkt += ')';
return wkt;
}
QDomElement QgsCompoundCurve::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
{
// GML2 does not support curves
std::unique_ptr< QgsLineString > line( curveToLine() );
QDomElement gml = line->asGml2( doc, precision, ns, axisOrder );
return gml;
}
QDomElement QgsCompoundCurve::asGml3( QDomDocument &doc, int precision, const QString &ns, const QgsAbstractGeometry::AxisOrder axisOrder ) const
{
QDomElement compoundCurveElem = doc.createElementNS( ns, QStringLiteral( "CompositeCurve" ) );
if ( isEmpty() )
return compoundCurveElem;
for ( const QgsCurve *curve : mCurves )
{
QDomElement curveMemberElem = doc.createElementNS( ns, QStringLiteral( "curveMember" ) );
QDomElement curveElem = curve->asGml3( doc, precision, ns, axisOrder );
curveMemberElem.appendChild( curveElem );
compoundCurveElem.appendChild( curveMemberElem );
}
return compoundCurveElem;
}
QString QgsCompoundCurve::asJson( int precision ) const
{
// GeoJSON does not support curves
std::unique_ptr< QgsLineString > line( curveToLine() );
QString json = line->asJson( precision );
return json;
}
double QgsCompoundCurve::length() const
{
double length = 0;
for ( const QgsCurve *curve : mCurves )
{
length += curve->length();
}
return length;
}
QgsPoint QgsCompoundCurve::startPoint() const
{
if ( mCurves.empty() )
{
return QgsPoint();
}
return mCurves.at( 0 )->startPoint();
}
QgsPoint QgsCompoundCurve::endPoint() const
{
if ( mCurves.empty() )
{
return QgsPoint();
}
return mCurves.at( mCurves.size() - 1 )->endPoint();
}
void QgsCompoundCurve::points( QgsPointSequence &pts ) const
{
pts.clear();
if ( mCurves.empty() )
{
return;
}
mCurves[0]->points( pts );
for ( int i = 1; i < mCurves.size(); ++i )
{
QgsPointSequence pList;
mCurves[i]->points( pList );
pList.removeFirst(); //first vertex already added in previous line
pts.append( pList );
}
}
int QgsCompoundCurve::numPoints() const
{
int nPoints = 0;
int nCurves = mCurves.size();
if ( nCurves < 1 )
{
return 0;
}
for ( int i = 0; i < nCurves; ++i )
{
nPoints += mCurves.at( i )->numPoints() - 1; //last vertex is equal to first of next section
}
nPoints += 1; //last vertex was removed above
return nPoints;
}
bool QgsCompoundCurve::isEmpty() const
{
if ( mCurves.isEmpty() )
return true;
for ( QgsCurve *curve : mCurves )
{
if ( !curve->isEmpty() )
return false;
}
return true;
}
QgsLineString *QgsCompoundCurve::curveToLine( double tolerance, SegmentationToleranceType toleranceType ) const
{
QgsLineString *line = new QgsLineString();
std::unique_ptr< QgsLineString > currentLine;
for ( const QgsCurve *curve : mCurves )
{
currentLine.reset( curve->curveToLine( tolerance, toleranceType ) );
line->append( currentLine.get() );
}
return line;
}
QgsCompoundCurve *QgsCompoundCurve::snappedToGrid( double hSpacing, double vSpacing, double dSpacing, double mSpacing ) const
{
std::unique_ptr<QgsCompoundCurve> result( createEmptyWithSameType() );
for ( QgsCurve *curve : mCurves )
{
std::unique_ptr<QgsCurve> gridified( static_cast< QgsCurve * >( curve->snappedToGrid( hSpacing, vSpacing, dSpacing, mSpacing ) ) );
if ( gridified )
{
result->mCurves.append( gridified.release() );
}
}
if ( result->mCurves.empty() )
return nullptr;
else
return result.release();
}
bool QgsCompoundCurve::removeDuplicateNodes( double epsilon, bool useZValues )
{
bool result = false;
const QVector< QgsCurve * > curves = mCurves;
int i = 0;
QgsPoint lastEnd;
for ( QgsCurve *curve : curves )
{
result = result || curve->removeDuplicateNodes( epsilon, useZValues );
if ( curve->numPoints() == 0 || qgsDoubleNear( curve->length(), 0.0, epsilon ) )
{
// empty curve, remove it
delete mCurves.takeAt( i );
result = true;
}
else
{
// ensure this line starts exactly where previous line ended
if ( i > 0 )
{
curve->moveVertex( QgsVertexId( -1, -1, 0 ), lastEnd );
}
lastEnd = curve->vertexAt( QgsVertexId( -1, -1, curve->numPoints() - 1 ) );
}
i++;
}
return result;
}
const QgsCurve *QgsCompoundCurve::curveAt( int i ) const
{
if ( i < 0 || i >= mCurves.size() )
{
return nullptr;
}
return mCurves.at( i );
}
void QgsCompoundCurve::addCurve( QgsCurve *c )
{
if ( c )
{
if ( mCurves.empty() )
{
setZMTypeFromSubGeometry( c, QgsWkbTypes::CompoundCurve );
}
mCurves.append( c );
if ( QgsWkbTypes::hasZ( mWkbType ) && !QgsWkbTypes::hasZ( c->wkbType() ) )
{
c->addZValue();
}
else if ( !QgsWkbTypes::hasZ( mWkbType ) && QgsWkbTypes::hasZ( c->wkbType() ) )
{
c->dropZValue();
}
if ( QgsWkbTypes::hasM( mWkbType ) && !QgsWkbTypes::hasM( c->wkbType() ) )
{
c->addMValue();
}
else if ( !QgsWkbTypes::hasM( mWkbType ) && QgsWkbTypes::hasM( c->wkbType() ) )
{
c->dropMValue();
}
clearCache();
}
}
void QgsCompoundCurve::removeCurve( int i )
{
if ( i < 0 || i >= mCurves.size() )
{
return;
}
delete mCurves.takeAt( i );
clearCache();
}
void QgsCompoundCurve::addVertex( const QgsPoint &pt )
{
if ( mCurves.isEmpty() || mWkbType == QgsWkbTypes::Unknown )
{
setZMTypeFromSubGeometry( &pt, QgsWkbTypes::CompoundCurve );
}
//is last curve QgsLineString
QgsCurve *lastCurve = nullptr;
if ( !mCurves.isEmpty() )
{
lastCurve = mCurves.at( mCurves.size() - 1 );
}
QgsLineString *line = nullptr;
if ( !lastCurve || QgsWkbTypes::flatType( lastCurve->wkbType() ) != QgsWkbTypes::LineString )
{
line = new QgsLineString();
mCurves.append( line );
if ( lastCurve )
{
line->addVertex( lastCurve->endPoint() );
}
lastCurve = line;
}
else //create new QgsLineString* with point in it
{
line = static_cast<QgsLineString *>( lastCurve );
}
line->addVertex( pt );
clearCache();
}
void QgsCompoundCurve::draw( QPainter &p ) const
{
for ( const QgsCurve *curve : mCurves )
{
curve->draw( p );
}
}
void QgsCompoundCurve::transform( const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d, bool transformZ )
{
for ( QgsCurve *curve : qgis::as_const( mCurves ) )
{
curve->transform( ct, d, transformZ );
}
clearCache();
}
void QgsCompoundCurve::transform( const QTransform &t, double zTranslate, double zScale, double mTranslate, double mScale )
{
for ( QgsCurve *curve : qgis::as_const( mCurves ) )
{
curve->transform( t, zTranslate, zScale, mTranslate, mScale );
}
clearCache();
}
void QgsCompoundCurve::addToPainterPath( QPainterPath &path ) const
{
QPainterPath pp;
for ( const QgsCurve *curve : mCurves )
{
curve->addToPainterPath( pp );
}
path.addPath( pp );
}
void QgsCompoundCurve::drawAsPolygon( QPainter &p ) const
{
QPainterPath pp;
for ( const QgsCurve *curve : mCurves )
{
curve->addToPainterPath( pp );
}
p.drawPath( pp );
}
bool QgsCompoundCurve::insertVertex( QgsVertexId position, const QgsPoint &vertex )
{
QVector< QPair<int, QgsVertexId> > curveIds = curveVertexId( position );
if ( curveIds.empty() )
{
return false;
}
int curveId = curveIds.at( 0 ).first;
if ( curveId >= mCurves.size() )
{
return false;
}
bool success = mCurves.at( curveId )->insertVertex( curveIds.at( 0 ).second, vertex );
if ( success )
{
clearCache(); //bbox changed
}
return success;
}
bool QgsCompoundCurve::moveVertex( QgsVertexId position, const QgsPoint &newPos )
{
QVector< QPair<int, QgsVertexId> > curveIds = curveVertexId( position );
QVector< QPair<int, QgsVertexId> >::const_iterator idIt = curveIds.constBegin();
for ( ; idIt != curveIds.constEnd(); ++idIt )
{
mCurves.at( idIt->first )->moveVertex( idIt->second, newPos );
}
bool success = !curveIds.isEmpty();
if ( success )
{
clearCache(); //bbox changed
}
return success;
}
bool QgsCompoundCurve::deleteVertex( QgsVertexId position )
{
QVector< QPair<int, QgsVertexId> > curveIds = curveVertexId( position );
if ( curveIds.size() == 1 )
{
if ( !mCurves.at( curveIds.at( 0 ).first )->deleteVertex( curveIds.at( 0 ).second ) )
{
clearCache(); //bbox may have changed
return false;
}
if ( mCurves.at( curveIds.at( 0 ).first )->numPoints() == 0 )
{
removeCurve( curveIds.at( 0 ).first );
}
}
else if ( curveIds.size() == 2 )
{
Q_ASSERT( curveIds.at( 1 ).first == curveIds.at( 0 ).first + 1 );
Q_ASSERT( curveIds.at( 0 ).second.vertex == mCurves.at( curveIds.at( 0 ).first )->numPoints() - 1 );
Q_ASSERT( curveIds.at( 1 ).second.vertex == 0 );
QgsPoint startPoint = mCurves.at( curveIds.at( 0 ).first ) ->startPoint();
QgsPoint endPoint = mCurves.at( curveIds.at( 1 ).first ) ->endPoint();
if ( QgsWkbTypes::flatType( mCurves.at( curveIds.at( 0 ).first )->wkbType() ) == QgsWkbTypes::LineString &&
QgsWkbTypes::flatType( mCurves.at( curveIds.at( 1 ).first )->wkbType() ) == QgsWkbTypes::CircularString &&
mCurves.at( curveIds.at( 1 ).first )->numPoints() > 3 )
{
QgsPoint intermediatePoint;
QgsVertexId::VertexType type;
mCurves.at( curveIds.at( 1 ).first ) ->pointAt( 2, intermediatePoint, type );
mCurves.at( curveIds.at( 0 ).first )->moveVertex(
QgsVertexId( 0, 0, mCurves.at( curveIds.at( 0 ).first )->numPoints() - 1 ), intermediatePoint );
}
else if ( !mCurves.at( curveIds.at( 0 ).first )->deleteVertex( curveIds.at( 0 ).second ) )
{
clearCache(); //bbox may have changed
return false;
}
if ( QgsWkbTypes::flatType( mCurves.at( curveIds.at( 0 ).first )->wkbType() ) == QgsWkbTypes::CircularString &&
mCurves.at( curveIds.at( 0 ).first )->numPoints() > 0 &&
QgsWkbTypes::flatType( mCurves.at( curveIds.at( 1 ).first )->wkbType() ) == QgsWkbTypes::LineString )
{
QgsPoint intermediatePoint = mCurves.at( curveIds.at( 0 ).first ) ->endPoint();
mCurves.at( curveIds.at( 1 ).first )->moveVertex( QgsVertexId( 0, 0, 0 ), intermediatePoint );
}
else if ( !mCurves.at( curveIds.at( 1 ).first )->deleteVertex( curveIds.at( 1 ).second ) )
{
clearCache(); //bbox may have changed
return false;
}
if ( mCurves.at( curveIds.at( 0 ).first )->numPoints() == 0 &&
mCurves.at( curveIds.at( 1 ).first )->numPoints() != 0 )
{
mCurves.at( curveIds.at( 1 ).first )->moveVertex( QgsVertexId( 0, 0, 0 ), startPoint );
removeCurve( curveIds.at( 0 ).first );
}
else if ( mCurves.at( curveIds.at( 0 ).first )->numPoints() != 0 &&
mCurves.at( curveIds.at( 1 ).first )->numPoints() == 0 )
{
mCurves.at( curveIds.at( 0 ).first )->moveVertex(
QgsVertexId( 0, 0, mCurves.at( curveIds.at( 0 ).first )->numPoints() - 1 ), endPoint );
removeCurve( curveIds.at( 1 ).first );
}
else if ( mCurves.at( curveIds.at( 0 ).first )->numPoints() == 0 &&
mCurves.at( curveIds.at( 1 ).first )->numPoints() == 0 )
{
removeCurve( curveIds.at( 1 ).first );
removeCurve( curveIds.at( 0 ).first );
QgsLineString *line = new QgsLineString();
line->insertVertex( QgsVertexId( 0, 0, 0 ), startPoint );
line->insertVertex( QgsVertexId( 0, 0, 1 ), endPoint );
mCurves.insert( curveIds.at( 0 ).first, line );
}
else
{
QgsPoint endPointOfFirst = mCurves.at( curveIds.at( 0 ).first ) ->endPoint();
QgsPoint startPointOfSecond = mCurves.at( curveIds.at( 1 ).first ) ->startPoint();
if ( endPointOfFirst != startPointOfSecond )
{
QgsLineString *line = new QgsLineString();
line->insertVertex( QgsVertexId( 0, 0, 0 ), endPointOfFirst );
line->insertVertex( QgsVertexId( 0, 0, 1 ), startPointOfSecond );
mCurves.insert( curveIds.at( 1 ).first, line );
}
}
}
bool success = !curveIds.isEmpty();
if ( success )
{
clearCache(); //bbox changed
}
return success;
}
QVector< QPair<int, QgsVertexId> > QgsCompoundCurve::curveVertexId( QgsVertexId id ) const
{
QVector< QPair<int, QgsVertexId> > curveIds;
int currentVertexIndex = 0;
for ( int i = 0; i < mCurves.size(); ++i )
{
int increment = mCurves.at( i )->numPoints() - 1;
if ( id.vertex >= currentVertexIndex && id.vertex <= currentVertexIndex + increment )
{
int curveVertexId = id.vertex - currentVertexIndex;
QgsVertexId vid;
vid.part = 0;
vid.ring = 0;
vid.vertex = curveVertexId;
curveIds.append( qMakePair( i, vid ) );
if ( curveVertexId == increment && i < ( mCurves.size() - 1 ) ) //add first vertex of next curve
{
vid.vertex = 0;
curveIds.append( qMakePair( i + 1, vid ) );
}
break;
}
currentVertexIndex += increment;
}
return curveIds;
}
double QgsCompoundCurve::closestSegment( const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon ) const
{
return QgsGeometryUtils::closestSegmentFromComponents( mCurves, QgsGeometryUtils::Vertex, pt, segmentPt, vertexAfter, leftOf, epsilon );
}
bool QgsCompoundCurve::pointAt( int node, QgsPoint &point, QgsVertexId::VertexType &type ) const
{
int currentVertexId = 0;
for ( int j = 0; j < mCurves.size(); ++j )
{
int nCurvePoints = mCurves.at( j )->numPoints();
if ( ( node - currentVertexId ) < nCurvePoints )
{
return ( mCurves.at( j )->pointAt( node - currentVertexId, point, type ) );
}
currentVertexId += ( nCurvePoints - 1 );
}
return false;
}
double QgsCompoundCurve::xAt( int index ) const
{
int currentVertexId = 0;
for ( int j = 0; j < mCurves.size(); ++j )
{
int nCurvePoints = mCurves.at( j )->numPoints();
if ( ( index - currentVertexId ) < nCurvePoints )
{
return mCurves.at( j )->xAt( index - currentVertexId );
}
currentVertexId += ( nCurvePoints - 1 );
}
return 0.0;
}
double QgsCompoundCurve::yAt( int index ) const
{
int currentVertexId = 0;
for ( int j = 0; j < mCurves.size(); ++j )
{
int nCurvePoints = mCurves.at( j )->numPoints();
if ( ( index - currentVertexId ) < nCurvePoints )
{
return mCurves.at( j )->yAt( index - currentVertexId );
}
currentVertexId += ( nCurvePoints - 1 );
}
return 0.0;
}
void QgsCompoundCurve::filterVertices( const std::function<bool ( const QgsPoint & )> &filter )
{
for ( QgsCurve *curve : qgis::as_const( mCurves ) )
{
curve->filterVertices( filter );
}
clearCache();
}
void QgsCompoundCurve::transformVertices( const std::function<QgsPoint( const QgsPoint & )> &transform )
{
for ( QgsCurve *curve : qgis::as_const( mCurves ) )
{
curve->transformVertices( transform );
}
clearCache();
}
void QgsCompoundCurve::sumUpArea( double &sum ) const
{
for ( const QgsCurve *curve : mCurves )
{
curve->sumUpArea( sum );
}
}
void QgsCompoundCurve::close()
{
if ( numPoints() < 1 || isClosed() )
{
return;
}
addVertex( startPoint() );
}
bool QgsCompoundCurve::hasCurvedSegments() const
{
for ( const QgsCurve *curve : mCurves )
{
if ( curve->hasCurvedSegments() )
{
return true;
}
}
return false;
}
double QgsCompoundCurve::vertexAngle( QgsVertexId vertex ) const
{
QVector< QPair<int, QgsVertexId> > curveIds = curveVertexId( vertex );
if ( curveIds.size() == 1 )
{
QgsCurve *curve = mCurves[curveIds.at( 0 ).first];
return curve->vertexAngle( curveIds.at( 0 ).second );
}
else if ( curveIds.size() > 1 )
{
QgsCurve *curve1 = mCurves[curveIds.at( 0 ).first];
QgsCurve *curve2 = mCurves[curveIds.at( 1 ).first];
double angle1 = curve1->vertexAngle( curveIds.at( 0 ).second );
double angle2 = curve2->vertexAngle( curveIds.at( 1 ).second );
return QgsGeometryUtils::averageAngle( angle1, angle2 );
}
else
{
return 0.0;
}
}
double QgsCompoundCurve::segmentLength( QgsVertexId startVertex ) const
{
QVector< QPair<int, QgsVertexId> > curveIds = curveVertexId( startVertex );
double length = 0.0;
for ( auto it = curveIds.constBegin(); it != curveIds.constEnd(); ++it )
{
length += mCurves.at( it->first )->segmentLength( it->second );
}
return length;
}
QgsCompoundCurve *QgsCompoundCurve::reversed() const
{
QgsCompoundCurve *clone = new QgsCompoundCurve();
for ( int i = mCurves.count() - 1; i >= 0; --i )
{
QgsCurve *reversedCurve = mCurves.at( i )->reversed();
clone->addCurve( reversedCurve );
}
return clone;
}
QgsPoint *QgsCompoundCurve::interpolatePoint( const double distance ) const
{
if ( distance < 0 )
return nullptr;
double distanceTraversed = 0;
for ( const QgsCurve *curve : mCurves )
{
const double thisCurveLength = curve->length();
if ( distanceTraversed + thisCurveLength > distance || qgsDoubleNear( distanceTraversed + thisCurveLength, distance ) )
{
// point falls on this segment - truncate to segment length if qgsDoubleNear test was actually > segment length
const double distanceToPoint = std::min( distance - distanceTraversed, thisCurveLength );
// point falls on this curve
return curve->interpolatePoint( distanceToPoint );
}
distanceTraversed += thisCurveLength;
}
return nullptr;
}
QgsCompoundCurve *QgsCompoundCurve::curveSubstring( double startDistance, double endDistance ) const
{
if ( startDistance < 0 && endDistance < 0 )
return createEmptyWithSameType();
endDistance = std::max( startDistance, endDistance );
std::unique_ptr< QgsCompoundCurve > substring = qgis::make_unique< QgsCompoundCurve >();
double distanceTraversed = 0;
for ( const QgsCurve *curve : mCurves )
{
const double thisCurveLength = curve->length();
if ( distanceTraversed + thisCurveLength < startDistance )
{
// keep going - haven't found start yet, so no need to include this curve at all
}
else
{
std::unique_ptr< QgsCurve > part( curve->curveSubstring( startDistance - distanceTraversed, endDistance - distanceTraversed ) );
if ( part )
substring->addCurve( part.release() );
}
distanceTraversed += thisCurveLength;
if ( distanceTraversed > endDistance )
break;
}
return substring.release();
}
bool QgsCompoundCurve::addZValue( double zValue )
{
if ( QgsWkbTypes::hasZ( mWkbType ) )
return false;
mWkbType = QgsWkbTypes::addZ( mWkbType );
for ( QgsCurve *curve : qgis::as_const( mCurves ) )
{
curve->addZValue( zValue );
}
clearCache();
return true;
}
bool QgsCompoundCurve::addMValue( double mValue )
{
if ( QgsWkbTypes::hasM( mWkbType ) )
return false;
mWkbType = QgsWkbTypes::addM( mWkbType );
for ( QgsCurve *curve : qgis::as_const( mCurves ) )
{
curve->addMValue( mValue );
}
clearCache();
return true;
}
bool QgsCompoundCurve::dropZValue()
{
if ( !QgsWkbTypes::hasZ( mWkbType ) )
return false;
mWkbType = QgsWkbTypes::dropZ( mWkbType );
for ( QgsCurve *curve : qgis::as_const( mCurves ) )
{
curve->dropZValue();
}
clearCache();
return true;
}
bool QgsCompoundCurve::dropMValue()
{
if ( !QgsWkbTypes::hasM( mWkbType ) )
return false;
mWkbType = QgsWkbTypes::dropM( mWkbType );
for ( QgsCurve *curve : qgis::as_const( mCurves ) )
{
curve->dropMValue();
}
clearCache();
return true;
}
void QgsCompoundCurve::swapXy()
{
for ( QgsCurve *curve : qgis::as_const( mCurves ) )
{
curve->swapXy();
}
clearCache();
}