QGIS API Documentation  2.18.3-Las Palmas (77b8c3d)
qgsmultisurfacev2.cpp
Go to the documentation of this file.
1 
2 /***************************************************************************
3  qgsmultisurfacev2.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 "qgsmultisurfacev2.h"
18 #include "qgsapplication.h"
19 #include "qgsgeometryutils.h"
20 #include "qgssurfacev2.h"
21 #include "qgslinestringv2.h"
22 #include "qgspolygonv2.h"
23 #include "qgscurvepolygonv2.h"
24 #include "qgsmulticurvev2.h"
25 
28 {
30 }
31 
33 {
34  return new QgsMultiSurfaceV2( *this );
35 }
36 
38 {
39  return fromCollectionWkt( wkt,
41  "Polygon" );
42 }
43 
44 QDomElement QgsMultiSurfaceV2::asGML2( QDomDocument& doc, int precision, const QString& ns ) const
45 {
46  // GML2 does not support curves
47  QDomElement elemMultiPolygon = doc.createElementNS( ns, "MultiPolygon" );
48  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
49  {
50  if ( dynamic_cast<const QgsSurfaceV2*>( geom ) )
51  {
52  QgsPolygonV2* polygon = static_cast<const QgsSurfaceV2*>( geom )->surfaceToPolygon();
53 
54  QDomElement elemPolygonMember = doc.createElementNS( ns, "polygonMember" );
55  elemPolygonMember.appendChild( polygon->asGML2( doc, precision, ns ) );
56  elemMultiPolygon.appendChild( elemPolygonMember );
57 
58  delete polygon;
59  }
60  }
61 
62  return elemMultiPolygon;
63 }
64 
65 QDomElement QgsMultiSurfaceV2::asGML3( QDomDocument& doc, int precision, const QString& ns ) const
66 {
67  QDomElement elemMultiSurface = doc.createElementNS( ns, "MultiSurface" );
68  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
69  {
70  if ( dynamic_cast<const QgsSurfaceV2*>( geom ) )
71  {
72  QDomElement elemSurfaceMember = doc.createElementNS( ns, "surfaceMember" );
73  elemSurfaceMember.appendChild( geom->asGML3( doc, precision, ns ) );
74  elemMultiSurface.appendChild( elemSurfaceMember );
75  }
76  }
77 
78  return elemMultiSurface;
79 }
80 
81 QString QgsMultiSurfaceV2::asJSON( int precision ) const
82 {
83  // GeoJSON does not support curves
84  QString json = "{\"type\": \"MultiPolygon\", \"coordinates\": [";
85  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
86  {
87  if ( dynamic_cast<const QgsSurfaceV2*>( geom ) )
88  {
89  json += '[';
90 
91  QgsPolygonV2* polygon = static_cast<const QgsSurfaceV2*>( geom )->surfaceToPolygon();
92 
93  QgsLineStringV2* exteriorLineString = polygon->exteriorRing()->curveToLine();
94  QgsPointSequenceV2 exteriorPts;
95  exteriorLineString->points( exteriorPts );
96  json += QgsGeometryUtils::pointsToJSON( exteriorPts, precision ) + ", ";
97  delete exteriorLineString;
98 
99  for ( int i = 0, n = polygon->numInteriorRings(); i < n; ++i )
100  {
101  QgsLineStringV2* interiorLineString = polygon->interiorRing( i )->curveToLine();
102  QgsPointSequenceV2 interiorPts;
103  interiorLineString->points( interiorPts );
104  json += QgsGeometryUtils::pointsToJSON( interiorPts, precision ) + ", ";
105  delete interiorLineString;
106  }
107  if ( json.endsWith( ", " ) )
108  {
109  json.chop( 2 ); // Remove last ", "
110  }
111 
112  delete polygon;
113 
114  json += "], ";
115  }
116  }
117  if ( json.endsWith( ", " ) )
118  {
119  json.chop( 2 ); // Remove last ", "
120  }
121  json += "] }";
122  return json;
123 }
124 
126 {
127  if ( !dynamic_cast<QgsSurfaceV2*>( g ) )
128  {
129  delete g;
130  return false;
131  }
132 
135 }
136 
138 {
139  QgsMultiCurveV2* multiCurve = new QgsMultiCurveV2();
140  for ( int i = 0; i < mGeometries.size(); ++i )
141  {
142  if ( QgsSurfaceV2* surface = dynamic_cast<QgsSurfaceV2*>( mGeometries.at( i ) ) )
143  {
144  multiCurve->addGeometry( surface->boundary() );
145  }
146  }
147  if ( multiCurve->numGeometries() == 0 )
148  {
149  delete multiCurve;
150  return nullptr;
151  }
152  return multiCurve;
153 }
const QgsCurveV2 * exteriorRing() const
QgsMultiSurfaceV2 * clone() const override
Clones the geometry by performing a deep copy.
QDomElement asGML2(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML2 representation of the geometry.
QDomNode appendChild(const QDomNode &newChild)
void points(QgsPointSequenceV2 &pt) const override
Returns a list of points within the curve.
Multi curve geometry collection.
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 createElementNS(const QString &nsURI, const QString &qName)
void chop(int n)
virtual QgsAbstractGeometryV2 * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
Polygon geometry type.
Definition: qgspolygonv2.h:29
Line string geometry type, with support for z-dimension and m-values.
QDomElement asGML3(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML3 representation of the geometry.
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
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.
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
QDomElement asGML2(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML2 representation of the geometry.
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...
Curve polygon geometry type.
int numInteriorRings() const
int size() const
QString asJSON(int precision=17) const override
Returns a GeoJSON representation of the geometry.