|
Quantum GIS API Documentation
master-ce49b66
|
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 #include <QDomDocument> 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 #include "qgsfeature.h" 00035 00036 #include <QSet> 00037 00038 class QgsVectorLayer; 00039 00041 typedef QVector<QgsPoint> QgsPolyline; 00042 00044 typedef QVector<QgsPolyline> QgsPolygon; 00045 00047 typedef QVector<QgsPoint> QgsMultiPoint; 00048 00050 typedef QVector<QgsPolyline> QgsMultiPolyline; 00051 00053 typedef QVector<QgsPolygon> QgsMultiPolygon; 00054 00055 class QgsRectangle; 00056 00072 class CORE_EXPORT QgsGeometry 00073 { 00074 public: 00076 QgsGeometry(); 00077 00079 QgsGeometry( const QgsGeometry & ); 00080 00084 QgsGeometry & operator=( QgsGeometry const & rhs ); 00085 00087 ~QgsGeometry(); 00088 00090 static QgsGeometry* fromWkt( QString wkt ); 00091 00093 static QgsGeometry* fromPoint( const QgsPoint& point ); 00095 static QgsGeometry* fromMultiPoint( const QgsMultiPoint& multipoint ); 00097 static QgsGeometry* fromPolyline( const QgsPolyline& polyline ); 00099 static QgsGeometry* fromMultiPolyline( const QgsMultiPolyline& multiline ); 00101 static QgsGeometry* fromPolygon( const QgsPolygon& polygon ); 00103 static QgsGeometry* fromMultiPolygon( const QgsMultiPolygon& multipoly ); 00105 static QgsGeometry* fromRect( const QgsRectangle& rect ); 00111 void fromGeos( GEOSGeometry* geos ); 00116 void fromWkb( unsigned char * wkb, size_t length ); 00117 00122 unsigned char * asWkb(); 00123 00127 size_t wkbSize(); 00128 00133 GEOSGeometry* asGeos(); 00134 00136 QGis::WkbType wkbType(); 00137 00139 QGis::GeometryType type(); 00140 00142 bool isMultipart(); 00143 00147 bool isGeosEqual( QgsGeometry & ); 00148 00152 bool isGeosValid(); 00153 00157 bool isGeosEmpty(); 00158 00162 double area(); 00163 00167 double length(); 00168 00169 double distance( QgsGeometry& geom ); 00170 00175 QgsPoint closestVertex( const QgsPoint& point, int& atVertex, int& beforeVertex, int& afterVertex, double& sqrDist ); 00176 00177 00190 void adjacentVertices( int atVertex, int& beforeVertex, int& afterVertex ); 00191 00192 00204 bool insertVertex( double x, double y, int beforeVertex ); 00205 00212 bool moveVertex( double x, double y, int atVertex ); 00213 00224 bool deleteVertex( int atVertex ); 00225 00231 QgsPoint vertexAt( int atVertex ); 00232 00238 double sqrDistToVertexAt( QgsPoint& point, int atVertex ); 00239 00246 double closestVertexWithContext( const QgsPoint& point, int& atVertex ); 00247 00258 double closestSegmentWithContext( const QgsPoint& point, QgsPoint& minDistPoint, int& afterVertex, double* leftOf = 0, double epsilon = DEFAULT_SEGMENT_EPSILON ); 00259 00263 int addRing( const QList<QgsPoint>& ring ); 00264 00268 int addPart( const QList<QgsPoint> &points ); 00269 00272 int translate( double dx, double dy ); 00273 00276 int transform( const QgsCoordinateTransform& ct ); 00277 00285 int splitGeometry( const QList<QgsPoint>& splitLine, 00286 QList<QgsGeometry*>&newGeometries, 00287 bool topological, 00288 QList<QgsPoint> &topologyTestPoints ); 00289 00293 int reshapeGeometry( const QList<QgsPoint>& reshapeWithLine ); 00294 00298 int makeDifference( QgsGeometry* other ); 00299 00301 QgsRectangle boundingBox(); 00302 00304 bool intersects( const QgsRectangle& r ); 00305 00307 bool intersects( QgsGeometry* geometry ); 00308 00310 bool contains( QgsPoint* p ); 00311 00314 bool contains( QgsGeometry* geometry ); 00315 00318 bool disjoint( QgsGeometry* geometry ); 00319 00322 bool equals( QgsGeometry* geometry ); 00323 00326 bool touches( QgsGeometry* geometry ); 00327 00330 bool overlaps( QgsGeometry* geometry ); 00331 00334 bool within( QgsGeometry* geometry ); 00335 00338 bool crosses( QgsGeometry* geometry ); 00339 00342 QgsGeometry* buffer( double distance, int segments ); 00343 00345 QgsGeometry* simplify( double tolerance ); 00346 00350 QgsGeometry* centroid(); 00351 00353 QgsGeometry* convexHull(); 00354 00355 /* Return interpolated point on line at distance 00356 * @note added in 1.9 00357 */ 00358 QgsGeometry* interpolate( double distance ); 00359 00361 QgsGeometry* intersection( QgsGeometry* geometry ); 00362 00366 QgsGeometry* combine( QgsGeometry* geometry ); 00367 00369 QgsGeometry* difference( QgsGeometry* geometry ); 00370 00372 QgsGeometry* symDifference( QgsGeometry* geometry ); 00373 00377 QString exportToWkt(); 00378 00384 QString exportToGeoJSON(); 00385 00386 /* Accessor functions for getting geometry data */ 00387 00390 QgsPoint asPoint(); 00391 00394 QgsPolyline asPolyline(); 00395 00398 QgsPolygon asPolygon(); 00399 00402 QgsMultiPoint asMultiPoint(); 00403 00406 QgsMultiPolyline asMultiPolyline(); 00407 00410 QgsMultiPolygon asMultiPolygon(); 00411 00414 QList<QgsGeometry*> asGeometryCollection(); 00415 00420 bool deleteRing( int ringNum, int partNum = 0 ); 00421 00425 bool deletePart( int partNum ); 00426 00430 bool convertToMultiType(); 00431 00440 int avoidIntersections( QMap<QgsVectorLayer*, QSet<QgsFeatureId> > ignoreFeatures = ( QMap<QgsVectorLayer*, QSet<QgsFeatureId> >() ) ); 00441 00442 class Error 00443 { 00444 QString message; 00445 QgsPoint location; 00446 bool hasLocation; 00447 public: 00448 Error() : message( "none" ), hasLocation( false ) {} 00449 Error( QString m ) : message( m ), hasLocation( false ) {} 00450 Error( QString m, QgsPoint p ) : message( m ), location( p ), hasLocation( true ) {} 00451 00452 QString what() { return message; }; 00453 QgsPoint where() { return location; } 00454 bool hasWhere() { return hasLocation; } 00455 }; 00456 00461 void validateGeometry( QList<Error> &errors ); 00462 00463 private: 00464 // Private variables 00465 00466 // All of these are mutable since there may be on-the-fly 00467 // conversions between WKB, GEOS and Wkt; 00468 // However the intent is the const functions do not 00469 // semantically change the value that this object represents. 00470 00474 unsigned char * mGeometry; 00475 00477 size_t mGeometrySize; 00478 00480 GEOSGeometry* mGeos; 00481 00483 bool mDirtyWkb; 00484 00486 bool mDirtyGeos; 00487 00488 00489 // Private functions 00490 00494 bool exportWkbToGeos(); 00495 00499 bool exportGeosToWkb(); 00500 00514 bool insertVertex( double x, double y, 00515 int beforeVertex, 00516 const GEOSCoordSequence* old_sequence, 00517 GEOSCoordSequence** new_sequence ); 00518 00524 void translateVertex( int& wkbPosition, double dx, double dy, bool hasZValue ); 00525 00530 void transformVertex( int& wkbPosition, const QgsCoordinateTransform& ct, bool hasZValue ); 00531 00532 //helper functions for geometry splitting 00533 00538 int splitLinearGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries ); 00541 int splitPolygonGeometry( GEOSGeometry *splitLine, QList<QgsGeometry*>& newGeometries ); 00544 int topologicalTestPointsSplit( const GEOSGeometry* splitLine, QList<QgsPoint>& testPoints ) const; 00545 00551 static GEOSGeometry* reshapeLine( const GEOSGeometry* origLine, const GEOSGeometry* reshapeLineGeos ); 00552 00558 static GEOSGeometry* reshapePolygon( const GEOSGeometry* polygon, const GEOSGeometry* reshapeLineGeos ); 00559 00562 static GEOSGeometry* nodeGeometries( const GEOSGeometry *splitLine, const GEOSGeometry *poly ); 00563 00566 static int lineContainedInLine( const GEOSGeometry* line1, const GEOSGeometry* line2 ); 00567 00572 static int pointContainedInLine( const GEOSGeometry* point, const GEOSGeometry* line ); 00573 00575 static bool geomInDegrees( const GEOSGeometry* geom ); 00576 00578 int numberOfGeometries( GEOSGeometry* g ) const; 00579 00580 int mergeGeometriesMultiTypeSplit( QVector<GEOSGeometry*>& splitResult ); 00581 00583 QgsPoint asPoint( unsigned char*& ptr, bool hasZValue ); 00584 00586 QgsPolyline asPolyline( unsigned char*& ptr, bool hasZValue ); 00587 00589 QgsPolygon asPolygon( unsigned char*& ptr, bool hasZValue ); 00590 00591 static bool geosRelOp( char( *op )( const GEOSGeometry*, const GEOSGeometry * ), 00592 QgsGeometry *a, QgsGeometry *b ); 00593 00595 double leftOf( double x, double y, double& x1, double& y1, double& x2, double& y2 ); 00596 }; // class QgsGeometry 00597 00598 Q_DECLARE_METATYPE( QgsGeometry ); 00599 00600 #endif