QGIS API Documentation 3.37.0-Master (fa0c88a90b0)
qgsmultilinestring.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmultilinestring.cpp
3 -------------------------------------------------------------------
4Date : 28 Oct 2014
5Copyright : (C) 2014 by Marco Hugentobler
6email : 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 "qgsmultilinestring.h"
17#include "qgsabstractgeometry.h"
18#include "qgsapplication.h"
19#include "qgscurve.h"
20#include "qgscircularstring.h"
21#include "qgscompoundcurve.h"
22#include "qgsgeometryutils.h"
23#include "qgslinestring.h"
24#include "qgsmulticurve.h"
25
26#include <nlohmann/json.hpp>
27#include <QJsonObject>
28
30{
32}
33
35{
36 return qgsgeometry_cast< QgsLineString * >( geometryN( index ) );
37}
38
40{
41 return qgsgeometry_cast< const QgsLineString * >( geometryN( index ) );
42}
43
45{
46 return QStringLiteral( "MultiLineString" );
47}
48
50{
51 auto result = std::make_unique< QgsMultiLineString >();
52 result->mWkbType = mWkbType;
53 return result.release();
54}
55
57{
58 return new QgsMultiLineString( *this );
59}
60
62{
65}
66
67bool QgsMultiLineString::fromWkt( const QString &wkt )
68{
69 return fromCollectionWkt( wkt, QVector<QgsAbstractGeometry *>() << new QgsLineString, QStringLiteral( "LineString" ) );
70}
71
72QDomElement QgsMultiLineString::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
73{
74 QDomElement elemMultiLineString = doc.createElementNS( ns, QStringLiteral( "MultiLineString" ) );
75
76 if ( isEmpty() )
77 return elemMultiLineString;
78
79 for ( const QgsAbstractGeometry *geom : mGeometries )
80 {
81 if ( const QgsLineString *lineString = qgsgeometry_cast<const QgsLineString *>( geom ) )
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
92QDomElement QgsMultiLineString::asGml3( QDomDocument &doc, int precision, const QString &ns, const QgsAbstractGeometry::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 ( const QgsLineString *lineString = qgsgeometry_cast<const QgsLineString *>( geom ) )
102 {
103 QDomElement elemCurveMember = doc.createElementNS( ns, QStringLiteral( "curveMember" ) );
104 elemCurveMember.appendChild( lineString->asGml3( doc, precision, ns, axisOrder ) );
105 elemMultiCurve.appendChild( elemCurveMember );
106 }
107 }
108
109 return elemMultiCurve;
110}
111
113{
114 json coordinates( json::array( ) );
115 for ( const QgsAbstractGeometry *geom : mGeometries )
116 {
117 if ( qgsgeometry_cast<const QgsCurve *>( geom ) )
118 {
119 const QgsLineString *lineString = static_cast<const QgsLineString *>( geom );
121 lineString->points( pts );
122 coordinates.push_back( QgsGeometryUtils::pointsToJson( pts, precision ) );
123 }
124 }
125 return
126 {
127 { "type", "MultiLineString" },
128 { "coordinates", coordinates }
129 };
130}
131
133{
134 if ( !dynamic_cast<QgsLineString *>( g ) )
135 {
136 delete g;
137 return false;
138 }
139
140 if ( mGeometries.empty() )
141 {
143 }
144 if ( is3D() && !g->is3D() )
145 g->addZValue();
146 else if ( !is3D() && g->is3D() )
147 g->dropZValue();
148 if ( isMeasure() && !g->isMeasure() )
149 g->addMValue();
150 else if ( !isMeasure() && g->isMeasure() )
151 g->dropMValue();
152 return QgsGeometryCollection::addGeometry( g ); // NOLINT(bugprone-parent-virtual-call) clazy:exclude=skipped-base-method
153}
154
156{
158 {
159 delete g;
160 return false;
161 }
162
163 return QgsMultiCurve::insertGeometry( g, index );
164}
165
167{
168 QgsMultiCurve *multiCurve = new QgsMultiCurve();
169 multiCurve->reserve( mGeometries.size() );
170 for ( int i = 0; i < mGeometries.size(); ++i )
171 {
172 multiCurve->addGeometry( mGeometries.at( i )->toCurveType() );
173 }
174 return multiCurve;
175}
176
178{
179 return true;
180}
181
182QgsMultiLineString *QgsMultiLineString::measuredLine( double start, double end ) const
183{
184 std::unique_ptr< QgsMultiLineString > result = std::make_unique< QgsMultiLineString >();
185 if ( isEmpty() )
186 {
187 result->convertTo( QgsWkbTypes::addM( mWkbType ) );
188 return result.release();
189 }
190
191 /* Calculate the total length of the line */
192 const double length{this->length()};
193 const double range{end - start};
194 double lengthSoFar{0.0};
195
196 result->reserve( numGeometries() );
197 for ( int i = 0; i < numGeometries(); i++ )
198 {
199 const double subLength{geometryN( i )->length()};
200
201 const double subStart{ ( start + range *lengthSoFar / length ) };
202 const double subEnd{ ( start + range * ( lengthSoFar + subLength ) / length ) };
203
204 result->addGeometry( qgsgeometry_cast<QgsLineString *>( geometryN( i ) )->measuredLine( subStart, subEnd ) );
205
206 lengthSoFar += subLength;
207 }
208
209 return result.release();
210}
@ LineString
LineString.
@ MultiLineString
MultiLineString.
Abstract base class for all geometries.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
bool isMeasure() const
Returns true if the geometry contains m values.
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
AxisOrder
Axis order for GML generation.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
Qgis::WkbType wkbType() const
Returns the WKB type of the geometry.
void setZMTypeFromSubGeometry(const QgsAbstractGeometry *subggeom, Qgis::WkbType baseGeomType)
Updates the geometry type based on whether sub geometries contain z or m values.
virtual double length() const
Returns the planar, 2-dimensional length of the geometry.
virtual bool dropZValue()=0
Drops any z-dimensions which exist in the geometry.
QVector< QgsAbstractGeometry * > mGeometries
void reserve(int size)
Attempts to allocate memory for at least size geometries.
bool isEmpty() const override
Returns true if the geometry is empty.
double length() const override
Returns the planar, 2-dimensional length of 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.
int numGeometries() const
Returns the number of geometries within the collection.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
static json pointsToJson(const QgsPointSequence &points, int precision)
Returns coordinates as json object.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:45
void points(QgsPointSequence &pt) const override
Returns a list of points within the curve.
Multi curve geometry collection.
Definition: qgsmulticurve.h:29
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
void clear() override
Clears the geometry, ie reset it to a null geometry.
Multi line string geometry collection.
void clear() override
Clears the geometry, ie reset it to a null geometry.
QString geometryType() const override
Returns a unique string representing the geometry type.
QgsMultiCurve * toCurveType() const override
Returns the geometry converted to the more generic curve type QgsMultiCurve.
bool wktOmitChildType() const override
Returns whether child type names are omitted from Wkt representations of the collection.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
json asJsonObject(int precision=17) const override
Returns a json object representation of the 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.
QgsMultiLineString * measuredLine(double start, double end) const
Re-write the measure ordinate (or add one, if it isn't already there) interpolating the measure betwe...
QgsMultiLineString * clone() const override
Clones the geometry by performing a deep copy.
QgsMultiLineString * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
QgsMultiLineString()
Constructor for an empty multilinestring geometry.
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
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.
QgsLineString * lineStringN(int index)
Returns the line string with the specified index.
static Qgis::WkbType addM(Qgis::WkbType type)
Adds the m dimension to a WKB type and returns the new type.
Definition: qgswkbtypes.h:1092
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:628
QVector< QgsPoint > QgsPointSequence
int precision