QGIS API Documentation  2.14.0-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  QgsDebugMsg( "WKB exception: " + e.what() );
55  delete geom;
56  geom = nullptr;
57  }
58  }
59 
60  return geom;
61 }
62 
64 {
65  QgsAbstractGeometryV2* geom = nullptr;
66  if ( text.startsWith( "Point", Qt::CaseInsensitive ) )
67  {
68  geom = new QgsPointV2();
69  }
70  else if ( text.startsWith( "LineString", Qt::CaseInsensitive ) )
71  {
72  geom = new QgsLineStringV2();
73  }
74  else if ( text.startsWith( "CircularString", Qt::CaseInsensitive ) )
75  {
76  geom = new QgsCircularStringV2();
77  }
78  else if ( text.startsWith( "CompoundCurve" , Qt::CaseInsensitive ) )
79  {
80  geom = new QgsCompoundCurveV2();
81  }
82  else if ( text.startsWith( "Polygon", Qt::CaseInsensitive ) )
83  {
84  geom = new QgsPolygonV2();
85  }
86  else if ( text.startsWith( "CurvePolygon", Qt::CaseInsensitive ) )
87  {
88  geom = new QgsCurvePolygonV2();
89  }
90  else if ( text.startsWith( "MultiPoint", Qt::CaseInsensitive ) )
91  {
92  geom = new QgsMultiPointV2();
93  }
94  else if ( text.startsWith( "MultiCurve", Qt::CaseInsensitive ) )
95  {
96  geom = new QgsMultiCurveV2();
97  }
98  else if ( text.startsWith( "MultiLineString", Qt::CaseInsensitive ) )
99  {
100  geom = new QgsMultiLineStringV2();
101  }
102  else if ( text.startsWith( "MultiSurface", Qt::CaseInsensitive ) )
103  {
104  geom = new QgsMultiSurfaceV2();
105  }
106  else if ( text.startsWith( "MultiPolygon", Qt::CaseInsensitive ) )
107  {
108  geom = new QgsMultiPolygonV2();
109  }
110  else if ( text.startsWith( "GeometryCollection", Qt::CaseInsensitive ) )
111  {
112  geom = new QgsGeometryCollectionV2();
113  }
114 
115  if ( geom )
116  {
117  if ( !geom->fromWkt( text ) )
118  {
119  delete geom;
120  return nullptr;
121  }
122  }
123  return geom;
124 }
125 
127 {
128  return new QgsPointV2( point.x(), point.y() );
129 }
130 
132 {
133  QgsMultiPointV2* mp = new QgsMultiPointV2();
134  QgsMultiPoint::const_iterator ptIt = multipoint.constBegin();
135  for ( ; ptIt != multipoint.constEnd(); ++ptIt )
136  {
137  QgsPointV2* pt = new QgsPointV2( ptIt->x(), ptIt->y() );
138  mp->addGeometry( pt );
139  }
140  return mp;
141 }
142 
144 {
145  return linestringFromPolyline( polyline );
146 }
147 
149 {
151  for ( int i = 0; i < multiline.size(); ++i )
152  {
153  mLine->addGeometry( fromPolyline( multiline.at( i ) ) );
154  }
155  return mLine;
156 }
157 
159 {
160  QgsPolygonV2* poly = new QgsPolygonV2();
161 
162  QList<QgsCurveV2*> holes;
163  for ( int i = 0; i < polygon.size(); ++i )
164  {
165  QgsLineStringV2* l = linestringFromPolyline( polygon.at( i ) );
166  l->close();
167 
168  if ( i == 0 )
169  {
170  poly->setExteriorRing( l );
171  }
172  else
173  {
174  holes.push_back( l );
175  }
176  }
177  poly->setInteriorRings( holes );
178  return poly;
179 }
180 
182 {
184  for ( int i = 0; i < multipoly.size(); ++i )
185  {
186  mp->addGeometry( fromPolygon( multipoly.at( i ) ) );
187  }
188  return mp;
189 }
190 
192 {
193  QgsPolyline ring;
194  ring.append( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
195  ring.append( QgsPoint( rect.xMaximum(), rect.yMinimum() ) );
196  ring.append( QgsPoint( rect.xMaximum(), rect.yMaximum() ) );
197  ring.append( QgsPoint( rect.xMinimum(), rect.yMaximum() ) );
198  ring.append( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
199 
200  QgsPolygon polygon;
201  polygon.append( ring );
202 
203  return fromPolygon( polygon );
204 }
205 
206 QgsLineStringV2* QgsGeometryFactory::linestringFromPolyline( const QgsPolyline& polyline )
207 {
208  QgsLineStringV2* line = new QgsLineStringV2();
209 
210  QgsPointSequenceV2 points;
211  QgsPolyline::const_iterator it = polyline.constBegin();
212  for ( ; it != polyline.constEnd(); ++it )
213  {
214  points.append( QgsPointV2( it->x(), it->y() ) );
215  }
216  line->setPoints( points );
217  return line;
218 }
219 
221 {
223  switch ( type )
224  {
225  case QgsWKBTypes::Point:
226  return new QgsPointV2();
228  return new QgsLineStringV2();
230  return new QgsCircularStringV2();
232  return new QgsCompoundCurveV2();
234  return new QgsPolygonV2();
236  return new QgsCurvePolygonV2();
238  return new QgsMultiLineStringV2();
240  return new QgsMultiPolygonV2();
242  return new QgsMultiPointV2();
244  return new QgsMultiCurveV2();
246  return new QgsMultiSurfaceV2();
248  return new QgsGeometryCollectionV2();
249  default:
250  return nullptr;
251  }
252 }
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...
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.
double yMaximum() const
Get the y maximum value (top side of rectangle)
Definition: qgsrectangle.h:197
#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 x() const
Get the x value of the point.
Definition: qgspoint.h:128
static QgsAbstractGeometryV2 * geomFromWkt(const QString &text)
Construct geometry from a WKT string.
QgsWKBTypes::Type readHeader() const
Definition: qgswkbptr.cpp:37
Polygon geometry type.
Definition: qgspolygonv2.h:29
static QgsAbstractGeometryV2 * fromMultiPoint(const QgsMultiPoint &multipoint)
Construct geometry from a multipoint.
void setInteriorRings(const QList< QgsCurveV2 * > &rings)
Sets all interior rings (takes ownership)
void append(const T &value)
virtual void setExteriorRing(QgsCurveV2 *ring) override
Sets the exterior ring of the polygon.
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
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.
QString what() const
Definition: qgsexception.h:36
const T & at(int i) const
const_iterator constBegin() const
static QgsAbstractGeometryV2 * fromRect(const QgsRectangle &rect)
Construct geometry from a rectangle.
static QgsAbstractGeometryV2 * fromMultiPolyline(const QgsMultiPolyline &multiline)
Construct geometry from a multipolyline.
double y() const
Get the y value of the point.
Definition: qgspoint.h:136
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:366
Multi polygon geometry collection.
typedef const_iterator
Curve polygon geometry type.
int size() const
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:192