QGIS API Documentation  3.0.2-Girona (307d082)
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 
26 {
28 }
29 
31 {
32  return QStringLiteral( "MultiLineString" );
33 }
34 
36 {
37  auto result = qgis::make_unique< QgsMultiLineString >();
38  result->mWkbType = mWkbType;
39  return result.release();
40 }
41 
43 {
44  return new QgsMultiLineString( *this );
45 }
46 
48 {
51 }
52 
53 bool QgsMultiLineString::fromWkt( const QString &wkt )
54 {
55  return fromCollectionWkt( wkt, QVector<QgsAbstractGeometry *>() << new QgsLineString, QStringLiteral( "LineString" ) );
56 }
57 
58 QDomElement QgsMultiLineString::asGml2( QDomDocument &doc, int precision, const QString &ns ) const
59 {
60  QDomElement elemMultiLineString = doc.createElementNS( ns, QStringLiteral( "MultiLineString" ) );
61 
62  if ( isEmpty() )
63  return elemMultiLineString;
64 
65  for ( const QgsAbstractGeometry *geom : mGeometries )
66  {
67  if ( const QgsLineString *lineString = qgsgeometry_cast<const QgsLineString *>( geom ) )
68  {
69  QDomElement elemLineStringMember = doc.createElementNS( ns, QStringLiteral( "lineStringMember" ) );
70  elemLineStringMember.appendChild( lineString->asGml2( doc, precision, ns ) );
71  elemMultiLineString.appendChild( elemLineStringMember );
72  }
73  }
74 
75  return elemMultiLineString;
76 }
77 
78 QDomElement QgsMultiLineString::asGml3( QDomDocument &doc, int precision, const QString &ns ) const
79 {
80  QDomElement elemMultiCurve = doc.createElementNS( ns, QStringLiteral( "MultiCurve" ) );
81 
82  if ( isEmpty() )
83  return elemMultiCurve;
84 
85  for ( const QgsAbstractGeometry *geom : mGeometries )
86  {
87  if ( const QgsLineString *lineString = qgsgeometry_cast<const QgsLineString *>( geom ) )
88  {
89  QDomElement elemCurveMember = doc.createElementNS( ns, QStringLiteral( "curveMember" ) );
90  elemCurveMember.appendChild( lineString->asGml3( doc, precision, ns ) );
91  elemMultiCurve.appendChild( elemCurveMember );
92  }
93  }
94 
95  return elemMultiCurve;
96 }
97 
98 QString QgsMultiLineString::asJson( int precision ) const
99 {
100  QString json = QStringLiteral( "{\"type\": \"MultiLineString\", \"coordinates\": [" );
101  for ( const QgsAbstractGeometry *geom : mGeometries )
102  {
103  if ( qgsgeometry_cast<const QgsCurve *>( geom ) )
104  {
105  const QgsLineString *lineString = static_cast<const QgsLineString *>( geom );
106  QgsPointSequence pts;
107  lineString->points( pts );
108  json += QgsGeometryUtils::pointsToJSON( pts, precision ) + ", ";
109  }
110  }
111  if ( json.endsWith( QLatin1String( ", " ) ) )
112  {
113  json.chop( 2 ); // Remove last ", "
114  }
115  json += QLatin1String( "] }" );
116  return json;
117 }
118 
120 {
121  if ( !dynamic_cast<QgsLineString *>( g ) )
122  {
123  delete g;
124  return false;
125  }
126 
127  if ( mGeometries.empty() )
128  {
130  }
131  if ( is3D() && !g->is3D() )
132  g->addZValue();
133  else if ( !is3D() && g->is3D() )
134  g->dropZValue();
135  if ( isMeasure() && !g->isMeasure() )
136  g->addMValue();
137  else if ( !isMeasure() && g->isMeasure() )
138  g->dropMValue();
139  return QgsGeometryCollection::addGeometry( g ); // clazy:exclude=skipped-base-method
140 }
141 
143 {
145  {
146  delete g;
147  return false;
148  }
149 
150  return QgsMultiCurve::insertGeometry( g, index ); // clazy:exclude=skipped-base-method
151 }
152 
154 {
155  QgsMultiCurve *multiCurve = new QgsMultiCurve();
156  for ( int i = 0; i < mGeometries.size(); ++i )
157  {
158  multiCurve->addGeometry( mGeometries.at( i )->toCurveType() );
159  }
160  return multiCurve;
161 }
162 
164 {
165  return true;
166 }
167 
bool isMeasure() const
Returns true if the geometry contains m values.
QDomElement asGml3(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML3 representation of the geometry.
QgsMultiLineString * createEmptyWithSameType() const override
Creates a new geometry with the same class and same WKB type as the original and transfers ownership...
static QString pointsToJSON(const QgsPointSequence &points, int precision)
Returns a geoJSON coordinates string.
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.
QDomElement asGml2(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML2 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.
bool addGeometry(QgsAbstractGeometry *g) override
Adds a geometry and takes ownership. Returns true in case of success.
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.
Line string geometry type, with support for z-dimension and m-values.
Definition: qgslinestring.h:41
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.
QString asJson(int precision=17) const override
Returns a GeoJSON representation of the 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:427
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.