QGIS API Documentation 3.41.0-Master (3440c17df1d)
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#include "qgsgeometryutils.h"
19#include "qgssurface.h"
20#include "qgslinestring.h"
21#include "qgspolygon.h"
22#include "qgscurvepolygon.h"
23#include "qgsmulticurve.h"
24
25#include <QJsonObject>
26#include <nlohmann/json.hpp>
27
32
34{
35 return qgsgeometry_cast< QgsSurface * >( geometryN( index ) );
36}
37
38const QgsSurface *QgsMultiSurface::surfaceN( int index ) const
39{
40 return qgsgeometry_cast< const QgsSurface * >( geometryN( index ) );
41}
42
44{
45 return QStringLiteral( "MultiSurface" );
46}
47
53
55{
56 auto result = std::make_unique< QgsMultiSurface >();
57 result->mWkbType = mWkbType;
58 return result.release();
59}
60
62{
63 return new QgsMultiSurface( *this );
64}
65
70
71bool QgsMultiSurface::fromWkt( const QString &wkt )
72{
73 return fromCollectionWkt( wkt,
74 QVector<QgsAbstractGeometry *>() << new QgsPolygon << new QgsCurvePolygon,
75 QStringLiteral( "Polygon" ) );
76}
77
78QDomElement QgsMultiSurface::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
79{
80 // GML2 does not support curves
81 QDomElement elemMultiPolygon = doc.createElementNS( ns, QStringLiteral( "MultiPolygon" ) );
82
83 if ( isEmpty() )
84 return elemMultiPolygon;
85
86 for ( const QgsAbstractGeometry *geom : mGeometries )
87 {
88 if ( qgsgeometry_cast<const QgsCurvePolygon *>( geom ) )
89 {
90 std::unique_ptr< QgsPolygon > polygon( static_cast<const QgsCurvePolygon *>( geom )->surfaceToPolygon() );
91
92 QDomElement elemPolygonMember = doc.createElementNS( ns, QStringLiteral( "polygonMember" ) );
93 elemPolygonMember.appendChild( polygon->asGml2( doc, precision, ns, axisOrder ) );
94 elemMultiPolygon.appendChild( elemPolygonMember );
95 }
96 }
97
98 return elemMultiPolygon;
99}
100
101QDomElement QgsMultiSurface::asGml3( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
102{
103 QDomElement elemMultiSurface = doc.createElementNS( ns, QStringLiteral( "MultiSurface" ) );
104
105 if ( isEmpty() )
106 return elemMultiSurface;
107
108 for ( const QgsAbstractGeometry *geom : mGeometries )
109 {
110 if ( qgsgeometry_cast<const QgsSurface *>( geom ) )
111 {
112 QDomElement elemSurfaceMember = doc.createElementNS( ns, QStringLiteral( "surfaceMember" ) );
113 elemSurfaceMember.appendChild( geom->asGml3( doc, precision, ns, axisOrder ) );
114 elemMultiSurface.appendChild( elemSurfaceMember );
115 }
116 }
117
118 return elemMultiSurface;
119}
120
121
123{
124 json polygons( json::array( ) );
125 for ( const QgsAbstractGeometry *geom : std::as_const( mGeometries ) )
126 {
127 if ( qgsgeometry_cast<const QgsCurvePolygon *>( geom ) )
128 {
129 json coordinates( json::array( ) );
130 std::unique_ptr< QgsPolygon >polygon( static_cast<const QgsCurvePolygon *>( geom )->surfaceToPolygon() );
131 std::unique_ptr< QgsLineString > exteriorLineString( polygon->exteriorRing()->curveToLine() );
132 QgsPointSequence exteriorPts;
133 exteriorLineString->points( exteriorPts );
134 coordinates.push_back( QgsGeometryUtils::pointsToJson( exteriorPts, precision ) );
135
136 std::unique_ptr< QgsLineString > interiorLineString;
137 for ( int i = 0, n = polygon->numInteriorRings(); i < n; ++i )
138 {
139 interiorLineString.reset( polygon->interiorRing( i )->curveToLine() );
140 QgsPointSequence interiorPts;
141 interiorLineString->points( interiorPts );
142 coordinates.push_back( QgsGeometryUtils::pointsToJson( interiorPts, precision ) );
143 }
144 polygons.push_back( coordinates );
145 }
146 }
147 return
148 {
149 { "type", "MultiPolygon" },
150 { "coordinates", polygons }
151 };
152}
153
155{
156 if ( !qgsgeometry_cast<QgsSurface *>( g ) )
157 {
158 delete g;
159 return false;
160 }
161
162 if ( mGeometries.empty() )
163 {
165 }
166 if ( is3D() && !g->is3D() )
167 g->addZValue();
168 else if ( !is3D() && g->is3D() )
169 g->dropZValue();
170 if ( isMeasure() && !g->isMeasure() )
171 g->addMValue();
172 else if ( !isMeasure() && g->isMeasure() )
173 g->dropMValue();
174
176}
177
178bool QgsMultiSurface::addGeometries( const QVector<QgsAbstractGeometry *> &geometries )
179{
180 for ( QgsAbstractGeometry *g : geometries )
181 {
182 if ( !qgsgeometry_cast<QgsSurface *>( g ) )
183 {
184 qDeleteAll( geometries );
185 return false;
186 }
187 }
188
189 if ( mGeometries.empty() && !geometries.empty() )
190 {
192 }
193 mGeometries.reserve( mGeometries.size() + geometries.size() );
194 for ( QgsAbstractGeometry *g : geometries )
195 {
196 if ( is3D() && !g->is3D() )
197 g->addZValue();
198 else if ( !is3D() && g->is3D() )
199 g->dropZValue();
200 if ( isMeasure() && !g->isMeasure() )
201 g->addMValue();
202 else if ( !isMeasure() && g->isMeasure() )
203 g->dropMValue();
204 mGeometries.append( g );
205 }
206
207 clearCache();
208 return true;
209}
210
212{
213 if ( !g || !qgsgeometry_cast< QgsSurface * >( g ) )
214 {
215 delete g;
216 return false;
217 }
218
219 return QgsGeometryCollection::insertGeometry( g, index );
220}
221
223{
224 std::unique_ptr< QgsMultiCurve > multiCurve( new QgsMultiCurve() );
225 multiCurve->reserve( mGeometries.size() );
226 for ( int i = 0; i < mGeometries.size(); ++i )
227 {
228 if ( QgsSurface *surface = qgsgeometry_cast<QgsSurface *>( mGeometries.at( i ) ) )
229 {
230 multiCurve->addGeometry( surface->boundary() );
231 }
232 }
233 if ( multiCurve->numGeometries() == 0 )
234 {
235 return nullptr;
236 }
237 return multiCurve.release();
238}
239
241{
242 std::unique_ptr< QgsMultiSurface > res = std::make_unique< QgsMultiSurface >();
243 res->reserve( mGeometries.size() );
244 for ( int i = 0; i < mGeometries.size(); ++i )
245 {
246 res->addGeometry( mGeometries.at( i )->simplifyByDistance( tolerance ) );
247 }
248 return res.release();
249}
@ MultiSurface
MultiSurface.
Abstract base class for all geometries.
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.
Curve polygon geometry type.
QVector< QgsAbstractGeometry * > mGeometries
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.
bool fromCollectionWkt(const QString &wkt, const QVector< QgsAbstractGeometry * > &subtypes, const QString &defaultChildWkbType=QString())
Reads a collection from a WKT string.
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.
Multi curve geometry collection.
Multi surface geometry collection.
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.
Polygon geometry type.
Definition qgspolygon.h:33
Surface geometry type.
Definition qgssurface.h:34
QVector< QgsPoint > QgsPointSequence
int precision