QGIS API Documentation  2.14.7-Essen
qgsgeometry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometry.cpp - Geometry (stored as Open Geospatial Consortium WKB)
3  -------------------------------------------------------------------
4 Date : 02 May 2005
5 Copyright : (C) 2005 by Brendan Morley
6 email : morb at ozemail dot com dot au
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include <limits>
17 #include <cstdarg>
18 #include <cstdio>
19 #include <cmath>
20 
21 #include "qgis.h"
22 #include "qgsgeometry.h"
23 #include "qgsgeometryeditutils.h"
24 #include "qgsgeometryfactory.h"
25 #include "qgsgeometryutils.h"
27 #include "qgsgeos.h"
28 #include "qgsapplication.h"
29 #include "qgslogger.h"
30 #include "qgsmessagelog.h"
31 #include "qgspoint.h"
32 #include "qgsrectangle.h"
33 
34 #include "qgsmaplayerregistry.h"
35 #include "qgsvectorlayer.h"
36 #include "qgsproject.h"
37 #include "qgsgeometryvalidator.h"
38 
39 #include "qgsmulticurvev2.h"
40 #include "qgsmultilinestringv2.h"
41 #include "qgsmultipointv2.h"
42 #include "qgsmultipolygonv2.h"
43 #include "qgsmultisurfacev2.h"
44 #include "qgspointv2.h"
45 #include "qgspolygonv2.h"
46 #include "qgslinestringv2.h"
47 
48 #ifndef Q_WS_WIN
49 #include <netinet/in.h>
50 #else
51 #include <winsock.h>
52 #endif
53 
55 {
56  QgsGeometryPrivate(): ref( 1 ), geometry( nullptr ), mWkb( nullptr ), mWkbSize( 0 ), mGeos( nullptr ) {}
57  ~QgsGeometryPrivate() { delete geometry; delete[] mWkb; GEOSGeom_destroy_r( QgsGeos::getGEOSHandler(), mGeos ); }
60  mutable const unsigned char* mWkb; //store wkb pointer for backward compatibility
61  mutable int mWkbSize;
62  mutable GEOSGeometry* mGeos;
63 };
64 
66 {
67 }
68 
70 {
71  if ( !d->ref.deref() )
72  delete d;
73 }
74 
76 {
77  d->geometry = geom;
78  d->ref = QAtomicInt( 1 );
79 }
80 
82 {
83  d = other.d;
84  d->ref.ref();
85 }
86 
88 {
89  if ( !d->ref.deref() )
90  {
91  delete d;
92  }
93 
94  d = other.d;
95  d->ref.ref();
96  return *this;
97 }
98 
99 void QgsGeometry::detach( bool cloneGeom )
100 {
101  if ( d->ref > 1 )
102  {
103  ( void )d->ref.deref();
104  QgsAbstractGeometryV2* cGeom = nullptr;
105 
106  if ( d->geometry && cloneGeom )
107  {
108  cGeom = d->geometry->clone();
109  }
110 
111  d = new QgsGeometryPrivate();
112  d->geometry = cGeom;
113  }
114 }
115 
116 void QgsGeometry::removeWkbGeos()
117 {
118  delete[] d->mWkb;
119  d->mWkb = nullptr;
120  d->mWkbSize = 0;
121  if ( d->mGeos )
122  {
123  GEOSGeom_destroy_r( QgsGeos::getGEOSHandler(), d->mGeos );
124  d->mGeos = nullptr;
125  }
126 }
127 
129 {
130  return d->geometry;
131 }
132 
134 {
135  if ( d->geometry == geometry )
136  {
137  return;
138  }
139 
140  detach( false );
141  if ( d->geometry )
142  {
143  delete d->geometry;
144  d->geometry = nullptr;
145  }
146  removeWkbGeos();
147 
148  d->geometry = geometry;
149 }
150 
152 {
153  return !d->geometry;
154 }
155 
157 {
159  if ( !geom )
160  {
161  return nullptr;
162  }
163  return new QgsGeometry( geom );
164 }
165 
167 {
169  if ( geom )
170  {
171  return new QgsGeometry( geom );
172  }
173  return nullptr;
174 }
175 
177 {
179  if ( geom )
180  {
181  return new QgsGeometry( geom );
182  }
183  return nullptr;
184 }
185 
187 {
189  if ( geom )
190  {
191  return new QgsGeometry( geom );
192  }
193  return nullptr;
194 }
195 
197 {
199  if ( geom )
200  {
201  return new QgsGeometry( geom );
202  }
203  return nullptr;
204 }
205 
207 {
209  if ( geom )
210  {
211  return new QgsGeometry( geom );
212  }
213  return nullptr;
214 }
215 
217 {
219  if ( geom )
220  {
221  return new QgsGeometry( geom );
222  }
223  return nullptr;
224 }
225 
227 {
228  QgsPolyline ring;
229  ring.append( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
230  ring.append( QgsPoint( rect.xMaximum(), rect.yMinimum() ) );
231  ring.append( QgsPoint( rect.xMaximum(), rect.yMaximum() ) );
232  ring.append( QgsPoint( rect.xMinimum(), rect.yMaximum() ) );
233  ring.append( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
234 
235  QgsPolygon polygon;
236  polygon.append( ring );
237 
238  return fromPolygon( polygon );
239 }
240 
241 void QgsGeometry::fromWkb( unsigned char *wkb, int length )
242 {
243  detach( false );
244 
245  if ( d->geometry )
246  {
247  delete d->geometry;
248  removeWkbGeos();
249  }
251  if ( d->geometry )
252  {
253  d->mWkb = wkb;
254  d->mWkbSize = length;
255  }
256  else
257  {
258  delete [] wkb;
259  d->mWkb = nullptr;
260  d->mWkbSize = 0;
261  }
262 }
263 
264 const unsigned char *QgsGeometry::asWkb() const
265 {
266  if ( !d->geometry )
267  {
268  return nullptr;
269  }
270 
271  if ( !d->mWkb )
272  {
273  d->mWkb = d->geometry->asWkb( d->mWkbSize );
274  }
275  return d->mWkb;
276 }
277 
279 {
280  if ( !d->geometry )
281  {
282  return 0;
283  }
284 
285  if ( !d->mWkb )
286  {
287  d->mWkb = d->geometry->asWkb( d->mWkbSize );
288  }
289  return d->mWkbSize;
290 }
291 
292 const GEOSGeometry* QgsGeometry::asGeos( double precision ) const
293 {
294  if ( !d->geometry )
295  {
296  return nullptr;
297  }
298 
299  if ( !d->mGeos )
300  {
301  d->mGeos = QgsGeos::asGeos( d->geometry, precision );
302  }
303  return d->mGeos;
304 }
305 
306 
308 {
309  if ( !d->geometry )
310  {
311  return QGis::WKBUnknown;
312  }
313  else
314  {
315  return QGis::fromNewWkbType( d->geometry->wkbType() );
316  }
317 }
318 
319 
321 {
322  if ( !d->geometry )
323  {
324  return QGis::UnknownGeometry;
325  }
326  return static_cast< QGis::GeometryType >( QgsWKBTypes::geometryType( d->geometry->wkbType() ) );
327 }
328 
330 {
331  if ( !d->geometry )
332  {
333  return false;
334  }
335  return QgsWKBTypes::isMultiType( d->geometry->wkbType() );
336 }
337 
338 void QgsGeometry::fromGeos( GEOSGeometry *geos )
339 {
340  detach( false );
341  delete d->geometry;
342  d->geometry = QgsGeos::fromGeos( geos );
343  d->mGeos = geos;
344 }
345 
346 QgsPoint QgsGeometry::closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist ) const
347 {
348  if ( !d->geometry )
349  {
350  return QgsPoint( 0, 0 );
351  }
352 
353  QgsPointV2 pt( point.x(), point.y() );
354  QgsVertexId id;
355 
356  QgsPointV2 vp = QgsGeometryUtils::closestVertex( *( d->geometry ), pt, id );
357  if ( !id.isValid() )
358  {
359  sqrDist = -1;
360  return QgsPoint( 0, 0 );
361  }
362  sqrDist = QgsGeometryUtils::sqrDistance2D( pt, vp );
363 
364  atVertex = vertexNrFromVertexId( id );
365  adjacentVertices( atVertex, beforeVertex, afterVertex );
366  return QgsPoint( vp.x(), vp.y() );
367 }
368 
369 void QgsGeometry::adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex ) const
370 {
371  if ( !d->geometry )
372  {
373  return;
374  }
375 
376  QgsVertexId id;
377  if ( !vertexIdFromVertexNr( atVertex, id ) )
378  {
379  beforeVertex = -1;
380  afterVertex = -1;
381  return;
382  }
383 
384  QgsVertexId beforeVertexId, afterVertexId;
385  QgsGeometryUtils::adjacentVertices( *( d->geometry ), id, beforeVertexId, afterVertexId );
386  beforeVertex = vertexNrFromVertexId( beforeVertexId );
387  afterVertex = vertexNrFromVertexId( afterVertexId );
388 }
389 
390 bool QgsGeometry::moveVertex( double x, double y, int atVertex )
391 {
392  if ( !d->geometry )
393  {
394  return false;
395  }
396 
397  QgsVertexId id;
398  if ( !vertexIdFromVertexNr( atVertex, id ) )
399  {
400  return false;
401  }
402 
403  detach( true );
404 
405  removeWkbGeos();
406  return d->geometry->moveVertex( id, QgsPointV2( x, y ) );
407 }
408 
409 bool QgsGeometry::moveVertex( const QgsPointV2& p, int atVertex )
410 {
411  if ( !d->geometry )
412  {
413  return false;
414  }
415 
416  QgsVertexId id;
417  if ( !vertexIdFromVertexNr( atVertex, id ) )
418  {
419  return false;
420  }
421 
422  detach( true );
423 
424  removeWkbGeos();
425  return d->geometry->moveVertex( id, p );
426 }
427 
428 bool QgsGeometry::deleteVertex( int atVertex )
429 {
430  if ( !d->geometry )
431  {
432  return false;
433  }
434 
435  //maintain compatibility with < 2.10 API
436  if ( d->geometry->geometryType() == "MultiPoint" )
437  {
438  detach( true );
439  removeWkbGeos();
440  //delete geometry instead of point
441  return static_cast< QgsGeometryCollectionV2* >( d->geometry )->removeGeometry( atVertex );
442  }
443 
444  //if it is a point, set the geometry to nullptr
446  {
447  detach( false );
448  delete d->geometry;
449  removeWkbGeos();
450  d->geometry = nullptr;
451  return true;
452  }
453 
454  QgsVertexId id;
455  if ( !vertexIdFromVertexNr( atVertex, id ) )
456  {
457  return false;
458  }
459 
460  detach( true );
461 
462  removeWkbGeos();
463  return d->geometry->deleteVertex( id );
464 }
465 
466 bool QgsGeometry::insertVertex( double x, double y, int beforeVertex )
467 {
468  if ( !d->geometry )
469  {
470  return false;
471  }
472 
473  //maintain compatibility with < 2.10 API
474  if ( d->geometry->geometryType() == "MultiPoint" )
475  {
476  detach( true );
477  removeWkbGeos();
478  //insert geometry instead of point
479  return static_cast< QgsGeometryCollectionV2* >( d->geometry )->insertGeometry( new QgsPointV2( x, y ), beforeVertex );
480  }
481 
482  QgsVertexId id;
483  if ( !vertexIdFromVertexNr( beforeVertex, id ) )
484  {
485  return false;
486  }
487 
488  detach( true );
489 
490  removeWkbGeos();
491 
492  return d->geometry->insertVertex( id, QgsPointV2( x, y ) );
493 }
494 
495 QgsPoint QgsGeometry::vertexAt( int atVertex ) const
496 {
497  if ( !d->geometry )
498  {
499  return QgsPoint( 0, 0 );
500  }
501 
502  QgsVertexId vId;
503  ( void )vertexIdFromVertexNr( atVertex, vId );
504  if ( vId.vertex < 0 )
505  {
506  return QgsPoint( 0, 0 );
507  }
508  QgsPointV2 pt = d->geometry->vertexAt( vId );
509  return QgsPoint( pt.x(), pt.y() );
510 }
511 
512 double QgsGeometry::sqrDistToVertexAt( QgsPoint& point, int atVertex ) const
513 {
514  QgsPoint vertexPoint = vertexAt( atVertex );
515  return QgsGeometryUtils::sqrDistance2D( QgsPointV2( vertexPoint.x(), vertexPoint.y() ), QgsPointV2( point.x(), point.y() ) );
516 }
517 
519 {
520  QgsGeos geos( d->geometry );
521  return geos.closestPoint( other );
522 }
523 
525 {
526  QgsGeos geos( d->geometry );
527  return geos.shortestLine( other );
528 }
529 
530 double QgsGeometry::closestVertexWithContext( const QgsPoint& point, int& atVertex ) const
531 {
532  if ( !d->geometry )
533  {
534  return 0.0;
535  }
536 
537  QgsVertexId vId;
538  QgsPointV2 pt( point.x(), point.y() );
539  QgsPointV2 closestPoint = QgsGeometryUtils::closestVertex( *( d->geometry ), pt, vId );
540  atVertex = vertexNrFromVertexId( vId );
541  return QgsGeometryUtils::sqrDistance2D( closestPoint, pt );
542 }
543 
545  const QgsPoint& point,
546  QgsPoint& minDistPoint,
547  int& afterVertex,
548  double *leftOf,
549  double epsilon ) const
550 {
551  if ( !d->geometry )
552  {
553  return 0;
554  }
555 
556  QgsPointV2 segmentPt;
557  QgsVertexId vertexAfter;
558  bool leftOfBool;
559 
560  double sqrDist = d->geometry->closestSegment( QgsPointV2( point.x(), point.y() ), segmentPt, vertexAfter, &leftOfBool, epsilon );
561 
562  minDistPoint.setX( segmentPt.x() );
563  minDistPoint.setY( segmentPt.y() );
564  afterVertex = vertexNrFromVertexId( vertexAfter );
565  if ( leftOf )
566  {
567  *leftOf = leftOfBool ? 1.0 : -1.0;
568  }
569  return sqrDist;
570 }
571 
573 {
574  detach( true );
575 
576  removeWkbGeos();
577  QgsLineStringV2* ringLine = new QgsLineStringV2();
578  QgsPointSequenceV2 ringPoints;
579  convertPointList( ring, ringPoints );
580  ringLine->setPoints( ringPoints );
581  return addRing( ringLine );
582 }
583 
585 {
586  if ( !d->geometry )
587  {
588  delete ring;
589  return 1;
590  }
591 
592  detach( true );
593 
594  removeWkbGeos();
595  return QgsGeometryEditUtils::addRing( d->geometry, ring );
596 }
597 
599 {
601  convertPointList( points, l );
602  return addPart( l, geomType );
603 }
604 
606 {
607  QgsAbstractGeometryV2* partGeom = nullptr;
608  if ( points.size() == 1 )
609  {
610  partGeom = new QgsPointV2( points[0] );
611  }
612  else if ( points.size() > 1 )
613  {
614  QgsLineStringV2* ringLine = new QgsLineStringV2();
615  ringLine->setPoints( points );
616  partGeom = ringLine;
617  }
618  return addPart( partGeom, geomType );
619 }
620 
622 {
623  if ( !d->geometry )
624  {
625  detach( false );
626  switch ( geomType )
627  {
628  case QGis::Point:
629  d->geometry = new QgsMultiPointV2();
630  break;
631  case QGis::Line:
632  d->geometry = new QgsMultiLineStringV2();
633  break;
634  case QGis::Polygon:
635  d->geometry = new QgsMultiPolygonV2();
636  break;
637  default:
638  return 1;
639  }
640  }
641  else
642  {
643  detach( true );
644  removeWkbGeos();
645  }
646 
648  return QgsGeometryEditUtils::addPart( d->geometry, part );
649 }
650 
651 int QgsGeometry::addPart( const QgsGeometry *newPart )
652 {
653  if ( !d->geometry || !newPart || !newPart->d || !newPart->d->geometry )
654  {
655  return 1;
656  }
657 
658  return addPart( newPart->d->geometry->clone() );
659 }
660 
661 int QgsGeometry::addPart( GEOSGeometry *newPart )
662 {
663  if ( !d->geometry || !newPart )
664  {
665  return 1;
666  }
667 
668  detach( true );
669 
670  QgsAbstractGeometryV2* geom = QgsGeos::fromGeos( newPart );
671  removeWkbGeos();
672  return QgsGeometryEditUtils::addPart( d->geometry, geom );
673 }
674 
675 int QgsGeometry::translate( double dx, double dy )
676 {
677  if ( !d->geometry )
678  {
679  return 1;
680  }
681 
682  detach( true );
683 
685  removeWkbGeos();
686  return 0;
687 }
688 
689 int QgsGeometry::rotate( double rotation, const QgsPoint& center )
690 {
691  if ( !d->geometry )
692  {
693  return 1;
694  }
695 
696  detach( true );
697 
698  QTransform t = QTransform::fromTranslate( center.x(), center.y() );
699  t.rotate( -rotation );
700  t.translate( -center.x(), -center.y() );
701  d->geometry->transform( t );
702  removeWkbGeos();
703  return 0;
704 }
705 
706 int QgsGeometry::splitGeometry( const QList<QgsPoint>& splitLine, QList<QgsGeometry*>& newGeometries, bool topological, QList<QgsPoint> &topologyTestPoints )
707 {
708  if ( !d->geometry )
709  {
710  return 0;
711  }
712 
714  QgsLineStringV2 splitLineString;
715  QgsPointSequenceV2 splitLinePointsV2;
716  convertPointList( splitLine, splitLinePointsV2 );
717  splitLineString.setPoints( splitLinePointsV2 );
719 
720  QgsGeos geos( d->geometry );
721  int result = geos.splitGeometry( splitLineString, newGeoms, topological, tp );
722 
723  if ( result == 0 )
724  {
725  detach( false );
726  d->geometry = newGeoms.at( 0 );
727 
728  newGeometries.clear();
729  for ( int i = 1; i < newGeoms.size(); ++i )
730  {
731  newGeometries.push_back( new QgsGeometry( newGeoms.at( i ) ) );
732  }
733  }
734 
735  convertPointList( tp, topologyTestPoints );
736  removeWkbGeos();
737  return result;
738 }
739 
741 int QgsGeometry::reshapeGeometry( const QList<QgsPoint>& reshapeWithLine )
742 {
743  if ( !d->geometry )
744  {
745  return 0;
746  }
747 
748  QgsPointSequenceV2 reshapeLine;
749  convertPointList( reshapeWithLine, reshapeLine );
750  QgsLineStringV2 reshapeLineString;
751  reshapeLineString.setPoints( reshapeLine );
752 
753  QgsGeos geos( d->geometry );
754  int errorCode = 0;
755  QgsAbstractGeometryV2* geom = geos.reshapeGeometry( reshapeLineString, &errorCode );
756  if ( errorCode == 0 && geom )
757  {
758  detach( false );
759  delete d->geometry;
760  d->geometry = geom;
761  removeWkbGeos();
762  return 0;
763  }
764  return errorCode;
765 }
766 
768 {
769  if ( !d->geometry || !other->d->geometry )
770  {
771  return 0;
772  }
773 
774  QgsGeos geos( d->geometry );
775 
776  QgsAbstractGeometryV2* diffGeom = geos.intersection( *( other->geometry() ) );
777  if ( !diffGeom )
778  {
779  return 1;
780  }
781 
782  detach( false );
783 
784  delete d->geometry;
785  d->geometry = diffGeom;
786  removeWkbGeos();
787  return 0;
788 }
789 
791 {
792  if ( d->geometry )
793  {
794  return d->geometry->boundingBox();
795  }
796  return QgsRectangle();
797 }
798 
799 bool QgsGeometry::intersects( const QgsRectangle& r ) const
800 {
801  QgsGeometry* g = fromRect( r );
802  bool res = intersects( g );
803  delete g;
804  return res;
805 }
806 
808 {
809  if ( !d->geometry || !geometry || !geometry->d->geometry )
810  {
811  return false;
812  }
813 
814  QgsGeos geos( d->geometry );
815  return geos.intersects( *( geometry->d->geometry ) );
816 }
817 
818 bool QgsGeometry::contains( const QgsPoint* p ) const
819 {
820  if ( !d->geometry || !p )
821  {
822  return false;
823  }
824 
825  QgsPointV2 pt( p->x(), p->y() );
826  QgsGeos geos( d->geometry );
827  return geos.contains( pt );
828 }
829 
831 {
832  if ( !d->geometry || !geometry || !geometry->d->geometry )
833  {
834  return false;
835  }
836 
837  QgsGeos geos( d->geometry );
838  return geos.contains( *( geometry->d->geometry ) );
839 }
840 
842 {
843  if ( !d->geometry || !geometry || !geometry->d->geometry )
844  {
845  return false;
846  }
847 
848  QgsGeos geos( d->geometry );
849  return geos.disjoint( *( geometry->d->geometry ) );
850 }
851 
853 {
854  if ( !d->geometry || !geometry || !geometry->d->geometry )
855  {
856  return false;
857  }
858 
859  QgsGeos geos( d->geometry );
860  return geos.isEqual( *( geometry->d->geometry ) );
861 }
862 
864 {
865  if ( !d->geometry || !geometry || !geometry->d->geometry )
866  {
867  return false;
868  }
869 
870  QgsGeos geos( d->geometry );
871  return geos.touches( *( geometry->d->geometry ) );
872 }
873 
875 {
876  if ( !d->geometry || !geometry || !geometry->d->geometry )
877  {
878  return false;
879  }
880 
881  QgsGeos geos( d->geometry );
882  return geos.overlaps( *( geometry->d->geometry ) );
883 }
884 
886 {
887  if ( !d->geometry || !geometry || !geometry->d->geometry )
888  {
889  return false;
890  }
891 
892  QgsGeos geos( d->geometry );
893  return geos.within( *( geometry->d->geometry ) );
894 }
895 
897 {
898  if ( !d->geometry || !geometry || !geometry->d->geometry )
899  {
900  return false;
901  }
902 
903  QgsGeos geos( d->geometry );
904  return geos.crosses( *( geometry->d->geometry ) );
905 }
906 
907 QString QgsGeometry::exportToWkt( int precision ) const
908 {
909  if ( !d->geometry )
910  {
911  return QString();
912  }
913  return d->geometry->asWkt( precision );
914 }
915 
916 QString QgsGeometry::exportToGeoJSON( int precision ) const
917 {
918  if ( !d->geometry )
919  {
920  return QString( "null" );
921  }
922  return d->geometry->asJSON( precision );
923 }
924 
925 QgsGeometry* QgsGeometry::convertToType( QGis::GeometryType destType, bool destMultipart ) const
926 {
927  switch ( destType )
928  {
929  case QGis::Point:
930  return convertToPoint( destMultipart );
931 
932  case QGis::Line:
933  return convertToLine( destMultipart );
934 
935  case QGis::Polygon:
936  return convertToPolygon( destMultipart );
937 
938  default:
939  return nullptr;
940  }
941 }
942 
944 {
945  if ( !d->geometry )
946  {
947  return false;
948  }
949 
950  if ( isMultipart() ) //already multitype, no need to convert
951  {
952  return true;
953  }
954 
955  QgsGeometryCollectionV2* multiGeom = dynamic_cast<QgsGeometryCollectionV2*>
957  if ( !multiGeom )
958  {
959  return false;
960  }
961 
962  detach( true );
963  multiGeom->addGeometry( d->geometry );
964  d->geometry = multiGeom;
965  removeWkbGeos();
966  return true;
967 }
968 
970 {
971  if ( !d->geometry )
972  {
973  return false;
974  }
975 
976  if ( !isMultipart() ) //already single part, no need to convert
977  {
978  return true;
979  }
980 
981  QgsGeometryCollectionV2* multiGeom = dynamic_cast<QgsGeometryCollectionV2*>( d->geometry );
982  if ( !multiGeom || multiGeom->partCount() < 1 )
983  return false;
984 
985  QgsAbstractGeometryV2* firstPart = multiGeom->geometryN( 0 )->clone();
986  detach( false );
987 
988  d->geometry = firstPart;
989  removeWkbGeos();
990  return true;
991 }
992 
994 {
995  if ( !d->geometry || d->geometry->geometryType() != "Point" )
996  {
997  return QgsPoint();
998  }
999  QgsPointV2* pt = dynamic_cast<QgsPointV2*>( d->geometry );
1000  if ( !pt )
1001  {
1002  return QgsPoint();
1003  }
1004 
1005  return QgsPoint( pt->x(), pt->y() );
1006 }
1007 
1009 {
1010  QgsPolyline polyLine;
1011  if ( !d->geometry )
1012  {
1013  return polyLine;
1014  }
1015 
1016  bool doSegmentation = ( d->geometry->geometryType() == "CompoundCurve" || d->geometry->geometryType() == "CircularString" );
1017  QgsLineStringV2* line = nullptr;
1018  if ( doSegmentation )
1019  {
1020  QgsCurveV2* curve = dynamic_cast<QgsCurveV2*>( d->geometry );
1021  if ( !curve )
1022  {
1023  return polyLine;
1024  }
1025  line = curve->curveToLine();
1026  }
1027  else
1028  {
1029  line = dynamic_cast<QgsLineStringV2*>( d->geometry );
1030  if ( !line )
1031  {
1032  return polyLine;
1033  }
1034  }
1035 
1036  int nVertices = line->numPoints();
1037  polyLine.resize( nVertices );
1038  for ( int i = 0; i < nVertices; ++i )
1039  {
1040  QgsPointV2 pt = line->pointN( i );
1041  polyLine[i].setX( pt.x() );
1042  polyLine[i].setY( pt.y() );
1043  }
1044 
1045  if ( doSegmentation )
1046  {
1047  delete line;
1048  }
1049 
1050  return polyLine;
1051 }
1052 
1054 {
1055  if ( !d->geometry )
1056  return QgsPolygon();
1057 
1058  bool doSegmentation = ( d->geometry->geometryType() == "CurvePolygon" );
1059 
1060  QgsPolygonV2* p = nullptr;
1061  if ( doSegmentation )
1062  {
1063  QgsCurvePolygonV2* curvePoly = dynamic_cast<QgsCurvePolygonV2*>( d->geometry );
1064  if ( !curvePoly )
1065  {
1066  return QgsPolygon();
1067  }
1068  p = curvePoly->toPolygon();
1069  }
1070  else
1071  {
1072  p = dynamic_cast<QgsPolygonV2*>( d->geometry );
1073  }
1074 
1075  if ( !p )
1076  {
1077  return QgsPolygon();
1078  }
1079 
1080  QgsPolygon polygon;
1081  convertPolygon( *p, polygon );
1082 
1083  if ( doSegmentation )
1084  {
1085  delete p;
1086  }
1087  return polygon;
1088 }
1089 
1091 {
1092  if ( !d->geometry || d->geometry->geometryType() != "MultiPoint" )
1093  {
1094  return QgsMultiPoint();
1095  }
1096 
1097  const QgsMultiPointV2* mp = dynamic_cast<QgsMultiPointV2*>( d->geometry );
1098  if ( !mp )
1099  {
1100  return QgsMultiPoint();
1101  }
1102 
1103  int nPoints = mp->numGeometries();
1104  QgsMultiPoint multiPoint( nPoints );
1105  for ( int i = 0; i < nPoints; ++i )
1106  {
1107  const QgsPointV2* pt = static_cast<const QgsPointV2*>( mp->geometryN( i ) );
1108  multiPoint[i].setX( pt->x() );
1109  multiPoint[i].setY( pt->y() );
1110  }
1111  return multiPoint;
1112 }
1113 
1115 {
1116  if ( !d->geometry )
1117  {
1118  return QgsMultiPolyline();
1119  }
1120 
1121  QgsGeometryCollectionV2* geomCollection = dynamic_cast<QgsGeometryCollectionV2*>( d->geometry );
1122  if ( !geomCollection )
1123  {
1124  return QgsMultiPolyline();
1125  }
1126 
1127  int nLines = geomCollection->numGeometries();
1128  if ( nLines < 1 )
1129  {
1130  return QgsMultiPolyline();
1131  }
1132 
1133  QgsMultiPolyline mpl;
1134  for ( int i = 0; i < nLines; ++i )
1135  {
1136  bool deleteLine = false;
1137  const QgsLineStringV2* line = dynamic_cast<const QgsLineStringV2*>( geomCollection->geometryN( i ) );
1138  if ( !line )
1139  {
1140  const QgsCurveV2* curve = dynamic_cast<const QgsCurveV2*>( geomCollection->geometryN( i ) );
1141  if ( !curve )
1142  {
1143  continue;
1144  }
1145  deleteLine = true;
1146  line = curve->curveToLine();
1147  }
1148 
1149  QgsPointSequenceV2 lineCoords;
1150  line->points( lineCoords );
1151  QgsPolyline polyLine;
1152  convertToPolyline( lineCoords, polyLine );
1153  mpl.append( polyLine );
1154 
1155  if ( deleteLine )
1156  {
1157  delete line;
1158  }
1159  }
1160  return mpl;
1161 }
1162 
1164 {
1165  if ( !d->geometry )
1166  {
1167  return QgsMultiPolygon();
1168  }
1169 
1170  QgsGeometryCollectionV2* geomCollection = dynamic_cast<QgsGeometryCollectionV2*>( d->geometry );
1171  if ( !geomCollection )
1172  {
1173  return QgsMultiPolygon();
1174  }
1175 
1176  int nPolygons = geomCollection->numGeometries();
1177  if ( nPolygons < 1 )
1178  {
1179  return QgsMultiPolygon();
1180  }
1181 
1182  QgsMultiPolygon mp;
1183  for ( int i = 0; i < nPolygons; ++i )
1184  {
1185  const QgsPolygonV2* polygon = dynamic_cast<const QgsPolygonV2*>( geomCollection->geometryN( i ) );
1186  if ( !polygon )
1187  {
1188  const QgsCurvePolygonV2* cPolygon = dynamic_cast<const QgsCurvePolygonV2*>( geomCollection->geometryN( i ) );
1189  if ( cPolygon )
1190  {
1191  polygon = cPolygon->toPolygon();
1192  }
1193  else
1194  {
1195  continue;
1196  }
1197  }
1198 
1199  QgsPolygon poly;
1200  convertPolygon( *polygon, poly );
1201  mp.append( poly );
1202  }
1203  return mp;
1204 }
1205 
1206 double QgsGeometry::area() const
1207 {
1208  if ( !d->geometry )
1209  {
1210  return -1.0;
1211  }
1212  QgsGeos g( d->geometry );
1213 
1214 #if 0
1215  //debug: compare geos area with calculation in QGIS
1216  double geosArea = g.area();
1217  double qgisArea = 0;
1218  QgsSurfaceV2* surface = dynamic_cast<QgsSurfaceV2*>( d->geometry );
1219  if ( surface )
1220  {
1221  qgisArea = surface->area();
1222  }
1223 #endif
1224 
1225  return g.area();
1226 }
1227 
1228 double QgsGeometry::length() const
1229 {
1230  if ( !d->geometry )
1231  {
1232  return -1.0;
1233  }
1234  QgsGeos g( d->geometry );
1235  return g.length();
1236 }
1237 
1238 double QgsGeometry::distance( const QgsGeometry& geom ) const
1239 {
1240  if ( !d->geometry || !geom.d->geometry )
1241  {
1242  return -1.0;
1243  }
1244 
1245  QgsGeos g( d->geometry );
1246  return g.distance( *( geom.d->geometry ) );
1247 }
1248 
1249 QgsGeometry* QgsGeometry::buffer( double distance, int segments ) const
1250 {
1251  if ( !d->geometry )
1252  {
1253  return nullptr;
1254  }
1255 
1256  QgsGeos g( d->geometry );
1257  QgsAbstractGeometryV2* geom = g.buffer( distance, segments );
1258  if ( !geom )
1259  {
1260  return nullptr;
1261  }
1262  return new QgsGeometry( geom );
1263 }
1264 
1265 QgsGeometry* QgsGeometry::buffer( double distance, int segments, int endCapStyle, int joinStyle, double mitreLimit ) const
1266 {
1267  if ( !d->geometry )
1268  {
1269  return nullptr;
1270  }
1271 
1272  QgsGeos g( d->geometry );
1273  QgsAbstractGeometryV2* geom = g.buffer( distance, segments, endCapStyle, joinStyle, mitreLimit );
1274  if ( !geom )
1275  {
1276  return nullptr;
1277  }
1278  return new QgsGeometry( geom );
1279 }
1280 
1281 QgsGeometry* QgsGeometry::offsetCurve( double distance, int segments, int joinStyle, double mitreLimit ) const
1282 {
1283  if ( !d->geometry )
1284  {
1285  return nullptr;
1286  }
1287 
1288  QgsGeos geos( d->geometry );
1289  QgsAbstractGeometryV2* offsetGeom = geos.offsetCurve( distance, segments, joinStyle, mitreLimit );
1290  if ( !offsetGeom )
1291  {
1292  return nullptr;
1293  }
1294  return new QgsGeometry( offsetGeom );
1295 }
1296 
1297 QgsGeometry* QgsGeometry::simplify( double tolerance ) const
1298 {
1299  if ( !d->geometry )
1300  {
1301  return nullptr;
1302  }
1303 
1304  QgsGeos geos( d->geometry );
1305  QgsAbstractGeometryV2* simplifiedGeom = geos.simplify( tolerance );
1306  if ( !simplifiedGeom )
1307  {
1308  return nullptr;
1309  }
1310  return new QgsGeometry( simplifiedGeom );
1311 }
1312 
1314 {
1315  if ( !d->geometry )
1316  {
1317  return nullptr;
1318  }
1319 
1320  QgsGeos geos( d->geometry );
1322  bool ok = geos.centroid( centroid );
1323  if ( !ok )
1324  {
1325  return nullptr;
1326  }
1327  return new QgsGeometry( centroid.clone() );
1328 }
1329 
1331 {
1332  if ( !d->geometry )
1333  {
1334  return nullptr;
1335  }
1336 
1337  QgsGeos geos( d->geometry );
1338  QgsPointV2 pt;
1339  bool ok = geos.pointOnSurface( pt );
1340  if ( !ok )
1341  {
1342  return nullptr;
1343  }
1344  return new QgsGeometry( pt.clone() );
1345 }
1346 
1348 {
1349  if ( !d->geometry )
1350  {
1351  return nullptr;
1352  }
1353  QgsGeos geos( d->geometry );
1354  QgsAbstractGeometryV2* cHull = geos.convexHull();
1355  if ( !cHull )
1356  {
1357  return nullptr;
1358  }
1359  return new QgsGeometry( cHull );
1360 }
1361 
1363 {
1364  if ( !d->geometry )
1365  {
1366  return nullptr;
1367  }
1368  QgsGeos geos( d->geometry );
1369  QgsAbstractGeometryV2* result = geos.interpolate( distance );
1370  if ( !result )
1371  {
1372  return nullptr;
1373  }
1374  return new QgsGeometry( result );
1375 }
1376 
1378 {
1379  if ( !d->geometry || !geometry->d->geometry )
1380  {
1381  return nullptr;
1382  }
1383 
1384  QgsGeos geos( d->geometry );
1385 
1386  QgsAbstractGeometryV2* resultGeom = geos.intersection( *( geometry->d->geometry ) );
1387  return new QgsGeometry( resultGeom );
1388 }
1389 
1391 {
1392  if ( !d->geometry || !geometry->d->geometry )
1393  {
1394  return nullptr;
1395  }
1396 
1397  QgsGeos geos( d->geometry );
1398 
1399  QgsAbstractGeometryV2* resultGeom = geos.combine( *( geometry->d->geometry ) );
1400  if ( !resultGeom )
1401  {
1402  return nullptr;
1403  }
1404  return new QgsGeometry( resultGeom );
1405 }
1406 
1408 {
1409  if ( !d->geometry || !geometry->d->geometry )
1410  {
1411  return nullptr;
1412  }
1413 
1414  QgsGeos geos( d->geometry );
1415 
1416  QgsAbstractGeometryV2* resultGeom = geos.difference( *( geometry->d->geometry ) );
1417  if ( !resultGeom )
1418  {
1419  return nullptr;
1420  }
1421  return new QgsGeometry( resultGeom );
1422 }
1423 
1425 {
1426  if ( !d->geometry || !geometry->d->geometry )
1427  {
1428  return nullptr;
1429  }
1430 
1431  QgsGeos geos( d->geometry );
1432 
1433  QgsAbstractGeometryV2* resultGeom = geos.symDifference( *( geometry->d->geometry ) );
1434  if ( !resultGeom )
1435  {
1436  return nullptr;
1437  }
1438  return new QgsGeometry( resultGeom );
1439 }
1440 
1441 QgsGeometry QgsGeometry::extrude( double x, double y )
1442 {
1443  QgsInternalGeometryEngine engine( *this );
1444 
1445  return engine.extrude( x, y );
1446 }
1447 
1449 {
1450  QList<QgsGeometry*> geometryList;
1451  if ( !d->geometry )
1452  {
1453  return geometryList;
1454  }
1455 
1456  QgsGeometryCollectionV2* gc = dynamic_cast<QgsGeometryCollectionV2*>( d->geometry );
1457  if ( gc )
1458  {
1459  int numGeom = gc->numGeometries();
1460  geometryList.reserve( numGeom );
1461  for ( int i = 0; i < numGeom; ++i )
1462  {
1463  geometryList.append( new QgsGeometry( gc->geometryN( i )->clone() ) );
1464  }
1465  }
1466  else //a singlepart geometry
1467  {
1468  geometryList.append( new QgsGeometry( d->geometry->clone() ) );
1469  }
1470 
1471  return geometryList;
1472 }
1473 
1475 {
1476  QgsPoint point = asPoint();
1477  return point.toQPointF();
1478 }
1479 
1481 {
1482  QPolygonF result;
1483  QgsPolyline polyline;
1485  if ( type == QGis::WKBLineString || type == QGis::WKBLineString25D )
1486  {
1487  polyline = asPolyline();
1488  }
1489  else if ( type == QGis::WKBPolygon || type == QGis::WKBPolygon25D )
1490  {
1491  QgsPolygon polygon = asPolygon();
1492  if ( polygon.size() < 1 )
1493  return result;
1494  polyline = polygon.at( 0 );
1495  }
1496  else
1497  {
1498  return result;
1499  }
1500 
1501  QgsPolyline::const_iterator lineIt = polyline.constBegin();
1502  for ( ; lineIt != polyline.constEnd(); ++lineIt )
1503  {
1504  result << lineIt->toQPointF();
1505  }
1506  return result;
1507 }
1508 
1509 bool QgsGeometry::deleteRing( int ringNum, int partNum )
1510 {
1511  if ( !d->geometry )
1512  {
1513  return false;
1514  }
1515 
1516  detach( true );
1517  bool ok = QgsGeometryEditUtils::deleteRing( d->geometry, ringNum, partNum );
1518  removeWkbGeos();
1519  return ok;
1520 }
1521 
1522 bool QgsGeometry::deletePart( int partNum )
1523 {
1524  if ( !d->geometry )
1525  {
1526  return false;
1527  }
1528 
1529  if ( !isMultipart() && partNum < 1 )
1530  {
1531  setGeometry( nullptr );
1532  return true;
1533  }
1534 
1535  detach( true );
1536  bool ok = QgsGeometryEditUtils::deletePart( d->geometry, partNum );
1537  removeWkbGeos();
1538  return ok;
1539 }
1540 
1542 {
1543  if ( !d->geometry )
1544  {
1545  return 1;
1546  }
1547 
1548  QgsAbstractGeometryV2* diffGeom = QgsGeometryEditUtils::avoidIntersections( *( d->geometry ), ignoreFeatures );
1549  if ( diffGeom )
1550  {
1551  detach( false );
1552  d->geometry = diffGeom;
1553  removeWkbGeos();
1554  }
1555  return 0;
1556 }
1557 
1559 {
1561 }
1562 
1564 {
1565  if ( !d->geometry )
1566  {
1567  return false;
1568  }
1569 
1570  QgsGeos geos( d->geometry );
1571  return geos.isValid();
1572 }
1573 
1575 {
1576  if ( !d->geometry || !g.d->geometry )
1577  {
1578  return false;
1579  }
1580 
1581  QgsGeos geos( d->geometry );
1582  return geos.isEqual( *( g.d->geometry ) );
1583 }
1584 
1586 {
1587  if ( !d->geometry )
1588  {
1589  return false;
1590  }
1591 
1592  QgsGeos geos( d->geometry );
1593  return geos.isEmpty();
1594 }
1595 
1597 {
1598  QgsGeos geos( nullptr );
1599 
1600  QList<QgsAbstractGeometryV2*> geomV2List;
1601  QList<QgsGeometry*>::const_iterator it = geometryList.constBegin();
1602  for ( ; it != geometryList.constEnd(); ++it )
1603  {
1604  if ( *it && !(( *it )->isEmpty() ) )
1605  {
1606  geomV2List.append(( *it )->geometry() );
1607  }
1608  }
1609 
1610  QgsAbstractGeometryV2* geom = geos.combine( geomV2List );
1611  return new QgsGeometry( geom );
1612 }
1613 
1615 {
1617  {
1618  return;
1619  }
1620 
1621  QgsAbstractGeometryV2* straightGeom = d->geometry->segmentize();
1622  detach( false );
1623 
1624  d->geometry = straightGeom;
1625  removeWkbGeos();
1626 }
1627 
1629 {
1630  if ( !d->geometry )
1631  {
1632  return false;
1633  }
1634 
1635  return d->geometry->hasCurvedSegments();
1636 }
1637 
1639 {
1640  if ( !d->geometry )
1641  {
1642  return 1;
1643  }
1644 
1645  detach();
1646  d->geometry->transform( ct );
1647  removeWkbGeos();
1648  return 0;
1649 }
1650 
1652 {
1653  if ( !d->geometry )
1654  {
1655  return 1;
1656  }
1657 
1658  detach();
1659  d->geometry->transform( ct );
1660  removeWkbGeos();
1661  return 0;
1662 }
1663 
1665 {
1666  if ( d->geometry )
1667  {
1668  detach();
1669  d->geometry->transform( mtp.transform() );
1670  removeWkbGeos();
1671  }
1672 }
1673 
1674 #if 0
1675 void QgsGeometry::clip( const QgsRectangle& rect )
1676 {
1677  if ( d->geometry )
1678  {
1679  detach();
1680  d->geometry->clip( rect );
1681  removeWkbGeos();
1682  }
1683 }
1684 #endif
1685 
1686 void QgsGeometry::draw( QPainter& p ) const
1687 {
1688  if ( d->geometry )
1689  {
1690  d->geometry->draw( p );
1691  }
1692 }
1693 
1695 {
1696  if ( !d->geometry )
1697  {
1698  return false;
1699  }
1700 
1702 
1703  int vertexCount = 0;
1704  for ( int part = 0; part < coords.size(); ++part )
1705  {
1706  const QgsRingSequenceV2 &featureCoords = coords.at( part );
1707  for ( int ring = 0; ring < featureCoords.size(); ++ring )
1708  {
1709  const QgsPointSequenceV2 &ringCoords = featureCoords.at( ring );
1710  for ( int vertex = 0; vertex < ringCoords.size(); ++vertex )
1711  {
1712  if ( vertexCount == nr )
1713  {
1714  id.part = part;
1715  id.ring = ring;
1716  id.vertex = vertex;
1717  return true;
1718  }
1719  ++vertexCount;
1720  }
1721  }
1722  }
1723  return false;
1724 }
1725 
1727 {
1728  if ( !d->geometry )
1729  {
1730  return -1;
1731  }
1732 
1734 
1735  int vertexCount = 0;
1736  for ( int part = 0; part < coords.size(); ++part )
1737  {
1738  const QgsRingSequenceV2 &featureCoords = coords.at( part );
1739  for ( int ring = 0; ring < featureCoords.size(); ++ring )
1740  {
1741  const QgsPointSequenceV2 &ringCoords = featureCoords.at( ring );
1742  for ( int vertex = 0; vertex < ringCoords.size(); ++vertex )
1743  {
1744  if ( vertex == id.vertex && ring == id.ring && part == id.part )
1745  {
1746  return vertexCount;
1747  }
1748  ++vertexCount;
1749  }
1750  }
1751  }
1752  return -1;
1753 }
1754 
1756 {
1757  output.clear();
1759  for ( ; it != input.constEnd(); ++it )
1760  {
1761  output.append( QgsPointV2( it->x(), it->y() ) );
1762  }
1763 }
1764 
1766 {
1767  output.clear();
1769  for ( ; it != input.constEnd(); ++it )
1770  {
1771  output.append( QgsPoint( it->x(), it->y() ) );
1772  }
1773 }
1774 
1775 void QgsGeometry::convertToPolyline( const QgsPointSequenceV2 &input, QgsPolyline& output )
1776 {
1777  output.clear();
1778  output.resize( input.size() );
1779 
1780  for ( int i = 0; i < input.size(); ++i )
1781  {
1782  const QgsPointV2& pt = input.at( i );
1783  output[i].setX( pt.x() );
1784  output[i].setY( pt.y() );
1785  }
1786 }
1787 
1788 void QgsGeometry::convertPolygon( const QgsPolygonV2& input, QgsPolygon& output )
1789 {
1790  output.clear();
1792  if ( coords.size() < 1 )
1793  {
1794  return;
1795  }
1796  const QgsRingSequenceV2 &rings = coords[0];
1797  output.resize( rings.size() );
1798  for ( int i = 0; i < rings.size(); ++i )
1799  {
1800  convertToPolyline( rings[i], output[i] );
1801  }
1802 }
1803 
1804 GEOSContextHandle_t QgsGeometry::getGEOSHandler()
1805 {
1806  return QgsGeos::getGEOSHandler();
1807 }
1808 
1810 {
1811  return new QgsGeometry( new QgsPointV2( point.x(), point.y() ) );
1812 }
1813 
1815 {
1816  if ( polygon.isClosed() )
1817  {
1819  }
1820  else
1821  {
1823  }
1824 }
1825 
1827 {
1828  QgsPolygon result;
1829  result << createPolylineFromQPolygonF( polygon );
1830  return result;
1831 }
1832 
1834 {
1835  QgsPolyline result;
1836  QPolygonF::const_iterator it = polygon.constBegin();
1837  for ( ; it != polygon.constEnd(); ++it )
1838  {
1839  result.append( QgsPoint( *it ) );
1840  }
1841  return result;
1842 }
1843 
1844 bool QgsGeometry::compare( const QgsPolyline &p1, const QgsPolyline &p2, double epsilon )
1845 {
1846  if ( p1.count() != p2.count() )
1847  return false;
1848 
1849  for ( int i = 0; i < p1.count(); ++i )
1850  {
1851  if ( !p1.at( i ).compare( p2.at( i ), epsilon ) )
1852  return false;
1853  }
1854  return true;
1855 }
1856 
1857 bool QgsGeometry::compare( const QgsPolygon &p1, const QgsPolygon &p2, double epsilon )
1858 {
1859  if ( p1.count() != p2.count() )
1860  return false;
1861 
1862  for ( int i = 0; i < p1.count(); ++i )
1863  {
1864  if ( !QgsGeometry::compare( p1.at( i ), p2.at( i ), epsilon ) )
1865  return false;
1866  }
1867  return true;
1868 }
1869 
1870 
1871 bool QgsGeometry::compare( const QgsMultiPolygon &p1, const QgsMultiPolygon &p2, double epsilon )
1872 {
1873  if ( p1.count() != p2.count() )
1874  return false;
1875 
1876  for ( int i = 0; i < p1.count(); ++i )
1877  {
1878  if ( !QgsGeometry::compare( p1.at( i ), p2.at( i ), epsilon ) )
1879  return false;
1880  }
1881  return true;
1882 }
1883 
1884 QgsGeometry* QgsGeometry::smooth( const unsigned int iterations, const double offset ) const
1885 {
1886  switch ( wkbType() )
1887  {
1888  case QGis::WKBPoint:
1889  case QGis::WKBPoint25D:
1890  case QGis::WKBMultiPoint:
1892  //can't smooth a point based geometry
1893  return new QgsGeometry( *this );
1894 
1895  case QGis::WKBLineString:
1897  {
1898  QgsPolyline line = asPolyline();
1899  return QgsGeometry::fromPolyline( smoothLine( line, iterations, offset ) );
1900  }
1901 
1904  {
1905  QgsMultiPolyline multiline = asMultiPolyline();
1906  QgsMultiPolyline resultMultiline;
1907  QgsMultiPolyline::const_iterator lineIt = multiline.constBegin();
1908  for ( ; lineIt != multiline.constEnd(); ++lineIt )
1909  {
1910  resultMultiline << smoothLine( *lineIt, iterations, offset );
1911  }
1912  return QgsGeometry::fromMultiPolyline( resultMultiline );
1913  }
1914 
1915  case QGis::WKBPolygon:
1916  case QGis::WKBPolygon25D:
1917  {
1918  QgsPolygon poly = asPolygon();
1919  return QgsGeometry::fromPolygon( smoothPolygon( poly, iterations, offset ) );
1920  }
1921 
1922  case QGis::WKBMultiPolygon:
1924  {
1925  QgsMultiPolygon multipoly = asMultiPolygon();
1926  QgsMultiPolygon resultMultipoly;
1927  QgsMultiPolygon::const_iterator polyIt = multipoly.constBegin();
1928  for ( ; polyIt != multipoly.constEnd(); ++polyIt )
1929  {
1930  resultMultipoly << smoothPolygon( *polyIt, iterations, offset );
1931  }
1932  return QgsGeometry::fromMultiPolygon( resultMultipoly );
1933  }
1934 
1935  case QGis::WKBUnknown:
1936  default:
1937  return new QgsGeometry( *this );
1938  }
1939 }
1940 
1941 inline QgsPoint interpolatePointOnLine( const QgsPoint& p1, const QgsPoint& p2, const double offset )
1942 {
1943  double deltaX = p2.x() - p1.x();
1944  double deltaY = p2.y() - p1.y();
1945  return QgsPoint( p1.x() + deltaX * offset, p1.y() + deltaY * offset );
1946 }
1947 
1948 QgsPolyline QgsGeometry::smoothLine( const QgsPolyline& polyline, const unsigned int iterations, const double offset ) const
1949 {
1950  QgsPolyline result = polyline;
1951  for ( unsigned int iteration = 0; iteration < iterations; ++iteration )
1952  {
1953  QgsPolyline outputLine = QgsPolyline();
1954  outputLine.reserve( 2 * ( result.count() - 1 ) );
1955  for ( int i = 0; i < result.count() - 1; i++ )
1956  {
1957  const QgsPoint& p1 = result.at( i );
1958  const QgsPoint& p2 = result.at( i + 1 );
1959  outputLine << ( i == 0 ? result.at( i ) : interpolatePointOnLine( p1, p2, offset ) );
1960  outputLine << ( i == result.count() - 2 ? result.at( i + 1 ) : interpolatePointOnLine( p1, p2, 1.0 - offset ) );
1961  }
1962  result = outputLine;
1963  }
1964  return result;
1965 }
1966 
1967 QgsPolygon QgsGeometry::smoothPolygon( const QgsPolygon& polygon, const unsigned int iterations, const double offset ) const
1968 {
1969  QgsPolygon resultPoly;
1970  QgsPolygon::const_iterator ringIt = polygon.constBegin();
1971  for ( ; ringIt != polygon.constEnd(); ++ringIt )
1972  {
1973  QgsPolyline resultRing = *ringIt;
1974  for ( unsigned int iteration = 0; iteration < iterations; ++iteration )
1975  {
1976  QgsPolyline outputRing = QgsPolyline();
1977  outputRing.reserve( 2 * ( resultRing.count() - 1 ) + 1 );
1978  for ( int i = 0; i < resultRing.count() - 1; ++i )
1979  {
1980  const QgsPoint& p1 = resultRing.at( i );
1981  const QgsPoint& p2 = resultRing.at( i + 1 );
1982  outputRing << interpolatePointOnLine( p1, p2, offset );
1983  outputRing << interpolatePointOnLine( p1, p2, 1.0 - offset );
1984  }
1985  //close polygon
1986  outputRing << outputRing.at( 0 );
1987 
1988  resultRing = outputRing;
1989  }
1990  resultPoly << resultRing;
1991  }
1992  return resultPoly;
1993 }
1994 
1995 QgsGeometry* QgsGeometry::convertToPoint( bool destMultipart ) const
1996 {
1997  switch ( type() )
1998  {
1999  case QGis::Point:
2000  {
2001  bool srcIsMultipart = isMultipart();
2002 
2003  if (( destMultipart && srcIsMultipart ) ||
2004  ( !destMultipart && !srcIsMultipart ) )
2005  {
2006  // return a copy of the same geom
2007  return new QgsGeometry( *this );
2008  }
2009  if ( destMultipart )
2010  {
2011  // layer is multipart => make a multipoint with a single point
2012  return fromMultiPoint( QgsMultiPoint() << asPoint() );
2013  }
2014  else
2015  {
2016  // destination is singlepart => make a single part if possible
2017  QgsMultiPoint multiPoint = asMultiPoint();
2018  if ( multiPoint.count() == 1 )
2019  {
2020  return fromPoint( multiPoint[0] );
2021  }
2022  }
2023  return nullptr;
2024  }
2025 
2026  case QGis::Line:
2027  {
2028  // only possible if destination is multipart
2029  if ( !destMultipart )
2030  return nullptr;
2031 
2032  // input geometry is multipart
2033  if ( isMultipart() )
2034  {
2035  QgsMultiPolyline multiLine = asMultiPolyline();
2036  QgsMultiPoint multiPoint;
2037  for ( QgsMultiPolyline::const_iterator multiLineIt = multiLine.constBegin(); multiLineIt != multiLine.constEnd(); ++multiLineIt )
2038  for ( QgsPolyline::const_iterator lineIt = ( *multiLineIt ).constBegin(); lineIt != ( *multiLineIt ).constEnd(); ++lineIt )
2039  multiPoint << *lineIt;
2040  return fromMultiPoint( multiPoint );
2041  }
2042  // input geometry is not multipart: copy directly the line into a multipoint
2043  else
2044  {
2045  QgsPolyline line = asPolyline();
2046  if ( !line.isEmpty() )
2047  return fromMultiPoint( line );
2048  }
2049  return nullptr;
2050  }
2051 
2052  case QGis::Polygon:
2053  {
2054  // can only transform if destination is multipoint
2055  if ( !destMultipart )
2056  return nullptr;
2057 
2058  // input geometry is multipart: make a multipoint from multipolygon
2059  if ( isMultipart() )
2060  {
2061  QgsMultiPolygon multiPolygon = asMultiPolygon();
2062  QgsMultiPoint multiPoint;
2063  for ( QgsMultiPolygon::const_iterator polygonIt = multiPolygon.constBegin(); polygonIt != multiPolygon.constEnd(); ++polygonIt )
2064  for ( QgsMultiPolyline::const_iterator multiLineIt = ( *polygonIt ).constBegin(); multiLineIt != ( *polygonIt ).constEnd(); ++multiLineIt )
2065  for ( QgsPolyline::const_iterator lineIt = ( *multiLineIt ).constBegin(); lineIt != ( *multiLineIt ).constEnd(); ++lineIt )
2066  multiPoint << *lineIt;
2067  return fromMultiPoint( multiPoint );
2068  }
2069  // input geometry is not multipart: make a multipoint from polygon
2070  else
2071  {
2072  QgsPolygon polygon = asPolygon();
2073  QgsMultiPoint multiPoint;
2074  for ( QgsMultiPolyline::const_iterator multiLineIt = polygon.constBegin(); multiLineIt != polygon.constEnd(); ++multiLineIt )
2075  for ( QgsPolyline::const_iterator lineIt = ( *multiLineIt ).constBegin(); lineIt != ( *multiLineIt ).constEnd(); ++lineIt )
2076  multiPoint << *lineIt;
2077  return fromMultiPoint( multiPoint );
2078  }
2079  }
2080 
2081  default:
2082  return nullptr;
2083  }
2084 }
2085 
2086 QgsGeometry* QgsGeometry::convertToLine( bool destMultipart ) const
2087 {
2088  switch ( type() )
2089  {
2090  case QGis::Point:
2091  {
2092  if ( !isMultipart() )
2093  return nullptr;
2094 
2095  QgsMultiPoint multiPoint = asMultiPoint();
2096  if ( multiPoint.count() < 2 )
2097  return nullptr;
2098 
2099  if ( destMultipart )
2100  return fromMultiPolyline( QgsMultiPolyline() << multiPoint );
2101  else
2102  return fromPolyline( multiPoint );
2103  }
2104 
2105  case QGis::Line:
2106  {
2107  bool srcIsMultipart = isMultipart();
2108 
2109  if (( destMultipart && srcIsMultipart ) ||
2110  ( !destMultipart && ! srcIsMultipart ) )
2111  {
2112  // return a copy of the same geom
2113  return new QgsGeometry( *this );
2114  }
2115  if ( destMultipart )
2116  {
2117  // destination is multipart => makes a multipoint with a single line
2118  QgsPolyline line = asPolyline();
2119  if ( !line.isEmpty() )
2120  return fromMultiPolyline( QgsMultiPolyline() << line );
2121  }
2122  else
2123  {
2124  // destination is singlepart => make a single part if possible
2125  QgsMultiPolyline multiLine = asMultiPolyline();
2126  if ( multiLine.count() == 1 )
2127  return fromPolyline( multiLine[0] );
2128  }
2129  return nullptr;
2130  }
2131 
2132  case QGis::Polygon:
2133  {
2134  // input geometry is multipolygon
2135  if ( isMultipart() )
2136  {
2137  QgsMultiPolygon multiPolygon = asMultiPolygon();
2138  QgsMultiPolyline multiLine;
2139  for ( QgsMultiPolygon::const_iterator polygonIt = multiPolygon.constBegin(); polygonIt != multiPolygon.constEnd(); ++polygonIt )
2140  for ( QgsMultiPolyline::const_iterator multiLineIt = ( *polygonIt ).constBegin(); multiLineIt != ( *polygonIt ).constEnd(); ++multiLineIt )
2141  multiLine << *multiLineIt;
2142 
2143  if ( destMultipart )
2144  {
2145  // destination is multipart
2146  return fromMultiPolyline( multiLine );
2147  }
2148  else if ( multiLine.count() == 1 )
2149  {
2150  // destination is singlepart => make a single part if possible
2151  return fromPolyline( multiLine[0] );
2152  }
2153  }
2154  // input geometry is single polygon
2155  else
2156  {
2157  QgsPolygon polygon = asPolygon();
2158  // if polygon has rings
2159  if ( polygon.count() > 1 )
2160  {
2161  // cannot fit a polygon with rings in a single line layer
2162  // TODO: would it be better to remove rings?
2163  if ( destMultipart )
2164  {
2165  QgsPolygon polygon = asPolygon();
2166  QgsMultiPolyline multiLine;
2167  for ( QgsMultiPolyline::const_iterator multiLineIt = polygon.constBegin(); multiLineIt != polygon.constEnd(); ++multiLineIt )
2168  multiLine << *multiLineIt;
2169  return fromMultiPolyline( multiLine );
2170  }
2171  }
2172  // no rings
2173  else if ( polygon.count() == 1 )
2174  {
2175  if ( destMultipart )
2176  {
2177  return fromMultiPolyline( polygon );
2178  }
2179  else
2180  {
2181  return fromPolyline( polygon[0] );
2182  }
2183  }
2184  }
2185  return nullptr;
2186  }
2187 
2188  default:
2189  return nullptr;
2190  }
2191 }
2192 
2193 QgsGeometry* QgsGeometry::convertToPolygon( bool destMultipart ) const
2194 {
2195  switch ( type() )
2196  {
2197  case QGis::Point:
2198  {
2199  if ( !isMultipart() )
2200  return nullptr;
2201 
2202  QgsMultiPoint multiPoint = asMultiPoint();
2203  if ( multiPoint.count() < 3 )
2204  return nullptr;
2205 
2206  if ( multiPoint.last() != multiPoint.first() )
2207  multiPoint << multiPoint.first();
2208 
2209  QgsPolygon polygon = QgsPolygon() << multiPoint;
2210  if ( destMultipart )
2211  return fromMultiPolygon( QgsMultiPolygon() << polygon );
2212  else
2213  return fromPolygon( polygon );
2214  }
2215 
2216  case QGis::Line:
2217  {
2218  // input geometry is multiline
2219  if ( isMultipart() )
2220  {
2221  QgsMultiPolyline multiLine = asMultiPolyline();
2222  QgsMultiPolygon multiPolygon;
2223  for ( QgsMultiPolyline::iterator multiLineIt = multiLine.begin(); multiLineIt != multiLine.end(); ++multiLineIt )
2224  {
2225  // do not create polygon for a 1 segment line
2226  if (( *multiLineIt ).count() < 3 )
2227  return nullptr;
2228  if (( *multiLineIt ).count() == 3 && ( *multiLineIt ).first() == ( *multiLineIt ).last() )
2229  return nullptr;
2230 
2231  // add closing node
2232  if (( *multiLineIt ).first() != ( *multiLineIt ).last() )
2233  *multiLineIt << ( *multiLineIt ).first();
2234  multiPolygon << ( QgsPolygon() << *multiLineIt );
2235  }
2236  // check that polygons were inserted
2237  if ( !multiPolygon.isEmpty() )
2238  {
2239  if ( destMultipart )
2240  {
2241  return fromMultiPolygon( multiPolygon );
2242  }
2243  else if ( multiPolygon.count() == 1 )
2244  {
2245  // destination is singlepart => make a single part if possible
2246  return fromPolygon( multiPolygon[0] );
2247  }
2248  }
2249  }
2250  // input geometry is single line
2251  else
2252  {
2253  QgsPolyline line = asPolyline();
2254 
2255  // do not create polygon for a 1 segment line
2256  if ( line.count() < 3 )
2257  return nullptr;
2258  if ( line.count() == 3 && line.first() == line.last() )
2259  return nullptr;
2260 
2261  // add closing node
2262  if ( line.first() != line.last() )
2263  line << line.first();
2264 
2265  // destination is multipart
2266  if ( destMultipart )
2267  {
2268  return fromMultiPolygon( QgsMultiPolygon() << ( QgsPolygon() << line ) );
2269  }
2270  else
2271  {
2272  return fromPolygon( QgsPolygon() << line );
2273  }
2274  }
2275  return nullptr;
2276  }
2277 
2278  case QGis::Polygon:
2279  {
2280  bool srcIsMultipart = isMultipart();
2281 
2282  if (( destMultipart && srcIsMultipart ) ||
2283  ( !destMultipart && ! srcIsMultipart ) )
2284  {
2285  // return a copy of the same geom
2286  return new QgsGeometry( *this );
2287  }
2288  if ( destMultipart )
2289  {
2290  // destination is multipart => makes a multipoint with a single polygon
2291  QgsPolygon polygon = asPolygon();
2292  if ( !polygon.isEmpty() )
2293  return fromMultiPolygon( QgsMultiPolygon() << polygon );
2294  }
2295  else
2296  {
2297  QgsMultiPolygon multiPolygon = asMultiPolygon();
2298  if ( multiPolygon.count() == 1 )
2299  {
2300  // destination is singlepart => make a single part if possible
2301  return fromPolygon( multiPolygon[0] );
2302  }
2303  }
2304  return nullptr;
2305  }
2306 
2307  default:
2308  return nullptr;
2309  }
2310 }
2311 
2313 {
2314  return new QgsGeos( geometry );
2315 }
2316 
2318 {
2319  QByteArray byteArray = QByteArray::fromRawData( reinterpret_cast< const char * >( geometry.asWkb() ), geometry.wkbSize() ); // does not copy data and does not take ownership
2320  out << byteArray;
2321  return out;
2322 }
2323 
2325 {
2326  QByteArray byteArray;
2327  in >> byteArray;
2328  if ( byteArray.isEmpty() )
2329  {
2330  geometry.setGeometry( nullptr );
2331  return in;
2332  }
2333 
2334  char *data = new char[byteArray.size()];
2335  memcpy( data, byteArray.data(), byteArray.size() );
2336  geometry.fromWkb( reinterpret_cast< unsigned char* >( data ), byteArray.size() );
2337  return in;
2338 }
QgsPolygon asPolygon() const
Return contents of the geometry as a polygon if wkbType is WKBPolygon, otherwise an empty list...
QTransform fromTranslate(qreal dx, qreal dy)
static QgsGeometry * fromQPolygonF(const QPolygonF &polygon)
Construct geometry from a QPolygonF.
void clear()
QgsMultiPolyline asMultiPolyline() const
Return contents of the geometry as a multi linestring if wkbType is WKBMultiLineString, otherwise an empty list.
A rectangle specified with double values.
Definition: qgsrectangle.h:35
int numGeometries() const
Returns the number of geometries within the collection.
QGis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
QgsPoint asPoint() const
Return contents of the geometry as a point if wkbType is WKBPoint, otherwise returns [0...
bool intersects(const QgsRectangle &r) const
Test for intersection with a rectangle (uses GEOS)
QgsGeometry * symDifference(const QgsGeometry *geometry) const
Returns a geometry representing the points making up this geometry that do not make up other...
QDataStream & operator<<(QDataStream &out, const QgsGeometry &geometry)
Writes the geometry to stream out.
GeometryType
Definition: qgis.h:111
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
virtual QgsCoordinateSequenceV2 coordinateSequence() const =0
Retrieves the sequence of geometries, rings and nodes.
double distance(const QgsGeometry &geom) const
Returns the minimum distance between this geometry and another geometry, using GEOS.
QgsAbstractGeometryV2 * combine(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:188
QPointF toQPointF() const
Converts a point to a QPointF.
Definition: qgspoint.cpp:121
int reshapeGeometry(const QList< QgsPoint > &reshapeWithLine)
Replaces a part of this geometry with another line.
void append(const T &value)
iterator begin()
void push_back(const T &value)
void fromWkb(unsigned char *wkb, int length)
Set the geometry, feeding in the buffer containing OGC Well-Known Binary and the buffer&#39;s length...
bool isEmpty(QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:1453
void points(QgsPointSequenceV2 &pt) const override
Returns a list of points within the curve.
bool touches(const QgsGeometry *geometry) const
Test for if geometry touch another (uses GEOS)
typedef iterator
static bool compare(const QgsPolyline &p1, const QgsPolyline &p2, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compares two polylines for equality within a specified tolerance.
void reserve(int alloc)
QgsGeometry shortestLine(const QgsGeometry &other, QString *errorMsg=nullptr) const
Returns the shortest line joining this geometry to the other geometry.
Definition: qgsgeos.cpp:1811
double area(QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:345
QVector< QgsPoint > QgsPolyline
Polyline is represented as a vector of points.
Definition: qgsgeometry.h:44
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:240
virtual QgsCoordinateSequenceV2 coordinateSequence() const override
Retrieves the sequence of geometries, rings and nodes.
const_iterator constEnd() const
const T & at(int i) const
T & last()
virtual QString asJSON(int precision=17) const =0
Returns a GeoJSON representation of the geometry.
static QGis::WkbType fromNewWkbType(QgsWKBTypes::Type type)
Converts from new (post 2.10) WKB type (OGC) to old WKB type.
Definition: qgis.cpp:144
void setGeometry(QgsAbstractGeometryV2 *geometry)
Sets the underlying geometry store.
static bool isMultiType(Type type)
Returns true if the WKB type is a multi type.
Definition: qgswkbtypes.h:476
virtual bool hasCurvedSegments() const
Returns true if the geometry contains curved segments.
int makeDifference(const QgsGeometry *other)
Changes this geometry such that it does not intersect the other geometry.
bool moveVertex(double x, double y, int atVertex)
Moves the vertex at the given position number and item (first number is index 0) to the given coordin...
static QgsAbstractGeometryV2 * fromPolygon(const QgsPolygon &polygon)
Construct geometry from a polygon.
Abstract base class for all geometries.
double length(QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:362
bool ref()
int addPart(const QList< QgsPoint > &points, QGis::GeometryType geomType=QGis::UnknownGeometry)
Adds a new part to a the geometry.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
static QgsAbstractGeometryV2 * fromGeos(const GEOSGeometry *geos)
Create a geometry from a GEOSGeometry.
Definition: qgsgeos.cpp:862
static QgsPointV2 closestVertex(const QgsAbstractGeometryV2 &geom, const QgsPointV2 &pt, QgsVertexId &id)
Returns the closest vertex to a geometry for a specified point.
QByteArray fromRawData(const char *data, int size)
WkbType
Used for symbology operations.
Definition: qgis.h:57
QgsGeometry()
Constructor.
Definition: qgsgeometry.cpp:65
QgsAbstractGeometryV2 * symDifference(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:217
bool isValid(QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:1419
bool isGeosEmpty() const
Check if the geometry is empty using GEOS.
T & first()
Multi point geometry collection.
QgsAbstractGeometryV2 * simplify(double tolerance, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:1301
static QgsAbstractGeometryV2 * fromPolyline(const QgsPolyline &polyline)
Construct geometry from a polyline.
bool contains(const QgsPoint *p) const
Test for containment of a point (uses GEOS)
static QgsAbstractGeometryV2 * fromPoint(const QgsPoint &point)
Construct geometry from a point.
static QgsAbstractGeometryV2 * fromMultiPolygon(const QgsMultiPolygon &multipoly)
Construct geometry from a multipolygon.
double y() const
Returns the point&#39;s y-coordinate.
Definition: qgspointv2.h:74
QgsGeometry closestPoint(const QgsGeometry &other, QString *errorMsg=nullptr) const
Returns the closest point on the geometry to the other geometry.
Definition: qgsgeos.cpp:1776
bool insertVertex(double x, double y, int beforeVertex)
Insert a new vertex before the given vertex index, ring and item (first number is index 0) If the req...
static QgsAbstractGeometryV2 * geomFromWkb(QgsConstWkbPtr wkb)
Construct geometry from a WKB string.
QgsGeometry nearestPoint(const QgsGeometry &other) const
Returns the nearest point on this geometry to another geometry.
QgsGeometry & operator=(QgsGeometry const &rhs)
Assignments will prompt a deep copy of the object.
Definition: qgsgeometry.cpp:87
int wkbSize() const
Returns the size of the WKB in asWkb().
Multi line string geometry collection.
static QgsPolyline createPolylineFromQPolygonF(const QPolygonF &polygon)
Creates a QgsPolyline from a QPolygonF.
QgsPolyline smoothLine(const QgsPolyline &polyline, const unsigned int iterations=1, const double offset=0.25) const
Smooths a polyline using the Chaikin algorithm.
bool isGeosEqual(const QgsGeometry &) const
Compares the geometry with another geometry using GEOS.
static int addPart(QgsAbstractGeometryV2 *geom, QgsAbstractGeometryV2 *part)
Adds part to multi type geometry (taking ownership)
bool deleteRing(int ringNum, int partNum=0)
Delete a ring in polygon or multipolygon.
bool isGeosValid() const
Checks validity of the geometry using GEOS.
int splitGeometry(const QgsLineStringV2 &splitLine, QList< QgsAbstractGeometryV2 *> &newGeometries, bool topological, QgsPointSequenceV2 &topologyTestPoints, QString *errorMsg=nullptr) const override
Splits this geometry according to a given line.
Definition: qgsgeos.cpp:378
QgsPolyline asPolyline() const
Return contents of the geometry as a polyline if wkbType is WKBLineString, otherwise an empty list...
static GEOSContextHandle_t getGEOSHandler()
Definition: qgsgeos.cpp:2285
int size() const
double y() const
Get the y value of the point.
Definition: qgspoint.h:136
QgsGeometry * centroid() const
Returns the center of mass of a geometry.
QgsAbstractGeometryV2 * interpolate(double distance, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:1316
QgsGeometry * difference(const QgsGeometry *geometry) const
Returns a geometry representing the points making up this geometry that do not make up other...
static QgsAbstractGeometryV2 * geomFromWkt(const QString &text)
Construct geometry from a WKT string.
bool within(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:262
bool deletePart(int partNum)
Delete part identified by the part number.
bool contains(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:272
QgsMultiPoint asMultiPoint() const
Return contents of the geometry as a multi point if wkbType is WKBMultiPoint, otherwise an empty list...
QgsPoint transform(const QgsPoint &p) const
Transform the point from map (world) coordinates to device coordinates.
bool intersects(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:247
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
Polygon geometry type.
Definition: qgspolygonv2.h:29
void clear()
QgsGeometry * convexHull() const
Returns the smallest convex polygon that contains all the points in the geometry. ...
QTransform & translate(qreal dx, qreal dy)
static QgsGeometry * fromQPointF(QPointF point)
Construct geometry from a QPointF.
static QgsAbstractGeometryV2 * fromMultiPoint(const QgsMultiPoint &multipoint)
Construct geometry from a multipoint.
qreal x() const
qreal y() const
const QgsAbstractGeometryV2 * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
void append(const T &value)
void resize(int size)
int avoidIntersections(const QMap< QgsVectorLayer *, QSet< QgsFeatureId > > &ignoreFeatures=(QMap< QgsVectorLayer *, QSet< QgsFeatureId > >()))
Modifies geometry to avoid intersections with the layers specified in project properties.
static double sqrDistance2D(const QgsPointV2 &pt1, const QgsPointV2 &pt2)
Returns the squared 2D distance between two points.
bool convertToSingleType()
Converts multi type geometry into single type geometry e.g.
QgsGeometry * pointOnSurface() const
Returns a point within a geometry.
Utility class for identifying a unique vertex within a geometry.
Line string geometry type, with support for z-dimension and m-values.
double sqrDistToVertexAt(QgsPoint &point, int atVertex) const
Returns the squared cartesian distance between the given point to the given vertex index (vertex at t...
QgsPoint interpolatePointOnLine(const QgsPoint &p1, const QgsPoint &p2, const double offset)
void setPoints(const QgsPointSequenceV2 &points)
Resets the line string to match the specified list of points.
QgsGeometry * buffer(double distance, int segments) const
Returns a buffer region around this geometry having the given width and with a specified number of se...
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:34
QgsGeometry extrude(double x, double y)
Will extrude a line or (segmentized) curve by a given offset and return a polygon representation of i...
bool deleteVertex(int atVertex)
Deletes the vertex at the given position number and item (first number is index 0) Returns false if a...
QgsGeometry * smooth(const unsigned int iterations=1, const double offset=0.25) const
Smooths a geometry by rounding off corners using the Chaikin algorithm.
QgsAbstractGeometryV2 * intersection(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:178
QgsGeometry * interpolate(double distance) const
Return interpolated point on line at distance.
bool equals(const QgsGeometry *geometry) const
Test for if geometry equals another (uses GEOS)
bool crosses(const QgsGeometry *geometry) const
Test for if geometry crosses another (uses GEOS)
QVector< QgsPolygon > QgsMultiPolygon
A collection of QgsPolygons that share a common collection of attributes.
Definition: qgsgeometry.h:59
QVector< QgsPoint > QgsMultiPoint
A collection of QgsPoints that share a common collection of attributes.
Definition: qgsgeometry.h:53
void fromGeos(GEOSGeometry *geos)
Set the geometry, feeding in a geometry in GEOS format.
virtual unsigned char * asWkb(int &binarySize) const =0
Returns a WKB representation of the geometry.
bool isEmpty() const
Returns true if the geometry is empty (ie, contains no underlying geometry accessible via geometry)...
double x() const
Returns the point&#39;s x-coordinate.
Definition: qgspointv2.h:68
virtual QgsPointV2 * clone() const override
Clones the geometry by performing a deep copy.
Definition: qgspointv2.cpp:94
const unsigned char * asWkb() const
Returns the buffer containing this geometry in WKB format.
static void adjacentVertices(const QgsAbstractGeometryV2 &geom, QgsVertexId atVertex, QgsVertexId &beforeVertex, QgsVertexId &afterVertex)
Returns vertices adjacent to a specified vertex within a geometry.
virtual void transform(const QgsCoordinateTransform &ct, QgsCoordinateTransform::TransformDirection d=QgsCoordinateTransform::ForwardTransform)=0
Transforms the geometry using a coordinate transform.
QString exportToWkt(int precision=17) const
Exports the geometry to WKT.
virtual int partCount() const override
Returns count of parts contained in the geometry.
bool disjoint(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:277
QVector< QgsPolyline > QgsPolygon
Polygon: first item of the list is outer ring, inner rings (if any) start from second item...
Definition: qgsgeometry.h:50
bool within(const QgsGeometry *geometry) const
Test for if geometry is within another (uses GEOS)
QGis::GeometryType type() const
Returns type of the geometry as a QGis::GeometryType.
void validateGeometry(QList< Error > &errors)
Validate geometry and produce a list of geometry errors.
void convertToStraightSegment()
Converts the geometry to straight line segments, if it is a curved geometry type. ...
static QgsAbstractGeometryV2 * geomFromWkbType(QgsWKBTypes::Type t)
Return empty geometry from wkb type.
Does vector analysis using the geos library and handles import, export, exception handling*...
Definition: qgsgeos.h:31
int splitGeometry(const QList< QgsPoint > &splitLine, QList< QgsGeometry *> &newGeometries, bool topological, QList< QgsPoint > &topologyTestPoints)
Splits this geometry according to a given line.
bool deref()
A class to represent a point.
Definition: qgspoint.h:65
bool isClosed() const
QgsGeometry * convertToType(QGis::GeometryType destType, bool destMultipart=false) const
Try to convert the geometry to the requested type.
static QgsGeometry * fromPoint(const QgsPoint &point)
Creates a new geometry from a QgsPoint object.
int translate(double dx, double dy)
Translate this geometry by dx, dy.
QgsGeometry * combine(const QgsGeometry *geometry) const
Returns a geometry representing all the points in this geometry and other (a union geometry operation...
double length() const
Returns the length of geometry using GEOS.
This class offers geometry processing methods.
QgsGeometry * simplify(double tolerance) const
Returns a simplified version of this geometry using a specified tolerance value.
bool touches(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:252
static GeometryType geometryType(Type type)
Returns the geometry type for a WKB type, eg both MultiPolygon and CurvePolygon would have a PolygonG...
Definition: qgswkbtypes.h:573
void reserve(int size)
QgsGeometry extrude(double x, double y)
Returns an extruded version of this geometry.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometryV2 *geometry)
Creates and returns a new geometry engine.
static GEOSGeometry * asGeos(const QgsAbstractGeometryV2 *geom, double precision=0)
Definition: qgsgeos.cpp:1051
void draw(QPainter &p) const
Draws the geometry onto a QPainter.
QgsAbstractGeometryV2 * reshapeGeometry(const QgsLineStringV2 &reshapeWithLine, int *errorCode, QString *errorMsg=nullptr) const
Definition: qgsgeos.cpp:1668
void setX(double x)
Sets the x value of the point.
Definition: qgspoint.h:105
virtual QgsRectangle boundingBox() const =0
Returns the minimal bounding box for the geometry.
virtual QString geometryType() const =0
Returns a unique string representing the geometry type.
QList< QgsGeometry * > asGeometryCollection() const
Return contents of the geometry as a list of geometries.
void setY(double y)
Sets the y value of the point.
Definition: qgspoint.h:113
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:202
static void validateGeometry(const QgsGeometry *g, QList< QgsGeometry::Error > &errors)
Validate geometry and produce a list of geometry errors.
QPolygonF asQPolygonF() const
Return contents of the geometry as a QPolygonF.
QVector< QgsPolyline > QgsMultiPolyline
A collection of QgsPolylines that share a common collection of attributes.
Definition: qgsgeometry.h:56
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:187
bool crosses(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:257
static QgsGeometry * fromMultiPolyline(const QgsMultiPolyline &multiline)
Creates a new geometry from a QgsMultiPolyline object.
static QgsGeometry * unaryUnion(const QList< QgsGeometry *> &geometryList)
Compute the unary union on a list of geometries.
static bool deletePart(QgsAbstractGeometryV2 *geom, int partNum)
Deletes a part from a geometry.
int vertexNrFromVertexId(QgsVertexId i) const
Returns the vertex number corresponding to a vertex idd.
QTransform & rotate(qreal angle, Qt::Axis axis)
QgsPolygon smoothPolygon(const QgsPolygon &polygon, const unsigned int iterations=1, const double offset=0.25) const
Smooths a polygon using the Chaikin algorithm.
static bool deleteRing(QgsAbstractGeometryV2 *geom, int ringNum, int partNum=0)
Deletes a ring from a geometry.
static QgsAbstractGeometryV2 * avoidIntersections(const QgsAbstractGeometryV2 &geom, QMap< QgsVectorLayer *, QSet< QgsFeatureId > > ignoreFeatures=(QMap< QgsVectorLayer *, QSet< QgsFeatureId > >()))
Alters a geometry so that it avoids intersections with features from all open vector layers...
double closestSegmentWithContext(const QgsPoint &point, QgsPoint &minDistPoint, int &afterVertex, double *leftOf=nullptr, double epsilon=DEFAULT_SEGMENT_EPSILON) const
Searches for the closest segment of geometry to the given point.
static QgsPolygon createPolygonFromQPolygonF(const QPolygonF &polygon)
Creates a QgsPolygon from a QPolygonF.
const T & at(int i) const
virtual QgsLineStringV2 * curveToLine() const =0
Returns a new line string geometry corresponding to a segmentized approximation of the curve...
const_iterator constBegin() const
QgsPoint vertexAt(int atVertex) const
Returns coordinates of a vertex.
virtual bool deleteVertex(QgsVertexId position)=0
Deletes a vertex within the geometry.
bool convertToMultiType()
Converts single type geometry into multitype geometry e.g.
QgsAbstractGeometryV2 * geometry() const
Returns the underlying geometry store.
static GEOSContextHandle_t getGEOSHandler()
Return GEOS context handle.
QgsGeometry * offsetCurve(double distance, int segments, int joinStyle, double mitreLimit) const
Returns an offset line at a given distance and side from an input line.
virtual bool insertVertex(QgsVertexId position, const QgsPointV2 &vertex)=0
Inserts a vertex into the geometry.
bool requiresConversionToStraightSegments() const
Returns true if the geometry is a curved geometry type which requires conversion to display as straig...
void mapToPixel(const QgsMapToPixel &mtp)
Transforms the geometry from map units to pixels in place.
QString exportToGeoJSON(int precision=17) const
Exports the geometry to GeoJSON.
bool isEmpty() const
QgsWKBTypes::Type wkbType() const
Returns the WKB type of the geometry.
QgsMultiPolygon asMultiPolygon() const
Return contents of the geometry as a multi polygon if wkbType is WKBMultiPolygon, otherwise an empty ...
bool centroid(QgsPointV2 &pt, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:1331
bool vertexIdFromVertexNr(int nr, QgsVertexId &id) const
Calculates the vertex ID from a vertex number.
const unsigned char * mWkb
Definition: qgsgeometry.cpp:60
int rotate(double rotation, const QgsPoint &center)
Rotate this geometry around the Z axis.
QPointF asQPointF() const
Return contents of the geometry as a QPointF if wkbType is WKBPoint, otherwise returns a null QPointF...
const GEOSGeometry * asGeos(double precision=0) const
Returns a geos geometry.
QgsRectangle boundingBox() const
Returns the bounding box of this feature.
QgsGeometry shortestLine(const QgsGeometry &other) const
Returns the shortest line joining this geometry to another geometry.
virtual bool addGeometry(QgsAbstractGeometryV2 *g)
Adds a geometry and takes ownership.
virtual QgsPolygonV2 * toPolygon() const
int count(const T &value) const
bool isEqual(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:1433
QgsGeometry * intersection(const QgsGeometry *geometry) const
Returns a geometry representing the points shared by this geometry and other.
Class for doing transforms between two map coordinate systems.
virtual QString asWkt(int precision=17) const =0
Returns a WKT representation of the geometry.
static QgsGeometry * fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
virtual QgsAbstractGeometryV2 * segmentize() const
Returns a version of the geometry without curves.
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:192
int transform(const QgsCoordinateTransform &ct)
Transform this geometry as described by CoordinateTransform ct.
static QgsGeometry * fromMultiPoint(const QgsMultiPoint &multipoint)
Creates a new geometry from a QgsMultiPoint object.
static QgsAbstractGeometryV2 * fromMultiPolyline(const QgsMultiPolyline &multiline)
Construct geometry from a multipolyline.
bool pointOnSurface(QgsPointV2 &pt, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:1373
static QgsGeometry * fromPolyline(const QgsPolyline &polyline)
Creates a new geometry from a QgsPolyline object.
void adjacentVertices(int atVertex, int &beforeVertex, int &afterVertex) const
Returns the indexes of the vertices before and after the given vertex index.
double distance(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:222
static int addRing(QgsAbstractGeometryV2 *geom, QgsCurveV2 *ring)
Adds interior ring (taking ownership).
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:366
Multi polygon geometry collection.
Contains geometry relation and modification algorithms.
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:197
bool disjoint(const QgsGeometry *geometry) const
Test for if geometry is disjoint of another (uses GEOS)
virtual void draw(QPainter &p) const =0
Draws the geometry using the specified QPainter.
static QgsGeometry * fromMultiPolygon(const QgsMultiPolygon &multipoly)
Creates a new geometry from a QgsMultiPolygon.
typedef const_iterator
GEOSGeometry * mGeos
Definition: qgsgeometry.cpp:62
static QgsGeometry * fromPolygon(const QgsPolygon &polygon)
Creates a new geometry from a QgsPolygon.
~QgsGeometry()
Destructor.
Definition: qgsgeometry.cpp:69
double ANALYSIS_EXPORT leftOf(Point3D *thepoint, Point3D *p1, Point3D *p2)
Returns whether &#39;thepoint&#39; is left or right of the line from &#39;p1&#39; to &#39;p2&#39;.
Curve polygon geometry type.
int addRing(const QList< QgsPoint > &ring)
Adds a new ring to this geometry.
bool overlaps(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:267
static QgsGeometry * fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
double area() const
Returns the area of the geometry using GEOS.
QgsAbstractGeometryV2 * difference(const QgsAbstractGeometryV2 &geom, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:183
const_iterator constEnd() const
const_iterator constBegin() const
virtual QgsAbstractGeometryV2 * clone() const =0
Clones the geometry by performing a deep copy.
Abstract base class for curved geometry type.
Definition: qgscurvev2.h:32
virtual bool moveVertex(QgsVertexId position, const QgsPointV2 &newPos)=0
Moves a vertex within the geometry.
int size() const
Represents a vector layer which manages a vector based data sets.
virtual double closestSegment(const QgsPointV2 &pt, QgsPointV2 &segmentPt, QgsVertexId &vertexAfter, bool *leftOf, double epsilon) const =0
Searches for the closest segment of the geometry to a given point.
QgsPoint closestVertex(const QgsPoint &point, int &atVertex, int &beforeVertex, int &afterVertex, double &sqrDist) const
Returns the vertex closest to the given point, the corresponding vertex index, squared distance snap ...
iterator end()
QgsAbstractGeometryV2 * offsetCurve(double distance, int segments, int joinStyle, double mitreLimit, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:1652
virtual double area() const
Returns the area of the geometry.
static void convertPointList(const QList< QgsPoint > &input, QgsPointSequenceV2 &output)
Upgrades a point list from QgsPoint to QgsPointV2.
double closestVertexWithContext(const QgsPoint &point, int &atVertex) const
Searches for the closest vertex in this geometry to the given point.
bool overlaps(const QgsGeometry *geometry) const
Test for if geometry overlaps another (uses GEOS)
QgsAbstractGeometryV2 * convexHull(QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:1402
double x() const
Get the x value of the point.
Definition: qgspoint.h:128
virtual QgsPointV2 vertexAt(QgsVertexId id) const =0
Returns the point corresponding to a specified vertex id.
QgsAbstractGeometryV2 * geometry
Definition: qgsgeometry.cpp:59
QgsAbstractGeometryV2 * buffer(double distance, int segments, QString *errorMsg=nullptr) const override
Definition: qgsgeos.cpp:1263
int numPoints() const override
Returns the number of points in the curve.
QDataStream & operator>>(QDataStream &in, QgsGeometry &geometry)
Reads a geometry from stream in into geometry.
QgsPointV2 pointN(int i) const
Returns the specified point from inside the line string.