Quantum GIS API Documentation  1.8
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 
00016 #ifndef QGSGEOMETRY_H
00017 #define QGSGEOMETRY_H
00018 
00019 #include <QString>
00020 #include <QVector>
00021 
00022 #include "qgis.h"
00023 
00024 #include <geos_c.h>
00025 
00026 #if defined(GEOS_VERSION_MAJOR) && (GEOS_VERSION_MAJOR<3)
00027 #define GEOSGeometry struct GEOSGeom_t
00028 #define GEOSCoordSequence struct GEOSCoordSeq_t
00029 #endif
00030 
00031 #include "qgspoint.h"
00032 #include "qgscoordinatetransform.h"
00033 
00035 typedef QVector<QgsPoint> QgsPolyline;
00036 
00038 typedef QVector<QgsPolyline> QgsPolygon;
00039 
00041 typedef QVector<QgsPoint> QgsMultiPoint;
00042 
00044 typedef QVector<QgsPolyline> QgsMultiPolyline;
00045 
00047 typedef QVector<QgsPolygon> QgsMultiPolygon;
00048 
00049 class QgsRectangle;
00050 
00066 class CORE_EXPORT QgsGeometry
00067 {
00068   public:
00070     QgsGeometry();
00071 
00073     QgsGeometry( QgsGeometry const & );
00074 
00076     QgsGeometry & operator=( QgsGeometry const & rhs );
00077 
00079     ~QgsGeometry();
00080 
00082     static QgsGeometry* fromWkt( QString wkt );
00083 
00085     static QgsGeometry* fromPoint( const QgsPoint& point );
00087     static QgsGeometry* fromMultiPoint( const QgsMultiPoint& multipoint );
00089     static QgsGeometry* fromPolyline( const QgsPolyline& polyline );
00091     static QgsGeometry* fromMultiPolyline( const QgsMultiPolyline& multiline );
00093     static QgsGeometry* fromPolygon( const QgsPolygon& polygon );
00095     static QgsGeometry* fromMultiPolygon( const QgsMultiPolygon& multipoly );
00097     static QgsGeometry* fromRect( const QgsRectangle& rect );
00102     void fromGeos( GEOSGeometry* geos );
00107     void fromWkb( unsigned char * wkb, size_t length );
00108 
00113     unsigned char * asWkb();
00114 
00118     size_t wkbSize();
00119 
00122     GEOSGeometry* asGeos();
00123 
00125     QGis::WkbType wkbType();
00126 
00128     QGis::GeometryType type();
00129 
00131     bool isMultipart();
00132 
00136     bool isGeosEqual( QgsGeometry & );
00137 
00141     bool isGeosValid();
00142 
00146     bool isGeosEmpty();
00147 
00151     double area();
00152 
00156     double length();
00157 
00158     double distance( QgsGeometry& geom );
00159 
00164     QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist );
00165 
00166 
00179     void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex );
00180 
00181 
00193     bool insertVertex( double x, double y, int beforeVertex );
00194 
00201     bool moveVertex( double x, double y, int atVertex );
00202 
00213     bool deleteVertex( int atVertex );
00214 
00220     QgsPoint vertexAt( int atVertex );
00221 
00228     double sqrDistToVertexAt( QgsPoint& point, int atVertex );
00229 
00236     double closestVertexWithContext( const QgsPoint& point, int& atVertex );
00237 
00248     double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& afterVertex, double* leftOf = 0, double epsilon = DEFAULT_SEGMENT_EPSILON );
00249 
00253     int addRing( const QList<QgsPoint>& ring );
00254 
00258     int addPart( const QList<QgsPoint> &points );
00259     Q_DECL_DEPRECATED int addIsland( const QList<QgsPoint> &points ) { return addPart( points ); }
00260 
00263     int translate( double dx, double dy );
00264 
00267     int transform( const QgsCoordinateTransform& ct );
00268 
00276     int splitGeometry( const QList<QgsPoint>& splitLine,
00277                        QList<QgsGeometry*>&newGeometries,
00278                        bool topological,
00279                        QList<QgsPoint> &topologyTestPoints );
00280 
00284     int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine );
00285 
00289     int makeDifference( QgsGeometry* other );
00290 
00292     QgsRectangle boundingBox();
00293 
00295     bool intersects( const QgsRectangle& r );
00296 
00298     bool intersects( QgsGeometry* geometry );
00299 
00301     bool contains( QgsPoint* p );
00302 
00305     bool contains( QgsGeometry* geometry );
00306 
00309     bool disjoint( QgsGeometry* geometry );
00310 
00313     bool equals( QgsGeometry* geometry );
00314 
00317     bool touches( QgsGeometry* geometry );
00318 
00321     bool overlaps( QgsGeometry* geometry );
00322 
00325     bool within( QgsGeometry* geometry );
00326 
00329     bool crosses( QgsGeometry* geometry );
00330 
00333     QgsGeometry* buffer( double distance, int segments );
00334 
00336     QgsGeometry* simplify( double tolerance );
00337 
00341     QgsGeometry* centroid();
00342 
00344     QgsGeometry* convexHull();
00345 
00347     QgsGeometry* intersection( QgsGeometry* geometry );
00348 
00352     QgsGeometry* combine( QgsGeometry* geometry );
00353 
00355     QgsGeometry* difference( QgsGeometry* geometry );
00356 
00358     QgsGeometry* symDifference( QgsGeometry* geometry );
00359 
00363     QString exportToWkt();
00364 
00368     QString exportToGeoJSON();
00369 
00370     /* Accessor functions for getting geometry data */
00371 
00374     QgsPoint asPoint();
00375 
00378     QgsPolyline asPolyline();
00379 
00382     QgsPolygon asPolygon();
00383 
00386     QgsMultiPoint asMultiPoint();
00387 
00390     QgsMultiPolyline asMultiPolyline();
00391 
00394     QgsMultiPolygon asMultiPolygon();
00395 
00398     QList<QgsGeometry*> asGeometryCollection();
00399 
00404     bool deleteRing( int ringNum, int partNum = 0 );
00405 
00409     bool deletePart( int partNum );
00410 
00414     bool convertToMultiType();
00415 
00423     int avoidIntersections();
00424 
00425     class Error
00426     {
00427         QString message;
00428         QgsPoint location;
00429         bool hasLocation;
00430       public:
00431         Error() : message( "none" ), hasLocation( false ) {}
00432         Error( QString m ) : message( m ), hasLocation( false ) {}
00433         Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {}
00434 
00435         QString what() { return message; };
00436         QgsPoint where() { return location; }
00437         bool hasWhere() { return hasLocation; }
00438     };
00439 
00443     void validateGeometry( QList<Error> &errors );
00444 
00445   private:
00446     // Private variables
00447 
00448     // All of these are mutable since there may be on-the-fly
00449     // conversions between WKB, GEOS and Wkt;
00450     // However the intent is the const functions do not
00451     // semantically change the value that this object represents.
00452 
00456     unsigned char * mGeometry;
00457 
00459     size_t mGeometrySize;
00460 
00462     GEOSGeometry* mGeos;
00463 
00465     bool mDirtyWkb;
00466 
00468     bool mDirtyGeos;
00469 
00470 
00471     // Private functions
00472 
00476     bool exportWkbToGeos();
00477 
00481     bool exportGeosToWkb();
00482 
00496     bool insertVertex( double x, double y,
00497                        int beforeVertex,
00498                        const GEOSCoordSequence*  old_sequence,
00499                        GEOSCoordSequence** new_sequence );
00500 
00506     void translateVertex( int& wkbPosition, double dx, double dy, bool hasZValue );
00507 
00512     void transformVertex( int& wkbPosition, const QgsCoordinateTransform& ct, bool hasZValue );
00513 
00514     //helper functions for geometry splitting
00515 
00520     int splitLinearGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
00523     int splitPolygonGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries );
00526     int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPoint>& testPoints ) const;
00527 
00533     static GEOSGeometry* reshapeLine( const GEOSGeometry* origLine, const GEOSGeometry* reshapeLineGeos );
00534 
00540     static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos );
00541 
00544     static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *poly );
00545 
00548     static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 );
00549 
00554     static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line );
00555 
00557     static bool geomInDegrees( const GEOSGeometry* geom );
00558 
00560     int numberOfGeometries( GEOSGeometry* g ) const;
00561 
00562     int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult );
00563 
00565     QgsPoint asPoint( unsigned char*& ptr, bool hasZValue );
00566 
00568     QgsPolyline asPolyline( unsigned char*& ptr, bool hasZValue );
00569 
00571     QgsPolygon asPolygon( unsigned char*& ptr, bool hasZValue );
00572 
00573     static bool geosRelOp( char( *op )( const GEOSGeometry*, const GEOSGeometry * ),
00574                            QgsGeometry *a, QgsGeometry *b );
00575 
00577     double leftOf( double x, double y, double& x1, double& y1, double& x2, double& y2 );
00578 
00579 
00580     static int refcount;
00581 }; // class QgsGeometry
00582 
00583 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines