-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsgeos.cpp
3719 lines (3230 loc) · 106 KB
/
qgsgeos.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
/***************************************************************************
qgsgeos.cpp
-------------------------------------------------------------------
Date : 22 Sept 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 "qgsgeos.h"
#include "qgsabstractgeometry.h"
#include "qgsgeometrycollection.h"
#include "qgsgeometryfactory.h"
#include "qgslinestring.h"
#include "qgsmulticurve.h"
#include "qgsmultilinestring.h"
#include "qgsmultipoint.h"
#include "qgsmultipolygon.h"
#include "qgslogger.h"
#include "qgspolygon.h"
#include "qgsgeometryeditutils.h"
#include <limits>
#include <cstdio>
#define DEFAULT_QUADRANT_SEGMENTS 8
#define CATCH_GEOS(r) \
catch (GEOSException &) \
{ \
return r; \
}
#define CATCH_GEOS_WITH_ERRMSG(r) \
catch (GEOSException &e) \
{ \
if ( errorMsg ) \
{ \
*errorMsg = e.what(); \
} \
return r; \
}
/// @cond PRIVATE
static void throwGEOSException( const char *fmt, ... )
{
va_list ap;
char buffer[1024];
va_start( ap, fmt );
vsnprintf( buffer, sizeof buffer, fmt, ap );
va_end( ap );
QString message = QString::fromUtf8( buffer );
#ifdef _MSC_VER
// stupid stupid MSVC, *SOMETIMES* raises it's own exception if we throw GEOSException, resulting in a crash!
// see https://github.com/qgis/QGIS/issues/22709
// if you want to test alternative fixes for this, run the testqgsexpression.cpp test suite - that will crash
// and burn on the "line_interpolate_point point" test if a GEOSException is thrown.
// TODO - find a real fix for the underlying issue
try
{
throw GEOSException( message );
}
catch ( ... )
{
// oops, msvc threw an exception when we tried to throw the exception!
// just throw nothing instead (except your mouse at your monitor)
}
#else
throw GEOSException( message );
#endif
}
static void printGEOSNotice( const char *fmt, ... )
{
#if defined(QGISDEBUG)
va_list ap;
char buffer[1024];
va_start( ap, fmt );
vsnprintf( buffer, sizeof buffer, fmt, ap );
va_end( ap );
#else
Q_UNUSED( fmt )
#endif
}
//
// QgsGeosContext
//
#if defined(USE_THREAD_LOCAL) && !defined(Q_OS_WIN)
thread_local QgsGeosContext QgsGeosContext::sGeosContext;
#else
QThreadStorage< QgsGeosContext * > QgsGeosContext::sGeosContext;
#endif
QgsGeosContext::QgsGeosContext()
{
#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=5 )
mContext = GEOS_init_r();
GEOSContext_setNoticeHandler_r( mContext, printGEOSNotice );
GEOSContext_setErrorHandler_r( mContext, throwGEOSException );
#else
mContext = initGEOS_r( printGEOSNotice, throwGEOSException );
#endif
}
QgsGeosContext::~QgsGeosContext()
{
#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=5 )
GEOS_finish_r( mContext );
#else
finishGEOS_r( mContext );
#endif
}
GEOSContextHandle_t QgsGeosContext::get()
{
#if defined(USE_THREAD_LOCAL) && !defined(Q_OS_WIN)
return sGeosContext.mContext;
#else
GEOSContextHandle_t gContext = nullptr;
if ( sGeosContext.hasLocalData() )
{
gContext = sGeosContext.localData()->mContext;
}
else
{
sGeosContext.setLocalData( new QgsGeosContext() );
gContext = sGeosContext.localData()->mContext;
}
return gContext;
#endif
}
//
// geos
//
void geos::GeosDeleter::operator()( GEOSGeometry *geom ) const
{
GEOSGeom_destroy_r( QgsGeosContext::get(), geom );
}
void geos::GeosDeleter::operator()( const GEOSPreparedGeometry *geom ) const
{
GEOSPreparedGeom_destroy_r( QgsGeosContext::get(), geom );
}
void geos::GeosDeleter::operator()( GEOSBufferParams *params ) const
{
GEOSBufferParams_destroy_r( QgsGeosContext::get(), params );
}
void geos::GeosDeleter::operator()( GEOSCoordSequence *sequence ) const
{
GEOSCoordSeq_destroy_r( QgsGeosContext::get(), sequence );
}
///@endcond
QgsGeos::QgsGeos( const QgsAbstractGeometry *geometry, double precision, bool allowInvalidSubGeom )
: QgsGeometryEngine( geometry )
, mGeos( nullptr )
, mPrecision( precision )
{
cacheGeos( allowInvalidSubGeom );
}
QgsGeometry QgsGeos::geometryFromGeos( GEOSGeometry *geos )
{
QgsGeometry g( QgsGeos::fromGeos( geos ) );
GEOSGeom_destroy_r( QgsGeosContext::get(), geos );
return g;
}
QgsGeometry QgsGeos::geometryFromGeos( const geos::unique_ptr &geos )
{
QgsGeometry g( QgsGeos::fromGeos( geos.get() ) );
return g;
}
std::unique_ptr<QgsAbstractGeometry> QgsGeos::makeValid( Qgis::MakeValidMethod method, bool keepCollapsed, QString *errorMsg ) const
{
if ( !mGeos )
{
return nullptr;
}
GEOSContextHandle_t context = QgsGeosContext::get();
#if GEOS_VERSION_MAJOR==3 && GEOS_VERSION_MINOR<10
if ( method != Qgis::MakeValidMethod::Linework )
throw QgsNotSupportedException( QObject::tr( "The structured method to make geometries valid requires a QGIS build based on GEOS 3.10 or later" ) );
if ( keepCollapsed )
throw QgsNotSupportedException( QObject::tr( "The keep collapsed option for making geometries valid requires a QGIS build based on GEOS 3.10 or later" ) );
geos::unique_ptr geos;
try
{
geos.reset( GEOSMakeValid_r( context, mGeos.get() ) );
}
CATCH_GEOS_WITH_ERRMSG( nullptr )
#else
GEOSMakeValidParams *params = GEOSMakeValidParams_create_r( context );
switch ( method )
{
case Qgis::MakeValidMethod::Linework:
GEOSMakeValidParams_setMethod_r( context, params, GEOS_MAKE_VALID_LINEWORK );
break;
case Qgis::MakeValidMethod::Structure:
GEOSMakeValidParams_setMethod_r( context, params, GEOS_MAKE_VALID_STRUCTURE );
break;
}
GEOSMakeValidParams_setKeepCollapsed_r( context,
params,
keepCollapsed ? 1 : 0 );
geos::unique_ptr geos;
try
{
geos.reset( GEOSMakeValidWithParams_r( context, mGeos.get(), params ) );
GEOSMakeValidParams_destroy_r( context, params );
}
catch ( GEOSException &e )
{
if ( errorMsg )
{
*errorMsg = e.what();
}
GEOSMakeValidParams_destroy_r( context, params );
return nullptr;
}
#endif
return fromGeos( geos.get() );
}
geos::unique_ptr QgsGeos::asGeos( const QgsGeometry &geometry, double precision )
{
if ( geometry.isNull() )
{
return nullptr;
}
return asGeos( geometry.constGet(), precision );
}
Qgis::GeometryOperationResult QgsGeos::addPart( QgsGeometry &geometry, GEOSGeometry *newPart )
{
if ( geometry.isNull() )
{
return Qgis::GeometryOperationResult::InvalidBaseGeometry;
}
if ( !newPart )
{
return Qgis::GeometryOperationResult::AddPartNotMultiGeometry;
}
std::unique_ptr< QgsAbstractGeometry > geom = fromGeos( newPart );
return QgsGeometryEditUtils::addPart( geometry.get(), std::move( geom ) );
}
void QgsGeos::geometryChanged()
{
mGeos.reset();
mGeosPrepared.reset();
cacheGeos( false );
}
void QgsGeos::prepareGeometry()
{
if ( mGeosPrepared )
{
// Already prepared
return;
}
if ( mGeos )
{
mGeosPrepared.reset( GEOSPrepare_r( QgsGeosContext::get(), mGeos.get() ) );
}
}
void QgsGeos::cacheGeos( bool allowInvalidSubGeom ) const
{
if ( mGeos )
{
// Already cached
return;
}
if ( !mGeometry )
{
return;
}
mGeos = asGeos( mGeometry, mPrecision, allowInvalidSubGeom );
}
QgsAbstractGeometry *QgsGeos::intersection( const QgsAbstractGeometry *geom, QString *errorMsg, const QgsGeometryParameters ¶meters ) const
{
return overlay( geom, OverlayIntersection, errorMsg, parameters ).release();
}
QgsAbstractGeometry *QgsGeos::difference( const QgsAbstractGeometry *geom, QString *errorMsg, const QgsGeometryParameters ¶meters ) const
{
return overlay( geom, OverlayDifference, errorMsg, parameters ).release();
}
std::unique_ptr<QgsAbstractGeometry> QgsGeos::clip( const QgsRectangle &rect, QString *errorMsg ) const
{
if ( !mGeos || rect.isNull() || rect.isEmpty() )
{
return nullptr;
}
try
{
geos::unique_ptr opGeom( GEOSClipByRect_r( QgsGeosContext::get(), mGeos.get(), rect.xMinimum(), rect.yMinimum(), rect.xMaximum(), rect.yMaximum() ) );
return fromGeos( opGeom.get() );
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
}
return nullptr;
}
}
void QgsGeos::subdivideRecursive( const GEOSGeometry *currentPart, int maxNodes, int depth, QgsGeometryCollection *parts, const QgsRectangle &clipRect, double gridSize ) const
{
GEOSContextHandle_t context = QgsGeosContext::get();
int partType = GEOSGeomTypeId_r( context, currentPart );
if ( qgsDoubleNear( clipRect.width(), 0.0 ) && qgsDoubleNear( clipRect.height(), 0.0 ) )
{
if ( partType == GEOS_POINT )
{
parts->addGeometry( fromGeos( currentPart ).release() );
return;
}
else
{
return;
}
}
if ( partType == GEOS_MULTILINESTRING || partType == GEOS_MULTIPOLYGON || partType == GEOS_GEOMETRYCOLLECTION )
{
int partCount = GEOSGetNumGeometries_r( context, currentPart );
for ( int i = 0; i < partCount; ++i )
{
subdivideRecursive( GEOSGetGeometryN_r( context, currentPart, i ), maxNodes, depth, parts, clipRect, gridSize );
}
return;
}
if ( depth > 50 )
{
parts->addGeometry( fromGeos( currentPart ).release() );
return;
}
int vertexCount = GEOSGetNumCoordinates_r( context, currentPart );
if ( vertexCount == 0 )
{
return;
}
else if ( vertexCount < maxNodes )
{
parts->addGeometry( fromGeos( currentPart ).release() );
return;
}
// chop clipping rect in half by longest side
double width = clipRect.width();
double height = clipRect.height();
QgsRectangle halfClipRect1 = clipRect;
QgsRectangle halfClipRect2 = clipRect;
if ( width > height )
{
halfClipRect1.setXMaximum( clipRect.xMinimum() + width / 2.0 );
halfClipRect2.setXMinimum( halfClipRect1.xMaximum() );
}
else
{
halfClipRect1.setYMaximum( clipRect.yMinimum() + height / 2.0 );
halfClipRect2.setYMinimum( halfClipRect1.yMaximum() );
}
if ( height <= 0 )
{
halfClipRect1.setYMinimum( halfClipRect1.yMinimum() - std::numeric_limits<double>::epsilon() );
halfClipRect2.setYMinimum( halfClipRect2.yMinimum() - std::numeric_limits<double>::epsilon() );
halfClipRect1.setYMaximum( halfClipRect1.yMaximum() + std::numeric_limits<double>::epsilon() );
halfClipRect2.setYMaximum( halfClipRect2.yMaximum() + std::numeric_limits<double>::epsilon() );
}
if ( width <= 0 )
{
halfClipRect1.setXMinimum( halfClipRect1.xMinimum() - std::numeric_limits<double>::epsilon() );
halfClipRect2.setXMinimum( halfClipRect2.xMinimum() - std::numeric_limits<double>::epsilon() );
halfClipRect1.setXMaximum( halfClipRect1.xMaximum() + std::numeric_limits<double>::epsilon() );
halfClipRect2.setXMaximum( halfClipRect2.xMaximum() + std::numeric_limits<double>::epsilon() );
}
geos::unique_ptr clipPart1( GEOSClipByRect_r( context, currentPart, halfClipRect1.xMinimum(), halfClipRect1.yMinimum(), halfClipRect1.xMaximum(), halfClipRect1.yMaximum() ) );
geos::unique_ptr clipPart2( GEOSClipByRect_r( context, currentPart, halfClipRect2.xMinimum(), halfClipRect2.yMinimum(), halfClipRect2.xMaximum(), halfClipRect2.yMaximum() ) );
++depth;
if ( clipPart1 )
{
if ( gridSize > 0 )
{
clipPart1.reset( GEOSIntersectionPrec_r( context, mGeos.get(), clipPart1.get(), gridSize ) );
}
subdivideRecursive( clipPart1.get(), maxNodes, depth, parts, halfClipRect1, gridSize );
}
if ( clipPart2 )
{
if ( gridSize > 0 )
{
clipPart2.reset( GEOSIntersectionPrec_r( context, mGeos.get(), clipPart2.get(), gridSize ) );
}
subdivideRecursive( clipPart2.get(), maxNodes, depth, parts, halfClipRect2, gridSize );
}
}
std::unique_ptr<QgsAbstractGeometry> QgsGeos::subdivide( int maxNodes, QString *errorMsg, const QgsGeometryParameters ¶meters ) const
{
if ( !mGeos )
{
return nullptr;
}
// minimum allowed max is 8
maxNodes = std::max( maxNodes, 8 );
std::unique_ptr< QgsGeometryCollection > parts = QgsGeometryFactory::createCollectionOfType( mGeometry->wkbType() );
try
{
subdivideRecursive( mGeos.get(), maxNodes, 0, parts.get(), mGeometry->boundingBox(), parameters.gridSize() );
}
CATCH_GEOS_WITH_ERRMSG( nullptr )
return std::move( parts );
}
QgsAbstractGeometry *QgsGeos::combine( const QgsAbstractGeometry *geom, QString *errorMsg, const QgsGeometryParameters ¶meters ) const
{
return overlay( geom, OverlayUnion, errorMsg, parameters ).release();
}
QgsAbstractGeometry *QgsGeos::combine( const QVector<QgsAbstractGeometry *> &geomList, QString *errorMsg, const QgsGeometryParameters ¶meters ) const
{
std::vector<geos::unique_ptr> geosGeometries;
geosGeometries.reserve( geomList.size() );
for ( const QgsAbstractGeometry *g : geomList )
{
if ( !g )
continue;
geosGeometries.emplace_back( asGeos( g, mPrecision ) );
}
GEOSContextHandle_t context = QgsGeosContext::get();
geos::unique_ptr geomUnion;
try
{
geos::unique_ptr geomCollection = createGeosCollection( GEOS_GEOMETRYCOLLECTION, geosGeometries );
if ( parameters.gridSize() > 0 )
{
geomUnion.reset( GEOSUnaryUnionPrec_r( context, geomCollection.get(), parameters.gridSize() ) );
}
else
{
geomUnion.reset( GEOSUnaryUnion_r( context, geomCollection.get() ) );
}
}
CATCH_GEOS_WITH_ERRMSG( nullptr )
std::unique_ptr< QgsAbstractGeometry > result = fromGeos( geomUnion.get() );
return result.release();
}
QgsAbstractGeometry *QgsGeos::combine( const QVector<QgsGeometry> &geomList, QString *errorMsg, const QgsGeometryParameters ¶meters ) const
{
std::vector<geos::unique_ptr> geosGeometries;
geosGeometries.reserve( geomList.size() );
for ( const QgsGeometry &g : geomList )
{
if ( g.isNull() )
continue;
geosGeometries.emplace_back( asGeos( g.constGet(), mPrecision ) );
}
GEOSContextHandle_t context = QgsGeosContext::get();
geos::unique_ptr geomUnion;
try
{
geos::unique_ptr geomCollection = createGeosCollection( GEOS_GEOMETRYCOLLECTION, geosGeometries );
if ( parameters.gridSize() > 0 )
{
geomUnion.reset( GEOSUnaryUnionPrec_r( context, geomCollection.get(), parameters.gridSize() ) );
}
else
{
geomUnion.reset( GEOSUnaryUnion_r( context, geomCollection.get() ) );
}
}
CATCH_GEOS_WITH_ERRMSG( nullptr )
std::unique_ptr< QgsAbstractGeometry > result = fromGeos( geomUnion.get() );
return result.release();
}
QgsAbstractGeometry *QgsGeos::symDifference( const QgsAbstractGeometry *geom, QString *errorMsg, const QgsGeometryParameters ¶meters ) const
{
return overlay( geom, OverlaySymDifference, errorMsg, parameters ).release();
}
double QgsGeos::distance( const QgsAbstractGeometry *geom, QString *errorMsg ) const
{
double distance = -1.0;
if ( !mGeos )
{
return distance;
}
geos::unique_ptr otherGeosGeom( asGeos( geom, mPrecision ) );
if ( !otherGeosGeom )
{
return distance;
}
GEOSContextHandle_t context = QgsGeosContext::get();
try
{
if ( mGeosPrepared )
{
GEOSPreparedDistance_r( context, mGeosPrepared.get(), otherGeosGeom.get(), &distance );
}
else
{
GEOSDistance_r( context, mGeos.get(), otherGeosGeom.get(), &distance );
}
}
CATCH_GEOS_WITH_ERRMSG( -1.0 )
return distance;
}
double QgsGeos::distance( double x, double y, QString *errorMsg ) const
{
double distance = -1.0;
if ( !mGeos )
{
return distance;
}
geos::unique_ptr point = createGeosPointXY( x, y, false, 0, false, 0, 2, 0 );
if ( !point )
return distance;
GEOSContextHandle_t context = QgsGeosContext::get();
try
{
if ( mGeosPrepared )
{
GEOSPreparedDistance_r( context, mGeosPrepared.get(), point.get(), &distance );
}
else
{
GEOSDistance_r( context, mGeos.get(), point.get(), &distance );
}
}
CATCH_GEOS_WITH_ERRMSG( -1.0 )
return distance;
}
bool QgsGeos::distanceWithin( const QgsAbstractGeometry *geom, double maxdist, QString *errorMsg ) const
{
if ( !mGeos )
{
return false;
}
geos::unique_ptr otherGeosGeom( asGeos( geom, mPrecision ) );
if ( !otherGeosGeom )
{
return false;
}
// TODO: optimize implementation of this function to early-exit if
// any part of othergeosGeom is found to be within the given
// distance
double distance;
GEOSContextHandle_t context = QgsGeosContext::get();
try
{
if ( mGeosPrepared )
{
#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=10 )
return GEOSPreparedDistanceWithin_r( context, mGeosPrepared.get(), otherGeosGeom.get(), maxdist );
#else
GEOSPreparedDistance_r( context, mGeosPrepared.get(), otherGeosGeom.get(), &distance );
#endif
}
else
{
#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=10 )
return GEOSDistanceWithin_r( context, mGeos.get(), otherGeosGeom.get(), maxdist );
#else
GEOSDistance_r( context, mGeos.get(), otherGeosGeom.get(), &distance );
#endif
}
}
CATCH_GEOS_WITH_ERRMSG( false )
return distance <= maxdist;
}
bool QgsGeos::contains( double x, double y, QString *errorMsg ) const
{
bool result = false;
GEOSContextHandle_t context = QgsGeosContext::get();
try
{
#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=12 )
// defer point creation until after prepared geometry check, we may not need it
#else
geos::unique_ptr point = createGeosPointXY( x, y, false, 0, false, 0, 2, 0 );
if ( !point )
return false;
#endif
if ( mGeosPrepared ) //use faster version with prepared geometry
{
#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=12 )
return GEOSPreparedContainsXY_r( context, mGeosPrepared.get(), x, y ) == 1;
#else
return GEOSPreparedContains_r( context, mGeosPrepared.get(), point.get() ) == 1;
#endif
}
#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=12 )
geos::unique_ptr point = createGeosPointXY( x, y, false, 0, false, 0, 2, 0 );
if ( !point )
return false;
#endif
result = ( GEOSContains_r( context, mGeos.get(), point.get() ) == 1 );
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
}
return false;
}
return result;
}
double QgsGeos::hausdorffDistance( const QgsAbstractGeometry *geom, QString *errorMsg ) const
{
double distance = -1.0;
if ( !mGeos )
{
return distance;
}
geos::unique_ptr otherGeosGeom( asGeos( geom, mPrecision ) );
if ( !otherGeosGeom )
{
return distance;
}
try
{
GEOSHausdorffDistance_r( QgsGeosContext::get(), mGeos.get(), otherGeosGeom.get(), &distance );
}
CATCH_GEOS_WITH_ERRMSG( -1.0 )
return distance;
}
double QgsGeos::hausdorffDistanceDensify( const QgsAbstractGeometry *geom, double densifyFraction, QString *errorMsg ) const
{
double distance = -1.0;
if ( !mGeos )
{
return distance;
}
geos::unique_ptr otherGeosGeom( asGeos( geom, mPrecision ) );
if ( !otherGeosGeom )
{
return distance;
}
try
{
GEOSHausdorffDistanceDensify_r( QgsGeosContext::get(), mGeos.get(), otherGeosGeom.get(), densifyFraction, &distance );
}
CATCH_GEOS_WITH_ERRMSG( -1.0 )
return distance;
}
double QgsGeos::frechetDistance( const QgsAbstractGeometry *geom, QString *errorMsg ) const
{
double distance = -1.0;
if ( !mGeos )
{
return distance;
}
geos::unique_ptr otherGeosGeom( asGeos( geom, mPrecision ) );
if ( !otherGeosGeom )
{
return distance;
}
try
{
GEOSFrechetDistance_r( QgsGeosContext::get(), mGeos.get(), otherGeosGeom.get(), &distance );
}
CATCH_GEOS_WITH_ERRMSG( -1.0 )
return distance;
}
double QgsGeos::frechetDistanceDensify( const QgsAbstractGeometry *geom, double densifyFraction, QString *errorMsg ) const
{
double distance = -1.0;
if ( !mGeos )
{
return distance;
}
geos::unique_ptr otherGeosGeom( asGeos( geom, mPrecision ) );
if ( !otherGeosGeom )
{
return distance;
}
try
{
GEOSFrechetDistanceDensify_r( QgsGeosContext::get(), mGeos.get(), otherGeosGeom.get(), densifyFraction, &distance );
}
CATCH_GEOS_WITH_ERRMSG( -1.0 )
return distance;
}
bool QgsGeos::intersects( const QgsAbstractGeometry *geom, QString *errorMsg ) const
{
if ( !mGeos || !geom )
{
return false;
}
#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=12 )
// special optimised case for point intersects
if ( const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( geom->simplifiedTypeRef() ) )
{
if ( mGeosPrepared )
{
try
{
return GEOSPreparedIntersectsXY_r( QgsGeosContext::get(), mGeosPrepared.get(), point->x(), point->y() ) == 1;
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
}
return false;
}
}
}
#endif
return relation( geom, RelationIntersects, errorMsg );
}
bool QgsGeos::touches( const QgsAbstractGeometry *geom, QString *errorMsg ) const
{
return relation( geom, RelationTouches, errorMsg );
}
bool QgsGeos::crosses( const QgsAbstractGeometry *geom, QString *errorMsg ) const
{
return relation( geom, RelationCrosses, errorMsg );
}
bool QgsGeos::within( const QgsAbstractGeometry *geom, QString *errorMsg ) const
{
return relation( geom, RelationWithin, errorMsg );
}
bool QgsGeos::overlaps( const QgsAbstractGeometry *geom, QString *errorMsg ) const
{
return relation( geom, RelationOverlaps, errorMsg );
}
bool QgsGeos::contains( const QgsAbstractGeometry *geom, QString *errorMsg ) const
{
if ( !mGeos || !geom )
{
return false;
}
#if GEOS_VERSION_MAJOR>3 || ( GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR>=12 )
// special optimised case for point containment
if ( const QgsPoint *point = qgsgeometry_cast< const QgsPoint * >( geom->simplifiedTypeRef() ) )
{
if ( mGeosPrepared )
{
try
{
return GEOSPreparedContainsXY_r( QgsGeosContext::get(), mGeosPrepared.get(), point->x(), point->y() ) == 1;
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
}
return false;
}
}
}
#endif
return relation( geom, RelationContains, errorMsg );
}
bool QgsGeos::disjoint( const QgsAbstractGeometry *geom, QString *errorMsg ) const
{
return relation( geom, RelationDisjoint, errorMsg );
}
QString QgsGeos::relate( const QgsAbstractGeometry *geom, QString *errorMsg ) const
{
if ( !mGeos )
{
return QString();
}
geos::unique_ptr geosGeom( asGeos( geom, mPrecision ) );
if ( !geosGeom )
{
return QString();
}
QString result;
GEOSContextHandle_t context = QgsGeosContext::get();
try
{
char *r = GEOSRelate_r( context, mGeos.get(), geosGeom.get() );
if ( r )
{
result = QString( r );
GEOSFree_r( context, r );
}
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
}
}
return result;
}
bool QgsGeos::relatePattern( const QgsAbstractGeometry *geom, const QString &pattern, QString *errorMsg ) const
{
if ( !mGeos || !geom )
{
return false;
}
geos::unique_ptr geosGeom( asGeos( geom, mPrecision ) );
if ( !geosGeom )
{
return false;
}
bool result = false;
GEOSContextHandle_t context = QgsGeosContext::get();
try
{
result = ( GEOSRelatePattern_r( context, mGeos.get(), geosGeom.get(), pattern.toLocal8Bit().constData() ) == 1 );
}
catch ( GEOSException &e )
{
logError( QStringLiteral( "GEOS" ), e.what() );
if ( errorMsg )
{
*errorMsg = e.what();
}
}
return result;
}
double QgsGeos::area( QString *errorMsg ) const
{
double area = -1.0;
if ( !mGeos )
{
return area;
}
try
{
if ( GEOSArea_r( QgsGeosContext::get(), mGeos.get(), &area ) != 1 )
return -1.0;
}
CATCH_GEOS_WITH_ERRMSG( -1.0 )
return area;
}
double QgsGeos::length( QString *errorMsg ) const
{
double length = -1.0;
if ( !mGeos )
{
return length;
}
try
{
if ( GEOSLength_r( QgsGeosContext::get(), mGeos.get(), &length ) != 1 )
return -1.0;
}
CATCH_GEOS_WITH_ERRMSG( -1.0 )
return length;
}
QgsGeometryEngine::EngineOperationResult QgsGeos::splitGeometry( const QgsLineString &splitLine,
QVector<QgsGeometry> &newGeometries,
bool topological,
QgsPointSequence &topologyTestPoints,
QString *errorMsg, bool skipIntersectionCheck ) const
{
EngineOperationResult returnCode = Success;
if ( !mGeos || !mGeometry )
{
return InvalidBaseGeometry;
}
//return if this type is point/multipoint
if ( mGeometry->dimension() == 0 )
{
return SplitCannotSplitPoint; //cannot split points
}
GEOSContextHandle_t context = QgsGeosContext::get();
if ( !GEOSisValid_r( context, mGeos.get() ) )
return InvalidBaseGeometry;
//make sure splitLine is valid
if ( ( mGeometry->dimension() == 1 && splitLine.numPoints() < 1 ) ||
( mGeometry->dimension() == 2 && splitLine.numPoints() < 2 ) )
return InvalidInput;
newGeometries.clear();
geos::unique_ptr splitLineGeos;