QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsmultisurface.cpp
Go to the documentation of this file.
1
2/***************************************************************************
3 qgsmultisurface.cpp
4 -------------------------------------------------------------------
5Date : 28 Oct 2014
6Copyright : (C) 2014 by Marco Hugentobler
7email : marco.hugentobler at sourcepole dot com
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgsmultisurface.h"
18
19#include <nlohmann/json.hpp>
20
21#include "qgscurvepolygon.h"
22#include "qgsgeometryutils.h"
23#include "qgslinestring.h"
24#include "qgsmulticurve.h"
25#include "qgspolygon.h"
26#include "qgssurface.h"
27
28#include <QJsonObject>
29#include <QString>
30
31using namespace Qt::StringLiterals;
32
37
42
43const QgsSurface *QgsMultiSurface::surfaceN( int index ) const
44{
46}
47
49{
50 return u"MultiSurface"_s;
51}
52
58
60{
61 auto result = std::make_unique< QgsMultiSurface >();
62 result->mWkbType = mWkbType;
63 return result.release();
64}
65
67{
68 return new QgsMultiSurface( *this );
69}
70
75
76bool QgsMultiSurface::fromWkt( const QString &wkt )
77{
78 return fromCollectionWkt( wkt,
80 u"Polygon"_s );
81}
82
83QDomElement QgsMultiSurface::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
84{
85 // GML2 does not support curves
86 QDomElement elemMultiPolygon = doc.createElementNS( ns, u"MultiPolygon"_s );
87
88 if ( isEmpty() )
89 return elemMultiPolygon;
90
91 for ( const QgsAbstractGeometry *geom : mGeometries )
92 {
94 {
95 std::unique_ptr< QgsPolygon > polygon( static_cast<const QgsCurvePolygon *>( geom )->surfaceToPolygon() );
96
97 QDomElement elemPolygonMember = doc.createElementNS( ns, u"polygonMember"_s );
98 elemPolygonMember.appendChild( polygon->asGml2( doc, precision, ns, axisOrder ) );
99 elemMultiPolygon.appendChild( elemPolygonMember );
100 }
101 }
102
103 return elemMultiPolygon;
104}
105
106QDomElement QgsMultiSurface::asGml3( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
107{
108 QDomElement elemMultiSurface = doc.createElementNS( ns, u"MultiSurface"_s );
109
110 if ( isEmpty() )
111 return elemMultiSurface;
112
113 for ( const QgsAbstractGeometry *geom : mGeometries )
114 {
116 {
117 QDomElement elemSurfaceMember = doc.createElementNS( ns, u"surfaceMember"_s );
118 elemSurfaceMember.appendChild( geom->asGml3( doc, precision, ns, axisOrder ) );
119 elemMultiSurface.appendChild( elemSurfaceMember );
120 }
121 }
122
123 return elemMultiSurface;
124}
125
126
127json QgsMultiSurface::asJsonObject( int precision ) const
128{
129 json polygons( json::array( ) );
130 for ( const QgsAbstractGeometry *geom : std::as_const( mGeometries ) )
131 {
133 {
134 json coordinates( json::array( ) );
135 std::unique_ptr< QgsPolygon >polygon( static_cast<const QgsCurvePolygon *>( geom )->surfaceToPolygon() );
136 std::unique_ptr< QgsLineString > exteriorLineString( polygon->exteriorRing()->curveToLine() );
137 QgsPointSequence exteriorPts;
138 exteriorLineString->points( exteriorPts );
139 coordinates.push_back( QgsGeometryUtils::pointsToJson( exteriorPts, precision ) );
140
141 std::unique_ptr< QgsLineString > interiorLineString;
142 for ( int i = 0, n = polygon->numInteriorRings(); i < n; ++i )
143 {
144 interiorLineString.reset( polygon->interiorRing( i )->curveToLine() );
145 QgsPointSequence interiorPts;
146 interiorLineString->points( interiorPts );
147 coordinates.push_back( QgsGeometryUtils::pointsToJson( interiorPts, precision ) );
148 }
149 polygons.push_back( coordinates );
150 }
151 }
152 return
153 {
154 { "type", "MultiPolygon" },
155 { "coordinates", polygons }
156 };
157}
158
160{
162 {
163 delete g;
164 return false;
165 }
166
167 if ( mGeometries.empty() )
168 {
170 }
171 if ( is3D() && !g->is3D() )
172 g->addZValue();
173 else if ( !is3D() && g->is3D() )
174 g->dropZValue();
175 if ( isMeasure() && !g->isMeasure() )
176 g->addMValue();
177 else if ( !isMeasure() && g->isMeasure() )
178 g->dropMValue();
179
181}
182
183bool QgsMultiSurface::addGeometries( const QVector<QgsAbstractGeometry *> &geometries )
184{
185 for ( QgsAbstractGeometry *g : geometries )
186 {
188 {
189 qDeleteAll( geometries );
190 return false;
191 }
192 }
193
194 if ( mGeometries.empty() && !geometries.empty() )
195 {
197 }
198 mGeometries.reserve( mGeometries.size() + geometries.size() );
199 for ( QgsAbstractGeometry *g : geometries )
200 {
201 if ( is3D() && !g->is3D() )
202 g->addZValue();
203 else if ( !is3D() && g->is3D() )
204 g->dropZValue();
205 if ( isMeasure() && !g->isMeasure() )
206 g->addMValue();
207 else if ( !isMeasure() && g->isMeasure() )
208 g->dropMValue();
209 mGeometries.append( g );
210 }
211
212 clearCache();
213 return true;
214}
215
217{
218 if ( !g || !qgsgeometry_cast< QgsSurface * >( g ) )
219 {
220 delete g;
221 return false;
222 }
223
224 return QgsGeometryCollection::insertGeometry( g, index );
225}
226
228{
229 auto multiCurve = std::make_unique<QgsMultiCurve>();
230 multiCurve->reserve( mGeometries.size() );
231 for ( int i = 0; i < mGeometries.size(); ++i )
232 {
233 if ( QgsSurface *surface = qgsgeometry_cast<QgsSurface *>( mGeometries.at( i ) ) )
234 {
235 multiCurve->addGeometry( surface->boundary() );
236 }
237 }
238 if ( multiCurve->numGeometries() == 0 )
239 {
240 return nullptr;
241 }
242 return multiCurve.release();
243}
244
246{
247 auto res = std::make_unique< QgsMultiSurface >();
248 res->reserve( mGeometries.size() );
249 for ( int i = 0; i < mGeometries.size(); ++i )
250 {
251 res->addGeometry( mGeometries.at( i )->simplifyByDistance( tolerance ) );
252 }
253 return res.release();
254}
@ Polygon
Polygon.
Definition qgis.h:284
@ CurvePolygon
CurvePolygon.
Definition qgis.h:292
@ MultiSurface
MultiSurface.
Definition qgis.h:294
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
bool isMeasure() const
Returns true if the geometry contains m values.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
AxisOrder
Axis order for GML generation.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
void setZMTypeFromSubGeometry(const QgsAbstractGeometry *subggeom, Qgis::WkbType baseGeomType)
Updates the geometry type based on whether sub geometries contain z or m values.
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
QgsAbstractGeometry()=default
Curve polygon geometry type.
QVector< QgsAbstractGeometry * > mGeometries
bool fromCollectionWkt(const QString &wkt, const QVector< Qgis::WkbType > &subtypes, const QString &defaultChildWkbType=QString())
Reads a collection from a WKT string.
void clear() override
Clears the geometry, ie reset it to a null geometry.
void clearCache() const override
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
virtual bool insertGeometry(QgsAbstractGeometry *g, int index)
Inserts a geometry before a specified index and takes ownership.
bool isEmpty() const override
Returns true if the geometry is empty.
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
static json pointsToJson(const QgsPointSequence &points, int precision)
Returns coordinates as json object.
QgsMultiSurface()
Constructor for an empty multisurface geometry.
QgsMultiSurface * simplifyByDistance(double tolerance) const override
Simplifies the geometry by applying the Douglas Peucker simplification by distance algorithm.
QgsSurface * surfaceN(int index)
Returns the surface with the specified index.
QDomElement asGml2(QDomDocument &doc, int precision=17, const QString &ns="gml", QgsAbstractGeometry::AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const override
Returns a GML2 representation of the geometry.
QString geometryType() const override
Returns a unique string representing the geometry type.
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
QDomElement asGml3(QDomDocument &doc, int precision=17, const QString &ns="gml", QgsAbstractGeometry::AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const override
Returns a GML3 representation of the geometry.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
QgsMultiSurface * clone() const override
Clones the geometry by performing a deep copy.
bool addGeometries(const QVector< QgsAbstractGeometry * > &geometries) override
Adds a list of geometries to the collection, transferring ownership to the collection.
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
QgsMultiSurface * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
QgsMultiSurface * toCurveType() const override
Returns the geometry converted to the more generic curve type.
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
void clear() override
Clears the geometry, ie reset it to a null geometry.
Surface geometry type.
Definition qgssurface.h:34
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QVector< QgsPoint > QgsPointSequence