QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
26 {
27  return new QgsMultiSurfaceV2( *this );
28 }
29 
31 {
32  return fromCollectionWkt( wkt,
34  "Polygon" );
35 }
36 
37 QDomElement QgsMultiSurfaceV2::asGML2( QDomDocument& doc, int precision, const QString& ns ) const
38 {
39  // GML2 does not support curves
40  QDomElement elemMultiPolygon = doc.createElementNS( ns, "MultiPolygon" );
41  foreach ( const QgsAbstractGeometryV2 *geom, mGeometries )
42  {
43  if ( dynamic_cast<const QgsSurfaceV2*>( geom ) )
44  {
45  QgsPolygonV2* polygon = static_cast<const QgsSurfaceV2*>( geom )->surfaceToPolygon();
46 
47  QDomElement elemPolygonMember = doc.createElementNS( ns, "polygonMember" );
48  elemPolygonMember.appendChild( polygon->asGML2( doc, precision, ns ) );
49  elemMultiPolygon.appendChild( elemPolygonMember );
50 
51  delete polygon;
52  }
53  }
54 
55  return elemMultiPolygon;
56 }
57 
58 QDomElement QgsMultiSurfaceV2::asGML3( QDomDocument& doc, int precision, const QString& ns ) const
59 {
60  QDomElement elemMultiSurface = doc.createElementNS( ns, "MultiSurface" );
61  foreach ( const QgsAbstractGeometryV2 *geom, mGeometries )
62  {
63  if ( dynamic_cast<const QgsSurfaceV2*>( geom ) )
64  {
65  QDomElement elemSurfaceMember = doc.createElementNS( ns, "surfaceMember" );
66  elemSurfaceMember.appendChild( geom->asGML3( doc, precision, ns ) );
67  elemMultiSurface.appendChild( elemSurfaceMember );
68  }
69  }
70 
71  return elemMultiSurface;
72 }
73 
74 QString QgsMultiSurfaceV2::asJSON( int precision ) const
75 {
76  // GeoJSON does not support curves
77  QString json = "{\"type\": \"MultiPolygon\", \"coordinates\": [";
78  foreach ( const QgsAbstractGeometryV2 *geom, mGeometries )
79  {
80  if ( dynamic_cast<const QgsSurfaceV2*>( geom ) )
81  {
82  json += "[";
83 
84  QgsPolygonV2* polygon = static_cast<const QgsSurfaceV2*>( geom )->surfaceToPolygon();
85 
86  QgsLineStringV2* exteriorLineString = polygon->exteriorRing()->curveToLine();
87  QList<QgsPointV2> 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  QList<QgsPointV2> 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  delete polygon;
106 
107  json += "], ";
108  }
109  }
110  if ( json.endsWith( ", " ) )
111  {
112  json.chop( 2 ); // Remove last ", "
113  }
114  json += "] }";
115  return json;
116 }
117 
119 {
120  if ( !dynamic_cast<QgsSurfaceV2*>( g ) )
121  {
122  delete g;
123  return false;
124  }
125 
128 }
const QgsCurveV2 * exteriorRing() const
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)
QgsAbstractGeometryV2 * clone() const override
Clones the geometry by performing a deep copy.
void points(QList< QgsPointV2 > &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 QList< QgsPointV2 > &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)
Polygon geometry type.
Definition: qgspolygonv2.h:29
Line string geometry type.
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.
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
virtual QgsLineStringV2 * curveToLine() const =0
Returns a new line string geometry corresponding to a segmentized approximation of the curve...
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.
Curve polygon geometry type.
int numInteriorRings() const
QString asJSON(int precision=17) const override
Returns a GeoJSON representation of the geometry.