1 /***************************************************************************
2                          qgssurface.h
3                          --------------
4     begin                : September 2014
5     copyright            : (C) 2014 by Marco Hugentobler
6     email                : marco at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef QGSSURFACE_H
19 #define QGSSURFACE_H
20 
21 #include "qgis_core.h"
22 #include "qgis_sip.h"
23 #include "qgsabstractgeometry.h"
24 #include "qgsrectangle.h"
25 
26 class QgsPolygon;
27 
28 /**
29  * \ingroup core
30  * \class QgsSurface
31  * \brief Surface geometry type.
32  */
33 class CORE_EXPORT QgsSurface: public QgsAbstractGeometry
34 {
35   public:
36 
37     /**
38      * Gets a polygon representation of this surface.
39      * Ownership is transferred to the caller.
40      */
41     virtual QgsPolygon *surfaceToPolygon() const = 0 SIP_FACTORY;
42 
boundingBox()43     QgsRectangle boundingBox() const override
44     {
45       if ( mBoundingBox.isNull() )
46       {
47         mBoundingBox = calculateBoundingBox();
48       }
49       return mBoundingBox;
50     }
51 
52     bool isValid( QString &error SIP_OUT, Qgis::GeometryValidityFlags flags = Qgis::GeometryValidityFlags() ) const override;
53 
54 
55 #ifndef SIP_RUN
56 
57     /**
58      * Cast the \a geom to a QgsSurface.
59      * Should be used by qgsgeometry_cast<QgsSurface *>( geometry ).
60      *
61      * \note Not available in Python. Objects will be automatically be converted to the appropriate target type.
62      * \since QGIS 3.0
63      */
cast(const QgsAbstractGeometry * geom)64     inline static const QgsSurface *cast( const QgsAbstractGeometry *geom )
65     {
66       if ( !geom )
67         return nullptr;
68 
69       const QgsWkbTypes::Type flatType = QgsWkbTypes::flatType( geom->wkbType() );
70       if ( flatType == QgsWkbTypes::CurvePolygon
71            || flatType == QgsWkbTypes::Polygon
72            || flatType == QgsWkbTypes::Triangle )
73         return static_cast<const QgsSurface *>( geom );
74       return nullptr;
75     }
76 #endif
77   protected:
78 
79     void clearCache() const override;
80 
81     mutable QgsRectangle mBoundingBox;
82     mutable bool mHasCachedValidity = false;
83     mutable QString mValidityFailureReason;
84 };
85 
86 #endif // QGSSURFACE_H
87