QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
33 {
34  if ( !wkb )
35  {
36  return 0;
37  }
38 
39  //find out type (bytes 2-5)
40  int type;
41  memcpy( &type, wkb + 1, sizeof( int ) );
42  QgsAbstractGeometryV2* geom = 0;
43 
44  geom = geomFromWkbType( QgsWKBTypes::Type( type ) );
45 
46  if ( geom )
47  {
48  geom->fromWkb( wkb );
49  }
50  return geom;
51 }
52 
54 {
55  QgsAbstractGeometryV2* geom = 0;
56  if ( text.startsWith( "Point", Qt::CaseInsensitive ) )
57  {
58  geom = new QgsPointV2();
59  }
60  else if ( text.startsWith( "LineString", Qt::CaseInsensitive ) )
61  {
62  geom = new QgsLineStringV2();
63  }
64  else if ( text .startsWith( "CircularString", Qt::CaseInsensitive ) )
65  {
66  geom = new QgsCircularStringV2();
67  }
68  else if ( text.startsWith( "CompoundCurve" , Qt::CaseInsensitive ) )
69  {
70  geom = new QgsCompoundCurveV2();
71  }
72  else if ( text.startsWith( "Polygon", Qt::CaseInsensitive ) )
73  {
74  geom = new QgsPolygonV2();
75  }
76  else if ( text.startsWith( "CurvePolygon", Qt::CaseInsensitive ) )
77  {
78  geom = new QgsCurvePolygonV2();
79  }
80  else if ( text.startsWith( "MultiPoint", Qt::CaseInsensitive ) )
81  {
82  geom = new QgsMultiPointV2();
83  }
84  else if ( text.startsWith( "MultiCurve", Qt::CaseInsensitive ) )
85  {
86  geom = new QgsMultiCurveV2();
87  }
88  else if ( text.startsWith( "MultiLineString", Qt::CaseInsensitive ) )
89  {
90  geom = new QgsMultiLineStringV2();
91  }
92  else if ( text.startsWith( "MultiSurface", Qt::CaseInsensitive ) )
93  {
94  geom = new QgsMultiSurfaceV2();
95  }
96  else if ( text.startsWith( "MultiPolygon", Qt::CaseInsensitive ) )
97  {
98  geom = new QgsMultiPolygonV2();
99  }
100  else if ( text.startsWith( "GeometryCollection", Qt::CaseInsensitive ) )
101  {
102  geom = new QgsGeometryCollectionV2();
103  }
104 
105  if ( geom )
106  {
107  if ( !geom->fromWkt( text ) )
108  {
109  delete geom; return 0;
110  }
111  }
112  return geom;
113 }
114 
116 {
117  return new QgsPointV2( point.x(), point.y() );
118 }
119 
121 {
122  QgsMultiPointV2* mp = new QgsMultiPointV2();
123  QgsMultiPoint::const_iterator ptIt = multipoint.constBegin();
124  for ( ; ptIt != multipoint.constEnd(); ++ptIt )
125  {
126  QgsPointV2* pt = new QgsPointV2( ptIt->x(), ptIt->y() );
127  mp->addGeometry( pt );
128  }
129  return mp;
130 }
131 
133 {
134  return linestringFromPolyline( polyline );
135 }
136 
138 {
140  for ( int i = 0; i < multiline.size(); ++i )
141  {
142  mLine->addGeometry( fromPolyline( multiline.at( i ) ) );
143  }
144  return mLine;
145 }
146 
148 {
149  QgsPolygonV2* poly = new QgsPolygonV2();
150 
151  QList<QgsCurveV2*> holes;
152  for ( int i = 0; i < polygon.size(); ++i )
153  {
154  QgsLineStringV2* l = linestringFromPolyline( polygon.at( i ) );
155  l->close();
156 
157  if ( i == 0 )
158  {
159  poly->setExteriorRing( l );
160  }
161  else
162  {
163  holes.push_back( l );
164  }
165  }
166  poly->setInteriorRings( holes );
167  return poly;
168 }
169 
171 {
173  for ( int i = 0; i < multipoly.size(); ++i )
174  {
175  mp->addGeometry( fromPolygon( multipoly.at( i ) ) );
176  }
177  return mp;
178 }
179 
181 {
182  QgsPolyline ring;
183  ring.append( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
184  ring.append( QgsPoint( rect.xMaximum(), rect.yMinimum() ) );
185  ring.append( QgsPoint( rect.xMaximum(), rect.yMaximum() ) );
186  ring.append( QgsPoint( rect.xMinimum(), rect.yMaximum() ) );
187  ring.append( QgsPoint( rect.xMinimum(), rect.yMinimum() ) );
188 
189  QgsPolygon polygon;
190  polygon.append( ring );
191 
192  return fromPolygon( polygon );
193 }
194 
195 QgsLineStringV2* QgsGeometryFactory::linestringFromPolyline( const QgsPolyline& polyline )
196 {
197  QgsLineStringV2* line = new QgsLineStringV2();
198 
199  QList<QgsPointV2> points;
200  QgsPolyline::const_iterator it = polyline.constBegin();
201  for ( ; it != polyline.constEnd(); ++it )
202  {
203  points.append( QgsPointV2( it->x(), it->y() ) );
204  }
205  line->setPoints( points );
206  return line;
207 }
208 
210 {
212  switch ( type )
213  {
214  case QgsWKBTypes::Point:
215  return new QgsPointV2();
217  return new QgsLineStringV2();
219  return new QgsCircularStringV2();
221  return new QgsCompoundCurveV2();
223  return new QgsPolygonV2();
225  return new QgsCurvePolygonV2();
227  return new QgsMultiLineStringV2();
229  return new QgsMultiPolygonV2();
231  return new QgsMultiPointV2();
233  return new QgsMultiCurveV2();
235  return new QgsMultiSurfaceV2();
237  return new QgsGeometryCollectionV2();
238  default:
239  return 0;
240  }
241 }
virtual bool addGeometry(QgsAbstractGeometryV2 *g) override
Adds a geometry and takes ownership.
void close()
Appends first point if not already closed.
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:192
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
Multi line string geometry collection.
double x() const
Definition: qgspoint.h:126
static QgsAbstractGeometryV2 * geomFromWkt(const QString &text)
Construct geometry from a WKT string.
Polygon geometry type.
Definition: qgspolygonv2.h:29
static Type flatType(Type type)
Definition: qgswkbtypes.cpp:46
static QgsAbstractGeometryV2 * fromMultiPoint(const QgsMultiPoint &multipoint)
construct geometry from a multipoint
void append(const T &value)
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:197
double xMaximum() const
Get the x maximum value (right side of rectangle)
Definition: qgsrectangle.h:182
Line string geometry type.
virtual bool fromWkb(const unsigned char *wkb)=0
Sets the geometry from a WKB string.
Point geometry type.
Definition: qgspointv2.h:29
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:63
Compound curve geometry type.
static QgsAbstractGeometryV2 * geomFromWkb(const unsigned char *wkb)
Construct geometry from a WKB string.
const T & at(int i) const
const_iterator constBegin() const
static QgsAbstractGeometryV2 * fromRect(const QgsRectangle &rect)
construct geometry from a rectangle
void setPoints(const QList< QgsPointV2 > &points)
void setExteriorRing(QgsCurveV2 *ring)
Sets exterior ring (takes ownership)
static QgsAbstractGeometryV2 * fromMultiPolyline(const QgsMultiPolyline &multiline)
construct geometry from a multipolyline
double y() const
Definition: qgspoint.h:134
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:187
void setInteriorRings(QList< QgsCurveV2 * > rings)
Sets all interior rings (takes ownership)