QGIS API Documentation 3.32.0-Lima (311a8cb8a6)
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
29{
31}
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
49{
52}
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
67{
68 return clone();
69}
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 QgsSurface *>( geom ) )
89 {
90 std::unique_ptr< QgsPolygon > polygon( static_cast<const QgsSurface *>( 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 QgsSurface *>( geom ) )
128 {
129 json coordinates( json::array( ) );
130 std::unique_ptr< QgsPolygon >polygon( static_cast<const QgsSurface *>( 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
179{
180 if ( !g || !qgsgeometry_cast< QgsSurface * >( g ) )
181 {
182 delete g;
183 return false;
184 }
185
186 return QgsGeometryCollection::insertGeometry( g, index );
187}
188
190{
191 std::unique_ptr< QgsMultiCurve > multiCurve( new QgsMultiCurve() );
192 multiCurve->reserve( mGeometries.size() );
193 for ( int i = 0; i < mGeometries.size(); ++i )
194 {
195 if ( QgsSurface *surface = qgsgeometry_cast<QgsSurface *>( mGeometries.at( i ) ) )
196 {
197 multiCurve->addGeometry( surface->boundary() );
198 }
199 }
200 if ( multiCurve->numGeometries() == 0 )
201 {
202 return nullptr;
203 }
204 return multiCurve.release();
205}
@ 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 is3D() const SIP_HOLDGIL
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.
bool isMeasure() const SIP_HOLDGIL
Returns true if the geometry contains m values.
Curve polygon geometry type.
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
QVector< QgsAbstractGeometry * > mGeometries
void clear() override
Clears the geometry, ie reset it to a null geometry.
virtual bool insertGeometry(QgsAbstractGeometry *g, int index)
Inserts a geometry before a specified index and takes ownership.
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.
Definition: qgsmulticurve.h:30
Multi surface geometry collection.
QgsMultiSurface() SIP_HOLDGIL
Constructor for an empty multisurface geometry.
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.
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.
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.
QString geometryType() const override SIP_HOLDGIL
Returns a unique string representing the geometry type.
Polygon geometry type.
Definition: qgspolygon.h:34
Surface geometry type.
Definition: qgssurface.h:34
QVector< QgsPoint > QgsPointSequence
int precision