QGIS API Documentation  2.14.11-Essen
qgsgeometryfactory.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeometryfactory.cpp
3  ------------------------
4  begin : September 2014
5  copyright : (C) 2014 by Marco Hugentobler
6  email : marco at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsgeometryfactory.h"
19 #include "qgscircularstringv2.h"
20 #include "qgscompoundcurvev2.h"
21 #include "qgscurvepolygonv2.h"
22 #include "qgspointv2.h"
23 #include "qgspolygonv2.h"
24 #include "qgslinestringv2.h"
25 #include "qgsmulticurvev2.h"
26 #include "qgsmultilinestringv2.h"
27 #include "qgsmultipointv2.h"
28 #include "qgsmultipolygonv2.h"
29 #include "qgsmultisurfacev2.h"
30 #include "qgswkbtypes.h"
31 #include "qgslogger.h"
32 
34 {
35  if ( !wkbPtr )
36  return nullptr;
37 
38  //find out type (bytes 2-5)
39  QgsWKBTypes::Type type = wkbPtr.readHeader();
40  wkbPtr -= 1 + sizeof( int );
41 
42  QgsAbstractGeometryV2* geom = nullptr;
43 
44  geom = geomFromWkbType( type );
45 
46  if ( geom )
47  {
48  try
49  {
50  geom->fromWkb( wkbPtr );
51  }
52  catch ( const QgsWkbException &e )
53  {
54  Q_UNUSED( e );
55  QgsDebugMsg( "WKB exception: " + e.what() );
56  delete geom;
57  geom = nullptr;
58  }
59  }
60 
61  return geom;
62 }
63 
65 {
66  QgsAbstractGeometryV2* geom = nullptr;
67  if ( text.startsWith( "Point", Qt::CaseInsensitive ) )
68  {
69  geom = new QgsPointV2();
70  }
71  else if ( text.startsWith( "LineString", Qt::CaseInsensitive ) )
72  {
73  geom = new QgsLineStringV2();
74  }
75  else if ( text.startsWith( "CircularString", Qt::CaseInsensitive ) )
76  {
77  geom = new QgsCircularStringV2();
78  }
79  else if ( text.startsWith( "CompoundCurve" , Qt::CaseInsensitive ) )
80  {
81  geom = new QgsCompoundCurveV2();
82  }
83  else if ( text.startsWith( "Polygon", Qt::CaseInsensitive ) )
84  {
85  geom = new QgsPolygonV2();
86  }
87  else if ( text.startsWith( "CurvePolygon", Qt::CaseInsensitive ) )
88  {
89  geom = new QgsCurvePolygonV2();
90  }
91  else if ( text.startsWith( "MultiPoint", Qt::CaseInsensitive ) )
92  {
93  geom = new QgsMultiPointV2();
94  }
95  else if ( text.startsWith( "MultiCurve", Qt::CaseInsensitive ) )
96  {
97  geom = new QgsMultiCurveV2();
98  }
99  else if ( text.startsWith( "MultiLineString", Qt::CaseInsensitive ) )
100  {
101  geom = new QgsMultiLineStringV2();
102  }
103  else if ( text.startsWith( "MultiSurface", Qt::CaseInsensitive ) )
104  {
105  geom = new QgsMultiSurfaceV2();
106  }
107  else if ( text.startsWith( "MultiPolygon", Qt::CaseInsensitive ) )
108  {
109  geom = new QgsMultiPolygonV2();
110  }
111  else if ( text.startsWith( "GeometryCollection", Qt::CaseInsensitive ) )
112  {
113  geom = new QgsGeometryCollectionV2();
114  }
115 
116  if ( geom )
117  {
118  if ( !geom->fromWkt( text ) )
119  {
120  delete geom;
121  return nullptr;
122  }
123  }
124  return geom;
125 }
126 
128 {
129  return new QgsPointV2( point.x(), point.y() );
130 }
131 
133 {
134  QgsMultiPointV2* mp = new QgsMultiPointV2();
135  QgsMultiPoint::const_iterator ptIt = multipoint.constBegin();
136  for ( ; ptIt != multipoint.constEnd(); ++ptIt )
137  {
138  QgsPointV2* pt = new QgsPointV2( ptIt->x(), ptIt->y() );
139  mp->addGeometry( pt );
140  }
141  return mp;
142 }
143 
145 {
146  return linestringFromPolyline( polyline );
147 }
148 
150 {
152  for ( int i = 0; i < multiline.size(); ++i )
153  {
154  mLine->addGeometry( fromPolyline( multiline.at( i ) ) );
155  }
156  return mLine;
157 }
158 
160 {
161  QgsPolygonV2* poly = new QgsPolygonV2();
162 
163  QList<QgsCurveV2*> holes;
164  for ( int i = 0; i < polygon.size(); ++i )
165  {
166  QgsLineStringV2* l = linestringFromPolyline( polygon.at( i ) );
167  l->close();
168 
169  if ( i == 0 )
170  {
171  poly->setExteriorRing( l );
172  }
173  else
174  {
175  holes.push_back( l );
176  }
177  }
178  poly->setInteriorRings( holes );
179  return poly;
180 }
181 
183 {
185  for ( int i = 0; i < multipoly.size(); ++i )
186  {
187  mp->addGeometry( fromPolygon( multipoly.at( i ) ) );
188  }
189  return mp;
190 }
191 
193 {
194  QgsPolyline ring;
195  ring.append( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
196  ring.append( QgsPoint( rect.xMaximum(), rect.yMinimum() ) );
197  ring.append( QgsPoint( rect.xMaximum(), rect.yMaximum() ) );
198  ring.append( QgsPoint( rect.xMinimum(), rect.yMaximum() ) );
199  ring.append( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
200 
201  QgsPolygon polygon;
202  polygon.append( ring );
203 
204  return fromPolygon( polygon );
205 }
206 
207 QgsLineStringV2* QgsGeometryFactory::linestringFromPolyline( const QgsPolyline& polyline )
208 {
209  QgsLineStringV2* line = new QgsLineStringV2();
210 
211  QgsPointSequenceV2 points;
212  QgsPolyline::const_iterator it = polyline.constBegin();
213  for ( ; it != polyline.constEnd(); ++it )
214  {
215  points.append( QgsPointV2( it->x(), it->y() ) );
216  }
217  line->setPoints( points );
218  return line;
219 }
220 
222 {
224  switch ( type )
225  {
226  case QgsWKBTypes::Point:
227  return new QgsPointV2();
229  return new QgsLineStringV2();
231  return new QgsCircularStringV2();
233  return new QgsCompoundCurveV2();
235  return new QgsPolygonV2();
237  return new QgsCurvePolygonV2();
239  return new QgsMultiLineStringV2();
241  return new QgsMultiPolygonV2();
243  return new QgsMultiPointV2();
245  return new QgsMultiCurveV2();
247  return new QgsMultiSurfaceV2();
249  return new QgsGeometryCollectionV2();
250  default:
251  return nullptr;
252  }
253 }
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
void close()
Closes the line string by appending the first point to the end of the line, if it is not already clos...
void setInteriorRings(const QList< QgsCurveV2 *> &rings)
Sets all interior rings (takes ownership)
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void append(const T &value)
void push_back(const T &value)
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
Circular string geometry type.
Multi curve geometry collection.
const_iterator constEnd() const
static QgsAbstractGeometryV2 * fromPolygon(const QgsPolygon &polygon)
Construct geometry from a polygon.
Abstract base class for all geometries.
Multi point geometry collection.
static QgsAbstractGeometryV2 * fromPolyline(const QgsPolyline &polyline)
Construct geometry from a polyline.
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
static QgsAbstractGeometryV2 * fromPoint(const QgsPoint &point)
Construct geometry from a point.
static QgsAbstractGeometryV2 * fromMultiPolygon(const QgsMultiPolygon &multipoly)
Construct geometry from a multipolygon.
static QgsAbstractGeometryV2 * geomFromWkb(QgsConstWkbPtr wkb)
Construct geometry from a WKB string.
Multi line string geometry collection.
double y() const
Get the y value of the point.
Definition: qgspoint.h:136
QString what() const
Definition: qgsexception.h:36
static QgsAbstractGeometryV2 * geomFromWkt(const QString &text)
Construct geometry from a WKT string.
Polygon geometry type.
Definition: qgspolygonv2.h:29
static QgsAbstractGeometryV2 * fromMultiPoint(const QgsMultiPoint &multipoint)
Construct geometry from a multipoint.
void append(const T &value)
virtual void setExteriorRing(QgsCurveV2 *ring) override
Sets the exterior ring of the polygon.
Line string geometry type, with support for z-dimension and m-values.
virtual bool fromWkb(QgsConstWkbPtr wkb)=0
Sets the geometry from a WKB string.
void setPoints(const QgsPointSequenceV2 &points)
Resets the line string to match the specified list of points.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspointv2.h:34
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
Multi surface geometry collection.
static QgsAbstractGeometryV2 * geomFromWkbType(QgsWKBTypes::Type t)
Return empty geometry from wkb type.
virtual bool fromWkt(const QString &wkt)=0
Sets the geometry from a WKT string.
A class to represent a point.
Definition: qgspoint.h:65
Compound curve geometry type.
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:202
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:187
const T & at(int i) const
const_iterator constBegin() const
static QgsAbstractGeometryV2 * fromRect(const QgsRectangle &rect)
Construct geometry from a rectangle.
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:192
static QgsAbstractGeometryV2 * fromMultiPolyline(const QgsMultiPolyline &multiline)
Construct geometry from a multipolyline.
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:366
Multi polygon geometry collection.
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:197
typedef const_iterator
Curve polygon geometry type.
QgsWKBTypes::Type readHeader() const
Definition: qgswkbptr.cpp:37
int size() const
double x() const
Get the x value of the point.
Definition: qgspoint.h:128