QGIS API Documentation  2.14.0-Essen
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 
25 {
26  return new QgsMultiPolygonV2( *this );
27 }
28 
30 {
31  return fromCollectionWkt( wkt, QList<QgsAbstractGeometryV2*>() << new QgsPolygonV2, "Polygon" );
32 }
33 
34 QDomElement QgsMultiPolygonV2::asGML2( QDomDocument& doc, int precision, const QString& ns ) const
35 {
36  // GML2 does not support curves
37  QDomElement elemMultiPolygon = doc.createElementNS( ns, "MultiPolygon" );
38  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
39  {
40  if ( dynamic_cast<const QgsPolygonV2*>( geom ) )
41  {
42  QDomElement elemPolygonMember = doc.createElementNS( ns, "polygonMember" );
43  elemPolygonMember.appendChild( geom->asGML2( doc, precision, ns ) );
44  elemMultiPolygon.appendChild( elemPolygonMember );
45  }
46  }
47 
48  return elemMultiPolygon;
49 }
50 
51 QDomElement QgsMultiPolygonV2::asGML3( QDomDocument& doc, int precision, const QString& ns ) const
52 {
53  QDomElement elemMultiSurface = doc.createElementNS( ns, "MultiPolygon" );
54  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
55  {
56  if ( dynamic_cast<const QgsPolygonV2*>( geom ) )
57  {
58  QDomElement elemSurfaceMember = doc.createElementNS( ns, "polygonMember" );
59  elemSurfaceMember.appendChild( geom->asGML3( doc, precision, ns ) );
60  elemMultiSurface.appendChild( elemSurfaceMember );
61  }
62  }
63 
64  return elemMultiSurface;
65 }
66 
67 QString QgsMultiPolygonV2::asJSON( int precision ) const
68 {
69  // GeoJSON does not support curves
70  QString json = "{\"type\": \"MultiPolygon\", \"coordinates\": [";
71  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
72  {
73  if ( dynamic_cast<const QgsPolygonV2*>( geom ) )
74  {
75  json += '[';
76 
77  const QgsPolygonV2* polygon = static_cast<const QgsPolygonV2*>( geom );
78 
79  QgsLineStringV2* exteriorLineString = polygon->exteriorRing()->curveToLine();
80  QgsPointSequenceV2 exteriorPts;
81  exteriorLineString->points( exteriorPts );
82  json += QgsGeometryUtils::pointsToJSON( exteriorPts, precision ) + ", ";
83  delete exteriorLineString;
84 
85  for ( int i = 0, n = polygon->numInteriorRings(); i < n; ++i )
86  {
87  QgsLineStringV2* interiorLineString = polygon->interiorRing( i )->curveToLine();
88  QgsPointSequenceV2 interiorPts;
89  interiorLineString->points( interiorPts );
90  json += QgsGeometryUtils::pointsToJSON( interiorPts, precision ) + ", ";
91  delete interiorLineString;
92  }
93  if ( json.endsWith( ", " ) )
94  {
95  json.chop( 2 ); // Remove last ", "
96  }
97 
98  json += "], ";
99  }
100  }
101  if ( json.endsWith( ", " ) )
102  {
103  json.chop( 2 ); // Remove last ", "
104  }
105  json += "] }";
106  return json;
107 }
108 
110 {
111  if ( !dynamic_cast<QgsCurvePolygonV2*>( g ) )
112  {
113  delete g;
114  return false;
115  }
116 
119 }
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.
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.
QDomElement createElementNS(const QString &nsURI, const QString &qName)
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
void chop(int n)
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
QgsMultiPolygonV2 * clone() const override
Clones the geometry by performing a deep copy.
Line string geometry type, with support for z-dimension and m-values.
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.
const QgsCurveV2 * interiorRing(int i) const
QVector< QgsAbstractGeometryV2 * > mGeometries
virtual QgsLineStringV2 * curveToLine() const =0
Returns a new line string geometry corresponding to a segmentized approximation of the curve...
virtual bool addGeometry(QgsAbstractGeometryV2 *g)
Adds a geometry and takes ownership.
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