QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsmultipolygon.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmultipolygon.cpp
3  -------------------------------------------------------------------
4 Date : 28 Oct 2014
5 Copyright : (C) 2014 by Marco Hugentobler
6 email : marco.hugentobler at sourcepole dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgsmultipolygon.h"
17 #include "qgsapplication.h"
18 #include "qgsgeometryutils.h"
19 #include "qgssurface.h"
20 #include "qgslinestring.h"
21 #include "qgspolygon.h"
22 #include "qgscurvepolygon.h"
23 #include "qgsmultilinestring.h"
24 
25 #include <QJsonObject>
26 #include <nlohmann/json.hpp>
27 
29 {
31 }
32 
34 {
35  return qgsgeometry_cast< QgsPolygon * >( geometryN( index ) );
36 }
37 
38 const QgsPolygon *QgsMultiPolygon::polygonN( int index ) const
39 {
40  return qgsgeometry_cast< const QgsPolygon * >( geometryN( index ) );
41 }
42 
44 {
45  return QStringLiteral( "MultiPolygon" );
46 }
47 
49 {
52 }
53 
55 {
56  auto result = qgis::make_unique< QgsMultiPolygon >();
57  result->mWkbType = mWkbType;
58  return result.release();
59 }
60 
62 {
63  return new QgsMultiPolygon( *this );
64 }
65 
66 bool QgsMultiPolygon::fromWkt( const QString &wkt )
67 {
68  return fromCollectionWkt( wkt, QVector<QgsAbstractGeometry *>() << new QgsPolygon, QStringLiteral( "Polygon" ) );
69 }
70 
71 QDomElement QgsMultiPolygon::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
72 {
73  // GML2 does not support curves
74  QDomElement elemMultiPolygon = doc.createElementNS( ns, QStringLiteral( "MultiPolygon" ) );
75 
76  if ( isEmpty() )
77  return elemMultiPolygon;
78 
79  for ( const QgsAbstractGeometry *geom : mGeometries )
80  {
81  if ( qgsgeometry_cast<const QgsPolygon *>( geom ) )
82  {
83  QDomElement elemPolygonMember = doc.createElementNS( ns, QStringLiteral( "polygonMember" ) );
84  elemPolygonMember.appendChild( geom->asGml2( doc, precision, ns, axisOrder ) );
85  elemMultiPolygon.appendChild( elemPolygonMember );
86  }
87  }
88 
89  return elemMultiPolygon;
90 }
91 
92 QDomElement QgsMultiPolygon::asGml3( QDomDocument &doc, int precision, const QString &ns, const QgsAbstractGeometry::AxisOrder axisOrder ) const
93 {
94  QDomElement elemMultiSurface = doc.createElementNS( ns, QStringLiteral( "MultiPolygon" ) );
95 
96  if ( isEmpty() )
97  return elemMultiSurface;
98 
99  for ( const QgsAbstractGeometry *geom : mGeometries )
100  {
101  if ( qgsgeometry_cast<const QgsPolygon *>( geom ) )
102  {
103  QDomElement elemSurfaceMember = doc.createElementNS( ns, QStringLiteral( "polygonMember" ) );
104  elemSurfaceMember.appendChild( geom->asGml3( doc, precision, ns, axisOrder ) );
105  elemMultiSurface.appendChild( elemSurfaceMember );
106  }
107  }
108 
109  return elemMultiSurface;
110 }
111 
113 {
114  json polygons( json::array( ) );
115  for ( const QgsAbstractGeometry *geom : qgis::as_const( mGeometries ) )
116  {
117  if ( qgsgeometry_cast<const QgsPolygon *>( geom ) )
118  {
119  json coordinates( json::array( ) );
120  const QgsPolygon *polygon = static_cast<const QgsPolygon *>( geom );
121 
122  std::unique_ptr< QgsLineString > exteriorLineString( polygon->exteriorRing()->curveToLine() );
123  QgsPointSequence exteriorPts;
124  exteriorLineString->points( exteriorPts );
125  coordinates.push_back( QgsGeometryUtils::pointsToJson( exteriorPts, precision ) );
126 
127  std::unique_ptr< QgsLineString > interiorLineString;
128  for ( int i = 0, n = polygon->numInteriorRings(); i < n; ++i )
129  {
130  interiorLineString.reset( polygon->interiorRing( i )->curveToLine() );
131  QgsPointSequence interiorPts;
132  interiorLineString->points( interiorPts );
133  coordinates.push_back( QgsGeometryUtils::pointsToJson( interiorPts, precision ) );
134  }
135  polygons.push_back( coordinates );
136  }
137  }
138  return
139  {
140  { "type", "MultiPolygon" },
141  { "coordinates", polygons }
142  };
143 }
144 
146 {
147  if ( !qgsgeometry_cast<QgsPolygon *>( g ) )
148  {
149  delete g;
150  return false;
151  }
152 
153  if ( mGeometries.empty() )
154  {
156  }
157  if ( is3D() && !g->is3D() )
158  g->addZValue();
159  else if ( !is3D() && g->is3D() )
160  g->dropZValue();
161  if ( isMeasure() && !g->isMeasure() )
162  g->addMValue();
163  else if ( !isMeasure() && g->isMeasure() )
164  g->dropMValue();
165 
166  return QgsGeometryCollection::addGeometry( g ); // clazy:exclude=skipped-base-method
167 }
168 
170 {
171  if ( !g || !qgsgeometry_cast< QgsPolygon * >( g ) )
172  {
173  delete g;
174  return false;
175  }
176 
177  return QgsMultiSurface::insertGeometry( g, index );
178 }
179 
181 {
182  QgsMultiSurface *multiSurface = new QgsMultiSurface();
183  multiSurface->reserve( mGeometries.size() );
184  for ( int i = 0; i < mGeometries.size(); ++i )
185  {
186  multiSurface->addGeometry( mGeometries.at( i )->clone() );
187  }
188  return multiSurface;
189 }
190 
192 {
193  std::unique_ptr< QgsMultiLineString > multiLine( new QgsMultiLineString() );
194  multiLine->reserve( mGeometries.size() );
195  for ( int i = 0; i < mGeometries.size(); ++i )
196  {
197  if ( QgsPolygon *polygon = qgsgeometry_cast<QgsPolygon *>( mGeometries.at( i ) ) )
198  {
199  QgsAbstractGeometry *polygonBoundary = polygon->boundary();
200 
201  if ( QgsLineString *lineStringBoundary = qgsgeometry_cast< QgsLineString * >( polygonBoundary ) )
202  {
203  multiLine->addGeometry( lineStringBoundary );
204  }
205  else if ( QgsMultiLineString *multiLineStringBoundary = qgsgeometry_cast< QgsMultiLineString * >( polygonBoundary ) )
206  {
207  for ( int j = 0; j < multiLineStringBoundary->numGeometries(); ++j )
208  {
209  multiLine->addGeometry( multiLineStringBoundary->geometryN( j )->clone() );
210  }
211  delete multiLineStringBoundary;
212  }
213  else
214  {
215  delete polygonBoundary;
216  }
217  }
218  }
219  if ( multiLine->numGeometries() == 0 )
220  {
221  return nullptr;
222  }
223  return multiLine.release();
224 }
225 
227 {
228  return true;
229 }
qgspolygon.h
QgsAbstractGeometry::dropMValue
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
QgsGeometryCollection::isEmpty
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
Definition: qgsgeometrycollection.cpp:213
QgsAbstractGeometry::dropZValue
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
QgsMultiPolygon::boundary
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
Definition: qgsmultipolygon.cpp:191
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.
QgsWkbTypes::MultiPolygon
@ MultiPolygon
Definition: qgswkbtypes.h:78
QgsMultiPolygon::clone
QgsMultiPolygon * clone() const override
Clones the geometry by performing a deep copy.
Definition: qgsmultipolygon.cpp:61
QgsCurvePolygon::exteriorRing
const QgsCurve * exteriorRing() const SIP_HOLDGIL
Returns the curve polygon's exterior ring.
Definition: qgscurvepolygon.h:87
QgsAbstractGeometry::addMValue
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
QgsMultiLineString
Multi line string geometry collection.
Definition: qgsmultilinestring.h:32
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
QgsLineString
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:44
QgsMultiPolygon::wktOmitChildType
bool wktOmitChildType() const override
Returns whether child type names are omitted from Wkt representations of the collection.
Definition: qgsmultipolygon.cpp:226
QgsMultiPolygon::clear
void clear() override
Clears the geometry, ie reset it to a null geometry.
Definition: qgsmultipolygon.cpp:48
qgsapplication.h
QgsAbstractGeometry::isMeasure
bool isMeasure() const SIP_HOLDGIL
Returns true if the geometry contains m values.
Definition: qgsabstractgeometry.h:215
QgsCurvePolygon::numInteriorRings
int numInteriorRings() const SIP_HOLDGIL
Returns the number of interior rings contained with the curve polygon.
Definition: qgscurvepolygon.h:77
precision
int precision
Definition: qgswfsgetfeature.cpp:49
qgsmultipolygon.h
QgsAbstractGeometry::AxisOrder
AxisOrder
Axis order for GML generation.
Definition: qgsabstractgeometry.h:133
QgsGeometryCollection::addGeometry
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
Definition: qgsgeometrycollection.cpp:226
QgsMultiPolygon::QgsMultiPolygon
QgsMultiPolygon() SIP_HOLDGIL
Constructor for an empty multipolygon geometry.
Definition: qgsmultipolygon.cpp:28
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
QgsMultiPolygon::fromWkt
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
Definition: qgsmultipolygon.cpp:66
QgsMultiPolygon::geometryType
QString geometryType() const override SIP_HOLDGIL
Returns a unique string representing the geometry type.
Definition: qgsmultipolygon.cpp:43
QgsMultiPolygon
Multi polygon geometry collection.
Definition: qgsmultipolygon.h:32
QgsMultiSurface::clear
void clear() override
Clears the geometry, ie reset it to a null geometry.
Definition: qgsmultisurface.cpp:49
QgsCurvePolygon::interiorRing
const QgsCurve * interiorRing(int i) const SIP_HOLDGIL
Retrieves an interior ring from the curve polygon.
Definition: qgscurvepolygon.h:100
QgsMultiPolygon::createEmptyWithSameType
QgsMultiPolygon * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
Definition: qgsmultipolygon.cpp:54
qgscurvepolygon.h
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:74
QgsMultiPolygon::asJsonObject
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
Definition: qgsmultipolygon.cpp:112
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
QgsPointSequence
QVector< QgsPoint > QgsPointSequence
Definition: qgsabstractgeometry.h:46
QgsMultiPolygon::addGeometry
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
Definition: qgsmultipolygon.cpp:145
QgsMultiPolygon::toCurveType
QgsMultiSurface * toCurveType() const override
Returns the geometry converted to the more generic curve type QgsMultiSurface.
Definition: qgsmultipolygon.cpp:180
QgsMultiPolygon::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: qgsmultipolygon.cpp:92
QgsMultiSurface::addGeometry
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
Definition: qgsmultisurface.cpp:155
QgsAbstractGeometry::boundary
virtual QgsAbstractGeometry * boundary() const =0
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
QgsMultiPolygon::insertGeometry
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
Definition: qgsmultipolygon.cpp:169
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
QgsGeometryCollection::reserve
void reserve(int size) SIP_HOLDGIL
Attempts to allocate memory for at least size geometries.
Definition: qgsgeometrycollection.cpp:202
QgsMultiSurface::QgsMultiSurface
QgsMultiSurface() SIP_HOLDGIL
Constructor for an empty multisurface geometry.
Definition: qgsmultisurface.cpp:29
QgsCurve::curveToLine
virtual QgsLineString * curveToLine(double tolerance=M_PI_2/90, SegmentationToleranceType toleranceType=MaximumAngle) const =0
Returns a new line string geometry corresponding to a segmentized approximation of the curve.
QgsMultiPolygon::polygonN
QgsPolygon * polygonN(int index)
Returns the polygon with the specified index.
Definition: qgsmultipolygon.cpp:33
qgsmultilinestring.h
qgssurface.h
QgsMultiPolygon::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: qgsmultipolygon.cpp:71