QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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)
40  try
41  {
42  type = wkbPtr.readHeader();
43  }
44  catch ( const QgsWkbException &e )
45  {
46  Q_UNUSED( e );
47  QgsDebugMsg( "WKB exception while reading header: " + e.what() );
48  return nullptr;
49  }
50  wkbPtr -= 1 + sizeof( int );
51 
52  QgsAbstractGeometryV2* geom = nullptr;
53 
54  geom = geomFromWkbType( type );
55 
56  if ( geom )
57  {
58  try
59  {
60  geom->fromWkb( wkbPtr );
61  }
62  catch ( const QgsWkbException &e )
63  {
64  Q_UNUSED( e );
65  QgsDebugMsg( "WKB exception: " + e.what() );
66  delete geom;
67  geom = nullptr;
68  }
69  }
70 
71  return geom;
72 }
73 
75 {
76  QgsAbstractGeometryV2* geom = nullptr;
77  if ( text.startsWith( "Point", Qt::CaseInsensitive ) )
78  {
79  geom = new QgsPointV2();
80  }
81  else if ( text.startsWith( "LineString", Qt::CaseInsensitive ) )
82  {
83  geom = new QgsLineStringV2();
84  }
85  else if ( text.startsWith( "CircularString", Qt::CaseInsensitive ) )
86  {
87  geom = new QgsCircularStringV2();
88  }
89  else if ( text.startsWith( "CompoundCurve" , Qt::CaseInsensitive ) )
90  {
91  geom = new QgsCompoundCurveV2();
92  }
93  else if ( text.startsWith( "Polygon", Qt::CaseInsensitive ) )
94  {
95  geom = new QgsPolygonV2();
96  }
97  else if ( text.startsWith( "CurvePolygon", Qt::CaseInsensitive ) )
98  {
99  geom = new QgsCurvePolygonV2();
100  }
101  else if ( text.startsWith( "MultiPoint", Qt::CaseInsensitive ) )
102  {
103  geom = new QgsMultiPointV2();
104  }
105  else if ( text.startsWith( "MultiCurve", Qt::CaseInsensitive ) )
106  {
107  geom = new QgsMultiCurveV2();
108  }
109  else if ( text.startsWith( "MultiLineString", Qt::CaseInsensitive ) )
110  {
111  geom = new QgsMultiLineStringV2();
112  }
113  else if ( text.startsWith( "MultiSurface", Qt::CaseInsensitive ) )
114  {
115  geom = new QgsMultiSurfaceV2();
116  }
117  else if ( text.startsWith( "MultiPolygon", Qt::CaseInsensitive ) )
118  {
119  geom = new QgsMultiPolygonV2();
120  }
121  else if ( text.startsWith( "GeometryCollection", Qt::CaseInsensitive ) )
122  {
123  geom = new QgsGeometryCollectionV2();
124  }
125 
126  if ( geom )
127  {
128  if ( !geom->fromWkt( text ) )
129  {
130  delete geom;
131  return nullptr;
132  }
133  }
134  return geom;
135 }
136 
138 {
139  return new QgsPointV2( point.x(), point.y() );
140 }
141 
143 {
144  QgsMultiPointV2* mp = new QgsMultiPointV2();
145  QgsMultiPoint::const_iterator ptIt = multipoint.constBegin();
146  for ( ; ptIt != multipoint.constEnd(); ++ptIt )
147  {
148  QgsPointV2* pt = new QgsPointV2( ptIt->x(), ptIt->y() );
149  mp->addGeometry( pt );
150  }
151  return mp;
152 }
153 
155 {
156  return linestringFromPolyline( polyline );
157 }
158 
160 {
162  for ( int i = 0; i < multiline.size(); ++i )
163  {
164  mLine->addGeometry( fromPolyline( multiline.at( i ) ) );
165  }
166  return mLine;
167 }
168 
170 {
171  QgsPolygonV2* poly = new QgsPolygonV2();
172 
173  QList<QgsCurveV2*> holes;
174  for ( int i = 0; i < polygon.size(); ++i )
175  {
176  QgsLineStringV2* l = linestringFromPolyline( polygon.at( i ) );
177  l->close();
178 
179  if ( i == 0 )
180  {
181  poly->setExteriorRing( l );
182  }
183  else
184  {
185  holes.push_back( l );
186  }
187  }
188  poly->setInteriorRings( holes );
189  return poly;
190 }
191 
193 {
195  for ( int i = 0; i < multipoly.size(); ++i )
196  {
197  mp->addGeometry( fromPolygon( multipoly.at( i ) ) );
198  }
199  return mp;
200 }
201 
203 {
204  QgsPolyline ring;
205  ring.append( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
206  ring.append( QgsPoint( rect.xMaximum(), rect.yMinimum() ) );
207  ring.append( QgsPoint( rect.xMaximum(), rect.yMaximum() ) );
208  ring.append( QgsPoint( rect.xMinimum(), rect.yMaximum() ) );
209  ring.append( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
210 
211  QgsPolygon polygon;
212  polygon.append( ring );
213 
214  return fromPolygon( polygon );
215 }
216 
217 QgsLineStringV2* QgsGeometryFactory::linestringFromPolyline( const QgsPolyline& polyline )
218 {
219  QgsLineStringV2* line = new QgsLineStringV2();
220 
221  QgsPointSequenceV2 points;
222  QgsPolyline::const_iterator it = polyline.constBegin();
223  for ( ; it != polyline.constEnd(); ++it )
224  {
225  points.append( QgsPointV2( it->x(), it->y() ) );
226  }
227  line->setPoints( points );
228  return line;
229 }
230 
232 {
234  switch ( type )
235  {
236  case QgsWKBTypes::Point:
237  return new QgsPointV2();
239  return new QgsLineStringV2();
241  return new QgsCircularStringV2();
243  return new QgsCompoundCurveV2();
245  return new QgsPolygonV2();
247  return new QgsCurvePolygonV2();
249  return new QgsMultiLineStringV2();
251  return new QgsMultiPolygonV2();
253  return new QgsMultiPointV2();
255  return new QgsMultiCurveV2();
257  return new QgsMultiSurfaceV2();
259  return new QgsGeometryCollectionV2();
260  default:
261  return nullptr;
262  }
263 }
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:193
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:117
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:38
int size() const
double x() const
Get the x value of the point.
Definition: qgspoint.h:185