QGIS API Documentation  2.14.0-Essen
qgsmultipointv2.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmultipointv2.cpp
3  -------------------------------------------------------------------
4 Date : 29 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 "qgsmultipointv2.h"
17 #include "qgsapplication.h"
18 #include "qgsgeometryutils.h"
19 #include "qgspointv2.h"
20 #include "qgswkbptr.h"
21 
23 {
24  return new QgsMultiPointV2( *this );
25 }
26 
28 {
29  QString collectionWkt( wkt );
30  //test for non-standard MultiPoint(x1 y1, x2 y2) format
31  QRegExp regex( "^\\s*MultiPoint\\s*[ZM]*\\s*\\(\\s*\\d" );
32  regex.setCaseSensitivity( Qt::CaseInsensitive );
33  if ( regex.indexIn( collectionWkt ) >= 0 )
34  {
35  //alternate style without extra brackets, upgrade to standard
36  collectionWkt.replace( '(', "((" ).replace( ')', "))" ).replace( ',', "),(" );
37  }
38 
39  return fromCollectionWkt( collectionWkt, QList<QgsAbstractGeometryV2*>() << new QgsPointV2, "Point" );
40 }
41 
42 QDomElement QgsMultiPointV2::asGML2( QDomDocument& doc, int precision, const QString& ns ) const
43 {
44  QDomElement elemMultiPoint = doc.createElementNS( ns, "MultiPoint" );
45  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
46  {
47  if ( dynamic_cast<const QgsPointV2*>( geom ) )
48  {
49  QDomElement elemPointMember = doc.createElementNS( ns, "pointMember" );
50  elemPointMember.appendChild( geom->asGML2( doc, precision, ns ) );
51  elemMultiPoint.appendChild( elemPointMember );
52  }
53  }
54 
55  return elemMultiPoint;
56 }
57 
58 QDomElement QgsMultiPointV2::asGML3( QDomDocument& doc, int precision, const QString& ns ) const
59 {
60  QDomElement elemMultiPoint = doc.createElementNS( ns, "MultiPoint" );
61  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
62  {
63  if ( dynamic_cast<const QgsPointV2*>( geom ) )
64  {
65  QDomElement elemPointMember = doc.createElementNS( ns, "pointMember" );
66  elemPointMember.appendChild( geom->asGML3( doc, precision, ns ) );
67  elemMultiPoint.appendChild( elemPointMember );
68  }
69  }
70 
71  return elemMultiPoint;
72 }
73 
74 QString QgsMultiPointV2::asJSON( int precision ) const
75 {
76  QString json = "{\"type\": \"MultiPoint\", \"coordinates\": ";
77 
79  Q_FOREACH ( const QgsAbstractGeometryV2 *geom, mGeometries )
80  {
81  if ( dynamic_cast<const QgsPointV2*>( geom ) )
82  {
83  const QgsPointV2* point = static_cast<const QgsPointV2*>( geom );
84  pts << *point;
85  }
86  }
87  json += QgsGeometryUtils::pointsToJSON( pts, precision );
88  json += " }";
89  return json;
90 }
91 
93 {
94  if ( !dynamic_cast<QgsPointV2*>( g ) )
95  {
96  delete g;
97  return false;
98  }
101 }
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
QDomNode appendChild(const QDomNode &newChild)
QDomElement asGML2(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML2 representation of the geometry.
static QString pointsToJSON(const QgsPointSequenceV2 &points, int precision)
Returns a geoJSON coordinates string.
Abstract base class for all geometries.
Multi point geometry collection.
QDomElement createElementNS(const QString &nsURI, const QString &qName)
int indexIn(const QString &str, int offset, CaretMode caretMode) const
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:34
bool fromWkt(const QString &wkt) override
Sets the geometry from a WKT string.
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.
bool fromCollectionWkt(const QString &wkt, const QList< QgsAbstractGeometryV2 * > &subtypes, const QString &defaultChildWkbType=QString())
Reads a collection from a WKT string.
void setCaseSensitivity(Qt::CaseSensitivity cs)
QVector< QgsAbstractGeometryV2 * > mGeometries
QString & replace(int position, int n, QChar after)
QgsMultiPointV2 * clone() const override
Clones the geometry by performing a deep copy.
virtual bool addGeometry(QgsAbstractGeometryV2 *g)
Adds a geometry and takes ownership.
virtual QDomElement asGML2(QDomDocument &doc, int precision=17, const QString &ns="gml") const =0
Returns a GML2 representation of the geometry.
QDomElement asGML3(QDomDocument &doc, int precision=17, const QString &ns="gml") const override
Returns a GML3 representation of the geometry.
QString asJSON(int precision=17) const override
Returns a GeoJSON representation of the geometry.