QGIS API Documentation  3.17.0-Master (3b262f2a79)
qgsmultilinestring.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmultilinestring.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 "qgsmultilinestring.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 "qgsmulticurve.h"
24 
25 #include <nlohmann/json.hpp>
26 #include <QJsonObject>
27 
29 {
31 }
32 
34 {
35  return qgsgeometry_cast< QgsLineString * >( geometryN( index ) );
36 }
37 
39 {
40  return qgsgeometry_cast< const QgsLineString * >( geometryN( index ) );
41 }
42 
44 {
45  return QStringLiteral( "MultiLineString" );
46 }
47 
49 {
50  auto result = qgis::make_unique< QgsMultiLineString >();
51  result->mWkbType = mWkbType;
52  return result.release();
53 }
54 
56 {
57  return new QgsMultiLineString( *this );
58 }
59 
61 {
64 }
65 
66 bool QgsMultiLineString::fromWkt( const QString &wkt )
67 {
68  return fromCollectionWkt( wkt, QVector<QgsAbstractGeometry *>() << new QgsLineString, QStringLiteral( "LineString" ) );
69 }
70 
71 QDomElement QgsMultiLineString::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
72 {
73  QDomElement elemMultiLineString = doc.createElementNS( ns, QStringLiteral( "MultiLineString" ) );
74 
75  if ( isEmpty() )
76  return elemMultiLineString;
77 
78  for ( const QgsAbstractGeometry *geom : mGeometries )
79  {
80  if ( const QgsLineString *lineString = qgsgeometry_cast<const QgsLineString *>( geom ) )
81  {
82  QDomElement elemLineStringMember = doc.createElementNS( ns, QStringLiteral( "lineStringMember" ) );
83  elemLineStringMember.appendChild( lineString->asGml2( doc, precision, ns, axisOrder ) );
84  elemMultiLineString.appendChild( elemLineStringMember );
85  }
86  }
87 
88  return elemMultiLineString;
89 }
90 
91 QDomElement QgsMultiLineString::asGml3( QDomDocument &doc, int precision, const QString &ns, const QgsAbstractGeometry::AxisOrder axisOrder ) const
92 {
93  QDomElement elemMultiCurve = doc.createElementNS( ns, QStringLiteral( "MultiCurve" ) );
94 
95  if ( isEmpty() )
96  return elemMultiCurve;
97 
98  for ( const QgsAbstractGeometry *geom : mGeometries )
99  {
100  if ( const QgsLineString *lineString = qgsgeometry_cast<const QgsLineString *>( geom ) )
101  {
102  QDomElement elemCurveMember = doc.createElementNS( ns, QStringLiteral( "curveMember" ) );
103  elemCurveMember.appendChild( lineString->asGml3( doc, precision, ns, axisOrder ) );
104  elemMultiCurve.appendChild( elemCurveMember );
105  }
106  }
107 
108  return elemMultiCurve;
109 }
110 
112 {
113  json coordinates( json::array( ) );
114  for ( const QgsAbstractGeometry *geom : mGeometries )
115  {
116  if ( qgsgeometry_cast<const QgsCurve *>( geom ) )
117  {
118  const QgsLineString *lineString = static_cast<const QgsLineString *>( geom );
119  QgsPointSequence pts;
120  lineString->points( pts );
121  coordinates.push_back( QgsGeometryUtils::pointsToJson( pts, precision ) );
122  }
123  }
124  return
125  {
126  { "type", "MultiLineString" },
127  { "coordinates", coordinates }
128  };
129 }
130 
132 {
133  if ( !dynamic_cast<QgsLineString *>( g ) )
134  {
135  delete g;
136  return false;
137  }
138 
139  if ( mGeometries.empty() )
140  {
142  }
143  if ( is3D() && !g->is3D() )
144  g->addZValue();
145  else if ( !is3D() && g->is3D() )
146  g->dropZValue();
147  if ( isMeasure() && !g->isMeasure() )
148  g->addMValue();
149  else if ( !isMeasure() && g->isMeasure() )
150  g->dropMValue();
151  return QgsGeometryCollection::addGeometry( g ); // clazy:exclude=skipped-base-method
152 }
153 
155 {
157  {
158  delete g;
159  return false;
160  }
161 
162  return QgsMultiCurve::insertGeometry( g, index ); // clazy:exclude=skipped-base-method
163 }
164 
166 {
167  QgsMultiCurve *multiCurve = new QgsMultiCurve();
168  multiCurve->reserve( mGeometries.size() );
169  for ( int i = 0; i < mGeometries.size(); ++i )
170  {
171  multiCurve->addGeometry( mGeometries.at( i )->toCurveType() );
172  }
173  return multiCurve;
174 }
175 
177 {
178  return true;
179 }
180 
QgsMultiLineString * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
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.
int precision
void points(QgsPointSequence &pt) const override
Returns a list of points within the curve.
QgsMultiCurve * toCurveType() const override
Returns the geometry converted to the more generic curve type QgsMultiCurve.
QString geometryType() const override SIP_HOLDGIL
Returns a unique string representing the geometry type.
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
QgsMultiLineString * clone() const override
Clones the geometry by performing a deep copy.
Multi line string geometry collection.
static Type flatType(Type type) SIP_HOLDGIL
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:702
void clear() override
Clears the geometry, ie reset it to a null geometry.
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
QgsWkbTypes::Type mWkbType
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
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.
T qgsgeometry_cast(const QgsAbstractGeometry *geom)
Multi curve geometry collection.
Definition: qgsmulticurve.h:29
bool isEmpty() const override SIP_HOLDGIL
Returns true if the geometry is empty.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
Abstract base class for all geometries.
QgsMultiLineString() SIP_HOLDGIL
Constructor for an empty multilinestring geometry.
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.
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
static json pointsToJson(const QgsPointSequence &points, int precision)
Returns coordinates as json object.
void reserve(int size) SIP_HOLDGIL
Attempts to allocate memory for at least size geometries.
QVector< QgsPoint > QgsPointSequence
QVector< QgsAbstractGeometry *> mGeometries
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
bool isMeasure() const SIP_HOLDGIL
Returns true if the geometry contains m values.
bool is3D() const SIP_HOLDGIL
Returns true if the geometry is 3D and contains a z-value.
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
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.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
bool wktOmitChildType() const override
Returns whether child type names are omitted from Wkt representations of the collection.
const QgsAbstractGeometry * geometryN(int n) const
Returns a const reference to a geometry from within the collection.
void clear() override
Clears the geometry, ie reset it to a null geometry.
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
QgsLineString * lineStringN(int index)
Returns the line string with the specified index.
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.