QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 
QgsAbstractGeometry::dropMValue
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
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
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::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.
qgscompoundcurve.h
QgsAbstractGeometry::addMValue
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
QgsWkbTypes::flatType
static Type flatType(Type type) SIP_HOLDGIL
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:702
QgsWkbTypes::LineString
@ LineString
Definition: qgswkbtypes.h:73
QgsMultiLineString::createEmptyWithSameType
QgsMultiLineString * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership.
Definition: qgsmultilinestring.cpp:48
QgsMultiLineString::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: qgsmultilinestring.cpp:91
QgsMultiLineString
Multi line string geometry collection.
Definition: qgsmultilinestring.h:32
QgsGeometryCollection::mGeometries
QVector< QgsAbstractGeometry * > mGeometries
Definition: qgsgeometrycollection.h:327
QgsMultiLineString::insertGeometry
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
Definition: qgsmultilinestring.cpp:154
QgsMultiLineString::geometryType
QString geometryType() const override SIP_HOLDGIL
Returns a unique string representing the geometry type.
Definition: qgsmultilinestring.cpp:43
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
QgsWkbTypes::MultiLineString
@ MultiLineString
Definition: qgswkbtypes.h:77
QgsMultiLineString::fromWkt
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
Definition: qgsmultilinestring.cpp:66
QgsMultiLineString::clone
QgsMultiLineString * clone() const override
Clones the geometry by performing a deep copy.
Definition: qgsmultilinestring.cpp:55
QgsAbstractGeometry::wkbType
QgsWkbTypes::Type wkbType() const SIP_HOLDGIL
Returns the WKB type of the geometry.
Definition: qgsabstractgeometry.h:193
QgsMultiLineString::clear
void clear() override
Clears the geometry, ie reset it to a null geometry.
Definition: qgsmultilinestring.cpp:60
QgsAbstractGeometry::AxisOrder
AxisOrder
Axis order for GML generation.
Definition: qgsabstractgeometry.h:133
QgsMultiCurve
Multi curve geometry collection.
Definition: qgsmulticurve.h:30
QgsGeometryCollection::addGeometry
virtual bool addGeometry(QgsAbstractGeometry *g)
Adds a geometry and takes ownership. Returns true in case of success.
Definition: qgsgeometrycollection.cpp:226
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
QgsMultiLineString::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: qgsmultilinestring.cpp:71
QgsMultiLineString::asJsonObject
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
Definition: qgsmultilinestring.cpp:111
qgscircularstring.h
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
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
QgsMultiLineString::lineStringN
QgsLineString * lineStringN(int index)
Returns the line string with the specified index.
Definition: qgsmultilinestring.cpp:33
QgsMultiLineString::QgsMultiLineString
QgsMultiLineString() SIP_HOLDGIL
Constructor for an empty multilinestring geometry.
Definition: qgsmultilinestring.cpp:28
QgsMultiLineString::addGeometry
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
Definition: qgsmultilinestring.cpp:131
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
QgsLineString::points
void points(QgsPointSequence &pt) const override
Returns a list of points within the curve.
Definition: qgslinestring.cpp:798
QgsMultiLineString::toCurveType
QgsMultiCurve * toCurveType() const override
Returns the geometry converted to the more generic curve type QgsMultiCurve.
Definition: qgsmultilinestring.cpp:165
qgsmultilinestring.h
QgsMultiCurve::insertGeometry
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
Definition: qgsmulticurve.cpp:168
QgsMultiLineString::wktOmitChildType
bool wktOmitChildType() const override
Returns whether child type names are omitted from Wkt representations of the collection.
Definition: qgsmultilinestring.cpp:176