Quantum GIS API Documentation  1.7.4
src/core/qgsgeometry.h
Go to the documentation of this file.
00001 /***************************************************************************
00002   qgsgeometry.h - Geometry (stored as Open Geospatial Consortium WKB)
00003   -------------------------------------------------------------------
00004 Date                 : 02 May 2005
00005 Copyright            : (C) 2005 by Brendan Morley
00006 email                : morb at ozemail dot com dot au
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 /* $Id$ */
00016 
00017 #ifndef QGSGEOMETRY_H
00018 #define QGSGEOMETRY_H
00019 
00020 #include <QString>
00021 #include <QVector>
00022 
00023 #include "qgis.h"
00024 
00025 #include <geos_c.h>
00026 
00027 #if defined(GEOS_VERSION_MAJOR) && (GEOS_VERSION_MAJOR<3)
00028 #define GEOSGeometry struct GEOSGeom_t
00029 #define GEOSCoordSequence struct GEOSCoordSeq_t
00030 #endif
00031 
00032 #include "qgspoint.h"
00033 #include "qgscoordinatetransform.h"
00034 
00036 typedef QVector<QgsPoint> QgsPolyline;
00037 
00039 typedef QVector<QgsPolyline> QgsPolygon;
00040 
00042 typedef QVector<QgsPoint> QgsMultiPoint;
00043 
00045 typedef QVector<QgsPolyline> QgsMultiPolyline;
00046 
00048 typedef QVector<QgsPolygon> QgsMultiPolygon;
00049 
00050 class QgsRectangle;
00051 
00067 class CORE_EXPORT QgsGeometry
00068 {
00069   public:
00071     QgsGeometry();
00072 
00074     QgsGeometry( QgsGeometry const & );
00075 
00077     QgsGeometry & operator=( QgsGeometry const & rhs );
00078 
00080     ~QgsGeometry();
00081 
00083     static QgsGeometry* fromWkt( QString wkt );
00084 
00086     static QgsGeometry* fromPoint( const QgsPoint& point );
00088     static QgsGeometry* fromMultiPoint( const QgsMultiPoint& multipoint );
00090     static QgsGeometry* fromPolyline( const QgsPolyline& polyline );
00092     static QgsGeometry* fromMultiPolyline( const QgsMultiPolyline& multiline );
00094     static QgsGeometry* fromPolygon( const QgsPolygon& polygon );
00096     static QgsGeometry* fromMultiPolygon( const QgsMultiPolygon& multipoly );
00098     static QgsGeometry* fromRect( const QgsRectangle& rect );
00103     void fromGeos( GEOSGeometry* geos );
00108     void fromWkb( unsigned char * wkb, size_t length );
00109 
00114     unsigned char * asWkb();
00115 
00119     size_t wkbSize();
00120 
00123     GEOSGeometry* asGeos();
00124 
00126     QGis::WkbType wkbType();
00127 
00129     QGis::GeometryType type();
00130 
00132     bool isMultipart();
00133 
00137     bool isGeosEqual( QgsGeometry & );
00138 
00142     bool isGeosValid();
00143 
00147     bool isGeosEmpty();
00148 
00152     double area();
00153 
00157     double length();
00158 
00159     double distance( QgsGeometry& geom );
00160 
00165     QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist );
00166 
00167 
00180     void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex );
00181 
00182 
00194     bool insertVertex( double x, double y, int beforeVertex );
00195 
00202     bool moveVertex( double x, double y, int atVertex );
00203 
00214     bool deleteVertex( int atVertex );
00215 
00221     QgsPoint vertexAt( int atVertex );
00222 
00229     double sqrDistToVertexAt( QgsPoint& point, int atVertex );
00230 
00237     double closestVertexWithContext( const QgsPoint& point, int& atVertex );
00238 
00247     double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& beforeVertex );
00248 
00252     int addRing( const QList<QgsPoint>& ring );
00253 
00257     int addIsland( const QList<QgsPoint>& ring );
00258 
00261     int translate( double dx, double dy );
00262 
00265     int transform( const QgsCoordinateTransform& ct );
00266 
00274     int splitGeometry( const QList<QgsPoint>& splitLine,
00275                        QList<QgsGeometry*>&newGeometries,
00276                        bool topological,
00277                        QList<QgsPoint> &topologyTestPoints );
00278 
00282     int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
00283 
00287     int makeDifference( QgsGeometry* other );
00288 
00290     QgsRectangle boundingBox();
00291 
00293     bool intersects( const QgsRectangle& r );
00294 
00296     bool intersects( QgsGeometry* geometry );
00297 
00299     bool contains( QgsPoint* p );
00300 
00303     bool contains( QgsGeometry* geometry );
00304 
00307     bool disjoint( QgsGeometry* geometry );
00308 
00311     bool equals( QgsGeometry* geometry );
00312 
00315     bool touches( QgsGeometry* geometry );
00316 
00319     bool overlaps( QgsGeometry* geometry );
00320 
00323     bool within( QgsGeometry* geometry );
00324 
00327     bool crosses( QgsGeometry* geometry );
00328 
00331     QgsGeometry* buffer( double distance, int segments );
00332 
00334     QgsGeometry* simplify( double tolerance );
00335 
00339     QgsGeometry* centroid();
00340 
00342     QgsGeometry* convexHull();
00343 
00345     QgsGeometry* intersection( QgsGeometry* geometry );
00346 
00350     QgsGeometry* combine( QgsGeometry* geometry );
00351 
00353     QgsGeometry* difference( QgsGeometry* geometry );
00354 
00356     QgsGeometry* symDifference( QgsGeometry* geometry );
00357 
00361     QString exportToWkt();
00362 
00363     /* Accessor functions for getting geometry data */
00364 
00367     QgsPoint asPoint();
00368 
00371     QgsPolyline asPolyline();
00372 
00375     QgsPolygon asPolygon();
00376 
00379     QgsMultiPoint asMultiPoint();
00380 
00383     QgsMultiPolyline asMultiPolyline();
00384 
00387     QgsMultiPolygon asMultiPolygon();
00388 
00391     QList<QgsGeometry*> asGeometryCollection();
00392 
00397     bool deleteRing( int ringNum, int partNum = 0 );
00398 
00402     bool deletePart( int partNum );
00403 
00407     bool convertToMultiType();
00408 
00416     int avoidIntersections();
00417 
00418 
00419     class Error
00420     {
00421         QString message;
00422         QgsPoint location;
00423         bool hasLocation;
00424       public:
00425         Error( QString m ) : message( m ), hasLocation( false ) {}
00426         Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
00427 
00428         QString what() { return message; };
00429         QgsPoint where() { return location; }
00430         bool hasWhere() { return hasLocation; }
00431     };
00432 
00436     void validateGeometry( QList<Error> &errors );
00437 
00438     static void validatePolyline( QList<Error> &errors, int i, QgsPolyline polyline, bool ring = false );
00439     static void validatePolygon( QList<Error> &errors, int i, const QgsPolygon &polygon );
00440 
00441   private:
00442     // Private variables
00443 
00444     // All of these are mutable since there may be on-the-fly
00445     // conversions between WKB, GEOS and Wkt;
00446     // However the intent is the const functions do not
00447     // semantically change the value that this object represents.
00448 
00452     unsigned char * mGeometry;
00453 
00455     size_t mGeometrySize;
00456 
00458     GEOSGeometry* mGeos;
00459 
00461     bool mDirtyWkb;
00462 
00464     bool mDirtyGeos;
00465 
00466 
00467     // Private functions
00468 
00472     bool exportWkbToGeos();
00473 
00477     bool exportGeosToWkb();
00478 
00492     bool insertVertex( double x, double y,
00493                        int beforeVertex,
00494                        const GEOSCoordSequence*  old_sequence,
00495                        GEOSCoordSequence** new_sequence );
00496 
00502     void translateVertex( int& wkbPosition, double dx, double dy, bool hasZValue );
00503 
00508     void transformVertex( int& wkbPosition, const QgsCoordinateTransform& ct, bool hasZValue );
00509 
00510     //helper functions for geometry splitting
00511 
00516     int splitLinearGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
00519     int splitPolygonGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
00522     int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPoint>& testPoints ) const;
00523 
00529     static GEOSGeometry* reshapeLine( const GEOSGeometry* origLine, const GEOSGeometry* reshapeLineGeos );
00530 
00536     static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos );
00537 
00540     static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *poly );
00541 
00544     static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
00545 
00550     static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
00551 
00553     static bool geomInDegrees( const GEOSGeometry* geom );
00554 
00556     int numberOfGeometries( GEOSGeometry* g ) const;
00557 
00558     int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult );
00559 
00561     QgsPoint asPoint( unsigned char*& ptr, bool hasZValue );
00562 
00564     QgsPolyline asPolyline( unsigned char*& ptr, bool hasZValue );
00565 
00567     QgsPolygon asPolygon( unsigned char*& ptr, bool hasZValue );
00568 
00569     static void checkRingIntersections( QList<Error> &errors,
00570                                         int p0, int i0, const QgsPolyline &ring0,
00571                                         int p1, int i1, const QgsPolyline &ring1 );
00572 
00573     static bool geosRelOp( char( *op )( const GEOSGeometry*, const GEOSGeometry * ),
00574                            QgsGeometry *a, QgsGeometry *b );
00575 
00576 
00577     static int refcount;
00578 }; // class QgsGeometry
00579 
00580 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines