QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsmulticurvev2.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmulticurvev2.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 "qgsmulticurvev2.h"
17 #include "qgsapplication.h"
18 #include "qgscurvev2.h"
19 #include "qgscircularstringv2.h"
20 #include "qgscompoundcurvev2.h"
21 #include "qgsgeometryutils.h"
22 #include "qgslinestringv2.h"
23 #include "qgsmultipointv2.h"
24 
27 {
29 }
30 
32 {
33  return new QgsMultiCurveV2( *this );
34 }
35 
37 {
38  return fromCollectionWkt( wkt,
40  "LineString" );
41 }
42 
43 QDomElement QgsMultiCurveV2::asGML2( QDomDocument& doc, int precision, const QString& ns ) const
44 {
45  // GML2 does not support curves
46  QDomElement elemMultiLineString = doc.createElementNS( ns, "MultiLineString" );
47  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
48  {
49  if ( dynamic_cast<const QgsCurveV2*>( geom ) )
50  {
51  QgsLineStringV2* lineString = static_cast<const QgsCurveV2*>( geom )->curveToLine();
52 
53  QDomElement elemLineStringMember = doc.createElementNS( ns, "lineStringMember" );
54  elemLineStringMember.appendChild( lineString->asGML2( doc, precision, ns ) );
55  elemMultiLineString.appendChild( elemLineStringMember );
56 
57  delete lineString;
58  }
59  }
60 
61  return elemMultiLineString;
62 }
63 
64 QDomElement QgsMultiCurveV2::asGML3( QDomDocument& doc, int precision, const QString& ns ) const
65 {
66  QDomElement elemMultiCurve = doc.createElementNS( ns, "MultiCurve" );
67  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
68  {
69  if ( dynamic_cast<const QgsCurveV2*>( geom ) )
70  {
71  const QgsCurveV2* curve = static_cast<const QgsCurveV2*>( geom );
72 
73  QDomElement elemCurveMember = doc.createElementNS( ns, "curveMember" );
74  elemCurveMember.appendChild( curve->asGML3( doc, precision, ns ) );
75  elemMultiCurve.appendChild( elemCurveMember );
76  }
77  }
78 
79  return elemMultiCurve;
80 }
81 
82 QString QgsMultiCurveV2::asJSON( int precision ) const
83 {
84  // GeoJSON does not support curves
85  QString json = "{\"type\": \"MultiLineString\", \"coordinates\": [";
86  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
87  {
88  if ( dynamic_cast<const QgsCurveV2*>( geom ) )
89  {
90  QgsLineStringV2* lineString = static_cast<const QgsCurveV2*>( geom )->curveToLine();
92  lineString->points( pts );
93  json += QgsGeometryUtils::pointsToJSON( pts, precision ) + ", ";
94  delete lineString;
95  }
96  }
97  if ( json.endsWith( ", " ) )
98  {
99  json.chop( 2 ); // Remove last ", "
100  }
101  json += "] }";
102  return json;
103 }
104 
106 {
107  if ( !dynamic_cast<QgsCurveV2*>( g ) )
108  {
109  delete g;
110  return false;
111  }
112 
115 }
116 
118 {
119  QgsMultiCurveV2* reversedMultiCurve = new QgsMultiCurveV2();
120  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
121  {
122  if ( dynamic_cast<const QgsCurveV2*>( geom ) )
123  {
124  reversedMultiCurve->addGeometry( static_cast<const QgsCurveV2*>( geom )->reversed() );
125  }
126  }
127  return reversedMultiCurve;
128 }
129 
131 {
132  QgsMultiPointV2* multiPoint = new QgsMultiPointV2();
133  for ( int i = 0; i < mGeometries.size(); ++i )
134  {
135  if ( QgsCurveV2* curve = dynamic_cast<QgsCurveV2*>( mGeometries.at( i ) ) )
136  {
137  if ( !curve->isClosed() )
138  {
139  multiPoint->addGeometry( new QgsPointV2( curve->startPoint() ) );
140  multiPoint->addGeometry( new QgsPointV2( curve->endPoint() ) );
141  }
142  }
143  }
144  if ( multiPoint->numGeometries() == 0 )
145  {
146  delete multiPoint;
147  return nullptr;
148  }
149  return multiPoint;
150 }
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
QDomElement asGML3(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML3 representation of the geometry.
int numGeometries() const
Returns the number of geometries within the collection.
QDomElement asGML2(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML2 representation of the geometry.
QDomNode appendChild(const QDomNode &newChild)
void points(QgsPointSequenceV2 &pt) const override
Returns a list of points within the curve.
Circular string geometry type.
Multi curve geometry collection.
QgsMultiCurveV2 * clone() const override
Clones the geometry by performing a deep copy.
static QString pointsToJSON(const QgsPointSequenceV2 &points, int precision)
Returns a geoJSON coordinates string.
Abstract base class for all geometries.
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
Multi point geometry collection.
QDomElement createElementNS(const QString &nsURI, const QString &qName)
void chop(int n)
Line string geometry type, with support for z-dimension and m-values.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:34
virtual QgsAbstractGeometryV2 * boundary() const override
Returns the closure of the combinatorial boundary of the geometry (ie the topological boundary of the...
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const
void setZMTypeFromSubGeometry(const QgsAbstractGeometryV2 *subggeom, QgsWKBTypes::Type baseGeomType)
Updates the geometry type based on whether sub geometries contain z or m values.
virtual QDomElement asGML3(QDomDocument &doc, int precision=17, const QString &ns="gml") const =0
Returns a GML3 representation of the geometry.
QgsMultiCurveV2 * reversed() const
Returns a copy of the multi curve, where each component curve has had its line direction reversed...
Compound curve geometry type.
QString asJSON(int precision=17) const override
Returns a GeoJSON representation of the geometry.
QVector< QgsAbstractGeometryV2 *> mGeometries
const T & at(int i) const
bool fromCollectionWkt(const QString &wkt, const QList< QgsAbstractGeometryV2 *> &subtypes, const QString &defaultChildWkbType=QString())
Reads a collection from a WKT string.
virtual bool addGeometry(QgsAbstractGeometryV2 *g)
Adds a geometry and takes ownership.
QDomElement asGML2(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML2 representation of the geometry.
Abstract base class for curved geometry type.
Definition: qgscurvev2.h:32
int size() const