QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 = std::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( "MultiSurface" ) );
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( "surfaceMember" ) );
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 : std::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 }
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 QgsAbstractGeometry * boundary() const =0
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
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.
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
QgsWkbTypes::Type mWkbType
bool isMeasure() const SIP_HOLDGIL
Returns true if the geometry contains m values.
void setZMTypeFromSubGeometry(const QgsAbstractGeometry *subggeom, QgsWkbTypes::Type baseGeomType)
Updates the geometry type based on whether sub geometries contain z or m values.
const QgsCurve * interiorRing(int i) const SIP_HOLDGIL
Retrieves an interior ring from the curve polygon.
const QgsCurve * exteriorRing() const SIP_HOLDGIL
Returns the curve polygon's exterior ring.
int numInteriorRings() const SIP_HOLDGIL
Returns the number of interior rings contained with the curve polygon.
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.
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
QVector< QgsAbstractGeometry * > mGeometries
void reserve(int size) SIP_HOLDGIL
Attempts to allocate memory for at least size geometries.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
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.
static json pointsToJson(const QgsPointSequence &points, int precision)
Returns coordinates as json object.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:44
Multi line string geometry collection.
Multi polygon geometry collection.
bool wktOmitChildType() const override
Returns whether child type names are omitted from Wkt representations of the collection.
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
QgsMultiSurface * toCurveType() const override
Returns the geometry converted to the more generic curve type QgsMultiSurface.
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.
QgsMultiPolygon() SIP_HOLDGIL
Constructor for an empty multipolygon geometry.
void clear() override
Clears the geometry, ie reset it to a null geometry.
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.
QgsMultiPolygon * clone() const override
Clones the geometry by performing a deep copy.
QString geometryType() const override SIP_HOLDGIL
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.
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
QgsPolygon * polygonN(int index)
Returns the polygon with the specified index.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
QgsMultiPolygon * 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...
Multi surface geometry collection.
QgsMultiSurface() SIP_HOLDGIL
Constructor for an empty multisurface geometry.
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
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:34
QVector< QgsPoint > QgsPointSequence
int precision