/
qgsgeometryutils.cpp
1167 lines (1016 loc) · 34.1 KB
/
qgsgeometryutils.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
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsgeometryutils.cpp
-------------------------------------------------------------------
Date : 21 Nov 2014
Copyright : (C) 2014 by Marco Hugentobler
email : marco.hugentobler at sourcepole dot com
***************************************************************************
* *
* 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 "qgsgeometryutils.h"
#include "qgscurve.h"
#include "qgscurvepolygon.h"
#include "qgsgeometrycollection.h"
#include "qgslinestring.h"
#include "qgswkbptr.h"
#include "qgslogger.h"
#include <memory>
#include <QStringList>
#include <QVector>
#include <QRegularExpression>
QVector<QgsLineString *> QgsGeometryUtils::extractLineStrings( const QgsAbstractGeometry *geom )
{
QVector< QgsLineString * > linestrings;
if ( !geom )
return linestrings;
QVector< const QgsAbstractGeometry * > geometries;
geometries << geom;
while ( ! geometries.isEmpty() )
{
const QgsAbstractGeometry *g = geometries.takeFirst();
if ( const QgsCurve *curve = qgsgeometry_cast< const QgsCurve * >( g ) )
{
linestrings << static_cast< QgsLineString * >( curve->segmentize() );
}
else if ( const QgsGeometryCollection *collection = qgsgeometry_cast< const QgsGeometryCollection * >( g ) )
{
for ( int i = 0; i < collection->numGeometries(); ++i )
{
geometries.append( collection->geometryN( i ) );
}
}
else if ( const QgsCurvePolygon *curvePolygon = qgsgeometry_cast< const QgsCurvePolygon * >( g ) )
{
if ( curvePolygon->exteriorRing() )
linestrings << static_cast< QgsLineString * >( curvePolygon->exteriorRing()->segmentize() );
for ( int i = 0; i < curvePolygon->numInteriorRings(); ++i )
{
linestrings << static_cast< QgsLineString * >( curvePolygon->interiorRing( i )->segmentize() );
}
}
}
return linestrings;
}
QgsPoint QgsGeometryUtils::closestVertex( const QgsAbstractGeometry &geom, const QgsPoint &pt, QgsVertexId &id )
{
double minDist = std::numeric_limits<double>::max();
double currentDist = 0;
QgsPoint minDistPoint;
id = QgsVertexId(); // set as invalid
QgsVertexId vertexId;
QgsPoint vertex;
while ( geom.nextVertex( vertexId, vertex ) )
{
currentDist = QgsGeometryUtils::sqrDistance2D( pt, vertex );
// The <= is on purpose: for geometries with closing vertices, this ensures
// that the closing vertex is retuned. For the node tool, the rubberband
// of the closing vertex is above the opening vertex, hence with the <=
// situations where the covered opening vertex rubberband is selected are
// avoided.
if ( currentDist <= minDist )
{
minDist = currentDist;
minDistPoint = vertex;
id.part = vertexId.part;
id.ring = vertexId.ring;
id.vertex = vertexId.vertex;
id.type = vertexId.type;
}
}
return minDistPoint;
}
QgsPoint QgsGeometryUtils::closestPoint( const QgsAbstractGeometry &geometry, const QgsPoint &point )
{
QgsPoint closestPoint;
QgsVertexId vertexAfter;
geometry.closestSegment( point, closestPoint, vertexAfter, nullptr, DEFAULT_SEGMENT_EPSILON );
if ( vertexAfter.isValid() )
{
QgsPoint pointAfter = geometry.vertexAt( vertexAfter );
if ( vertexAfter.vertex > 0 )
{
QgsVertexId vertexBefore = vertexAfter;
vertexBefore.vertex--;
QgsPoint pointBefore = geometry.vertexAt( vertexBefore );
double length = pointBefore.distance( pointAfter );
double distance = pointBefore.distance( closestPoint );
if ( qgsDoubleNear( distance, 0.0 ) )
closestPoint = pointBefore;
else if ( qgsDoubleNear( distance, length ) )
closestPoint = pointAfter;
else
{
if ( QgsWkbTypes::hasZ( geometry.wkbType() ) && length )
closestPoint.addZValue( pointBefore.z() + ( pointAfter.z() - pointBefore.z() ) * distance / length );
if ( QgsWkbTypes::hasM( geometry.wkbType() ) )
closestPoint.addMValue( pointBefore.m() + ( pointAfter.m() - pointBefore.m() ) * distance / length );
}
}
}
return closestPoint;
}
double QgsGeometryUtils::distanceToVertex( const QgsAbstractGeometry &geom, QgsVertexId id )
{
double currentDist = 0;
QgsVertexId vertexId;
QgsPoint vertex;
while ( geom.nextVertex( vertexId, vertex ) )
{
if ( vertexId == id )
{
//found target vertex
return currentDist;
}
currentDist += geom.segmentLength( vertexId );
}
//could not find target vertex
return -1;
}
bool QgsGeometryUtils::verticesAtDistance( const QgsAbstractGeometry &geometry, double distance, QgsVertexId &previousVertex, QgsVertexId &nextVertex )
{
double currentDist = 0;
previousVertex = QgsVertexId();
nextVertex = QgsVertexId();
QgsPoint point;
QgsPoint previousPoint;
if ( qgsDoubleNear( distance, 0.0 ) )
{
geometry.nextVertex( previousVertex, point );
nextVertex = previousVertex;
return true;
}
bool first = true;
while ( currentDist < distance && geometry.nextVertex( nextVertex, point ) )
{
if ( !first )
{
currentDist += std::sqrt( QgsGeometryUtils::sqrDistance2D( previousPoint, point ) );
}
if ( qgsDoubleNear( currentDist, distance ) )
{
// exact hit!
previousVertex = nextVertex;
return true;
}
if ( currentDist > distance )
{
return true;
}
previousVertex = nextVertex;
previousPoint = point;
first = false;
}
//could not find target distance
return false;
}
double QgsGeometryUtils::sqrDistance2D( const QgsPoint &pt1, const QgsPoint &pt2 )
{
return ( pt1.x() - pt2.x() ) * ( pt1.x() - pt2.x() ) + ( pt1.y() - pt2.y() ) * ( pt1.y() - pt2.y() );
}
double QgsGeometryUtils::sqrDistToLine( double ptX, double ptY, double x1, double y1, double x2, double y2, double &minDistX, double &minDistY, double epsilon )
{
minDistX = x1;
minDistY = y1;
double dx = x2 - x1;
double dy = y2 - y1;
if ( !qgsDoubleNear( dx, 0.0 ) || !qgsDoubleNear( dy, 0.0 ) )
{
double t = ( ( ptX - x1 ) * dx + ( ptY - y1 ) * dy ) / ( dx * dx + dy * dy );
if ( t > 1 )
{
minDistX = x2;
minDistY = y2;
}
else if ( t > 0 )
{
minDistX += dx * t;
minDistY += dy * t;
}
}
dx = ptX - minDistX;
dy = ptY - minDistY;
double dist = dx * dx + dy * dy;
//prevent rounding errors if the point is directly on the segment
if ( qgsDoubleNear( dist, 0.0, epsilon ) )
{
minDistX = ptX;
minDistY = ptY;
return 0.0;
}
return dist;
}
bool QgsGeometryUtils::lineIntersection( const QgsPoint &p1, QgsVector v1, const QgsPoint &p2, QgsVector v2, QgsPoint &intersection )
{
double d = v1.y() * v2.x() - v1.x() * v2.y();
if ( qgsDoubleNear( d, 0 ) )
return false;
double dx = p2.x() - p1.x();
double dy = p2.y() - p1.y();
double k = ( dy * v2.x() - dx * v2.y() ) / d;
intersection = QgsPoint( p1.x() + v1.x() * k, p1.y() + v1.y() * k );
return true;
}
bool QgsGeometryUtils::segmentIntersection( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &q1, const QgsPoint &q2, QgsPoint &intersectionPoint, bool &isIntersection, const double tolerance, bool acceptImproperIntersection )
{
isIntersection = false;
QgsVector v( p2.x() - p1.x(), p2.y() - p1.y() );
QgsVector w( q2.x() - q1.x(), q2.y() - q1.y() );
double vl = v.length();
double wl = w.length();
if ( qgsDoubleNear( vl, 0.0, tolerance ) || qgsDoubleNear( wl, 0.0, tolerance ) )
{
return false;
}
v = v / vl;
w = w / wl;
if ( !QgsGeometryUtils::lineIntersection( p1, v, q1, w, intersectionPoint ) )
{
return false;
}
isIntersection = true;
if ( acceptImproperIntersection )
{
if ( ( p1 == q1 ) || ( p1 == q2 ) )
{
intersectionPoint = p1;
return true;
}
else if ( ( p2 == q1 ) || ( p2 == q2 ) )
{
intersectionPoint = p2;
return true;
}
double x, y;
if (
// intersectionPoint = p1
qgsDoubleNear( QgsGeometryUtils::sqrDistToLine( p1.x(), p1.y(), q1.x(), q1.y(), q2.x(), q2.y(), x, y, tolerance ), 0.0, tolerance ) ||
// intersectionPoint = p2
qgsDoubleNear( QgsGeometryUtils::sqrDistToLine( p2.x(), p2.y(), q1.x(), q1.y(), q2.x(), q2.y(), x, y, tolerance ), 0.0, tolerance ) ||
// intersectionPoint = q1
qgsDoubleNear( QgsGeometryUtils::sqrDistToLine( q1.x(), q1.y(), p1.x(), p1.y(), p2.x(), p2.y(), x, y, tolerance ), 0.0, tolerance ) ||
// intersectionPoint = q2
qgsDoubleNear( QgsGeometryUtils::sqrDistToLine( q2.x(), q2.y(), p1.x(), p1.y(), p2.x(), p2.y(), x, y, tolerance ), 0.0, tolerance )
)
{
return true;
}
}
double lambdav = QgsVector( intersectionPoint.x() - p1.x(), intersectionPoint.y() - p1.y() ) * v;
if ( lambdav < 0. + tolerance || lambdav > vl - tolerance )
return false;
double lambdaw = QgsVector( intersectionPoint.x() - q1.x(), intersectionPoint.y() - q1.y() ) * w;
return !( lambdaw < 0. + tolerance || lambdaw >= wl - tolerance );
}
bool QgsGeometryUtils::lineCircleIntersection( const QgsPointXY ¢er, const double radius,
const QgsPointXY &linePoint1, const QgsPointXY &linePoint2,
QgsPointXY &intersection )
{
// formula taken from http://mathworld.wolfram.com/Circle-LineIntersection.html
const double x1 = linePoint1.x() - center.x();
const double y1 = linePoint1.y() - center.y();
const double x2 = linePoint2.x() - center.x();
const double y2 = linePoint2.y() - center.y();
const double dx = x2 - x1;
const double dy = y2 - y1;
const double dr = std::sqrt( std::pow( dx, 2 ) + std::pow( dy, 2 ) );
const double d = x1 * y2 - x2 * y1;
const double disc = std::pow( radius, 2 ) * std::pow( dr, 2 ) - std::pow( d, 2 );
if ( disc < 0 )
{
//no intersection or tangent
return false;
}
else
{
// two solutions
const int sgnDy = dy < 0 ? -1 : 1;
const double ax = center.x() + ( d * dy + sgnDy * dx * std::sqrt( std::pow( radius, 2 ) * std::pow( dr, 2 ) - std::pow( d, 2 ) ) ) / ( std::pow( dr, 2 ) );
const double ay = center.y() + ( -d * dx + std::fabs( dy ) * std::sqrt( std::pow( radius, 2 ) * std::pow( dr, 2 ) - std::pow( d, 2 ) ) ) / ( std::pow( dr, 2 ) );
const QgsPointXY p1( ax, ay );
const double bx = center.x() + ( d * dy - sgnDy * dx * std::sqrt( std::pow( radius, 2 ) * std::pow( dr, 2 ) - std::pow( d, 2 ) ) ) / ( std::pow( dr, 2 ) );
const double by = center.y() + ( -d * dx - std::fabs( dy ) * std::sqrt( std::pow( radius, 2 ) * std::pow( dr, 2 ) - std::pow( d, 2 ) ) ) / ( std::pow( dr, 2 ) );
const QgsPointXY p2( bx, by );
// snap to nearest intersection
if ( intersection.sqrDist( p1 ) < intersection.sqrDist( p2 ) )
{
intersection.set( p1.x(), p1.y() );
}
else
{
intersection.set( p2.x(), p2.y() );
}
return true;
}
}
QVector<QgsGeometryUtils::SelfIntersection> QgsGeometryUtils::getSelfIntersections( const QgsAbstractGeometry *geom, int part, int ring, double tolerance )
{
QVector<SelfIntersection> intersections;
int n = geom->vertexCount( part, ring );
bool isClosed = geom->vertexAt( QgsVertexId( part, ring, 0 ) ) == geom->vertexAt( QgsVertexId( part, ring, n - 1 ) );
// Check every pair of segments for intersections
for ( int i = 0, j = 1; j < n; i = j++ )
{
QgsPoint pi = geom->vertexAt( QgsVertexId( part, ring, i ) );
QgsPoint pj = geom->vertexAt( QgsVertexId( part, ring, j ) );
if ( QgsGeometryUtils::sqrDistance2D( pi, pj ) < tolerance * tolerance ) continue;
// Don't test neighboring edges
int start = j + 1;
int end = i == 0 && isClosed ? n - 1 : n;
for ( int k = start, l = start + 1; l < end; k = l++ )
{
QgsPoint pk = geom->vertexAt( QgsVertexId( part, ring, k ) );
QgsPoint pl = geom->vertexAt( QgsVertexId( part, ring, l ) );
QgsPoint inter;
bool intersection = false;
if ( !QgsGeometryUtils::segmentIntersection( pi, pj, pk, pl, inter, intersection, tolerance ) ) continue;
SelfIntersection s;
s.segment1 = i;
s.segment2 = k;
if ( s.segment1 > s.segment2 )
{
std::swap( s.segment1, s.segment2 );
}
s.point = inter;
intersections.append( s );
}
}
return intersections;
}
int QgsGeometryUtils::leftOfLine( double x, double y, double x1, double y1, double x2, double y2 )
{
double f1 = x - x1;
double f2 = y2 - y1;
double f3 = y - y1;
double f4 = x2 - x1;
double test = ( f1 * f2 - f3 * f4 );
// return -1, 0, or 1
return qgsDoubleNear( test, 0.0 ) ? 0 : ( test < 0 ? -1 : 1 );
}
QgsPoint QgsGeometryUtils::pointOnLineWithDistance( const QgsPoint &startPoint, const QgsPoint &directionPoint, double distance )
{
double dx = directionPoint.x() - startPoint.x();
double dy = directionPoint.y() - startPoint.y();
double length = std::sqrt( dx * dx + dy * dy );
if ( qgsDoubleNear( length, 0.0 ) )
{
return startPoint;
}
double scaleFactor = distance / length;
return QgsPoint( startPoint.x() + dx * scaleFactor, startPoint.y() + dy * scaleFactor );
}
double QgsGeometryUtils::ccwAngle( double dy, double dx )
{
double angle = std::atan2( dy, dx ) * 180 / M_PI;
if ( angle < 0 )
{
return 360 + angle;
}
else if ( angle > 360 )
{
return 360 - angle;
}
return angle;
}
void QgsGeometryUtils::circleCenterRadius( const QgsPoint &pt1, const QgsPoint &pt2, const QgsPoint &pt3, double &radius, double ¢erX, double ¢erY )
{
double dx21, dy21, dx31, dy31, h21, h31, d;
//closed circle
if ( qgsDoubleNear( pt1.x(), pt3.x() ) && qgsDoubleNear( pt1.y(), pt3.y() ) )
{
centerX = ( pt1.x() + pt2.x() ) / 2.0;
centerY = ( pt1.y() + pt2.y() ) / 2.0;
radius = std::sqrt( std::pow( centerX - pt1.x(), 2.0 ) + std::pow( centerY - pt1.y(), 2.0 ) );
return;
}
// Using Cartesian circumcenter eguations from page https://en.wikipedia.org/wiki/Circumscribed_circle
dx21 = pt2.x() - pt1.x();
dy21 = pt2.y() - pt1.y();
dx31 = pt3.x() - pt1.x();
dy31 = pt3.y() - pt1.y();
h21 = std::pow( dx21, 2.0 ) + std::pow( dy21, 2.0 );
h31 = std::pow( dx31, 2.0 ) + std::pow( dy31, 2.0 );
// 2*Cross product, d<0 means clockwise and d>0 counterclockwise sweeping angle
d = 2 * ( dx21 * dy31 - dx31 * dy21 );
// Check colinearity, Cross product = 0
if ( qgsDoubleNear( std::fabs( d ), 0.0, 0.00000000001 ) )
{
radius = -1.0;
return;
}
// Calculate centroid coordinates and radius
centerX = pt1.x() + ( h21 * dy31 - h31 * dy21 ) / d;
centerY = pt1.y() - ( h21 * dx31 - h31 * dx21 ) / d;
radius = std::sqrt( std::pow( centerX - pt1.x(), 2.0 ) + std::pow( centerY - pt1.y(), 2.0 ) );
}
bool QgsGeometryUtils::circleClockwise( double angle1, double angle2, double angle3 )
{
if ( angle3 >= angle1 )
{
return !( angle2 > angle1 && angle2 < angle3 );
}
else
{
return !( angle2 > angle1 || angle2 < angle3 );
}
}
bool QgsGeometryUtils::circleAngleBetween( double angle, double angle1, double angle2, bool clockwise )
{
if ( clockwise )
{
if ( angle2 < angle1 )
{
return ( angle <= angle1 && angle >= angle2 );
}
else
{
return ( angle <= angle1 || angle >= angle2 );
}
}
else
{
if ( angle2 > angle1 )
{
return ( angle >= angle1 && angle <= angle2 );
}
else
{
return ( angle >= angle1 || angle <= angle2 );
}
}
}
bool QgsGeometryUtils::angleOnCircle( double angle, double angle1, double angle2, double angle3 )
{
bool clockwise = circleClockwise( angle1, angle2, angle3 );
return circleAngleBetween( angle, angle1, angle3, clockwise );
}
double QgsGeometryUtils::circleLength( double x1, double y1, double x2, double y2, double x3, double y3 )
{
double centerX, centerY, radius;
circleCenterRadius( QgsPoint( x1, y1 ), QgsPoint( x2, y2 ), QgsPoint( x3, y3 ), radius, centerX, centerY );
double length = M_PI / 180.0 * radius * sweepAngle( centerX, centerY, x1, y1, x2, y2, x3, y3 );
if ( length < 0 )
{
length = -length;
}
return length;
}
double QgsGeometryUtils::sweepAngle( double centerX, double centerY, double x1, double y1, double x2, double y2, double x3, double y3 )
{
double p1Angle = QgsGeometryUtils::ccwAngle( y1 - centerY, x1 - centerX );
double p2Angle = QgsGeometryUtils::ccwAngle( y2 - centerY, x2 - centerX );
double p3Angle = QgsGeometryUtils::ccwAngle( y3 - centerY, x3 - centerX );
if ( p3Angle >= p1Angle )
{
if ( p2Angle > p1Angle && p2Angle < p3Angle )
{
return ( p3Angle - p1Angle );
}
else
{
return ( - ( p1Angle + ( 360 - p3Angle ) ) );
}
}
else
{
if ( p2Angle < p1Angle && p2Angle > p3Angle )
{
return ( -( p1Angle - p3Angle ) );
}
else
{
return ( p3Angle + ( 360 - p1Angle ) );
}
}
}
bool QgsGeometryUtils::segmentMidPoint( const QgsPoint &p1, const QgsPoint &p2, QgsPoint &result, double radius, const QgsPoint &mousePos )
{
QgsPoint midPoint( ( p1.x() + p2.x() ) / 2.0, ( p1.y() + p2.y() ) / 2.0 );
double midDist = std::sqrt( sqrDistance2D( p1, midPoint ) );
if ( radius < midDist )
{
return false;
}
double centerMidDist = std::sqrt( radius * radius - midDist * midDist );
double dist = radius - centerMidDist;
double midDx = midPoint.x() - p1.x();
double midDy = midPoint.y() - p1.y();
//get the four possible midpoints
QVector<QgsPoint> possibleMidPoints;
possibleMidPoints.append( pointOnLineWithDistance( midPoint, QgsPoint( midPoint.x() - midDy, midPoint.y() + midDx ), dist ) );
possibleMidPoints.append( pointOnLineWithDistance( midPoint, QgsPoint( midPoint.x() - midDy, midPoint.y() + midDx ), 2 * radius - dist ) );
possibleMidPoints.append( pointOnLineWithDistance( midPoint, QgsPoint( midPoint.x() + midDy, midPoint.y() - midDx ), dist ) );
possibleMidPoints.append( pointOnLineWithDistance( midPoint, QgsPoint( midPoint.x() + midDy, midPoint.y() - midDx ), 2 * radius - dist ) );
//take the closest one
double minDist = std::numeric_limits<double>::max();
int minDistIndex = -1;
for ( int i = 0; i < possibleMidPoints.size(); ++i )
{
double currentDist = sqrDistance2D( mousePos, possibleMidPoints.at( i ) );
if ( currentDist < minDist )
{
minDistIndex = i;
minDist = currentDist;
}
}
if ( minDistIndex == -1 )
{
return false;
}
result = possibleMidPoints.at( minDistIndex );
return true;
}
double QgsGeometryUtils::circleTangentDirection( const QgsPoint &tangentPoint, const QgsPoint &cp1,
const QgsPoint &cp2, const QgsPoint &cp3 )
{
//calculate circle midpoint
double mX, mY, radius;
circleCenterRadius( cp1, cp2, cp3, radius, mX, mY );
double p1Angle = QgsGeometryUtils::ccwAngle( cp1.y() - mY, cp1.x() - mX );
double p2Angle = QgsGeometryUtils::ccwAngle( cp2.y() - mY, cp2.x() - mX );
double p3Angle = QgsGeometryUtils::ccwAngle( cp3.y() - mY, cp3.x() - mX );
double angle = 0;
if ( circleClockwise( p1Angle, p2Angle, p3Angle ) )
{
angle = lineAngle( tangentPoint.x(), tangentPoint.y(), mX, mY ) - M_PI_2;
}
else
{
angle = lineAngle( mX, mY, tangentPoint.x(), tangentPoint.y() ) - M_PI_2;
}
if ( angle < 0 )
angle += 2 * M_PI;
return angle;
}
void QgsGeometryUtils::segmentizeArc( const QgsPoint &p1, const QgsPoint &p2, const QgsPoint &p3, QgsPointSequence &points, double tolerance, QgsAbstractGeometry::SegmentationToleranceType toleranceType, bool hasZ, bool hasM )
{
bool reversed = false;
int segSide = segmentSide( p1, p3, p2 );
QgsPoint circlePoint1;
const QgsPoint circlePoint2 = p2;
QgsPoint circlePoint3;
if ( segSide == -1 )
{
// Reverse !
circlePoint1 = p3;
circlePoint3 = p1;
reversed = true;
}
else
{
circlePoint1 = p1;
circlePoint3 = p3;
}
//adapted code from PostGIS
double radius = 0;
double centerX = 0;
double centerY = 0;
circleCenterRadius( circlePoint1, circlePoint2, circlePoint3, radius, centerX, centerY );
if ( circlePoint1 != circlePoint3 && ( radius < 0 || qgsDoubleNear( segSide, 0.0 ) ) ) //points are colinear
{
points.append( p1 );
points.append( p2 );
points.append( p3 );
return;
}
double increment = tolerance; //one segment per degree
if ( toleranceType == QgsAbstractGeometry::MaximumDifference )
{
double halfAngle = std::acos( -tolerance / radius + 1 );
increment = 2 * halfAngle;
}
//angles of pt1, pt2, pt3
double a1 = std::atan2( circlePoint1.y() - centerY, circlePoint1.x() - centerX );
double a2 = std::atan2( circlePoint2.y() - centerY, circlePoint2.x() - centerX );
double a3 = std::atan2( circlePoint3.y() - centerY, circlePoint3.x() - centerX );
// Make segmentation symmetric
const bool symmetric = true;
if ( symmetric )
{
double angle = a3 - a1;
if ( angle < 0 ) angle += M_PI * 2;
/* Number of segments in output */
int segs = ceil( angle / increment );
/* Tweak increment to be regular for all the arc */
increment = angle / segs;
}
/* Adjust a3 up so we can increment from a1 to a3 cleanly */
if ( a3 < a1 )
a3 += 2.0 * M_PI;
if ( a2 < a1 )
a2 += 2.0 * M_PI;
double x, y;
double z = 0;
double m = 0;
QVector<QgsPoint> stringPoints;
stringPoints.insert( 0, circlePoint1 );
if ( circlePoint2 != circlePoint3 && circlePoint1 != circlePoint2 ) //draw straight line segment if two points have the same position
{
QgsWkbTypes::Type pointWkbType = QgsWkbTypes::Point;
if ( hasZ )
pointWkbType = QgsWkbTypes::addZ( pointWkbType );
if ( hasM )
pointWkbType = QgsWkbTypes::addM( pointWkbType );
// As we're adding the last point in any case, we'll avoid
// including a point which is at less than 1% increment distance
// from it (may happen to find them due to numbers approximation).
// NOTE that this effectively allows in output some segments which
// are more distant than requested. This is at most 1% off
// from requested MaxAngle and less for MaxError.
double tolError = increment / 100;
double stopAngle = a3 - tolError;
for ( double angle = a1 + increment; angle < stopAngle; angle += increment )
{
x = centerX + radius * std::cos( angle );
y = centerY + radius * std::sin( angle );
if ( hasZ )
{
z = interpolateArcValue( angle, a1, a2, a3, circlePoint1.z(), circlePoint2.z(), circlePoint3.z() );
}
if ( hasM )
{
m = interpolateArcValue( angle, a1, a2, a3, circlePoint1.m(), circlePoint2.m(), circlePoint3.m() );
}
stringPoints.insert( stringPoints.size(), QgsPoint( pointWkbType, x, y, z, m ) );
}
}
stringPoints.insert( stringPoints.size(), circlePoint3 );
// TODO: check if or implement QgsPointSequence directly taking an iterator to append
if ( reversed )
{
std::reverse( stringPoints.begin(), stringPoints.end() );
}
if ( ! points.empty() && stringPoints.front() == points.back() ) stringPoints.pop_front();
points.append( stringPoints );
}
int QgsGeometryUtils::segmentSide( const QgsPoint &pt1, const QgsPoint &pt3, const QgsPoint &pt2 )
{
double side = ( ( pt2.x() - pt1.x() ) * ( pt3.y() - pt1.y() ) - ( pt3.x() - pt1.x() ) * ( pt2.y() - pt1.y() ) );
if ( side == 0.0 )
{
return 0;
}
else
{
if ( side < 0 )
{
return -1;
}
if ( side > 0 )
{
return 1;
}
return 0;
}
}
double QgsGeometryUtils::interpolateArcValue( double angle, double a1, double a2, double a3, double zm1, double zm2, double zm3 )
{
/* Counter-clockwise sweep */
if ( a1 < a2 )
{
if ( angle <= a2 )
return zm1 + ( zm2 - zm1 ) * ( angle - a1 ) / ( a2 - a1 );
else
return zm2 + ( zm3 - zm2 ) * ( angle - a2 ) / ( a3 - a2 );
}
/* Clockwise sweep */
else
{
if ( angle >= a2 )
return zm1 + ( zm2 - zm1 ) * ( a1 - angle ) / ( a1 - a2 );
else
return zm2 + ( zm3 - zm2 ) * ( a2 - angle ) / ( a2 - a3 );
}
}
QgsPointSequence QgsGeometryUtils::pointsFromWKT( const QString &wktCoordinateList, bool is3D, bool isMeasure )
{
int dim = 2 + is3D + isMeasure;
QgsPointSequence points;
const QStringList coordList = wktCoordinateList.split( ',', QString::SkipEmptyParts );
//first scan through for extra unexpected dimensions
bool foundZ = false;
bool foundM = false;
QRegularExpression rx( QStringLiteral( "\\s" ) );
for ( const QString &pointCoordinates : coordList )
{
QStringList coordinates = pointCoordinates.split( rx, QString::SkipEmptyParts );
if ( coordinates.size() == 3 && !foundZ && !foundM && !is3D && !isMeasure )
{
// 3 dimensional coordinates, but not specifically marked as such. We allow this
// anyway and upgrade geometry to have Z dimension
foundZ = true;
}
else if ( coordinates.size() >= 4 && ( !( is3D || foundZ ) || !( isMeasure || foundM ) ) )
{
// 4 (or more) dimensional coordinates, but not specifically marked as such. We allow this
// anyway and upgrade geometry to have Z&M dimensions
foundZ = true;
foundM = true;
}
}
for ( const QString &pointCoordinates : coordList )
{
QStringList coordinates = pointCoordinates.split( rx, QString::SkipEmptyParts );
if ( coordinates.size() < dim )
continue;
int idx = 0;
double x = coordinates[idx++].toDouble();
double y = coordinates[idx++].toDouble();
double z = 0;
if ( ( is3D || foundZ ) && coordinates.length() > idx )
z = coordinates[idx++].toDouble();
double m = 0;
if ( ( isMeasure || foundM ) && coordinates.length() > idx )
m = coordinates[idx++].toDouble();
QgsWkbTypes::Type t = QgsWkbTypes::Point;
if ( is3D || foundZ )
{
if ( isMeasure || foundM )
t = QgsWkbTypes::PointZM;
else
t = QgsWkbTypes::PointZ;
}
else
{
if ( isMeasure || foundM )
t = QgsWkbTypes::PointM;
else
t = QgsWkbTypes::Point;
}
points.append( QgsPoint( t, x, y, z, m ) );
}
return points;
}
void QgsGeometryUtils::pointsToWKB( QgsWkbPtr &wkb, const QgsPointSequence &points, bool is3D, bool isMeasure )
{
wkb << static_cast<quint32>( points.size() );
for ( const QgsPoint &point : points )
{
wkb << point.x() << point.y();
if ( is3D )
{
wkb << point.z();
}
if ( isMeasure )
{
wkb << point.m();
}
}
}
QString QgsGeometryUtils::pointsToWKT( const QgsPointSequence &points, int precision, bool is3D, bool isMeasure )
{
QString wkt = QStringLiteral( "(" );
for ( const QgsPoint &p : points )
{
wkt += qgsDoubleToString( p.x(), precision );
wkt += ' ' + qgsDoubleToString( p.y(), precision );
if ( is3D )
wkt += ' ' + qgsDoubleToString( p.z(), precision );
if ( isMeasure )
wkt += ' ' + qgsDoubleToString( p.m(), precision );
wkt += QLatin1String( ", " );
}
if ( wkt.endsWith( QLatin1String( ", " ) ) )
wkt.chop( 2 ); // Remove last ", "
wkt += ')';
return wkt;
}
QDomElement QgsGeometryUtils::pointsToGML2( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString &ns )
{
QDomElement elemCoordinates = doc.createElementNS( ns, QStringLiteral( "coordinates" ) );
// coordinate separator
QString cs = QStringLiteral( "," );
// tupel separator
QString ts = QStringLiteral( " " );
elemCoordinates.setAttribute( QStringLiteral( "cs" ), cs );
elemCoordinates.setAttribute( QStringLiteral( "ts" ), ts );
QString strCoordinates;
for ( const QgsPoint &p : points )
strCoordinates += qgsDoubleToString( p.x(), precision ) + cs + qgsDoubleToString( p.y(), precision ) + ts;
if ( strCoordinates.endsWith( ts ) )
strCoordinates.chop( 1 ); // Remove trailing space
elemCoordinates.appendChild( doc.createTextNode( strCoordinates ) );
return elemCoordinates;
}
QDomElement QgsGeometryUtils::pointsToGML3( const QgsPointSequence &points, QDomDocument &doc, int precision, const QString &ns, bool is3D )
{
QDomElement elemPosList = doc.createElementNS( ns, QStringLiteral( "posList" ) );
elemPosList.setAttribute( QStringLiteral( "srsDimension" ), is3D ? 3 : 2 );
QString strCoordinates;
for ( const QgsPoint &p : points )
{
strCoordinates += qgsDoubleToString( p.x(), precision ) + ' ' + qgsDoubleToString( p.y(), precision ) + ' ';
if ( is3D )
strCoordinates += qgsDoubleToString( p.z(), precision ) + ' ';
}
if ( strCoordinates.endsWith( ' ' ) )
strCoordinates.chop( 1 ); // Remove trailing space
elemPosList.appendChild( doc.createTextNode( strCoordinates ) );
return elemPosList;
}
QString QgsGeometryUtils::pointsToJSON( const QgsPointSequence &points, int precision )
{
QString json = QStringLiteral( "[ " );
for ( const QgsPoint &p : points )
{
json += '[' + qgsDoubleToString( p.x(), precision ) + ", " + qgsDoubleToString( p.y(), precision ) + "], ";
}
if ( json.endsWith( QLatin1String( ", " ) ) )
{
json.chop( 2 ); // Remove last ", "
}
json += ']';
return json;
}
double QgsGeometryUtils::normalizedAngle( double angle )
{
double clippedAngle = angle;
if ( clippedAngle >= M_PI * 2 || clippedAngle <= -2 * M_PI )
{
clippedAngle = std::fmod( clippedAngle, 2 * M_PI );
}
if ( clippedAngle < 0.0 )
{
clippedAngle += 2 * M_PI;
}
return clippedAngle;
}
QPair<QgsWkbTypes::Type, QString> QgsGeometryUtils::wktReadBlock( const QString &wkt )
{
QgsWkbTypes::Type wkbType = QgsWkbTypes::parseType( wkt );
QRegularExpression cooRegEx( QStringLiteral( "^[^\\(]*\\((.*)\\)[^\\)]*$" ) );
cooRegEx.setPatternOptions( QRegularExpression::DotMatchesEverythingOption );
QRegularExpressionMatch match = cooRegEx.match( wkt );
QString contents = match.hasMatch() ? match.captured( 1 ) : QString();
return qMakePair( wkbType, contents );
}
QStringList QgsGeometryUtils::wktGetChildBlocks( const QString &wkt, const QString &defaultType )
{
int level = 0;
QString block;
QStringList blocks;
for ( int i = 0, n = wkt.length(); i < n; ++i )
{
if ( ( wkt[i].isSpace() || wkt[i] == '\n' || wkt[i] == '\t' ) && level == 0 )
continue;
if ( wkt[i] == ',' && level == 0 )
{
if ( !block.isEmpty() )
{
if ( block.startsWith( '(' ) && !defaultType.isEmpty() )
block.prepend( defaultType + ' ' );
blocks.append( block );
}
block.clear();
continue;
}
if ( wkt[i] == '(' )