QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
26 #include <QJsonObject>
27 #include <nlohmann/json.hpp>
28 
30 {
32 }
33 
35 {
36  return QStringLiteral( "MultiSurface" );
37 }
38 
40 {
43 }
44 
46 {
47  auto result = qgis::make_unique< QgsMultiSurface >();
48  result->mWkbType = mWkbType;
49  return result.release();
50 }
51 
53 {
54  return new QgsMultiSurface( *this );
55 }
56 
58 {
59  return clone();
60 }
61 
62 bool QgsMultiSurface::fromWkt( const QString &wkt )
63 {
64  return fromCollectionWkt( wkt,
65  QVector<QgsAbstractGeometry *>() << new QgsPolygon << new QgsCurvePolygon,
66  QStringLiteral( "Polygon" ) );
67 }
68 
69 QDomElement QgsMultiSurface::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
70 {
71  // GML2 does not support curves
72  QDomElement elemMultiPolygon = doc.createElementNS( ns, QStringLiteral( "MultiPolygon" ) );
73 
74  if ( isEmpty() )
75  return elemMultiPolygon;
76 
77  for ( const QgsAbstractGeometry *geom : mGeometries )
78  {
79  if ( qgsgeometry_cast<const QgsSurface *>( geom ) )
80  {
81  std::unique_ptr< QgsPolygon > polygon( static_cast<const QgsSurface *>( geom )->surfaceToPolygon() );
82 
83  QDomElement elemPolygonMember = doc.createElementNS( ns, QStringLiteral( "polygonMember" ) );
84  elemPolygonMember.appendChild( polygon->asGml2( doc, precision, ns, axisOrder ) );
85  elemMultiPolygon.appendChild( elemPolygonMember );
86  }
87  }
88 
89  return elemMultiPolygon;
90 }
91 
92 QDomElement QgsMultiSurface::asGml3( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
93 {
94  QDomElement elemMultiSurface = doc.createElementNS( ns, QStringLiteral( "MultiSurface" ) );
95 
96  if ( isEmpty() )
97  return elemMultiSurface;
98 
99  for ( const QgsAbstractGeometry *geom : mGeometries )
100  {
101  if ( qgsgeometry_cast<const QgsSurface *>( geom ) )
102  {
103  QDomElement elemSurfaceMember = doc.createElementNS( ns, QStringLiteral( "surfaceMember" ) );
104  elemSurfaceMember.appendChild( geom->asGml3( doc, precision, ns, axisOrder ) );
105  elemMultiSurface.appendChild( elemSurfaceMember );
106  }
107  }
108 
109  return elemMultiSurface;
110 }
111 
112 
114 {
115  json polygons( json::array( ) );
116  for ( const QgsAbstractGeometry *geom : qgis::as_const( mGeometries ) )
117  {
118  if ( qgsgeometry_cast<const QgsSurface *>( geom ) )
119  {
120  json coordinates( json::array( ) );
121  std::unique_ptr< QgsPolygon >polygon( static_cast<const QgsSurface *>( geom )->surfaceToPolygon() );
122  std::unique_ptr< QgsLineString > exteriorLineString( polygon->exteriorRing()->curveToLine() );
123  QgsPointSequence exteriorPts;
124  exteriorLineString->points( exteriorPts );
125  coordinates.push_back( QgsGeometryUtils::pointsToJson( exteriorPts, precision ) );
126 
127  std::unique_ptr< QgsLineString > interiorLineString;
128  for ( int i = 0, n = polygon->numInteriorRings(); i < n; ++i )
129  {
130  interiorLineString.reset( polygon->interiorRing( i )->curveToLine() );
131  QgsPointSequence interiorPts;
132  interiorLineString->points( interiorPts );
133  coordinates.push_back( QgsGeometryUtils::pointsToJson( interiorPts, precision ) );
134  }
135  polygons.push_back( coordinates );
136  }
137  }
138  return
139  {
140  { "type", "MultiPolygon" },
141  { "coordinates", polygons }
142  };
143 }
144 
146 {
147  if ( !qgsgeometry_cast<QgsSurface *>( g ) )
148  {
149  delete g;
150  return false;
151  }
152 
153  if ( mGeometries.empty() )
154  {
156  }
157  if ( is3D() && !g->is3D() )
158  g->addZValue();
159  else if ( !is3D() && g->is3D() )
160  g->dropZValue();
161  if ( isMeasure() && !g->isMeasure() )
162  g->addMValue();
163  else if ( !isMeasure() && g->isMeasure() )
164  g->dropMValue();
165 
167 }
168 
170 {
171  if ( !g || !qgsgeometry_cast< QgsSurface * >( g ) )
172  {
173  delete g;
174  return false;
175  }
176 
177  return QgsGeometryCollection::insertGeometry( g, index );
178 }
179 
181 {
182  std::unique_ptr< QgsMultiCurve > multiCurve( new QgsMultiCurve() );
183  multiCurve->reserve( mGeometries.size() );
184  for ( int i = 0; i < mGeometries.size(); ++i )
185  {
186  if ( QgsSurface *surface = qgsgeometry_cast<QgsSurface *>( mGeometries.at( i ) ) )
187  {
188  multiCurve->addGeometry( surface->boundary() );
189  }
190  }
191  if ( multiCurve->numGeometries() == 0 )
192  {
193  return nullptr;
194  }
195  return multiCurve.release();
196 }
bool isMeasure() const
Returns true if the geometry contains m values.
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.
int precision
void clear() override
Clears the geometry, ie reset it to a null 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 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.
bool fromCollectionWkt(const QString &wkt, const QVector< QgsAbstractGeometry *> &subtypes, const QString &defaultChildWkbType=QString())
Reads a collection from a WKT string.
AxisOrder
Axis order for GML generation.
static json pointsToJson(const QgsPointSequence &points, int precision)
Returns coordinates as json object.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
QVector< QgsPoint > QgsPointSequence
QVector< QgsAbstractGeometry *> mGeometries
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset 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.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
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.
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...