QGIS API Documentation  2.18.3-Las Palmas (77b8c3d)
qgsmultipolygonv2.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmultipolygonv2.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 "qgsmultipolygonv2.h"
17 #include "qgsapplication.h"
18 #include "qgsgeometryutils.h"
19 #include "qgssurfacev2.h"
20 #include "qgslinestringv2.h"
21 #include "qgspolygonv2.h"
22 #include "qgscurvepolygonv2.h"
23 #include "qgsmultilinestringv2.h"
24 
27 {
29 }
30 
32 {
33  return new QgsMultiPolygonV2( *this );
34 }
35 
37 {
38  return fromCollectionWkt( wkt, QList<QgsAbstractGeometryV2*>() << new QgsPolygonV2, "Polygon" );
39 }
40 
41 QDomElement QgsMultiPolygonV2::asGML2( QDomDocument& doc, int precision, const QString& ns ) const
42 {
43  // GML2 does not support curves
44  QDomElement elemMultiPolygon = doc.createElementNS( ns, "MultiPolygon" );
45  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
46  {
47  if ( dynamic_cast<const QgsPolygonV2*>( geom ) )
48  {
49  QDomElement elemPolygonMember = doc.createElementNS( ns, "polygonMember" );
50  elemPolygonMember.appendChild( geom->asGML2( doc, precision, ns ) );
51  elemMultiPolygon.appendChild( elemPolygonMember );
52  }
53  }
54 
55  return elemMultiPolygon;
56 }
57 
58 QDomElement QgsMultiPolygonV2::asGML3( QDomDocument& doc, int precision, const QString& ns ) const
59 {
60  QDomElement elemMultiSurface = doc.createElementNS( ns, "MultiPolygon" );
61  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
62  {
63  if ( dynamic_cast<const QgsPolygonV2*>( geom ) )
64  {
65  QDomElement elemSurfaceMember = doc.createElementNS( ns, "polygonMember" );
66  elemSurfaceMember.appendChild( geom->asGML3( doc, precision, ns ) );
67  elemMultiSurface.appendChild( elemSurfaceMember );
68  }
69  }
70 
71  return elemMultiSurface;
72 }
73 
74 QString QgsMultiPolygonV2::asJSON( int precision ) const
75 {
76  // GeoJSON does not support curves
77  QString json = "{\"type\": \"MultiPolygon\", \"coordinates\": [";
78  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
79  {
80  if ( dynamic_cast<const QgsPolygonV2*>( geom ) )
81  {
82  json += '[';
83 
84  const QgsPolygonV2* polygon = static_cast<const QgsPolygonV2*>( geom );
85 
86  QgsLineStringV2* exteriorLineString = polygon->exteriorRing()->curveToLine();
87  QgsPointSequenceV2 exteriorPts;
88  exteriorLineString->points( exteriorPts );
89  json += QgsGeometryUtils::pointsToJSON( exteriorPts, precision ) + ", ";
90  delete exteriorLineString;
91 
92  for ( int i = 0, n = polygon->numInteriorRings(); i < n; ++i )
93  {
94  QgsLineStringV2* interiorLineString = polygon->interiorRing( i )->curveToLine();
95  QgsPointSequenceV2 interiorPts;
96  interiorLineString->points( interiorPts );
97  json += QgsGeometryUtils::pointsToJSON( interiorPts, precision ) + ", ";
98  delete interiorLineString;
99  }
100  if ( json.endsWith( ", " ) )
101  {
102  json.chop( 2 ); // Remove last ", "
103  }
104 
105  json += "], ";
106  }
107  }
108  if ( json.endsWith( ", " ) )
109  {
110  json.chop( 2 ); // Remove last ", "
111  }
112  json += "] }";
113  return json;
114 }
115 
117 {
118  if ( !dynamic_cast<QgsPolygonV2*>( g ) )
119  {
120  delete g;
121  return false;
122  }
123 
126 }
127 
129 {
130  QgsMultiSurfaceV2* multiSurface = new QgsMultiSurfaceV2();
131  for ( int i = 0; i < mGeometries.size(); ++i )
132  {
133  multiSurface->addGeometry( mGeometries.at( i )->clone() );
134  }
135  return multiSurface;
136 }
137 
139 {
140  QgsMultiLineStringV2* multiLine = new QgsMultiLineStringV2();
141  for ( int i = 0; i < mGeometries.size(); ++i )
142  {
143  if ( QgsPolygonV2* polygon = dynamic_cast<QgsPolygonV2*>( mGeometries.at( i ) ) )
144  {
145  QgsAbstractGeometryV2* polygonBoundary = polygon->boundary();
146 
147  if ( QgsLineStringV2* lineStringBoundary = dynamic_cast< QgsLineStringV2* >( polygonBoundary ) )
148  {
149  multiLine->addGeometry( lineStringBoundary );
150  }
151  else if ( QgsMultiLineStringV2* multiLineStringBoundary = dynamic_cast< QgsMultiLineStringV2* >( polygonBoundary ) )
152  {
153  for ( int j = 0; j < multiLineStringBoundary->numGeometries(); ++j )
154  {
155  multiLine->addGeometry( multiLineStringBoundary->geometryN( j )->clone() );
156  }
157  delete multiLineStringBoundary;
158  }
159  else
160  {
161  delete polygonBoundary;
162  }
163  }
164  }
165  if ( multiLine->numGeometries() == 0 )
166  {
167  delete multiLine;
168  return nullptr;
169  }
170  return multiLine;
171 }
const QgsCurveV2 * exteriorRing() const
QString asJSON(int precision=17) const override
Returns a GeoJSON representation of the geometry.
QDomNode appendChild(const QDomNode &newChild)
void points(QgsPointSequenceV2 &pt) const override
Returns a list of points within the curve.
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
static QString pointsToJSON(const QgsPointSequenceV2 &points, int precision)
Returns a geoJSON coordinates string.
Abstract base class for all geometries.
QDomElement asGML2(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML2 representation of the geometry.
QgsAbstractGeometryV2 * toCurveType() const override
Returns the geometry converted to the more generic curve type QgsMultiSurfaceV2.
QDomElement createElementNS(const QString &nsURI, const QString &qName)
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
void chop(int n)
Multi line string geometry collection.
QDomElement asGML3(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML3 representation of the geometry.
Polygon geometry type.
Definition: qgspolygonv2.h:29
virtual QgsAbstractGeometryV2 * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
QgsMultiPolygonV2 * clone() const override
Clones the geometry by performing a deep copy.
Line string geometry type, with support for z-dimension and m-values.
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
Multi surface geometry collection.
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
void setZMTypeFromSubGeometry(const QgsAbstractGeometryV2 *subggeom, QgsWKBTypes::Type baseGeomType)
Updates the geometry type based on whether sub geometries contain z or m values.
virtual QDomElement asGML3(QDomDocument &doc, int precision=17, const QString &ns="gml") const =0
Returns a GML3 representation of the geometry.
bool fromCollectionWkt(const QString &wkt, const QList< QgsAbstractGeometryV2 * > &subtypes, const QString &defaultChildWkbType=QString())
Reads a collection from a WKT string.
virtual QgsAbstractGeometryV2 * boundary() const =0
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
const QgsCurveV2 * interiorRing(int i) const
QVector< QgsAbstractGeometryV2 * > mGeometries
int numGeometries() const
Returns the number of geometries within the collection.
const T & at(int i) const
virtual bool addGeometry(QgsAbstractGeometryV2 *g)
Adds a geometry and takes ownership.
virtual QgsLineStringV2 * 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...
Multi polygon geometry collection.
virtual QDomElement asGML2(QDomDocument &doc, int precision=17, const QString &ns="gml") const =0
Returns a GML2 representation of the geometry.
int numInteriorRings() const
int size() const