QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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  for ( int i = 0; i < mGeometries.size(); ++i )
159  {
160  multiCurve->addGeometry( mGeometries.at( i )->toCurveType() );
161  }
162  return multiCurve;
163 }
164 
166 {
167  return true;
168 }
169 
bool isMeasure() const
Returns true if the geometry contains m values.
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.
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.
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
bool isEmpty() const override
Returns true if the geometry is empty.
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.
Multi curve geometry collection.
Definition: qgsmulticurve.h:29
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
Abstract base class for all geometries.
QgsWkbTypes::Type wkbType() const
Returns the WKB type of the 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.
nlohmann::json json
Definition: qgsjsonutils.h:27
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.
QVector< QgsPoint > QgsPointSequence
QVector< QgsAbstractGeometry *> mGeometries
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
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.
void clear() override
Clears the geometry, ie reset it to a null geometry.
virtual bool dropMValue()=0
Drops any measure values which exist in the geometry.
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:430
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
QString geometryType() const override
Returns a unique string representing the geometry type.
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.