QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsmultisurface.cpp
Go to the documentation of this file.
1 
2 /***************************************************************************
3  qgsmultisurface.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 "qgsmultisurface.h"
18 #include "qgsapplication.h"
19 #include "qgsgeometryutils.h"
20 #include "qgssurface.h"
21 #include "qgslinestring.h"
22 #include "qgspolygon.h"
23 #include "qgscurvepolygon.h"
24 #include "qgsmulticurve.h"
25 
27 {
29 }
30 
32 {
33  return QStringLiteral( "MultiSurface" );
34 }
35 
37 {
40 }
41 
43 {
44  auto result = qgis::make_unique< QgsMultiSurface >();
45  result->mWkbType = mWkbType;
46  return result.release();
47 }
48 
50 {
51  return new QgsMultiSurface( *this );
52 }
53 
55 {
56  return clone();
57 }
58 
59 bool QgsMultiSurface::fromWkt( const QString &wkt )
60 {
61  return fromCollectionWkt( wkt,
62  QVector<QgsAbstractGeometry *>() << new QgsPolygon << new QgsCurvePolygon,
63  QStringLiteral( "Polygon" ) );
64 }
65 
66 QDomElement QgsMultiSurface::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
67 {
68  // GML2 does not support curves
69  QDomElement elemMultiPolygon = doc.createElementNS( ns, QStringLiteral( "MultiPolygon" ) );
70 
71  if ( isEmpty() )
72  return elemMultiPolygon;
73 
74  for ( const QgsAbstractGeometry *geom : mGeometries )
75  {
76  if ( qgsgeometry_cast<const QgsSurface *>( geom ) )
77  {
78  std::unique_ptr< QgsPolygon > polygon( static_cast<const QgsSurface *>( geom )->surfaceToPolygon() );
79 
80  QDomElement elemPolygonMember = doc.createElementNS( ns, QStringLiteral( "polygonMember" ) );
81  elemPolygonMember.appendChild( polygon->asGml2( doc, precision, ns, axisOrder ) );
82  elemMultiPolygon.appendChild( elemPolygonMember );
83  }
84  }
85 
86  return elemMultiPolygon;
87 }
88 
89 QDomElement QgsMultiSurface::asGml3( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
90 {
91  QDomElement elemMultiSurface = doc.createElementNS( ns, QStringLiteral( "MultiSurface" ) );
92 
93  if ( isEmpty() )
94  return elemMultiSurface;
95 
96  for ( const QgsAbstractGeometry *geom : mGeometries )
97  {
98  if ( qgsgeometry_cast<const QgsSurface *>( geom ) )
99  {
100  QDomElement elemSurfaceMember = doc.createElementNS( ns, QStringLiteral( "surfaceMember" ) );
101  elemSurfaceMember.appendChild( geom->asGml3( doc, precision, ns, axisOrder ) );
102  elemMultiSurface.appendChild( elemSurfaceMember );
103  }
104  }
105 
106  return elemMultiSurface;
107 }
108 
109 QString QgsMultiSurface::asJson( int precision ) const
110 {
111  // GeoJSON does not support curves
112  QString json = QStringLiteral( "{\"type\": \"MultiPolygon\", \"coordinates\": [" );
113  for ( const QgsAbstractGeometry *geom : mGeometries )
114  {
115  if ( qgsgeometry_cast<const QgsSurface *>( geom ) )
116  {
117  json += '[';
118 
119  std::unique_ptr< QgsPolygon >polygon( static_cast<const QgsSurface *>( geom )->surfaceToPolygon() );
120 
121  std::unique_ptr< QgsLineString > exteriorLineString( polygon->exteriorRing()->curveToLine() );
122  QgsPointSequence exteriorPts;
123  exteriorLineString->points( exteriorPts );
124  json += QgsGeometryUtils::pointsToJSON( exteriorPts, precision ) + ", ";
125 
126  std::unique_ptr< QgsLineString > interiorLineString;
127  for ( int i = 0, n = polygon->numInteriorRings(); i < n; ++i )
128  {
129  interiorLineString.reset( polygon->interiorRing( i )->curveToLine() );
130  QgsPointSequence interiorPts;
131  interiorLineString->points( interiorPts );
132  json += QgsGeometryUtils::pointsToJSON( interiorPts, precision ) + ", ";
133  }
134  if ( json.endsWith( QLatin1String( ", " ) ) )
135  {
136  json.chop( 2 ); // Remove last ", "
137  }
138 
139  json += QLatin1String( "], " );
140  }
141  }
142  if ( json.endsWith( QLatin1String( ", " ) ) )
143  {
144  json.chop( 2 ); // Remove last ", "
145  }
146  json += QLatin1String( "] }" );
147  return json;
148 }
149 
151 {
152  if ( !qgsgeometry_cast<QgsSurface *>( g ) )
153  {
154  delete g;
155  return false;
156  }
157 
158  if ( mGeometries.empty() )
159  {
161  }
162  if ( is3D() && !g->is3D() )
163  g->addZValue();
164  else if ( !is3D() && g->is3D() )
165  g->dropZValue();
166  if ( isMeasure() && !g->isMeasure() )
167  g->addMValue();
168  else if ( !isMeasure() && g->isMeasure() )
169  g->dropMValue();
170 
172 }
173 
175 {
176  if ( !g || !qgsgeometry_cast< QgsSurface * >( g ) )
177  {
178  delete g;
179  return false;
180  }
181 
182  return QgsGeometryCollection::insertGeometry( g, index );
183 }
184 
186 {
187  std::unique_ptr< QgsMultiCurve > multiCurve( new QgsMultiCurve() );
188  for ( int i = 0; i < mGeometries.size(); ++i )
189  {
190  if ( QgsSurface *surface = qgsgeometry_cast<QgsSurface *>( mGeometries.at( i ) ) )
191  {
192  multiCurve->addGeometry( surface->boundary() );
193  }
194  }
195  if ( multiCurve->numGeometries() == 0 )
196  {
197  return nullptr;
198  }
199  return multiCurve.release();
200 }
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.
QgsMultiSurface * toCurveType() const override
Returns the geometry converted to the more generic curve type.
static QString pointsToJSON(const QgsPointSequence &points, int precision)
Returns a geoJSON coordinates string.
int precision
void clear() override
Clears the geometry, ie reset it to a null geometry.
QString asJson(int precision=17) const override
Returns a GeoJSON representation of the geometry.
QgsMultiSurface * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
Curve polygon geometry type.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
void clear() override
Clears the geometry, ie reset it to a null geometry.
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.
Multi surface geometry collection.
QgsWkbTypes::Type mWkbType
virtual bool insertGeometry(QgsAbstractGeometry *g, int index)
Inserts a geometry before a specified index and takes ownership.
bool isEmpty() const override
Returns true if the geometry is empty.
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
bool isMeasure() const
Returns true if the geometry contains m values.
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
void setZMTypeFromSubGeometry(const QgsAbstractGeometry *subggeom, QgsWkbTypes::Type baseGeomType)
Updates the geometry type based on whether sub geometries contain z or m values.
Multi curve geometry collection.
Definition: qgsmulticurve.h:29
Abstract base class for all geometries.
AxisOrder
Axis order for GML generation.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
QVector< QgsPoint > QgsPointSequence
QVector< QgsAbstractGeometry * > mGeometries
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
QString geometryType() const override
Returns a unique string representing the geometry type.
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
Polygon geometry type.
Definition: qgspolygon.h:31
QgsMultiSurface * clone() const override
Clones the geometry by performing a deep copy.
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
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.
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...