QGIS API Documentation  3.2.0-Bonn (bc43194)
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 "qgscircularstring.h"
20 #include "qgscompoundcurve.h"
21 #include "qgscurvepolygon.h"
22 #include "qgspoint.h"
23 #include "qgspolygon.h"
24 #include "qgslinestring.h"
25 #include "qgsmulticurve.h"
26 #include "qgsmultilinestring.h"
27 #include "qgsmultipoint.h"
28 #include "qgsmultipolygon.h"
29 #include "qgsmultisurface.h"
30 #include "qgstriangle.h"
31 #include "qgswkbtypes.h"
32 #include "qgslogger.h"
33 
34 std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkb( QgsConstWkbPtr &wkbPtr )
35 {
36  if ( !wkbPtr )
37  return nullptr;
38 
39  //find out type (bytes 2-5)
41  try
42  {
43  type = wkbPtr.readHeader();
44  }
45  catch ( const QgsWkbException &e )
46  {
47  Q_UNUSED( e );
48  QgsDebugMsg( "WKB exception while reading header: " + e.what() );
49  return nullptr;
50  }
51  wkbPtr -= 1 + sizeof( int );
52 
53  std::unique_ptr< QgsAbstractGeometry > geom = geomFromWkbType( type );
54 
55  if ( geom )
56  {
57  try
58  {
59  geom->fromWkb( wkbPtr ); // also updates wkbPtr
60  }
61  catch ( const QgsWkbException &e )
62  {
63  Q_UNUSED( e );
64  QgsDebugMsg( "WKB exception: " + e.what() );
65  geom.reset();
66  }
67  }
68 
69  return geom;
70 }
71 
72 std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkt( const QString &text )
73 {
74  QString trimmed = text.trimmed();
75  std::unique_ptr< QgsAbstractGeometry> geom;
76  if ( trimmed.startsWith( QLatin1String( "Point" ), Qt::CaseInsensitive ) )
77  {
78  geom = qgis::make_unique< QgsPoint >();
79  }
80  else if ( trimmed.startsWith( QLatin1String( "LineString" ), Qt::CaseInsensitive ) )
81  {
82  geom = qgis::make_unique< QgsLineString >();
83  }
84  else if ( trimmed.startsWith( QLatin1String( "CircularString" ), Qt::CaseInsensitive ) )
85  {
86  geom = qgis::make_unique< QgsCircularString >();
87  }
88  else if ( trimmed.startsWith( QLatin1String( "CompoundCurve" ), Qt::CaseInsensitive ) )
89  {
90  geom = qgis::make_unique< QgsCompoundCurve>();
91  }
92  else if ( trimmed.startsWith( QLatin1String( "Polygon" ), Qt::CaseInsensitive ) )
93  {
94  geom = qgis::make_unique< QgsPolygon >();
95  }
96  else if ( trimmed.startsWith( QLatin1String( "CurvePolygon" ), Qt::CaseInsensitive ) )
97  {
98  geom = qgis::make_unique< QgsCurvePolygon >();
99  }
100  else if ( trimmed.startsWith( QLatin1String( "MultiPoint" ), Qt::CaseInsensitive ) )
101  {
102  geom = qgis::make_unique< QgsMultiPoint >();
103  }
104  else if ( trimmed.startsWith( QLatin1String( "MultiCurve" ), Qt::CaseInsensitive ) )
105  {
106  geom = qgis::make_unique< QgsMultiCurve >();
107  }
108  else if ( trimmed.startsWith( QLatin1String( "MultiLineString" ), Qt::CaseInsensitive ) )
109  {
110  geom = qgis::make_unique< QgsMultiLineString >();
111  }
112  else if ( trimmed.startsWith( QLatin1String( "MultiSurface" ), Qt::CaseInsensitive ) )
113  {
114  geom = qgis::make_unique< QgsMultiSurface >();
115  }
116  else if ( trimmed.startsWith( QLatin1String( "MultiPolygon" ), Qt::CaseInsensitive ) )
117  {
118  geom = qgis::make_unique< QgsMultiPolygon >();
119  }
120  else if ( trimmed.startsWith( QLatin1String( "GeometryCollection" ), Qt::CaseInsensitive ) )
121  {
122  geom = qgis::make_unique< QgsGeometryCollection >();
123  }
124 
125  if ( geom )
126  {
127  if ( !geom->fromWkt( text ) )
128  {
129  return nullptr;
130  }
131  }
132  return geom;
133 }
134 
135 std::unique_ptr< QgsAbstractGeometry > QgsGeometryFactory::fromPointXY( const QgsPointXY &point )
136 {
137  return qgis::make_unique< QgsPoint >( point.x(), point.y() );
138 }
139 
140 std::unique_ptr<QgsMultiPoint> QgsGeometryFactory::fromMultiPointXY( const QgsMultiPointXY &multipoint )
141 {
142  std::unique_ptr< QgsMultiPoint > mp = qgis::make_unique< QgsMultiPoint >();
143  QgsMultiPointXY::const_iterator ptIt = multipoint.constBegin();
144  for ( ; ptIt != multipoint.constEnd(); ++ptIt )
145  {
146  QgsPoint *pt = new QgsPoint( ptIt->x(), ptIt->y() );
147  mp->addGeometry( pt );
148  }
149  return mp;
150 }
151 
152 std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromPolylineXY( const QgsPolylineXY &polyline )
153 {
154  return linestringFromPolyline( polyline );
155 }
156 
157 std::unique_ptr<QgsMultiLineString> QgsGeometryFactory::fromMultiPolylineXY( const QgsMultiPolylineXY &multiline )
158 {
159  std::unique_ptr< QgsMultiLineString > mLine = qgis::make_unique< QgsMultiLineString >();
160  for ( int i = 0; i < multiline.size(); ++i )
161  {
162  mLine->addGeometry( fromPolylineXY( multiline.at( i ) ).release() );
163  }
164  return mLine;
165 }
166 
167 std::unique_ptr<QgsPolygon> QgsGeometryFactory::fromPolygonXY( const QgsPolygonXY &polygon )
168 {
169  std::unique_ptr< QgsPolygon > poly = qgis::make_unique< QgsPolygon >();
170 
171  QVector<QgsCurve *> holes;
172  for ( int i = 0; i < polygon.size(); ++i )
173  {
174  std::unique_ptr< QgsLineString > l = linestringFromPolyline( polygon.at( i ) );
175  l->close();
176 
177  if ( i == 0 )
178  {
179  poly->setExteriorRing( l.release() );
180  }
181  else
182  {
183  holes.push_back( l.release() );
184  }
185  }
186  poly->setInteriorRings( holes );
187  return poly;
188 }
189 
190 std::unique_ptr< QgsMultiPolygon > QgsGeometryFactory::fromMultiPolygonXY( const QgsMultiPolygonXY &multipoly )
191 {
192  std::unique_ptr< QgsMultiPolygon > mp = qgis::make_unique< QgsMultiPolygon >();
193  for ( int i = 0; i < multipoly.size(); ++i )
194  {
195  mp->addGeometry( fromPolygonXY( multipoly.at( i ) ).release() );
196  }
197  return mp;
198 }
199 
200 std::unique_ptr<QgsLineString> QgsGeometryFactory::linestringFromPolyline( const QgsPolylineXY &polyline )
201 {
202  QVector< double > x;
203  x.reserve( polyline.size() );
204  QVector< double > y;
205  y.reserve( polyline.size() );
206  QgsPolylineXY::const_iterator it = polyline.constBegin();
207  for ( ; it != polyline.constEnd(); ++it )
208  {
209  x << it->x();
210  y << it->y();
211  }
212  std::unique_ptr< QgsLineString > line = qgis::make_unique< QgsLineString >( x, y );
213  return line;
214 }
215 
216 std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::Type t )
217 {
219  switch ( type )
220  {
221  case QgsWkbTypes::Point:
222  return qgis::make_unique< QgsPoint >();
224  return qgis::make_unique< QgsLineString >();
226  return qgis::make_unique< QgsCircularString >();
228  return qgis::make_unique< QgsCompoundCurve >();
230  return qgis::make_unique< QgsPolygon >();
232  return qgis::make_unique< QgsCurvePolygon >();
234  return qgis::make_unique< QgsMultiLineString >();
236  return qgis::make_unique< QgsMultiPolygon >();
238  return qgis::make_unique< QgsMultiPoint >();
240  return qgis::make_unique< QgsMultiCurve >();
242  return qgis::make_unique< QgsMultiSurface >();
244  return qgis::make_unique< QgsGeometryCollection >();
246  return qgis::make_unique< QgsTriangle >();
247  default:
248  return nullptr;
249  }
250 }
251 
252 std::unique_ptr<QgsGeometryCollection> QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::Type t )
253 {
255  std::unique_ptr< QgsGeometryCollection > collect;
256  switch ( type )
257  {
259  collect = qgis::make_unique< QgsMultiPoint >();
260  break;
262  collect = qgis::make_unique< QgsMultiLineString >();
263  break;
265  collect = qgis::make_unique< QgsMultiCurve >();
266  break;
268  collect = qgis::make_unique< QgsMultiPolygon >();
269  break;
271  collect = qgis::make_unique< QgsMultiSurface >();
272  break;
274  collect = qgis::make_unique< QgsGeometryCollection >();
275  break;
276  default:
277  // should not be possible
278  return nullptr;
279  }
280  if ( QgsWkbTypes::hasM( t ) )
281  collect->addMValue();
282  if ( QgsWkbTypes::hasZ( t ) )
283  collect->addZValue();
284 
285  return collect;
286 }
static std::unique_ptr< QgsAbstractGeometry > geomFromWkb(QgsConstWkbPtr &wkb)
Construct geometry from a WKB string.
static std::unique_ptr< QgsAbstractGeometry > geomFromWkbType(QgsWkbTypes::Type t)
Returns empty geometry from wkb type.
static Type multiType(Type type)
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:296
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
static std::unique_ptr< QgsAbstractGeometry > fromPointXY(const QgsPointXY &point)
Construct geometry from a point.
double y
Definition: qgspointxy.h:48
static std::unique_ptr< QgsAbstractGeometry > fromPolylineXY(const QgsPolylineXY &polyline)
Construct geometry from a polyline.
A class to represent a 2D point.
Definition: qgspointxy.h:43
QVector< QgsPolylineXY > QgsPolygonXY
Polygon: first item of the list is outer ring, inner rings (if any) start from second item...
Definition: qgsgeometry.h:66
static std::unique_ptr< QgsGeometryCollection > createCollectionOfType(QgsWkbTypes::Type type)
Returns a new geometry collection matching a specified WKB type.
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
Definition: qgsgeometry.h:72
static std::unique_ptr< QgsPolygon > fromPolygonXY(const QgsPolygonXY &polygon)
Construct geometry from a polygon.
static bool hasZ(Type type)
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:768
QVector< QgsPolygonXY > QgsMultiPolygonXY
A collection of QgsPolygons that share a common collection of attributes.
Definition: qgsgeometry.h:83
QString what() const
Definition: qgsexception.h:48
static std::unique_ptr< QgsMultiPoint > fromMultiPointXY(const QgsMultiPointXY &multipoint)
Construct geometry from a multipoint.
QVector< QgsPolylineXY > QgsMultiPolylineXY
A collection of QgsPolylines that share a common collection of attributes.
Definition: qgsgeometry.h:76
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
static std::unique_ptr< QgsMultiLineString > fromMultiPolylineXY(const QgsMultiPolylineXY &multiline)
Construct geometry from a multipolyline.
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
double x
Definition: qgspointxy.h:47
Custom exception class for Wkb related exceptions.
Definition: qgswkbptr.h:31
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition: qgsgeometry.h:42
static std::unique_ptr< QgsAbstractGeometry > geomFromWkt(const QString &text)
Construct geometry from a WKT string.
static bool hasM(Type type)
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:818
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:427
QgsWkbTypes::Type readHeader() const
readHeader
Definition: qgswkbptr.cpp:53
static std::unique_ptr< QgsMultiPolygon > fromMultiPolygonXY(const QgsMultiPolygonXY &multipoly)
Construct geometry from a multipolygon.