QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsmultisurface.cpp
Go to the documentation of this file.
1 
2 /***************************************************************************
3  qgsmultisurface.cpp
4  -------------------------------------------------------------------
5 Date : 28 Oct 2014
6 Copyright : (C) 2014 by Marco Hugentobler
7 email : 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 "qgsapplication.h"
19 #include "qgsgeometryutils.h"
20 #include "qgssurface.h"
21 #include "qgslinestring.h"
22 #include "qgspolygon.h"
23 #include "qgscurvepolygon.h"
24 #include "qgsmulticurve.h"
25 
26 #include <QJsonObject>
27 #include <nlohmann/json.hpp>
28 
30 {
32 }
33 
35 {
36  return qgsgeometry_cast< QgsSurface * >( geometryN( index ) );
37 }
38 
39 const QgsSurface *QgsMultiSurface::surfaceN( int index ) const
40 {
41  return qgsgeometry_cast< const QgsSurface * >( geometryN( index ) );
42 }
43 
45 {
46  return QStringLiteral( "MultiSurface" );
47 }
48 
50 {
53 }
54 
56 {
57  auto result = qgis::make_unique< QgsMultiSurface >();
58  result->mWkbType = mWkbType;
59  return result.release();
60 }
61 
63 {
64  return new QgsMultiSurface( *this );
65 }
66 
68 {
69  return clone();
70 }
71 
72 bool QgsMultiSurface::fromWkt( const QString &wkt )
73 {
74  return fromCollectionWkt( wkt,
75  QVector<QgsAbstractGeometry *>() << new QgsPolygon << new QgsCurvePolygon,
76  QStringLiteral( "Polygon" ) );
77 }
78 
79 QDomElement QgsMultiSurface::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
80 {
81  // GML2 does not support curves
82  QDomElement elemMultiPolygon = doc.createElementNS( ns, QStringLiteral( "MultiPolygon" ) );
83 
84  if ( isEmpty() )
85  return elemMultiPolygon;
86 
87  for ( const QgsAbstractGeometry *geom : mGeometries )
88  {
89  if ( qgsgeometry_cast<const QgsSurface *>( geom ) )
90  {
91  std::unique_ptr< QgsPolygon > polygon( static_cast<const QgsSurface *>( geom )->surfaceToPolygon() );
92 
93  QDomElement elemPolygonMember = doc.createElementNS( ns, QStringLiteral( "polygonMember" ) );
94  elemPolygonMember.appendChild( polygon->asGml2( doc, precision, ns, axisOrder ) );
95  elemMultiPolygon.appendChild( elemPolygonMember );
96  }
97  }
98 
99  return elemMultiPolygon;
100 }
101 
102 QDomElement QgsMultiSurface::asGml3( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
103 {
104  QDomElement elemMultiSurface = doc.createElementNS( ns, QStringLiteral( "MultiSurface" ) );
105 
106  if ( isEmpty() )
107  return elemMultiSurface;
108 
109  for ( const QgsAbstractGeometry *geom : mGeometries )
110  {
111  if ( qgsgeometry_cast<const QgsSurface *>( geom ) )
112  {
113  QDomElement elemSurfaceMember = doc.createElementNS( ns, QStringLiteral( "surfaceMember" ) );
114  elemSurfaceMember.appendChild( geom->asGml3( doc, precision, ns, axisOrder ) );
115  elemMultiSurface.appendChild( elemSurfaceMember );
116  }
117  }
118 
119  return elemMultiSurface;
120 }
121 
122 
124 {
125  json polygons( json::array( ) );
126  for ( const QgsAbstractGeometry *geom : qgis::as_const( mGeometries ) )
127  {
128  if ( qgsgeometry_cast<const QgsSurface *>( geom ) )
129  {
130  json coordinates( json::array( ) );
131  std::unique_ptr< QgsPolygon >polygon( static_cast<const QgsSurface *>( geom )->surfaceToPolygon() );
132  std::unique_ptr< QgsLineString > exteriorLineString( polygon->exteriorRing()->curveToLine() );
133  QgsPointSequence exteriorPts;
134  exteriorLineString->points( exteriorPts );
135  coordinates.push_back( QgsGeometryUtils::pointsToJson( exteriorPts, precision ) );
136 
137  std::unique_ptr< QgsLineString > interiorLineString;
138  for ( int i = 0, n = polygon->numInteriorRings(); i < n; ++i )
139  {
140  interiorLineString.reset( polygon->interiorRing( i )->curveToLine() );
141  QgsPointSequence interiorPts;
142  interiorLineString->points( interiorPts );
143  coordinates.push_back( QgsGeometryUtils::pointsToJson( interiorPts, precision ) );
144  }
145  polygons.push_back( coordinates );
146  }
147  }
148  return
149  {
150  { "type", "MultiPolygon" },
151  { "coordinates", polygons }
152  };
153 }
154 
156 {
157  if ( !qgsgeometry_cast<QgsSurface *>( g ) )
158  {
159  delete g;
160  return false;
161  }
162 
163  if ( mGeometries.empty() )
164  {
166  }
167  if ( is3D() && !g->is3D() )
168  g->addZValue();
169  else if ( !is3D() && g->is3D() )
170  g->dropZValue();
171  if ( isMeasure() && !g->isMeasure() )
172  g->addMValue();
173  else if ( !isMeasure() && g->isMeasure() )
174  g->dropMValue();
175 
177 }
178 
180 {
181  if ( !g || !qgsgeometry_cast< QgsSurface * >( g ) )
182  {
183  delete g;
184  return false;
185  }
186 
187  return QgsGeometryCollection::insertGeometry( g, index );
188 }
189 
191 {
192  std::unique_ptr< QgsMultiCurve > multiCurve( new QgsMultiCurve() );
193  multiCurve->reserve( mGeometries.size() );
194  for ( int i = 0; i < mGeometries.size(); ++i )
195  {
196  if ( QgsSurface *surface = qgsgeometry_cast<QgsSurface *>( mGeometries.at( i ) ) )
197  {
198  multiCurve->addGeometry( surface->boundary() );
199  }
200  }
201  if ( multiCurve->numGeometries() == 0 )
202  {
203  return nullptr;
204  }
205  return multiCurve.release();
206 }
qgspolygon.h
QgsAbstractGeometry::dropMValue
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
QgsGeometryCollection::insertGeometry
virtual bool insertGeometry(QgsAbstractGeometry *g, int index)
Inserts a geometry before a specified index and takes ownership.
Definition: qgsgeometrycollection.cpp:238
QgsGeometryCollection::isEmpty
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
Definition: qgsgeometrycollection.cpp:213
QgsMultiSurface::asGml2
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.
Definition: qgsmultisurface.cpp:79
QgsAbstractGeometry::dropZValue
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
QgsGeometryUtils::pointsToJson
static json pointsToJson(const QgsPointSequence &points, int precision)
Returns coordinates as json object.
Definition: qgsgeometryutils.cpp:1275
QgsPolygon
Polygon geometry type.
Definition: qgspolygon.h:34
qgslinestring.h
QgsAbstractGeometry::addZValue
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
QgsAbstractGeometry::addMValue
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
QgsCurvePolygon
Curve polygon geometry type.
Definition: qgscurvepolygon.h:35
QgsSurface
Definition: qgssurface.h:33
QgsGeometryCollection::mGeometries
QVector< QgsAbstractGeometry * > mGeometries
Definition: qgsgeometrycollection.h:327
QgsAbstractGeometry::mWkbType
QgsWkbTypes::Type mWkbType
Definition: qgsabstractgeometry.h:1030
QgsMultiSurface
Multi surface geometry collection.
Definition: qgsmultisurface.h:33
QgsMultiSurface::createEmptyWithSameType
QgsMultiSurface * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
Definition: qgsmultisurface.cpp:55
QgsMultiSurface::clone
QgsMultiSurface * clone() const override
Clones the geometry by performing a deep copy.
Definition: qgsmultisurface.cpp:62
QgsMultiSurface::surfaceN
QgsSurface * surfaceN(int index)
Returns the surface with the specified index.
Definition: qgsmultisurface.cpp:34
qgsapplication.h
QgsAbstractGeometry::isMeasure
bool isMeasure() const SIP_HOLDGIL
Returns true if the geometry contains m values.
Definition: qgsabstractgeometry.h:215
precision
int precision
Definition: qgswfsgetfeature.cpp:49
QgsAbstractGeometry::AxisOrder
AxisOrder
Axis order for GML generation.
Definition: qgsabstractgeometry.h:133
QgsMultiCurve
Multi curve geometry collection.
Definition: qgsmulticurve.h:30
QgsGeometryCollection::addGeometry
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
Definition: qgsgeometrycollection.cpp:226
QgsGeometryCollection::fromCollectionWkt
bool fromCollectionWkt(const QString &wkt, const QVector< QgsAbstractGeometry * > &subtypes, const QString &defaultChildWkbType=QString())
Reads a collection from a WKT string.
Definition: qgsgeometrycollection.cpp:702
QgsMultiSurface::clear
void clear() override
Clears the geometry, ie reset it to a null geometry.
Definition: qgsmultisurface.cpp:49
qgscurvepolygon.h
QgsMultiSurface::toCurveType
QgsMultiSurface * toCurveType() const override
Returns the geometry converted to the more generic curve type.
Definition: qgsmultisurface.cpp:67
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:74
qgsgeometryutils.h
QgsAbstractGeometry::is3D
bool is3D() const SIP_HOLDGIL
Returns true if the geometry is 3D and contains a z-value.
Definition: qgsabstractgeometry.h:206
QgsMultiSurface::insertGeometry
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
Definition: qgsmultisurface.cpp:179
QgsGeometryCollection::geometryN
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
Definition: qgsgeometrycollection.h:85
QgsWkbTypes::MultiSurface
@ MultiSurface
Definition: qgswkbtypes.h:84
QgsMultiSurface::asGml3
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.
Definition: qgsmultisurface.cpp:102
QgsPointSequence
QVector< QgsPoint > QgsPointSequence
Definition: qgsabstractgeometry.h:46
QgsMultiSurface::boundary
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
Definition: qgsmultisurface.cpp:190
QgsGeometryCollection::clear
void clear() override
Clears the geometry, ie reset it to a null geometry.
Definition: qgsgeometrycollection.cpp:123
QgsMultiSurface::addGeometry
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
Definition: qgsmultisurface.cpp:155
qgsmulticurve.h
QgsAbstractGeometry::setZMTypeFromSubGeometry
void setZMTypeFromSubGeometry(const QgsAbstractGeometry *subggeom, QgsWkbTypes::Type baseGeomType)
Updates the geometry type based on whether sub geometries contain z or m values.
Definition: qgsabstractgeometry.cpp:43
QgsMultiSurface::QgsMultiSurface
QgsMultiSurface() SIP_HOLDGIL
Constructor for an empty multisurface geometry.
Definition: qgsmultisurface.cpp:29
qgsmultisurface.h
QgsMultiSurface::geometryType
QString geometryType() const override SIP_HOLDGIL
Returns a unique string representing the geometry type.
Definition: qgsmultisurface.cpp:44
QgsMultiSurface::fromWkt
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
Definition: qgsmultisurface.cpp:72
QgsMultiSurface::asJsonObject
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
Definition: qgsmultisurface.cpp:123
qgssurface.h