QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
qgscompoundcurve.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgscompoundcurve.cpp
3 ----------------------
4 begin : September 2014
5 copyright : (C) 2014 by Marco Hugentobler
6 email : marco at sourcepole dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgscompoundcurve.h"
19
20#include <memory>
21#include <nlohmann/json.hpp>
22
23#include "qgsapplication.h"
24#include "qgscircularstring.h"
25#include "qgsfeedback.h"
26#include "qgsgeometryutils.h"
27#include "qgslinestring.h"
28#include "qgsmessagelog.h"
29#include "qgswkbptr.h"
30
31#include <QJsonObject>
32#include <QPainter>
33#include <QPainterPath>
34#include <QString>
35
36using namespace Qt::StringLiterals;
37
42
47
49{
50 auto result = std::make_unique< QgsCompoundCurve >();
51 result->mWkbType = mWkbType;
52 return result.release();
53}
54
56{
58 if ( !otherCurve )
59 return -1;
60
61 int i = 0;
62 int j = 0;
63 while ( i < mCurves.size() && j < otherCurve->mCurves.size() )
64 {
65 const QgsAbstractGeometry *aGeom = mCurves[i];
66 const QgsAbstractGeometry *bGeom = otherCurve->mCurves[j];
67 const int comparison = aGeom->compareTo( bGeom );
68 if ( comparison != 0 )
69 {
70 return comparison;
71 }
72 i++;
73 j++;
74 }
75 if ( i < mCurves.size() )
76 {
77 return 1;
78 }
79 if ( j < otherCurve->mCurves.size() )
80 {
81 return -1;
82 }
83 return 0;
84}
85
87{
88 return u"CompoundCurve"_s;
89}
90
92{
93 return 1;
94}
95
97{
98 mWkbType = curve.wkbType();
99 mCurves.reserve( curve.mCurves.size() );
100 for ( const QgsCurve *c : curve.mCurves )
101 {
102 mCurves.append( c->clone() );
103 }
104}
105
106// cppcheck-suppress operatorEqVarError
108{
109 if ( &curve != this )
110 {
111 QgsCurve::operator=( curve );
112 for ( const QgsCurve *c : curve.mCurves )
113 {
114 mCurves.append( c->clone() );
115 }
116 }
117 return *this;
118}
119
121{
122 return new QgsCompoundCurve( *this );
123}
124
126{
128 qDeleteAll( mCurves );
129 mCurves.clear();
130 clearCache();
131}
132
134{
135 if ( mCurves.empty() )
136 {
137 return QgsBox3D();
138 }
139
140 QgsBox3D bbox = mCurves.at( 0 )->boundingBox3D();
141 for ( int i = 1; i < mCurves.size(); ++i )
142 {
143 QgsBox3D curveBox = mCurves.at( i )->boundingBox3D();
144 bbox.combineWith( curveBox );
145 }
146 return bbox;
147}
148
150{
151 const int size = numPoints();
152 if ( index < 1 || index >= size - 1 )
153 return;
154
155 auto [p1, p2 ] = splitCurveAtVertex( index );
156
157 mCurves.clear();
159 {
160 // take the curves from the second part and make them our first lot of curves
161 mCurves = std::move( curve2->mCurves );
162 }
164 {
165 // take the curves from the first part and append them to our curves
166 mCurves.append( curve1->mCurves );
167 curve1->mCurves.clear();
168 }
169}
170
172{
173 clear();
174 if ( !wkbPtr )
175 {
176 return false;
177 }
178
179 Qgis::WkbType type = wkbPtr.readHeader();
181 {
182 return false;
183 }
184 mWkbType = type;
185
186 int nCurves;
187 wkbPtr >> nCurves;
188 QgsCurve *currentCurve = nullptr;
189 for ( int i = 0; i < nCurves; ++i )
190 {
191 Qgis::WkbType curveType = wkbPtr.readHeader();
192 wkbPtr -= 1 + sizeof( int );
194 {
195 currentCurve = new QgsLineString();
196 }
197 else if ( QgsWkbTypes::flatType( curveType ) == Qgis::WkbType::CircularString )
198 {
199 currentCurve = new QgsCircularString();
200 }
201 else
202 {
203 return false;
204 }
205 currentCurve->fromWkb( wkbPtr ); // also updates wkbPtr
206 mCurves.append( currentCurve );
207 }
208 return true;
209}
210
211bool QgsCompoundCurve::fromWkt( const QString &wkt )
212{
213 clear();
214
215 QPair<Qgis::WkbType, QString> parts = QgsGeometryUtils::wktReadBlock( wkt );
216
218 return false;
219 mWkbType = parts.first;
220
221 QString secondWithoutParentheses = parts.second;
222 secondWithoutParentheses = secondWithoutParentheses.remove( '(' ).remove( ')' ).simplified().remove( ' ' );
223 if ( ( parts.second.compare( "EMPTY"_L1, Qt::CaseInsensitive ) == 0 ) ||
224 secondWithoutParentheses.isEmpty() )
225 return true;
226
227 QString defaultChildWkbType = u"LineString%1%2"_s.arg( is3D() ? u"Z"_s : QString(), isMeasure() ? u"M"_s : QString() );
228
229 const QStringList blocks = QgsGeometryUtils::wktGetChildBlocks( parts.second, defaultChildWkbType );
230 for ( const QString &childWkt : blocks )
231 {
232 QPair<Qgis::WkbType, QString> childParts = QgsGeometryUtils::wktReadBlock( childWkt );
233
234 if ( QgsWkbTypes::flatType( childParts.first ) == Qgis::WkbType::LineString )
235 mCurves.append( new QgsLineString() );
236 else if ( QgsWkbTypes::flatType( childParts.first ) == Qgis::WkbType::CircularString )
237 mCurves.append( new QgsCircularString() );
238 else
239 {
240 clear();
241 return false;
242 }
243 if ( !mCurves.back()->fromWkt( childWkt ) )
244 {
245 clear();
246 return false;
247 }
248 }
249
250 //scan through curves and check if dimensionality of curves is different to compound curve.
251 //if so, update the type dimensionality of the compound curve to match
252 bool hasZ = false;
253 bool hasM = false;
254 for ( const QgsCurve *curve : std::as_const( mCurves ) )
255 {
256 hasZ = hasZ || curve->is3D();
257 hasM = hasM || curve->isMeasure();
258 if ( hasZ && hasM )
259 break;
260 }
261 if ( hasZ )
262 addZValue( 0 );
263 if ( hasM )
264 addMValue( 0 );
265
266 return true;
267}
268
270{
271 int binarySize = sizeof( char ) + sizeof( quint32 ) + sizeof( quint32 );
272 for ( const QgsCurve *curve : mCurves )
273 {
274 binarySize += curve->wkbSize( flags );
275 }
276 return binarySize;
277}
278
279QByteArray QgsCompoundCurve::asWkb( WkbFlags flags ) const
280{
281 QByteArray wkbArray;
282 wkbArray.resize( QgsCompoundCurve::wkbSize( flags ) );
283 QgsWkbPtr wkb( wkbArray );
284 wkb << static_cast<char>( QgsApplication::endian() );
285 wkb << static_cast<quint32>( wkbType() );
286 wkb << static_cast<quint32>( mCurves.size() );
287 for ( const QgsCurve *curve : mCurves )
288 {
289 wkb << curve->asWkb( flags );
290 }
291 return wkbArray;
292}
293
294QString QgsCompoundCurve::asWkt( int precision ) const
295{
296 QString wkt = wktTypeStr();
297 if ( isEmpty() )
298 wkt += " EMPTY"_L1;
299 else
300 {
301 wkt += " ("_L1;
302 for ( const QgsCurve *curve : mCurves )
303 {
304 QString childWkt = curve->asWkt( precision );
306 {
307 // Type names of linear geometries are omitted
308 childWkt = childWkt.mid( childWkt.indexOf( '(' ) );
309 }
310 wkt += childWkt + ',';
311 }
312 if ( wkt.endsWith( ',' ) )
313 {
314 wkt.chop( 1 );
315 }
316 wkt += ')';
317 }
318 return wkt;
319}
320
321QDomElement QgsCompoundCurve::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
322{
323 // GML2 does not support curves
324 std::unique_ptr< QgsLineString > line( curveToLine() );
325 QDomElement gml = line->asGml2( doc, precision, ns, axisOrder );
326 return gml;
327}
328
329QDomElement QgsCompoundCurve::asGml3( QDomDocument &doc, int precision, const QString &ns, const QgsAbstractGeometry::AxisOrder axisOrder ) const
330{
331 QDomElement compoundCurveElem = doc.createElementNS( ns, u"CompositeCurve"_s );
332
333 if ( isEmpty() )
334 return compoundCurveElem;
335
336 for ( const QgsCurve *curve : mCurves )
337 {
338 QDomElement curveMemberElem = doc.createElementNS( ns, u"curveMember"_s );
339 QDomElement curveElem = curve->asGml3( doc, precision, ns, axisOrder );
340 curveMemberElem.appendChild( curveElem );
341 compoundCurveElem.appendChild( curveMemberElem );
342 }
343
344 return compoundCurveElem;
345}
346
347json QgsCompoundCurve::asJsonObject( int precision ) const
348{
349 // GeoJSON does not support curves
350 std::unique_ptr< QgsLineString > line( curveToLine() );
351 return line->asJsonObject( precision );
352}
353
355{
356 double length = 0;
357 for ( const QgsCurve *curve : mCurves )
358 {
359 length += curve->length();
360 }
361 return length;
362}
363
365{
366 if ( mCurves.empty() )
367 {
368 return QgsPoint();
369 }
370 return mCurves.at( 0 )->startPoint();
371}
372
374{
375 if ( mCurves.empty() )
376 {
377 return QgsPoint();
378 }
379 return mCurves.at( mCurves.size() - 1 )->endPoint();
380}
381
383{
384 pts.clear();
385 if ( mCurves.empty() )
386 {
387 return;
388 }
389
390 mCurves[0]->points( pts );
391 for ( int i = 1; i < mCurves.size(); ++i )
392 {
393 QgsPointSequence pList;
394 mCurves[i]->points( pList );
395 pList.removeFirst(); //first vertex already added in previous line
396 pts.append( pList );
397 }
398}
399
401{
402 int nPoints = 0;
403 int nCurves = mCurves.size();
404 if ( nCurves < 1 )
405 {
406 return 0;
407 }
408
409 for ( int i = 0; i < nCurves; ++i )
410 {
411 nPoints += mCurves.at( i )->numPoints() - 1; //last vertex is equal to first of next section
412 }
413 nPoints += 1; //last vertex was removed above
414 return nPoints;
415}
416
418{
419 if ( mCurves.isEmpty() )
420 return true;
421
422 for ( QgsCurve *curve : mCurves )
423 {
424 if ( !curve->isEmpty() )
425 return false;
426 }
427 return true;
428}
429
431{
432 if ( mCurves.isEmpty() )
433 return true;
434
435 for ( int i = 0; i < mCurves.size() ; ++i )
436 {
437 if ( !mCurves[i]->isValid( error, flags ) )
438 {
439 error = QObject::tr( "Curve[%1]: %2" ).arg( i + 1 ).arg( error );
440 return false;
441 }
442 }
443 return QgsCurve::isValid( error, flags );
444}
445
446int QgsCompoundCurve::indexOf( const QgsPoint &point ) const
447{
448 int curveStart = 0;
449 for ( const QgsCurve *curve : mCurves )
450 {
451 const int curveIndex = curve->indexOf( point );
452 if ( curveIndex >= 0 )
453 return curveStart + curveIndex;
454 // subtract 1 here, because the next curve will start with the same
455 // vertex as this curve ended at
456 curveStart += curve->numPoints() - 1;
457 }
458 return -1;
459}
460
462{
463 QgsLineString *line = new QgsLineString();
464 std::unique_ptr< QgsLineString > currentLine;
465 for ( const QgsCurve *curve : mCurves )
466 {
467 currentLine.reset( curve->curveToLine( tolerance, toleranceType ) );
468 line->append( currentLine.get() );
469 }
470 return line;
471}
472
473QgsCompoundCurve *QgsCompoundCurve::snappedToGrid( double hSpacing, double vSpacing, double dSpacing, double mSpacing, bool removeRedundantPoints ) const
474{
475 std::unique_ptr<QgsCompoundCurve> result( createEmptyWithSameType() );
476
477 for ( QgsCurve *curve : mCurves )
478 {
479 std::unique_ptr<QgsCurve> gridified( static_cast< QgsCurve * >( curve->snappedToGrid( hSpacing, vSpacing, dSpacing, mSpacing, removeRedundantPoints ) ) );
480 if ( gridified )
481 {
482 result->mCurves.append( gridified.release() );
483 }
484 }
485
486 if ( result->mCurves.empty() )
487 return nullptr;
488 else
489 return result.release();
490}
491
493{
494 std::unique_ptr< QgsLineString > line( curveToLine() );
495 return line->simplifyByDistance( tolerance );
496}
497
498bool QgsCompoundCurve::removeDuplicateNodes( double epsilon, bool useZValues )
499{
500 bool result = false;
501 const QVector< QgsCurve * > curves = mCurves;
502 int i = 0;
503 QgsPoint lastEnd;
504 for ( QgsCurve *curve : curves )
505 {
506 result = curve->removeDuplicateNodes( epsilon, useZValues ) || result;
507 if ( curve->numPoints() == 0 || qgsDoubleNear( curve->length(), 0.0, epsilon ) )
508 {
509 // empty curve, remove it
510 delete mCurves.takeAt( i );
511 result = true;
512 }
513 else
514 {
515 // ensure this line starts exactly where previous line ended
516 if ( i > 0 )
517 {
518 curve->moveVertex( QgsVertexId( -1, -1, 0 ), lastEnd );
519 }
520 lastEnd = curve->vertexAt( QgsVertexId( -1, -1, curve->numPoints() - 1 ) );
521 }
522 i++;
523 }
524 return result;
525}
526
528{
529 if ( mCurves.empty() )
530 return false;
531
532 // if we already have the bounding box calculated, then this check is trivial!
533 if ( !mBoundingBox.isNull() )
534 {
535 return mBoundingBox.intersects( box3d );
536 }
537
538 // otherwise loop through each member curve and test the bounding box intersection.
539 // This gives us a chance to use optimisations which may be present on the individual
540 // curve subclasses, and at worst it will cause a calculation of the bounding box
541 // of each individual member curve which we would have to do anyway... (and these
542 // bounding boxes are cached, so would be reused without additional expense)
543 for ( const QgsCurve *curve : mCurves )
544 {
545 if ( curve->boundingBoxIntersects( box3d ) )
546 return true;
547 }
548
549 // even if we don't intersect the bounding box of any member curves, we may still intersect the
550 // bounding box of the overall compound curve.
551 // so here we fall back to the non-optimised base class check which has to first calculate
552 // the overall bounding box of the compound curve..
554}
555
557{
558 if ( mCurves.size() == 1 )
559 return mCurves.at( 0 );
560 else
561 return this;
562}
563
565{
566 if ( i < 0 || i >= mCurves.size() )
567 {
568 return nullptr;
569 }
570 return mCurves.at( i );
571}
572
573void QgsCompoundCurve::addCurve( QgsCurve *c, const bool extendPrevious )
574{
575 if ( !c )
576 return;
577
578 if ( mCurves.empty() )
579 {
581 }
582
583 if ( QgsWkbTypes::hasZ( mWkbType ) && !QgsWkbTypes::hasZ( c->wkbType() ) )
584 {
585 c->addZValue();
586 }
587 else if ( !QgsWkbTypes::hasZ( mWkbType ) && QgsWkbTypes::hasZ( c->wkbType() ) )
588 {
589 c->dropZValue();
590 }
591 if ( QgsWkbTypes::hasM( mWkbType ) && !QgsWkbTypes::hasM( c->wkbType() ) )
592 {
593 c->addMValue();
594 }
595 else if ( !QgsWkbTypes::hasM( mWkbType ) && QgsWkbTypes::hasM( c->wkbType() ) )
596 {
597 c->dropMValue();
598 }
599
600 QgsLineString *previousLineString = !mCurves.empty() ? qgsgeometry_cast< QgsLineString * >( mCurves.constLast() ) : nullptr;
602 const bool canExtendPrevious = extendPrevious && previousLineString && newLineString;
603 if ( canExtendPrevious )
604 {
605 previousLineString->append( newLineString );
606 // we are taking ownership, so delete the input curve
607 delete c;
608 c = nullptr;
609 }
610 else
611 {
612 mCurves.append( c );
613 }
614
615 clearCache();
616}
617
619{
620 if ( i < 0 || i >= mCurves.size() )
621 {
622 return;
623 }
624
625 delete mCurves.takeAt( i );
626 clearCache();
627}
628
630{
631 if ( mCurves.isEmpty() || mWkbType == Qgis::WkbType::Unknown )
632 {
634 }
635
636 //is last curve QgsLineString
637 QgsCurve *lastCurve = nullptr;
638 if ( !mCurves.isEmpty() )
639 {
640 lastCurve = mCurves.at( mCurves.size() - 1 );
641 }
642
643 QgsLineString *line = nullptr;
644 if ( !lastCurve || QgsWkbTypes::flatType( lastCurve->wkbType() ) != Qgis::WkbType::LineString )
645 {
646 line = new QgsLineString();
647 mCurves.append( line );
648 if ( lastCurve )
649 {
650 line->addVertex( lastCurve->endPoint() );
651 }
652 lastCurve = line;
653 }
654 else //create new QgsLineString* with point in it
655 {
656 line = static_cast<QgsLineString *>( lastCurve );
657 }
658 line->addVertex( pt );
659 clearCache();
660}
661
663{
664 QgsCurve *lastCurve = nullptr;
665 QVector< QgsCurve * > newCurves;
666 newCurves.reserve( mCurves.size() );
667 for ( QgsCurve *curve : std::as_const( mCurves ) )
668 {
669 if ( lastCurve && lastCurve->wkbType() == curve->wkbType() )
670 {
671 if ( QgsLineString *ls = qgsgeometry_cast< QgsLineString * >( lastCurve ) )
672 {
673 ls->append( qgsgeometry_cast< QgsLineString * >( curve ) );
674 delete curve;
675 }
677 {
678 cs->append( qgsgeometry_cast< QgsCircularString * >( curve ) );
679 delete curve;
680 }
681 }
682 else
683 {
684 lastCurve = curve;
685 newCurves << curve;
686 }
687 }
688 mCurves = newCurves;
689}
690
691void QgsCompoundCurve::draw( QPainter &p ) const
692{
693 for ( const QgsCurve *curve : mCurves )
694 {
695 curve->draw( p );
696 }
697}
698
700{
701 for ( QgsCurve *curve : std::as_const( mCurves ) )
702 {
703 curve->transform( ct, d, transformZ );
704 }
705 clearCache();
706}
707
708void QgsCompoundCurve::transform( const QTransform &t, double zTranslate, double zScale, double mTranslate, double mScale )
709{
710 for ( QgsCurve *curve : std::as_const( mCurves ) )
711 {
712 curve->transform( t, zTranslate, zScale, mTranslate, mScale );
713 }
714 clearCache();
715}
716
717void QgsCompoundCurve::addToPainterPath( QPainterPath &path ) const
718{
719 QPainterPath pp;
720
721 for ( const QgsCurve *curve : mCurves )
722 {
723 if ( curve != mCurves.at( 0 ) && pp.currentPosition() != curve->startPoint().toQPointF() )
724 {
725 pp.lineTo( curve->startPoint().toQPointF() );
726 }
727 curve->addToPainterPath( pp );
728 }
729 path.addPath( pp );
730}
731
732void QgsCompoundCurve::drawAsPolygon( QPainter &p ) const
733{
734 QPainterPath pp;
735 for ( const QgsCurve *curve : mCurves )
736 {
737 if ( curve != mCurves.at( 0 ) && pp.currentPosition() != curve->startPoint().toQPointF() )
738 {
739 pp.lineTo( curve->startPoint().toQPointF() );
740 }
741 curve->addToPainterPath( pp );
742 }
743 p.drawPath( pp );
744}
745
747{
748 QVector< QPair<int, QgsVertexId> > curveIds = curveVertexId( position );
749 if ( curveIds.empty() )
750 {
751 return false;
752 }
753 int curveId = curveIds.at( 0 ).first;
754 if ( curveId >= mCurves.size() )
755 {
756 return false;
757 }
758
759 bool success = mCurves.at( curveId )->insertVertex( curveIds.at( 0 ).second, vertex );
760 if ( success )
761 {
762 clearCache(); //bbox changed
763 }
764 return success;
765}
766
767bool QgsCompoundCurve::moveVertex( QgsVertexId position, const QgsPoint &newPos )
768{
769 QVector< QPair<int, QgsVertexId> > curveIds = curveVertexId( position );
770 QVector< QPair<int, QgsVertexId> >::const_iterator idIt = curveIds.constBegin();
771 for ( ; idIt != curveIds.constEnd(); ++idIt )
772 {
773 mCurves.at( idIt->first )->moveVertex( idIt->second, newPos );
774 }
775
776 bool success = !curveIds.isEmpty();
777 if ( success )
778 {
779 clearCache(); //bbox changed
780 }
781 return success;
782}
783
785{
786 const QVector< QPair<int, QgsVertexId> > curveIds = curveVertexId( position );
787 if ( curveIds.isEmpty() )
788 return false;
789
790 const int curveId = curveIds.at( 0 ).first;
791 QgsCurve *curve = mCurves.at( curveId );
792 const QgsVertexId subVertexId = curveIds.at( 0 ).second;
793
794 // We are on a vertex that belongs to one curve only
795 if ( curveIds.size() == 1 )
796 {
797 const QgsCircularString *circularString = qgsgeometry_cast<const QgsCircularString *>( curve );
798 // If the vertex to delete is the middle vertex of a CircularString, we transform
799 // this CircularString into a LineString without the middle vertex
800 if ( circularString && subVertexId.vertex % 2 == 1 )
801 {
802 {
804 circularString->points( points );
805
806 removeCurve( curveId );
807
808 if ( subVertexId.vertex < points.length() - 2 )
809 {
810 auto curveC = std::make_unique<QgsCircularString>();
811 curveC->setPoints( points.mid( subVertexId.vertex + 1 ) );
812 mCurves.insert( curveId, curveC.release() );
813 }
814
815 const QgsPointSequence partB = QgsPointSequence() << points[subVertexId.vertex - 1] << points[subVertexId.vertex + 1];
816 auto curveB = std::make_unique<QgsLineString>();
817 curveB->setPoints( partB );
818 mCurves.insert( curveId, curveB.release() );
819 curve = mCurves.at( curveId );
820
821 if ( subVertexId.vertex > 1 )
822 {
823 auto curveA = std::make_unique<QgsCircularString>();
824 curveA->setPoints( points.mid( 0, subVertexId.vertex ) );
825 mCurves.insert( curveId, curveA.release() );
826 }
827 }
828 }
829 else if ( !curve->deleteVertex( subVertexId ) )
830 {
831 clearCache(); //bbox may have changed
832 return false;
833 }
834 if ( curve->numPoints() == 0 )
835 {
836 removeCurve( curveId );
837 }
838 }
839 // We are on a vertex that belongs to two curves
840 else if ( curveIds.size() == 2 )
841 {
842 const int nextCurveId = curveIds.at( 1 ).first;
843 QgsCurve *nextCurve = mCurves.at( nextCurveId );
844 const QgsVertexId nextSubVertexId = curveIds.at( 1 ).second;
845
846 Q_ASSERT( nextCurveId == curveId + 1 );
847 Q_ASSERT( subVertexId.vertex == curve->numPoints() - 1 );
848 Q_ASSERT( nextSubVertexId.vertex == 0 );
849
850 // globals start and end points
851 const QgsPoint startPoint = curve->startPoint();
852 const QgsPoint endPoint = nextCurve->endPoint();
853
854 // delete the vertex on first curve
855 if ( !curve->deleteVertex( subVertexId ) )
856 {
857 clearCache(); //bbox may have changed
858 return false;
859 }
860
861 // delete the vertex on second curve
862 if ( !nextCurve->deleteVertex( nextSubVertexId ) )
863 {
864 clearCache(); //bbox may have changed
865 return false;
866 }
867
868 // if first curve is now empty and second is not then
869 // create a LineString to link from the global start point to the
870 // new start of the second curve and delete the first curve
871 if ( curve->numPoints() == 0 && nextCurve->numPoints() != 0 )
872 {
873 QgsPoint startPointOfSecond = nextCurve->startPoint();
874 removeCurve( curveId );
875 QgsLineString *line = new QgsLineString();
876 line->insertVertex( QgsVertexId( 0, 0, 0 ), startPoint );
877 line->insertVertex( QgsVertexId( 0, 0, 1 ), startPointOfSecond );
878 mCurves.insert( curveId, line );
879 }
880 // else, if the first curve is not empty and the second is
881 // then create a LineString to link from the new end of the first curve to the
882 // global end point and delete the first curve
883 else if ( curve->numPoints() != 0 && nextCurve->numPoints() == 0 )
884 {
885 QgsPoint endPointOfFirst = curve->endPoint();
886 removeCurve( nextCurveId );
887 QgsLineString *line = new QgsLineString();
888 line->insertVertex( QgsVertexId( 0, 0, 0 ), endPointOfFirst );
889 line->insertVertex( QgsVertexId( 0, 0, 1 ), endPoint );
890 mCurves.insert( nextCurveId, line );
891 }
892 // else, if both curves are empty then
893 // remove both curves and create a LineString to link
894 // the curves before and the curves after the whole geometry
895 else if ( curve->numPoints() == 0 &&
896 nextCurve->numPoints() == 0 )
897 {
898 removeCurve( nextCurveId );
899 removeCurve( curveId );
900 QgsLineString *line = new QgsLineString();
901 line->insertVertex( QgsVertexId( 0, 0, 0 ), startPoint );
902 line->insertVertex( QgsVertexId( 0, 0, 1 ), endPoint );
903 mCurves.insert( curveId, line );
904 }
905 // else, both curves still have vertices, create a LineString to link
906 // the curves if needed
907 else
908 {
909 QgsPoint endPointOfFirst = curve->endPoint();
910 QgsPoint startPointOfSecond = nextCurve->startPoint();
911 if ( endPointOfFirst != startPointOfSecond )
912 {
913 QgsLineString *line = new QgsLineString();
914 line->insertVertex( QgsVertexId( 0, 0, 0 ), endPointOfFirst );
915 line->insertVertex( QgsVertexId( 0, 0, 1 ), startPointOfSecond );
916 mCurves.insert( nextCurveId, line );
917 }
918 }
919 condenseCurves(); // We merge consecutive LineStrings and CircularStrings
920 }
921
922 bool success = !curveIds.isEmpty();
923 if ( success )
924 clearCache(); //bbox changed
925 return success;
926}
927
928QVector< QPair<int, QgsVertexId> > QgsCompoundCurve::curveVertexId( QgsVertexId id ) const
929{
930 QVector< QPair<int, QgsVertexId> > curveIds;
931
932 int currentVertexIndex = 0;
933 for ( int i = 0; i < mCurves.size(); ++i )
934 {
935 int increment = mCurves.at( i )->numPoints() - 1;
936 if ( id.vertex >= currentVertexIndex && id.vertex <= currentVertexIndex + increment )
937 {
938 int curveVertexId = id.vertex - currentVertexIndex;
939 QgsVertexId vid;
940 vid.part = 0;
941 vid.ring = 0;
942 vid.vertex = curveVertexId;
943 curveIds.append( qMakePair( i, vid ) );
944 if ( curveVertexId == increment && i < ( mCurves.size() - 1 ) ) //add first vertex of next curve
945 {
946 vid.vertex = 0;
947 curveIds.append( qMakePair( i + 1, vid ) );
948 }
949 break;
950 }
951 else if ( id.vertex >= currentVertexIndex && id.vertex == currentVertexIndex + increment + 1 && i == ( mCurves.size() - 1 ) )
952 {
953 int curveVertexId = id.vertex - currentVertexIndex;
954 QgsVertexId vid;
955 vid.part = 0;
956 vid.ring = 0;
957 vid.vertex = curveVertexId;
958 curveIds.append( qMakePair( i, vid ) );
959 break;
960 }
961 currentVertexIndex += increment;
962 }
963
964 return curveIds;
965}
966
968{
969
970 // First we find out the sub-curves that are contain that vertex.
971
972 // If there is more than one, it means the vertex was at the beginning or end
973 // of an arc, which we don't support.
974
975 // If there is exactly one, we may either be on a LineString, or on a CircularString.
976
977 // If on CircularString, we need to check if the vertex is a CurveVertex (odd index).
978 // If so, we split the subcurve at vertex -1 and +1, , drop the middle part and insert a LineString/CircularString
979 // instead with the same points.
980
981 // At the end, we call condenseCurves() to merge successible line/circular strings
982
983 QVector< QPair<int, QgsVertexId> > curveIds = curveVertexId( position );
984
985 // We cannot convert points at start/end of subcurves
986 if ( curveIds.length() != 1 )
987 return false;
988
989 int curveId = curveIds[0].first;
990 QgsVertexId subVertexId = curveIds[0].second;
991 QgsCurve *curve = mCurves[curveId];
992
993 // We cannot convert first/last point of curve
994 if ( subVertexId.vertex == 0 || subVertexId.vertex == curve->numPoints() - 1 )
995 return false;
996
997 if ( const QgsCircularString *circularString = qgsgeometry_cast<const QgsCircularString *>( curve ) )
998 {
999 // If it's a circular string, we convert to LineString
1000
1001 // We cannot convert start/end points of arcs
1002 if ( subVertexId.vertex % 2 == 0 ) // for some reason, subVertexId.type is always SegmentVertex...
1003 return false;
1004
1006 circularString->points( points );
1007
1008 const QgsPointSequence partA = points.mid( 0, subVertexId.vertex );
1009 const QgsPointSequence partB = QgsPointSequence() << points[subVertexId.vertex - 1] << points[subVertexId.vertex] << points[subVertexId.vertex + 1];
1010 const QgsPointSequence partC = points.mid( subVertexId.vertex + 1 );
1011
1012 auto curveA = std::make_unique<QgsCircularString>();
1013 curveA->setPoints( partA );
1014 auto curveB = std::make_unique<QgsLineString>();
1015 curveB->setPoints( partB );
1016 auto curveC = std::make_unique<QgsCircularString>();
1017 curveC->setPoints( partC );
1018
1019 removeCurve( curveId );
1020 if ( subVertexId.vertex < points.length() - 2 )
1021 mCurves.insert( curveId, curveC.release() );
1022 mCurves.insert( curveId, curveB.release() );
1023 if ( subVertexId.vertex > 1 )
1024 mCurves.insert( curveId, curveA.release() );
1025 }
1026 else if ( const QgsLineString *lineString = dynamic_cast<const QgsLineString *>( curve ) )
1027 {
1028 // If it's a linestring, we split and insert a curve
1029
1031 lineString->points( points );
1032
1033 const QgsPointSequence partA = points.mid( 0, subVertexId.vertex );
1034 const QgsPointSequence partB = QgsPointSequence() << points[subVertexId.vertex - 1] << points[subVertexId.vertex] << points[subVertexId.vertex + 1];
1035 const QgsPointSequence partC = points.mid( subVertexId.vertex + 1 );
1036
1037 auto curveA = std::make_unique<QgsLineString>();
1038 curveA->setPoints( partA );
1039 auto curveB = std::make_unique<QgsCircularString>();
1040 curveB->setPoints( partB );
1041 auto curveC = std::make_unique<QgsLineString>();
1042 curveC->setPoints( partC );
1043
1044 removeCurve( curveId );
1045 if ( subVertexId.vertex < points.length() - 2 )
1046 mCurves.insert( curveId, curveC.release() );
1047 mCurves.insert( curveId, curveB.release() );
1048 if ( subVertexId.vertex > 1 )
1049 mCurves.insert( curveId, curveA.release() );
1050 }
1051
1052 // We merge consecutive LineStrings
1054
1055 clearCache();
1056 return true;
1057}
1058
1059
1060double QgsCompoundCurve::closestSegment( const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon ) const
1061{
1062 return QgsGeometryUtils::closestSegmentFromComponents( mCurves, QgsGeometryUtils::Vertex, pt, segmentPt, vertexAfter, leftOf, epsilon );
1063}
1064
1065bool QgsCompoundCurve::pointAt( int node, QgsPoint &point, Qgis::VertexType &type ) const
1066{
1067 int currentVertexId = 0;
1068 for ( int j = 0; j < mCurves.size(); ++j )
1069 {
1070 int nCurvePoints = mCurves.at( j )->numPoints();
1071 if ( ( node - currentVertexId ) < nCurvePoints )
1072 {
1073 return ( mCurves.at( j )->pointAt( node - currentVertexId, point, type ) );
1074 }
1075 currentVertexId += ( nCurvePoints - 1 );
1076 }
1077 return false;
1078}
1079
1080double QgsCompoundCurve::xAt( int index ) const
1081{
1082 int currentVertexId = 0;
1083 for ( int j = 0; j < mCurves.size(); ++j )
1084 {
1085 int nCurvePoints = mCurves.at( j )->numPoints();
1086 if ( ( index - currentVertexId ) < nCurvePoints )
1087 {
1088 return mCurves.at( j )->xAt( index - currentVertexId );
1089 }
1090 currentVertexId += ( nCurvePoints - 1 );
1091 }
1092 return 0.0;
1093}
1094
1095double QgsCompoundCurve::yAt( int index ) const
1096{
1097 int currentVertexId = 0;
1098 for ( int j = 0; j < mCurves.size(); ++j )
1099 {
1100 int nCurvePoints = mCurves.at( j )->numPoints();
1101 if ( ( index - currentVertexId ) < nCurvePoints )
1102 {
1103 return mCurves.at( j )->yAt( index - currentVertexId );
1104 }
1105 currentVertexId += ( nCurvePoints - 1 );
1106 }
1107 return 0.0;
1108}
1109
1110double QgsCompoundCurve::zAt( int index ) const
1111{
1112 int currentVertexId = 0;
1113 for ( int j = 0; j < mCurves.size(); ++j )
1114 {
1115 int nCurvePoints = mCurves.at( j )->numPoints();
1116 if ( ( index - currentVertexId ) < nCurvePoints )
1117 {
1118 return mCurves.at( j )->zAt( index - currentVertexId );
1119 }
1120 currentVertexId += ( nCurvePoints - 1 );
1121 }
1122 return 0.0;
1123}
1124
1125double QgsCompoundCurve::mAt( int index ) const
1126{
1127 int currentVertexId = 0;
1128 for ( int j = 0; j < mCurves.size(); ++j )
1129 {
1130 int nCurvePoints = mCurves.at( j )->numPoints();
1131 if ( ( index - currentVertexId ) < nCurvePoints )
1132 {
1133 return mCurves.at( j )->mAt( index - currentVertexId );
1134 }
1135 currentVertexId += ( nCurvePoints - 1 );
1136 }
1137 return 0.0;
1138}
1139
1141{
1142 bool res = true;
1143 for ( QgsCurve *curve : std::as_const( mCurves ) )
1144 {
1145 if ( !curve->transform( transformer ) )
1146 {
1147 res = false;
1148 break;
1149 }
1150
1151 if ( feedback && feedback->isCanceled() )
1152 {
1153 res = false;
1154 break;
1155 }
1156 }
1157 clearCache();
1158 return res;
1159}
1160
1161void QgsCompoundCurve::filterVertices( const std::function<bool ( const QgsPoint & )> &filter )
1162{
1163 for ( QgsCurve *curve : std::as_const( mCurves ) )
1164 {
1165 curve->filterVertices( filter );
1166 }
1167 clearCache();
1168}
1169
1170void QgsCompoundCurve::transformVertices( const std::function<QgsPoint( const QgsPoint & )> &transform )
1171{
1172 for ( QgsCurve *curve : std::as_const( mCurves ) )
1173 {
1174 curve->transformVertices( transform );
1175 }
1176 clearCache();
1177}
1178
1179std::tuple<std::unique_ptr<QgsCurve>, std::unique_ptr<QgsCurve> > QgsCompoundCurve::splitCurveAtVertex( int index ) const
1180{
1181 if ( mCurves.empty() )
1182 return std::make_tuple( std::make_unique< QgsCompoundCurve >(), std::make_unique< QgsCompoundCurve >() );
1183
1184 int curveStart = 0;
1185
1186 auto curve1 = std::make_unique< QgsCompoundCurve >();
1187 std::unique_ptr< QgsCompoundCurve > curve2;
1188
1189 for ( const QgsCurve *curve : mCurves )
1190 {
1191 const int curveSize = curve->numPoints();
1192 if ( !curve2 && index < curveStart + curveSize )
1193 {
1194 // split the curve
1195 auto [ p1, p2 ] = curve->splitCurveAtVertex( index - curveStart );
1196 if ( !p1->isEmpty() )
1197 curve1->addCurve( p1.release() );
1198
1199 curve2 = std::make_unique< QgsCompoundCurve >();
1200 if ( !p2->isEmpty() )
1201 curve2->addCurve( p2.release() );
1202 }
1203 else
1204 {
1205 if ( curve2 )
1206 curve2->addCurve( curve->clone() );
1207 else
1208 curve1->addCurve( curve->clone() );
1209 }
1210
1211 // subtract 1 here, because the next curve will start with the same
1212 // vertex as this curve ended at
1213 curveStart += curve->numPoints() - 1;
1214 }
1215
1216 return std::make_tuple( std::move( curve1 ), curve2 ? std::move( curve2 ) : std::make_unique< QgsCompoundCurve >() );
1217}
1218
1219void QgsCompoundCurve::sumUpArea( double &sum ) const
1220{
1222 {
1223 sum += mSummedUpArea;
1224 return;
1225 }
1226
1227 mSummedUpArea = 0;
1228 for ( const QgsCurve *curve : mCurves )
1229 {
1230 curve->sumUpArea( mSummedUpArea );
1231 }
1233 sum += mSummedUpArea;
1234}
1235
1236void QgsCompoundCurve::sumUpArea3D( double &sum ) const
1237{
1239 {
1240 sum += mSummedUpArea3D;
1241 return;
1242 }
1243
1244 mSummedUpArea3D = 0;
1245 for ( const QgsCurve *curve : mCurves )
1246 {
1247 curve->sumUpArea3D( mSummedUpArea3D );
1248 }
1250 sum += mSummedUpArea3D;
1251}
1252
1254{
1255 if ( numPoints() < 1 || isClosed() )
1256 {
1257 return;
1258 }
1259 addVertex( startPoint() );
1260}
1261
1263{
1264 for ( const QgsCurve *curve : mCurves )
1265 {
1266 if ( curve->hasCurvedSegments() )
1267 {
1268 return true;
1269 }
1270 }
1271 return false;
1272}
1273
1275{
1276 QVector< QPair<int, QgsVertexId> > curveIds = curveVertexId( vertex );
1277 if ( curveIds.size() == 1 )
1278 {
1279 QgsCurve *curve = mCurves[curveIds.at( 0 ).first];
1280 return curve->vertexAngle( curveIds.at( 0 ).second );
1281 }
1282 else if ( curveIds.size() > 1 )
1283 {
1284 QgsCurve *curve1 = mCurves[curveIds.at( 0 ).first];
1285 QgsCurve *curve2 = mCurves[curveIds.at( 1 ).first];
1286 double angle1 = curve1->vertexAngle( curveIds.at( 0 ).second );
1287 double angle2 = curve2->vertexAngle( curveIds.at( 1 ).second );
1288 return QgsGeometryUtilsBase::averageAngle( angle1, angle2 );
1289 }
1290 else
1291 {
1292 return 0.0;
1293 }
1294}
1295
1297{
1298 QVector< QPair<int, QgsVertexId> > curveIds = curveVertexId( startVertex );
1299 double length = 0.0;
1300 for ( auto it = curveIds.constBegin(); it != curveIds.constEnd(); ++it )
1301 {
1302 length += mCurves.at( it->first )->segmentLength( it->second );
1303 }
1304 return length;
1305}
1306
1308{
1310 for ( int i = mCurves.count() - 1; i >= 0; --i )
1311 {
1312 QgsCurve *reversedCurve = mCurves.at( i )->reversed();
1313 clone->addCurve( reversedCurve );
1314 }
1315 return clone;
1316}
1317
1318QgsPoint *QgsCompoundCurve::interpolatePoint( const double distance ) const
1319{
1320 if ( distance < 0 )
1321 return nullptr;
1322
1323 double distanceTraversed = 0;
1324 for ( const QgsCurve *curve : mCurves )
1325 {
1326 const double thisCurveLength = curve->length();
1327 if ( distanceTraversed + thisCurveLength > distance || qgsDoubleNear( distanceTraversed + thisCurveLength, distance ) )
1328 {
1329 // point falls on this segment - truncate to segment length if qgsDoubleNear test was actually > segment length
1330 const double distanceToPoint = std::min( distance - distanceTraversed, thisCurveLength );
1331
1332 // point falls on this curve
1333 return curve->interpolatePoint( distanceToPoint );
1334 }
1335
1336 distanceTraversed += thisCurveLength;
1337 }
1338
1339 return nullptr;
1340}
1341
1342QgsCompoundCurve *QgsCompoundCurve::curveSubstring( double startDistance, double endDistance ) const
1343{
1344 if ( startDistance < 0 && endDistance < 0 )
1345 return createEmptyWithSameType();
1346
1347 endDistance = std::max( startDistance, endDistance );
1348 auto substring = std::make_unique< QgsCompoundCurve >();
1349
1350 double distanceTraversed = 0;
1351 for ( const QgsCurve *curve : mCurves )
1352 {
1353 const double thisCurveLength = curve->length();
1354 if ( distanceTraversed + thisCurveLength < startDistance )
1355 {
1356 // keep going - haven't found start yet, so no need to include this curve at all
1357 }
1358 else
1359 {
1360 std::unique_ptr< QgsCurve > part( curve->curveSubstring( startDistance - distanceTraversed, endDistance - distanceTraversed ) );
1361 if ( part )
1362 substring->addCurve( part.release() );
1363 }
1364
1365 distanceTraversed += thisCurveLength;
1366 if ( distanceTraversed > endDistance )
1367 break;
1368 }
1369
1370 return substring.release();
1371}
1372
1373bool QgsCompoundCurve::addZValue( double zValue )
1374{
1375 if ( QgsWkbTypes::hasZ( mWkbType ) )
1376 return false;
1377
1379
1380 for ( QgsCurve *curve : std::as_const( mCurves ) )
1381 {
1382 curve->addZValue( zValue );
1383 }
1384 clearCache();
1385 return true;
1386}
1387
1388bool QgsCompoundCurve::addMValue( double mValue )
1389{
1390 if ( QgsWkbTypes::hasM( mWkbType ) )
1391 return false;
1392
1394
1395 for ( QgsCurve *curve : std::as_const( mCurves ) )
1396 {
1397 curve->addMValue( mValue );
1398 }
1399 clearCache();
1400 return true;
1401}
1402
1404{
1405 if ( !QgsWkbTypes::hasZ( mWkbType ) )
1406 return false;
1407
1409 for ( QgsCurve *curve : std::as_const( mCurves ) )
1410 {
1411 curve->dropZValue();
1412 }
1413 clearCache();
1414 return true;
1415}
1416
1418{
1419 if ( !QgsWkbTypes::hasM( mWkbType ) )
1420 return false;
1421
1423 for ( QgsCurve *curve : std::as_const( mCurves ) )
1424 {
1425 curve->dropMValue();
1426 }
1427 clearCache();
1428 return true;
1429}
1430
1432{
1433 for ( QgsCurve *curve : std::as_const( mCurves ) )
1434 {
1435 curve->swapXy();
1436 }
1437 clearCache();
1438}
1439
1441{
1442 // Ensure fromVertex < toVertex for simplicity
1443 if ( fromVertex.vertex > toVertex.vertex )
1444 {
1445 return distanceBetweenVertices( toVertex, fromVertex );
1446 }
1447
1448 // Convert QgsVertexId to simple vertex numbers for compound curves (single ring, single part)
1449 if ( fromVertex.part != 0 || fromVertex.ring != 0 || toVertex.part != 0 || toVertex.ring != 0 )
1450 return -1.0;
1451
1452 const int fromVertexNumber = fromVertex.vertex;
1453 const int toVertexNumber = toVertex.vertex;
1454
1455 const int totalVertices = numPoints();
1456 if ( fromVertexNumber < 0 || fromVertexNumber >= totalVertices || toVertexNumber < 0 || toVertexNumber >= totalVertices )
1457 return -1.0;
1458
1459 if ( fromVertexNumber == toVertexNumber )
1460 return 0.0;
1461
1462 double totalDistance = 0.0;
1463
1464 // Find which curves contain our vertices and accumulate distances
1465 int currentVertexId = 0;
1466 int fromCurve = -1, toCurve = -1;
1467 int fromCurveVertex = -1, toCurveVertex = -1;
1468
1469 // First pass: find which curves contain from and to vertices
1470 for ( int j = 0; j < mCurves.size(); ++j )
1471 {
1472 int nCurvePoints = mCurves.at( j )->numPoints();
1473
1474 // Check if fromVertex is in this curve
1475 if ( fromCurve == -1 && fromVertexNumber >= currentVertexId && fromVertexNumber < currentVertexId + nCurvePoints )
1476 {
1477 fromCurve = j;
1478 fromCurveVertex = fromVertexNumber - currentVertexId;
1479 }
1480
1481 // Check if toVertex is in this curve
1482 if ( toCurve == -1 && toVertexNumber >= currentVertexId && toVertexNumber < currentVertexId + nCurvePoints )
1483 {
1484 toCurve = j;
1485 toCurveVertex = toVertexNumber - currentVertexId;
1486 break;
1487 }
1488
1489 currentVertexId += ( nCurvePoints - 1 ); // Subtract 1 because curves share endpoints
1490 }
1491
1492 if ( fromCurve == -1 || toCurve == -1 )
1493 return -1.0; // Invalid vertex IDs
1494
1495 if ( fromCurve == toCurve )
1496 {
1497 // Both vertices are on the same curve
1498 QgsVertexId fromId( 0, 0, fromCurveVertex );
1499 QgsVertexId toId( 0, 0, toCurveVertex );
1500 return mCurves.at( fromCurve )->distanceBetweenVertices( fromId, toId );
1501 }
1502 else
1503 {
1504 // Vertices are on different curves - accumulate distances across multiple curves
1505
1506 // Distance from fromVertex to end of its curve
1507 if ( fromCurveVertex < mCurves.at( fromCurve )->numPoints() - 1 )
1508 {
1509 QgsVertexId fromId( 0, 0, fromCurveVertex );
1510 QgsVertexId endId( 0, 0, mCurves.at( fromCurve )->numPoints() - 1 );
1511 totalDistance += mCurves.at( fromCurve )->distanceBetweenVertices( fromId, endId );
1512 }
1513
1514 // Distance of complete intermediate curves
1515 for ( int j = fromCurve + 1; j < toCurve; ++j )
1516 {
1517 totalDistance += mCurves.at( j )->length();
1518 }
1519
1520 // Distance from start of toCurve to toVertex
1521 if ( toCurveVertex > 0 )
1522 {
1523 QgsVertexId startId( 0, 0, 0 );
1524 QgsVertexId toId( 0, 0, toCurveVertex );
1525 totalDistance += mCurves.at( toCurve )->distanceBetweenVertices( startId, toId );
1526 }
1527 }
1528
1529 return totalDistance;
1530}
QFlags< GeometryValidityFlag > GeometryValidityFlags
Geometry validity flags.
Definition qgis.h:2133
VertexType
Types of vertex.
Definition qgis.h:3136
WkbType
The WKB type describes the number of dimensions a geometry has.
Definition qgis.h:280
@ CompoundCurve
CompoundCurve.
Definition qgis.h:291
@ LineString
LineString.
Definition qgis.h:283
@ Unknown
Unknown.
Definition qgis.h:281
@ CircularString
CircularString.
Definition qgis.h:290
TransformDirection
Indicates the direction (forward or inverse) of a transform.
Definition qgis.h:2729
An abstract base class for classes which transform geometries by transforming input points to output ...
virtual bool fromWkb(QgsConstWkbPtr &wkb)=0
Sets the geometry from a WKB string.
SegmentationToleranceType
Segmentation tolerance as maximum angle or maximum difference between approximation and circle.
virtual double vertexAngle(QgsVertexId vertex) const =0
Returns approximate angle at a vertex.
bool isMeasure() const
Returns true if the geometry contains m values.
QFlags< WkbFlag > WkbFlags
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
AxisOrder
Axis order for GML generation.
QString wktTypeStr() const
Returns the WKT type string of the geometry.
QgsAbstractGeometry & operator=(const QgsAbstractGeometry &geom)
Qgis::WkbType wkbType() const
Returns the WKB type of the geometry.
void setZMTypeFromSubGeometry(const QgsAbstractGeometry *subggeom, Qgis::WkbType baseGeomType)
Updates the geometry type based on whether sub geometries contain z or m values.
virtual bool boundingBoxIntersects(const QgsRectangle &rectangle) const
Returns true if the bounding box of this geometry intersects with a rectangle.
virtual bool deleteVertex(QgsVertexId position)=0
Deletes a vertex within the geometry.
virtual int compareTo(const QgsAbstractGeometry *other) const
Comparator for sorting of geometry.
QgsAbstractGeometry()=default
QgsGeometryConstPartIterator parts() const
Returns Java-style iterator for traversal of parts of the geometry.
static endian_t endian()
Returns whether this machine uses big or little endian.
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:45
void combineWith(const QgsBox3D &box)
Expands the bbox so that it covers both the original rectangle and the given rectangle.
Definition qgsbox3d.cpp:214
Circular string geometry type.
void points(QgsPointSequence &pts) const override
Returns a list of points within the curve.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
void draw(QPainter &p) const override
Draws the geometry using the specified QPainter.
void sumUpArea(double &sum) const override
Sums up the area of the curve by iterating over the vertices (shoelace formula).
QgsLineString * curveToLine(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const override
Returns a new line string geometry corresponding to a segmentized approximation of the curve.
bool insertVertex(QgsVertexId position, const QgsPoint &vertex) override
Inserts a vertex into the geometry.
bool removeDuplicateNodes(double epsilon=4 *std::numeric_limits< double >::epsilon(), bool useZValues=false) override
Removes duplicate nodes from the geometry, wherever removing the nodes does not result in a degenerat...
QgsAbstractGeometry * simplifyByDistance(double tolerance) const override
Simplifies the geometry by applying the Douglas Peucker simplification by distance algorithm.
int compareToSameClass(const QgsAbstractGeometry *other) const final
Compares to an other geometry of the same class, and returns a integer for sorting of the two geometr...
bool fromWkb(QgsConstWkbPtr &wkb) override
Sets the geometry from a WKB string.
QgsCompoundCurve * reversed() const override
Returns a reversed copy of the curve, where the direction of the curve has been flipped.
void condenseCurves()
Condenses the curves in this geometry by combining adjacent linestrings a to a single continuous line...
void close()
Appends first point if not already closed.
bool addMValue(double mValue=0) override
Adds a measure to the geometry, initialized to a preset value.
void drawAsPolygon(QPainter &p) const override
Draws the curve as a polygon on the specified QPainter.
int dimension() const override
Returns the inherent dimension of the geometry.
void filterVertices(const std::function< bool(const QgsPoint &) > &filter) override
Filters the vertices from the geometry in place, removing any which do not return true for the filter...
std::tuple< std::unique_ptr< QgsCurve >, std::unique_ptr< QgsCurve > > splitCurveAtVertex(int index) const final
Splits the curve at the specified vertex index, returning two curves which represent the portion of t...
bool boundingBoxIntersects(const QgsBox3D &box3d) const override
Returns true if the bounding box of this geometry intersects with a box3d.
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
QString geometryType() const override
Returns a unique string representing the geometry type.
double mAt(int index) const override
Returns the m-coordinate of the specified node in the line string.
double distanceBetweenVertices(QgsVertexId fromVertex, QgsVertexId toVertex) const override
Returns the distance along the curve between two vertices.
bool isEmpty() const override
Returns true if the geometry is empty.
double vertexAngle(QgsVertexId vertex) const override
Returns approximate angle at a vertex.
double segmentLength(QgsVertexId startVertex) const override
Returns the length of the segment of the geometry which begins at startVertex.
void transformVertices(const std::function< QgsPoint(const QgsPoint &) > &transform) override
Transforms the vertices from the geometry in place, applying the transform function to every vertex.
int nCurves() const
Returns the number of curves in the geometry.
bool toggleCircularAtVertex(QgsVertexId position)
Converts the vertex at the given position from/to circular.
QgsBox3D calculateBoundingBox3D() const override
Calculates the minimal 3D bounding box for the geometry.
QString asWkt(int precision=17) const override
Returns a WKT representation of the geometry.
const QgsAbstractGeometry * simplifiedTypeRef() const override
Returns a reference to the simplest lossless representation of this geometry, e.g.
int wkbSize(QgsAbstractGeometry::WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const override
Returns the length of the QByteArray returned by asWkb().
void sumUpArea3D(double &sum) const override
Sums up the 3d area of the curve by iterating over the vertices (shoelace formula).
void swapXy() override
Swaps the x and y coordinates from the geometry.
void removeCurve(int i)
Removes a curve from the geometry.
void addCurve(QgsCurve *c, bool extendPrevious=false)
Adds a curve to the geometry (takes ownership).
bool moveVertex(QgsVertexId position, const QgsPoint &newPos) override
Moves a vertex within the geometry.
bool deleteVertex(QgsVertexId position) override
Deletes a vertex within the geometry.
double closestSegment(const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf=nullptr, double epsilon=4 *std::numeric_limits< double >::epsilon()) const override
Searches for the closest segment of the geometry to a given point.
QDomElement asGml2(QDomDocument &doc, int precision=17, const QString &ns="gml", QgsAbstractGeometry::AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const override
Returns a GML2 representation of the geometry.
bool dropZValue() override
Drops any z-dimensions which exist in the geometry.
double xAt(int index) const override
Returns the x-coordinate of the specified node in the line string.
double yAt(int index) const override
Returns the y-coordinate of the specified node in the line string.
bool dropMValue() override
Drops any measure values which exist in the geometry.
QgsCompoundCurve & operator=(const QgsCompoundCurve &curve)
QgsCompoundCurve * clone() const override
Clones the geometry by performing a deep copy.
const QgsCurve * curveAt(int i) const
Returns the curve at the specified index.
void points(QgsPointSequence &pts) const override
Returns a list of points within the curve.
bool isValid(QString &error, Qgis::GeometryValidityFlags flags=Qgis::GeometryValidityFlags()) const override
Checks validity of the geometry, and returns true if the geometry is valid.
~QgsCompoundCurve() override
QByteArray asWkb(QgsAbstractGeometry::WkbFlags flags=QgsAbstractGeometry::WkbFlags()) const override
Returns a WKB representation of the geometry.
void clear() override
Clears the geometry, ie reset it to a null geometry.
int indexOf(const QgsPoint &point) const final
Returns the index of the first vertex matching the given point, or -1 if a matching vertex is not fou...
void transform(const QgsCoordinateTransform &ct, Qgis::TransformDirection d=Qgis::TransformDirection::Forward, bool transformZ=false) override
Transforms the geometry using a coordinate transform.
bool hasCurvedSegments() const override
Returns true if the geometry contains curved segments.
QgsCompoundCurve * curveSubstring(double startDistance, double endDistance) const override
Returns a new curve representing a substring of this curve.
void scroll(int firstVertexIndex) final
Scrolls the curve vertices so that they start with the vertex at the given index.
double length() const override
Returns the planar, 2-dimensional length of the geometry.
void addToPainterPath(QPainterPath &path) const override
Adds a curve to a painter path.
QgsPoint * interpolatePoint(double distance) const override
Returns an interpolated point on the curve at the specified distance.
QgsCompoundCurve * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
bool pointAt(int node, QgsPoint &point, Qgis::VertexType &type) const override
Returns the point and vertex id of a point within the curve.
QgsPoint startPoint() const override
Returns the starting point of the curve.
QgsCompoundCurve * snappedToGrid(double hSpacing, double vSpacing, double dSpacing=0, double mSpacing=0, bool removeRedundantPoints=false) const override
Makes a new geometry with all the points or vertices snapped to the closest point of the grid.
QgsPoint endPoint() const override
Returns the end point of the curve.
int numPoints() const override
Returns the number of points in the curve.
double zAt(int index) const override
Returns the z-coordinate of the specified node in the line string.
void addVertex(const QgsPoint &pt)
Adds a vertex to the end of the geometry.
bool addZValue(double zValue=0) override
Adds a z-dimension to the geometry, initialized to a preset value.
QDomElement asGml3(QDomDocument &doc, int precision=17, const QString &ns="gml", QgsAbstractGeometry::AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const override
Returns a GML3 representation of the geometry.
A const WKB pointer.
Definition qgswkbptr.h:139
Qgis::WkbType readHeader() const
readHeader
Definition qgswkbptr.cpp:60
Handles coordinate transforms between two coordinate systems.
virtual int numPoints() const =0
Returns the number of points in the curve.
void clearCache() const override
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
Definition qgscurve.cpp:294
double mSummedUpArea3D
Definition qgscurve.h:404
bool mHasCachedSummedUpArea
Definition qgscurve.h:401
bool mHasCachedSummedUpArea3D
Definition qgscurve.h:403
virtual bool isClosed() const
Returns true if the curve is closed.
Definition qgscurve.cpp:54
bool isValid(QString &error, Qgis::GeometryValidityFlags flags=Qgis::GeometryValidityFlags()) const override
Checks validity of the geometry, and returns true if the geometry is valid.
Definition qgscurve.cpp:248
QgsBox3D mBoundingBox
Cached bounding box.
Definition qgscurve.h:399
virtual QgsPoint startPoint() const =0
Returns the starting point of the curve.
virtual QgsPoint endPoint() const =0
Returns the end point of the curve.
double mSummedUpArea
Definition qgscurve.h:402
QgsCurve()=default
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:55
static double averageAngle(double x1, double y1, double x2, double y2, double x3, double y3)
Calculates the average angle (in radians) between the two linear segments from (x1,...
static QStringList wktGetChildBlocks(const QString &wkt, const QString &defaultType=QString())
Parses a WKT string and returns of list of blocks contained in the WKT.
static QPair< Qgis::WkbType, QString > wktReadBlock(const QString &wkt)
Parses a WKT block of the format "TYPE( contents )" and returns a pair of geometry type to contents (...
static double closestSegmentFromComponents(T &container, ComponentType ctype, const QgsPoint &pt, QgsPoint &segmentPt, QgsVertexId &vertexAfter, int *leftOf, double epsilon)
Line string geometry type, with support for z-dimension and m-values.
void append(const QgsLineString *line)
Appends the contents of another line string to the end of this line string.
bool insertVertex(QgsVertexId position, const QgsPoint &vertex) override
Inserts a vertex into the geometry.
void addVertex(const QgsPoint &pt)
Adds a new vertex to the end of the line string.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
QgsPoint vertexAt(QgsVertexId) const override
Returns the point corresponding to a specified vertex id.
Definition qgspoint.cpp:551
WKB pointer handler.
Definition qgswkbptr.h:45
static Qgis::WkbType dropM(Qgis::WkbType type)
Drops the m dimension (if present) for a WKB type and returns the new type.
static Qgis::WkbType dropZ(Qgis::WkbType type)
Drops the z dimension (if present) for a WKB type and returns the new type.
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
static Qgis::WkbType addZ(Qgis::WkbType type)
Adds the z dimension to a WKB type and returns the new type.
static Q_INVOKABLE bool hasZ(Qgis::WkbType type)
Tests whether a WKB type contains the z-dimension.
static Q_INVOKABLE bool hasM(Qgis::WkbType type)
Tests whether a WKB type contains m values.
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6935
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QVector< QgsPoint > QgsPointSequence
Utility class for identifying a unique vertex within a geometry.
Definition qgsvertexid.h:34
int vertex
Vertex number.
Definition qgsvertexid.h:98
int part
Part number.
Definition qgsvertexid.h:92
int ring
Ring number.
Definition qgsvertexid.h:95