QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
34
39
40const QgsSurface *QgsMultiSurface::surfaceN( int index ) const
41{
43}
44
46{
47 return QStringLiteral( "MultiSurface" );
48}
49
55
57{
58 auto result = std::make_unique< QgsMultiSurface >();
59 result->mWkbType = mWkbType;
60 return result.release();
61}
62
64{
65 return new QgsMultiSurface( *this );
66}
67
72
73bool QgsMultiSurface::fromWkt( const QString &wkt )
74{
75 return fromCollectionWkt( wkt,
77 QStringLiteral( "Polygon" ) );
78}
79
80QDomElement QgsMultiSurface::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
81{
82 // GML2 does not support curves
83 QDomElement elemMultiPolygon = doc.createElementNS( ns, QStringLiteral( "MultiPolygon" ) );
84
85 if ( isEmpty() )
86 return elemMultiPolygon;
87
88 for ( const QgsAbstractGeometry *geom : mGeometries )
89 {
91 {
92 std::unique_ptr< QgsPolygon > polygon( static_cast<const QgsCurvePolygon *>( geom )->surfaceToPolygon() );
93
94 QDomElement elemPolygonMember = doc.createElementNS( ns, QStringLiteral( "polygonMember" ) );
95 elemPolygonMember.appendChild( polygon->asGml2( doc, precision, ns, axisOrder ) );
96 elemMultiPolygon.appendChild( elemPolygonMember );
97 }
98 }
99
100 return elemMultiPolygon;
101}
102
103QDomElement QgsMultiSurface::asGml3( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
104{
105 QDomElement elemMultiSurface = doc.createElementNS( ns, QStringLiteral( "MultiSurface" ) );
106
107 if ( isEmpty() )
108 return elemMultiSurface;
109
110 for ( const QgsAbstractGeometry *geom : mGeometries )
111 {
113 {
114 QDomElement elemSurfaceMember = doc.createElementNS( ns, QStringLiteral( "surfaceMember" ) );
115 elemSurfaceMember.appendChild( geom->asGml3( doc, precision, ns, axisOrder ) );
116 elemMultiSurface.appendChild( elemSurfaceMember );
117 }
118 }
119
120 return elemMultiSurface;
121}
122
123
124json QgsMultiSurface::asJsonObject( int precision ) const
125{
126 json polygons( json::array( ) );
127 for ( const QgsAbstractGeometry *geom : std::as_const( mGeometries ) )
128 {
130 {
131 json coordinates( json::array( ) );
132 std::unique_ptr< QgsPolygon >polygon( static_cast<const QgsCurvePolygon *>( geom )->surfaceToPolygon() );
133 std::unique_ptr< QgsLineString > exteriorLineString( polygon->exteriorRing()->curveToLine() );
134 QgsPointSequence exteriorPts;
135 exteriorLineString->points( exteriorPts );
136 coordinates.push_back( QgsGeometryUtils::pointsToJson( exteriorPts, precision ) );
137
138 std::unique_ptr< QgsLineString > interiorLineString;
139 for ( int i = 0, n = polygon->numInteriorRings(); i < n; ++i )
140 {
141 interiorLineString.reset( polygon->interiorRing( i )->curveToLine() );
142 QgsPointSequence interiorPts;
143 interiorLineString->points( interiorPts );
144 coordinates.push_back( QgsGeometryUtils::pointsToJson( interiorPts, precision ) );
145 }
146 polygons.push_back( coordinates );
147 }
148 }
149 return
150 {
151 { "type", "MultiPolygon" },
152 { "coordinates", polygons }
153 };
154}
155
157{
159 {
160 delete g;
161 return false;
162 }
163
164 if ( mGeometries.empty() )
165 {
167 }
168 if ( is3D() && !g->is3D() )
169 g->addZValue();
170 else if ( !is3D() && g->is3D() )
171 g->dropZValue();
172 if ( isMeasure() && !g->isMeasure() )
173 g->addMValue();
174 else if ( !isMeasure() && g->isMeasure() )
175 g->dropMValue();
176
178}
179
180bool QgsMultiSurface::addGeometries( const QVector<QgsAbstractGeometry *> &geometries )
181{
182 for ( QgsAbstractGeometry *g : geometries )
183 {
185 {
186 qDeleteAll( geometries );
187 return false;
188 }
189 }
190
191 if ( mGeometries.empty() && !geometries.empty() )
192 {
194 }
195 mGeometries.reserve( mGeometries.size() + geometries.size() );
196 for ( QgsAbstractGeometry *g : geometries )
197 {
198 if ( is3D() && !g->is3D() )
199 g->addZValue();
200 else if ( !is3D() && g->is3D() )
201 g->dropZValue();
202 if ( isMeasure() && !g->isMeasure() )
203 g->addMValue();
204 else if ( !isMeasure() && g->isMeasure() )
205 g->dropMValue();
206 mGeometries.append( g );
207 }
208
209 clearCache();
210 return true;
211}
212
214{
215 if ( !g || !qgsgeometry_cast< QgsSurface * >( g ) )
216 {
217 delete g;
218 return false;
219 }
220
221 return QgsGeometryCollection::insertGeometry( g, index );
222}
223
225{
226 auto multiCurve = std::make_unique<QgsMultiCurve>();
227 multiCurve->reserve( mGeometries.size() );
228 for ( int i = 0; i < mGeometries.size(); ++i )
229 {
230 if ( QgsSurface *surface = qgsgeometry_cast<QgsSurface *>( mGeometries.at( i ) ) )
231 {
232 multiCurve->addGeometry( surface->boundary() );
233 }
234 }
235 if ( multiCurve->numGeometries() == 0 )
236 {
237 return nullptr;
238 }
239 return multiCurve.release();
240}
241
243{
244 auto res = std::make_unique< QgsMultiSurface >();
245 res->reserve( mGeometries.size() );
246 for ( int i = 0; i < mGeometries.size(); ++i )
247 {
248 res->addGeometry( mGeometries.at( i )->simplifyByDistance( tolerance ) );
249 }
250 return res.release();
251}
@ Polygon
Polygon.
Definition qgis.h:281
@ CurvePolygon
CurvePolygon.
Definition qgis.h:289
@ MultiSurface
MultiSurface.
Definition qgis.h:291
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