QGIS API Documentation  3.9.0-Master (224899f119)
qgsmulticurve.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmulticurve.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 "qgsmulticurve.h"
17 #include "qgsapplication.h"
18 #include "qgscurve.h"
19 #include "qgscircularstring.h"
20 #include "qgscompoundcurve.h"
21 #include "qgsgeometryutils.h"
22 #include "qgslinestring.h"
23 #include "qgsmultipoint.h"
24 
25 #include <QJsonObject>
26 #include <memory>
27 #include <nlohmann/json.hpp>
28 
30 {
32 }
33 
35 {
36  return QStringLiteral( "MultiCurve" );
37 }
38 
40 {
41  auto result = qgis::make_unique< QgsMultiCurve >();
42  result->mWkbType = mWkbType;
43  return result.release();
44 }
45 
47 {
48  return new QgsMultiCurve( *this );
49 }
50 
52 {
55 }
56 
58 {
59  return clone();
60 }
61 
62 bool QgsMultiCurve::fromWkt( const QString &wkt )
63 {
64  return fromCollectionWkt( wkt,
65  QVector<QgsAbstractGeometry *>() << new QgsLineString << new QgsCircularString << new QgsCompoundCurve,
66  QStringLiteral( "LineString" ) );
67 }
68 
69 QDomElement QgsMultiCurve::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
70 {
71  // GML2 does not support curves
72  QDomElement elemMultiLineString = doc.createElementNS( ns, QStringLiteral( "MultiLineString" ) );
73 
74  if ( isEmpty() )
75  return elemMultiLineString;
76 
77  for ( const QgsAbstractGeometry *geom : mGeometries )
78  {
79  if ( qgsgeometry_cast<const QgsCurve *>( geom ) )
80  {
81  std::unique_ptr< QgsLineString > lineString( static_cast<const QgsCurve *>( geom )->curveToLine() );
82 
83  QDomElement elemLineStringMember = doc.createElementNS( ns, QStringLiteral( "lineStringMember" ) );
84  elemLineStringMember.appendChild( lineString->asGml2( doc, precision, ns, axisOrder ) );
85  elemMultiLineString.appendChild( elemLineStringMember );
86  }
87  }
88 
89  return elemMultiLineString;
90 }
91 
92 QDomElement QgsMultiCurve::asGml3( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
93 {
94  QDomElement elemMultiCurve = doc.createElementNS( ns, QStringLiteral( "MultiCurve" ) );
95 
96  if ( isEmpty() )
97  return elemMultiCurve;
98 
99  for ( const QgsAbstractGeometry *geom : mGeometries )
100  {
101  if ( qgsgeometry_cast<const QgsCurve *>( geom ) )
102  {
103  const QgsCurve *curve = static_cast<const QgsCurve *>( geom );
104 
105  QDomElement elemCurveMember = doc.createElementNS( ns, QStringLiteral( "curveMember" ) );
106  elemCurveMember.appendChild( curve->asGml3( doc, precision, ns, axisOrder ) );
107  elemMultiCurve.appendChild( elemCurveMember );
108  }
109  }
110 
111  return elemMultiCurve;
112 }
113 
115 {
116  json coordinates( json::array( ) );
117  for ( const QgsAbstractGeometry *geom : qgis::as_const( mGeometries ) )
118  {
119  if ( qgsgeometry_cast<const QgsCurve *>( geom ) )
120  {
121  std::unique_ptr< QgsLineString > lineString( static_cast<const QgsCurve *>( geom )->curveToLine() );
122  QgsPointSequence pts;
123  lineString->points( pts );
124  coordinates.push_back( QgsGeometryUtils::pointsToJson( pts, precision ) );
125  }
126  }
127  return
128  {
129  { "type", "MultiLineString" },
130  { "coordinates", coordinates }
131  };
132 }
133 
135 {
136  if ( !qgsgeometry_cast<QgsCurve *>( g ) )
137  {
138  delete g;
139  return false;
140  }
141 
142  if ( mGeometries.empty() )
143  {
145  }
146  if ( is3D() && !g->is3D() )
147  g->addZValue();
148  else if ( !is3D() && g->is3D() )
149  g->dropZValue();
150  if ( isMeasure() && !g->isMeasure() )
151  g->addMValue();
152  else if ( !isMeasure() && g->isMeasure() )
153  g->dropMValue();
154 
156 }
157 
159 {
160  if ( !g || !qgsgeometry_cast<QgsCurve *>( g ) )
161  {
162  delete g;
163  return false;
164  }
165 
166  return QgsGeometryCollection::insertGeometry( g, index );
167 }
168 
170 {
171  QgsMultiCurve *reversedMultiCurve = new QgsMultiCurve();
172  reversedMultiCurve->reserve( mGeometries.size() );
173  for ( const QgsAbstractGeometry *geom : mGeometries )
174  {
175  if ( qgsgeometry_cast<const QgsCurve *>( geom ) )
176  {
177  reversedMultiCurve->addGeometry( static_cast<const QgsCurve *>( geom )->reversed() );
178  }
179  }
180  return reversedMultiCurve;
181 }
182 
184 {
185  std::unique_ptr< QgsMultiPoint > multiPoint( new QgsMultiPoint() );
186  multiPoint->reserve( mGeometries.size() * 2 );
187  for ( int i = 0; i < mGeometries.size(); ++i )
188  {
189  if ( QgsCurve *curve = qgsgeometry_cast<QgsCurve *>( mGeometries.at( i ) ) )
190  {
191  if ( !curve->isClosed() )
192  {
193  multiPoint->addGeometry( new QgsPoint( curve->startPoint() ) );
194  multiPoint->addGeometry( new QgsPoint( curve->endPoint() ) );
195  }
196  }
197  }
198  if ( multiPoint->numGeometries() == 0 )
199  {
200  return nullptr;
201  }
202  return multiPoint.release();
203 }
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.
int precision
QgsMultiCurve * toCurveType() const override
Returns the geometry converted to the more generic curve type.
void clear() override
Clears the geometry, ie reset it to a null geometry.
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
Multi point geometry collection.
Definition: qgsmultipoint.h:29
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
QgsWkbTypes::Type mWkbType
virtual bool insertGeometry(QgsAbstractGeometry *g, int index)
Inserts a geometry before a specified index and takes ownership.
QgsMultiCurve * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
bool isEmpty() const override
Returns true if the geometry is empty.
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 curved geometry type.
Definition: qgscurve.h:35
Abstract base class for all geometries.
QgsAbstractGeometry * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
QString geometryType() const override
Returns a unique string representing the geometry type.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
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.
QVector< QgsPoint > QgsPointSequence
QVector< QgsAbstractGeometry *> mGeometries
void reserve(int size)
Attempts to allocate memory for at least size geometries.
QgsMultiCurve * clone() const override
Clones the geometry by performing a deep copy.
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
void clear() override
Clears the geometry, ie reset it to a null geometry.
Compound curve geometry type.
Circular string geometry type.
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
QgsMultiCurve * reversed() const
Returns a copy of the multi curve, where each component curve has had its line direction reversed...
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.
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.
virtual QDomElement asGml3(QDomDocument &doc, int precision=17, const QString &ns="gml", AxisOrder axisOrder=QgsAbstractGeometry::AxisOrder::XY) const =0
Returns a GML3 representation of the geometry.