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