Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgsgeometry.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometry.h - 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 /* $Id$ */
16 
17 #ifndef QGSGEOMETRY_H
18 #define QGSGEOMETRY_H
19 
20 #include <QString>
21 #include <QVector>
22 
23 #include "qgis.h"
24 
25 #include <geos_c.h>
26 
27 #if defined(GEOS_VERSION_MAJOR) && (GEOS_VERSION_MAJOR<3)
28 #define GEOSGeometry struct GEOSGeom_t
29 #define GEOSCoordSequence struct GEOSCoordSeq_t
30 #endif
31 
32 #include "qgspoint.h"
33 #include "qgscoordinatetransform.h"
34 
36 typedef QVector<QgsPoint> QgsPolyline;
37 
39 typedef QVector<QgsPolyline> QgsPolygon;
40 
42 typedef QVector<QgsPoint> QgsMultiPoint;
43 
45 typedef QVector<QgsPolyline> QgsMultiPolyline;
46 
48 typedef QVector<QgsPolygon> QgsMultiPolygon;
49 
50 class QgsRectangle;
51 
67 class CORE_EXPORT QgsGeometry
68 {
69  public:
71  QgsGeometry();
72 
74  QgsGeometry( QgsGeometry const & );
75 
77  QgsGeometry & operator=( QgsGeometry const & rhs );
78 
80  ~QgsGeometry();
81 
83  static QgsGeometry* fromWkt( QString wkt );
84 
86  static QgsGeometry* fromPoint( const QgsPoint& point );
88  static QgsGeometry* fromMultiPoint( const QgsMultiPoint& multipoint );
90  static QgsGeometry* fromPolyline( const QgsPolyline& polyline );
92  static QgsGeometry* fromMultiPolyline( const QgsMultiPolyline& multiline );
94  static QgsGeometry* fromPolygon( const QgsPolygon& polygon );
96  static QgsGeometry* fromMultiPolygon( const QgsMultiPolygon& multipoly );
98  static QgsGeometry* fromRect( const QgsRectangle& rect );
103  void fromGeos( GEOSGeometry* geos );
108  void fromWkb( unsigned char * wkb, size_t length );
109 
114  unsigned char * asWkb();
115 
119  size_t wkbSize();
120 
123  GEOSGeometry* asGeos();
124 
126  QGis::WkbType wkbType();
127 
129  QGis::GeometryType type();
130 
132  bool isMultipart();
133 
137  bool isGeosEqual( QgsGeometry & );
138 
142  bool isGeosValid();
143 
147  bool isGeosEmpty();
148 
152  double area();
153 
157  double length();
158 
159  double distance( QgsGeometry& geom );
160 
165  QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist );
166 
167 
180  void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex );
181 
182 
194  bool insertVertex( double x, double y, int beforeVertex );
195 
202  bool moveVertex( double x, double y, int atVertex );
203 
214  bool deleteVertex( int atVertex );
215 
221  QgsPoint vertexAt( int atVertex );
222 
229  double sqrDistToVertexAt( QgsPoint& point, int atVertex );
230 
237  double closestVertexWithContext( const QgsPoint& point, int& atVertex );
238 
247  double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& beforeVertex );
248 
252  int addRing( const QList<QgsPoint>& ring );
253 
257  int addIsland( const QList<QgsPoint>& ring );
258 
261  int translate( double dx, double dy );
262 
265  int transform( const QgsCoordinateTransform& ct );
266 
274  int splitGeometry( const QList<QgsPoint>& splitLine,
275  QList<QgsGeometry*>&newGeometries,
276  bool topological,
277  QList<QgsPoint> &topologyTestPoints );
278 
282  int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
283 
287  int makeDifference( QgsGeometry* other );
288 
290  QgsRectangle boundingBox();
291 
293  bool intersects( const QgsRectangle& r );
294 
296  bool intersects( QgsGeometry* geometry );
297 
299  bool contains( QgsPoint* p );
300 
303  bool contains( QgsGeometry* geometry );
304 
307  bool disjoint( QgsGeometry* geometry );
308 
311  bool equals( QgsGeometry* geometry );
312 
315  bool touches( QgsGeometry* geometry );
316 
319  bool overlaps( QgsGeometry* geometry );
320 
323  bool within( QgsGeometry* geometry );
324 
327  bool crosses( QgsGeometry* geometry );
328 
331  QgsGeometry* buffer( double distance, int segments );
332 
334  QgsGeometry* simplify( double tolerance );
335 
339  QgsGeometry* centroid();
340 
342  QgsGeometry* convexHull();
343 
345  QgsGeometry* intersection( QgsGeometry* geometry );
346 
350  QgsGeometry* combine( QgsGeometry* geometry );
351 
353  QgsGeometry* difference( QgsGeometry* geometry );
354 
356  QgsGeometry* symDifference( QgsGeometry* geometry );
357 
361  QString exportToWkt();
362 
363  /* Accessor functions for getting geometry data */
364 
367  QgsPoint asPoint();
368 
371  QgsPolyline asPolyline();
372 
375  QgsPolygon asPolygon();
376 
379  QgsMultiPoint asMultiPoint();
380 
383  QgsMultiPolyline asMultiPolyline();
384 
387  QgsMultiPolygon asMultiPolygon();
388 
391  QList<QgsGeometry*> asGeometryCollection();
392 
397  bool deleteRing( int ringNum, int partNum = 0 );
398 
402  bool deletePart( int partNum );
403 
407  bool convertToMultiType();
408 
416  int avoidIntersections();
417 
418 
419  class Error
420  {
421  QString message;
424  public:
425  Error( QString m ) : message( m ), hasLocation( false ) {}
426  Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
427 
428  QString what() { return message; };
429  QgsPoint where() { return location; }
430  bool hasWhere() { return hasLocation; }
431  };
432 
436  void validateGeometry( QList<Error> &errors );
437 
438  static void validatePolyline( QList<Error> &errors, int i, QgsPolyline polyline, bool ring = false );
439  static void validatePolygon( QList<Error> &errors, int i, const QgsPolygon &polygon );
440 
441  private:
442  // Private variables
443 
444  // All of these are mutable since there may be on-the-fly
445  // conversions between WKB, GEOS and Wkt;
446  // However the intent is the const functions do not
447  // semantically change the value that this object represents.
448 
452  unsigned char * mGeometry;
453 
456 
458  GEOSGeometry* mGeos;
459 
461  bool mDirtyWkb;
462 
465 
466 
467  // Private functions
468 
472  bool exportWkbToGeos();
473 
477  bool exportGeosToWkb();
478 
492  bool insertVertex( double x, double y,
493  int beforeVertex,
494  const GEOSCoordSequence* old_sequence,
495  GEOSCoordSequence** new_sequence );
496 
502  void translateVertex( int& wkbPosition, double dx, double dy, bool hasZValue );
503 
508  void transformVertex( int& wkbPosition, const QgsCoordinateTransform& ct, bool hasZValue );
509 
510  //helper functions for geometry splitting
511 
516  int splitLinearGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
519  int splitPolygonGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
522  int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPoint>& testPoints ) const;
523 
529  static GEOSGeometry* reshapeLine( const GEOSGeometry* origLine, const GEOSGeometry* reshapeLineGeos );
530 
536  static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos );
537 
540  static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *poly );
541 
544  static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
545 
550  static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
551 
553  static bool geomInDegrees( const GEOSGeometry* geom );
554 
556  int numberOfGeometries( GEOSGeometry* g ) const;
557 
558  int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult );
559 
561  QgsPoint asPoint( unsigned char*& ptr, bool hasZValue );
562 
564  QgsPolyline asPolyline( unsigned char*& ptr, bool hasZValue );
565 
567  QgsPolygon asPolygon( unsigned char*& ptr, bool hasZValue );
568 
569  static void checkRingIntersections( QList<Error> &errors,
570  int p0, int i0, const QgsPolyline &ring0,
571  int p1, int i1, const QgsPolyline &ring1 );
572 
573  static bool geosRelOp( char( *op )( const GEOSGeometry*, const GEOSGeometry * ),
574  QgsGeometry *a, QgsGeometry *b );
575 
576 
577  static int refcount;
578 }; // class QgsGeometry
579 
580 #endif