QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 QStringLiteral( "MultiLineString" );
36 }
37 
39 {
40  auto result = qgis::make_unique< QgsMultiLineString >();
41  result->mWkbType = mWkbType;
42  return result.release();
43 }
44 
46 {
47  return new QgsMultiLineString( *this );
48 }
49 
51 {
54 }
55 
56 bool QgsMultiLineString::fromWkt( const QString &wkt )
57 {
58  return fromCollectionWkt( wkt, QVector<QgsAbstractGeometry *>() << new QgsLineString, QStringLiteral( "LineString" ) );
59 }
60 
61 QDomElement QgsMultiLineString::asGml2( QDomDocument &doc, int precision, const QString &ns, const AxisOrder axisOrder ) const
62 {
63  QDomElement elemMultiLineString = doc.createElementNS( ns, QStringLiteral( "MultiLineString" ) );
64 
65  if ( isEmpty() )
66  return elemMultiLineString;
67 
68  for ( const QgsAbstractGeometry *geom : mGeometries )
69  {
70  if ( const QgsLineString *lineString = qgsgeometry_cast<const QgsLineString *>( geom ) )
71  {
72  QDomElement elemLineStringMember = doc.createElementNS( ns, QStringLiteral( "lineStringMember" ) );
73  elemLineStringMember.appendChild( lineString->asGml2( doc, precision, ns, axisOrder ) );
74  elemMultiLineString.appendChild( elemLineStringMember );
75  }
76  }
77 
78  return elemMultiLineString;
79 }
80 
81 QDomElement QgsMultiLineString::asGml3( QDomDocument &doc, int precision, const QString &ns, const QgsAbstractGeometry::AxisOrder axisOrder ) const
82 {
83  QDomElement elemMultiCurve = doc.createElementNS( ns, QStringLiteral( "MultiCurve" ) );
84 
85  if ( isEmpty() )
86  return elemMultiCurve;
87 
88  for ( const QgsAbstractGeometry *geom : mGeometries )
89  {
90  if ( const QgsLineString *lineString = qgsgeometry_cast<const QgsLineString *>( geom ) )
91  {
92  QDomElement elemCurveMember = doc.createElementNS( ns, QStringLiteral( "curveMember" ) );
93  elemCurveMember.appendChild( lineString->asGml3( doc, precision, ns, axisOrder ) );
94  elemMultiCurve.appendChild( elemCurveMember );
95  }
96  }
97 
98  return elemMultiCurve;
99 }
100 
102 {
103  json coordinates( json::array( ) );
104  for ( const QgsAbstractGeometry *geom : mGeometries )
105  {
106  if ( qgsgeometry_cast<const QgsCurve *>( geom ) )
107  {
108  const QgsLineString *lineString = static_cast<const QgsLineString *>( geom );
109  QgsPointSequence pts;
110  lineString->points( pts );
111  coordinates.push_back( QgsGeometryUtils::pointsToJson( pts, precision ) );
112  }
113  }
114  return
115  {
116  { "type", "MultiLineString" },
117  { "coordinates", coordinates }
118  };
119 }
120 
122 {
123  if ( !dynamic_cast<QgsLineString *>( g ) )
124  {
125  delete g;
126  return false;
127  }
128 
129  if ( mGeometries.empty() )
130  {
132  }
133  if ( is3D() && !g->is3D() )
134  g->addZValue();
135  else if ( !is3D() && g->is3D() )
136  g->dropZValue();
137  if ( isMeasure() && !g->isMeasure() )
138  g->addMValue();
139  else if ( !isMeasure() && g->isMeasure() )
140  g->dropMValue();
141  return QgsGeometryCollection::addGeometry( g ); // clazy:exclude=skipped-base-method
142 }
143 
145 {
147  {
148  delete g;
149  return false;
150  }
151 
152  return QgsMultiCurve::insertGeometry( g, index ); // clazy:exclude=skipped-base-method
153 }
154 
156 {
157  QgsMultiCurve *multiCurve = new QgsMultiCurve();
158  multiCurve->reserve( mGeometries.size() );
159  for ( int i = 0; i < mGeometries.size(); ++i )
160  {
161  multiCurve->addGeometry( mGeometries.at( i )->toCurveType() );
162  }
163  return multiCurve;
164 }
165 
167 {
168  return true;
169 }
170 
QgsAbstractGeometry::dropMValue
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
QgsMultiCurve::addGeometry
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
Definition: qgsmulticurve.cpp:134
QgsAbstractGeometry::wkbType
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the geometry.
Definition: qgsabstractgeometry.h:189
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:1268
QgsMultiCurve::clear
void clear() override
Clears the geometry, ie reset it to a null geometry.
Definition: qgsmulticurve.cpp:51
qgslinestring.h
QgsAbstractGeometry::addZValue
virtual bool addZValue(double zValue=0)=0
Adds a z-dimension to the geometry, initialized to a preset value.
QgsGeometryCollection::reserve
void reserve(int size)
Attempts to allocate memory for at least size geometries.
Definition: qgsgeometrycollection.cpp:202
QgsMultiLineString::geometryType
QString geometryType() const override
Returns a unique string representing the geometry type.
Definition: qgsmultilinestring.cpp:33
qgscompoundcurve.h
QgsAbstractGeometry::addMValue
virtual bool addMValue(double mValue=0)=0
Adds a measure to the geometry, initialized to a preset value.
QgsWkbTypes::LineString
@ LineString
Definition: qgswkbtypes.h:72
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:38
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:81
QgsMultiLineString
Multi line string geometry collection.
Definition: qgsmultilinestring.h:29
QgsGeometryCollection::mGeometries
QVector< QgsAbstractGeometry * > mGeometries
Definition: qgsgeometrycollection.h:317
QgsMultiLineString::insertGeometry
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
Definition: qgsmultilinestring.cpp:144
QgsMultiCurve::QgsMultiCurve
QgsMultiCurve()
Definition: qgsmulticurve.cpp:29
QgsAbstractGeometry::mWkbType
QgsWkbTypes::Type mWkbType
Definition: qgsabstractgeometry.h:1005
QgsLineString
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:43
qgsapplication.h
precision
int precision
Definition: qgswfsgetfeature.cpp:103
QgsWkbTypes::MultiLineString
@ MultiLineString
Definition: qgswkbtypes.h:76
QgsMultiLineString::fromWkt
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
Definition: qgsmultilinestring.cpp:56
QgsMultiLineString::clone
QgsMultiLineString * clone() const override
Clones the geometry by performing a deep copy.
Definition: qgsmultilinestring.cpp:45
QgsMultiLineString::clear
void clear() override
Clears the geometry, ie reset it to a null geometry.
Definition: qgsmultilinestring.cpp:50
QgsAbstractGeometry::AxisOrder
AxisOrder
Axis order for GML generation.
Definition: qgsabstractgeometry.h:128
QgsGeometryCollection::isEmpty
bool isEmpty() const override
Returns true if the geometry is empty.
Definition: qgsgeometrycollection.cpp:213
QgsMultiCurve
Multi curve geometry collection.
Definition: qgsmulticurve.h:29
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:676
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:61
QgsMultiLineString::asJsonObject
json asJsonObject(int precision=17) const override
Returns a json object representation of the geometry.
Definition: qgsmultilinestring.cpp:101
QgsAbstractGeometry::is3D
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
Definition: qgsabstractgeometry.h:202
qgscircularstring.h
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:71
qgsgeometryutils.h
QgsPointSequence
QVector< QgsPoint > QgsPointSequence
Definition: qgsabstractgeometry.h:44
qgscurve.h
QgsMultiLineString::QgsMultiLineString
QgsMultiLineString()
Definition: qgsmultilinestring.cpp:28
QgsAbstractGeometry::isMeasure
bool isMeasure() const
Returns true if the geometry contains m values.
Definition: qgsabstractgeometry.h:211
QgsMultiLineString::addGeometry
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
Definition: qgsmultilinestring.cpp:121
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
QgsLineString::points
void points(QgsPointSequence &pt) const override
Returns a list of points within the curve.
Definition: qgslinestring.cpp:745
QgsMultiLineString::toCurveType
QgsMultiCurve * toCurveType() const override
Returns the geometry converted to the more generic curve type QgsMultiCurve.
Definition: qgsmultilinestring.cpp:155
QgsWkbTypes::flatType
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:701
qgsmultilinestring.h
QgsMultiCurve::insertGeometry
bool insertGeometry(QgsAbstractGeometry *g, int index) override
Inserts a geometry before a specified index and takes ownership.
Definition: qgsmulticurve.cpp:158
QgsMultiLineString::wktOmitChildType
bool wktOmitChildType() const override
Returns whether child type names are omitted from Wkt representations of the collection.
Definition: qgsmultilinestring.cpp:166