QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
18#include <nlohmann/json.hpp>
19
20#include "qgsabstractgeometry.h"
21#include "qgscurve.h"
22#include "qgsgeometryutils.h"
23#include "qgslinestring.h"
24#include "qgsmulticurve.h"
25
26#include <QJsonObject>
27
32
33QgsMultiLineString::QgsMultiLineString( const QList<QgsLineString> &linestrings )
34{
35 if ( linestrings.empty() )
36 return;
37
38 mGeometries.reserve( linestrings.size() );
39 for ( const QgsLineString &line : linestrings )
40 {
41 mGeometries.append( line.clone() );
42 }
43
45}
46
47QgsMultiLineString::QgsMultiLineString( const QList<QgsLineString *> &linestrings )
48{
49 if ( linestrings.empty() )
50 return;
51
52 mGeometries.reserve( linestrings.size() );
53 for ( QgsLineString *line : linestrings )
54 {
55 mGeometries.append( line );
56 }
57
59}
60
65
70
72{
73 return QStringLiteral( "MultiLineString" );
74}
75
77{
78 auto result = std::make_unique< QgsMultiLineString >();
79 result->mWkbType = mWkbType;
80 return result.release();
81}
82
84{
85 return new QgsMultiLineString( *this );
86}
87
93
94bool QgsMultiLineString::fromWkt( const QString &wkt )
95{
96 return fromCollectionWkt( wkt, {Qgis::WkbType::LineString }, QStringLiteral( "LineString" ) );
97}
98
99QDomElement QgsMultiLineString::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
100{
101 QDomElement elemMultiLineString = doc.createElementNS( ns, QStringLiteral( "MultiLineString" ) );
102
103 if ( isEmpty() )
104 return elemMultiLineString;
105
106 for ( const QgsAbstractGeometry *geom : mGeometries )
107 {
108 if ( const QgsLineString *lineString = qgsgeometry_cast<const QgsLineString *>( geom ) )
109 {
110 QDomElement elemLineStringMember = doc.createElementNS( ns, QStringLiteral( "lineStringMember" ) );
111 elemLineStringMember.appendChild( lineString->asGml2( doc, precision, ns, axisOrder ) );
112 elemMultiLineString.appendChild( elemLineStringMember );
113 }
114 }
115
116 return elemMultiLineString;
117}
118
119QDomElement QgsMultiLineString::asGml3( QDomDocument &doc, int precision, const QString &ns, const QgsAbstractGeometry::AxisOrder axisOrder ) const
120{
121 QDomElement elemMultiCurve = doc.createElementNS( ns, QStringLiteral( "MultiCurve" ) );
122
123 if ( isEmpty() )
124 return elemMultiCurve;
125
126 for ( const QgsAbstractGeometry *geom : mGeometries )
127 {
128 if ( const QgsLineString *lineString = qgsgeometry_cast<const QgsLineString *>( geom ) )
129 {
130 QDomElement elemCurveMember = doc.createElementNS( ns, QStringLiteral( "curveMember" ) );
131 elemCurveMember.appendChild( lineString->asGml3( doc, precision, ns, axisOrder ) );
132 elemMultiCurve.appendChild( elemCurveMember );
133 }
134 }
135
136 return elemMultiCurve;
137}
138
139json QgsMultiLineString::asJsonObject( int precision ) const
140{
141 json coordinates( json::array( ) );
142 for ( const QgsAbstractGeometry *geom : mGeometries )
143 {
145 {
146 const QgsLineString *lineString = static_cast<const QgsLineString *>( geom );
148 lineString->points( pts );
149 coordinates.push_back( QgsGeometryUtils::pointsToJson( pts, precision ) );
150 }
151 }
152 return
153 {
154 { "type", "MultiLineString" },
155 { "coordinates", coordinates }
156 };
157}
158
160{
161 if ( !dynamic_cast<QgsLineString *>( g ) )
162 {
163 delete g;
164 return false;
165 }
166
167 if ( mGeometries.empty() )
168 {
170 }
171 if ( is3D() && !g->is3D() )
172 g->addZValue();
173 else if ( !is3D() && g->is3D() )
174 g->dropZValue();
175 if ( isMeasure() && !g->isMeasure() )
176 g->addMValue();
177 else if ( !isMeasure() && g->isMeasure() )
178 g->dropMValue();
179 return QgsGeometryCollection::addGeometry( g ); // NOLINT(bugprone-parent-virtual-call) clazy:exclude=skipped-base-method
180}
181
182bool QgsMultiLineString::addGeometries( const QVector<QgsAbstractGeometry *> &geometries )
183{
184 for ( QgsAbstractGeometry *g : geometries )
185 {
187 {
188 qDeleteAll( geometries );
189 return false;
190 }
191 }
192
193 if ( mGeometries.empty() && !geometries.empty() )
194 {
196 }
197 mGeometries.reserve( mGeometries.size() + geometries.size() );
198 for ( QgsAbstractGeometry *g : geometries )
199 {
200 if ( is3D() && !g->is3D() )
201 g->addZValue();
202 else if ( !is3D() && g->is3D() )
203 g->dropZValue();
204 if ( isMeasure() && !g->isMeasure() )
205 g->addMValue();
206 else if ( !isMeasure() && g->isMeasure() )
207 g->dropMValue();
208 mGeometries.append( g );
209 }
210
211 clearCache();
212 return true;
213}
214
216{
218 {
219 delete g;
220 return false;
221 }
222
223 return QgsMultiCurve::insertGeometry( g, index );
224}
225
227{
228 auto result = std::make_unique< QgsMultiLineString >();
229 result->reserve( mGeometries.size() );
230 for ( int i = 0; i < mGeometries.size(); ++i )
231 {
232 result->addGeometry( mGeometries.at( i )->simplifyByDistance( tolerance ) );
233 }
234 return result.release();
235}
236
238{
239 QgsMultiCurve *multiCurve = new QgsMultiCurve();
240 multiCurve->reserve( mGeometries.size() );
241 for ( int i = 0; i < mGeometries.size(); ++i )
242 {
243 multiCurve->addGeometry( mGeometries.at( i )->toCurveType() );
244 }
245 return multiCurve;
246}
247
249{
250 return true;
251}
252
253QgsMultiLineString *QgsMultiLineString::measuredLine( double start, double end ) const
254{
255 auto result = std::make_unique< QgsMultiLineString >();
256 if ( isEmpty() )
257 {
258 result->convertTo( QgsWkbTypes::addM( mWkbType ) );
259 return result.release();
260 }
261
262 /* Calculate the total length of the line */
263 const double length{this->length()};
264 const double range{end - start};
265 double lengthSoFar{0.0};
266
267 result->reserve( numGeometries() );
268 for ( int i = 0; i < numGeometries(); i++ )
269 {
270 const double subLength{geometryN( i )->length()};
271
272 const double subStart{ ( start + range *lengthSoFar / length ) };
273 const double subEnd{ ( start + range * ( lengthSoFar + subLength ) / length ) };
274
275 std::unique_ptr< QgsLineString > measuredLine = qgsgeometry_cast<const QgsLineString *>( geometryN( i ) )->measuredLine( subStart, subEnd );
276 result->addGeometry( measuredLine.release() );
277
278 lengthSoFar += subLength;
279 }
280
281 return result.release();
282}
@ LineString
LineString.
Definition qgis.h:280
@ MultiLineString
MultiLineString.
Definition qgis.h:284
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.
QgsAbstractGeometry()=default
QVector< QgsAbstractGeometry * > mGeometries
bool fromCollectionWkt(const QString &wkt, const QVector< Qgis::WkbType > &subtypes, const QString &defaultChildWkbType=QString())
Reads a collection from a WKT string.
void reserve(int size)
Attempts to allocate memory for at least size geometries.
void clearCache() const override
Clears any cached parameters associated with the geometry, e.g., bounding boxes.
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.
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.
void points(QgsPointSequence &pt) const override
Returns a list of points within the curve.
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.
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.
bool addGeometries(const QVector< QgsAbstractGeometry * > &geometries) final
Adds a list of geometries to the collection, transferring ownership to the collection.
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.
QgsMultiLineString * simplifyByDistance(double tolerance) const override
Simplifies the geometry by applying the Douglas Peucker simplification by distance algorithm.
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.
static Qgis::WkbType flatType(Qgis::WkbType type)
Returns the flat type for a WKB type.
T qgsgeometry_cast(QgsAbstractGeometry *geom)
QVector< QgsPoint > QgsPointSequence