QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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  holes.reserve( polygon.size() );
173  for ( int i = 0; i < polygon.size(); ++i )
174  {
175  std::unique_ptr< QgsLineString > l = linestringFromPolyline( polygon.at( i ) );
176  l->close();
177 
178  if ( i == 0 )
179  {
180  poly->setExteriorRing( l.release() );
181  }
182  else
183  {
184  holes.push_back( l.release() );
185  }
186  }
187  poly->setInteriorRings( holes );
188  return poly;
189 }
190 
191 std::unique_ptr< QgsMultiPolygon > QgsGeometryFactory::fromMultiPolygonXY( const QgsMultiPolygonXY &multipoly )
192 {
193  std::unique_ptr< QgsMultiPolygon > mp = qgis::make_unique< QgsMultiPolygon >();
194  for ( int i = 0; i < multipoly.size(); ++i )
195  {
196  mp->addGeometry( fromPolygonXY( multipoly.at( i ) ).release() );
197  }
198  return mp;
199 }
200 
201 std::unique_ptr<QgsLineString> QgsGeometryFactory::linestringFromPolyline( const QgsPolylineXY &polyline )
202 {
203  QVector< double > x;
204  x.reserve( polyline.size() );
205  QVector< double > y;
206  y.reserve( polyline.size() );
207  QgsPolylineXY::const_iterator it = polyline.constBegin();
208  for ( ; it != polyline.constEnd(); ++it )
209  {
210  x << it->x();
211  y << it->y();
212  }
213  std::unique_ptr< QgsLineString > line = qgis::make_unique< QgsLineString >( x, y );
214  return line;
215 }
216 
217 std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::Type t )
218 {
220  switch ( type )
221  {
222  case QgsWkbTypes::Point:
223  return qgis::make_unique< QgsPoint >();
225  return qgis::make_unique< QgsLineString >();
227  return qgis::make_unique< QgsCircularString >();
229  return qgis::make_unique< QgsCompoundCurve >();
231  return qgis::make_unique< QgsPolygon >();
233  return qgis::make_unique< QgsCurvePolygon >();
235  return qgis::make_unique< QgsMultiLineString >();
237  return qgis::make_unique< QgsMultiPolygon >();
239  return qgis::make_unique< QgsMultiPoint >();
241  return qgis::make_unique< QgsMultiCurve >();
243  return qgis::make_unique< QgsMultiSurface >();
245  return qgis::make_unique< QgsGeometryCollection >();
247  return qgis::make_unique< QgsTriangle >();
248  default:
249  return nullptr;
250  }
251 }
252 
253 std::unique_ptr<QgsGeometryCollection> QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::Type t )
254 {
256  std::unique_ptr< QgsGeometryCollection > collect;
257  switch ( type )
258  {
260  collect = qgis::make_unique< QgsMultiPoint >();
261  break;
263  collect = qgis::make_unique< QgsMultiLineString >();
264  break;
266  collect = qgis::make_unique< QgsMultiCurve >();
267  break;
269  collect = qgis::make_unique< QgsMultiPolygon >();
270  break;
272  collect = qgis::make_unique< QgsMultiSurface >();
273  break;
275  collect = qgis::make_unique< QgsGeometryCollection >();
276  break;
277  default:
278  // should not be possible
279  return nullptr;
280  }
281  if ( QgsWkbTypes::hasM( t ) )
282  collect->addMValue();
283  if ( QgsWkbTypes::hasZ( t ) )
284  collect->addZValue();
285 
286  return collect;
287 }
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:298
#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:68
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:74
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:906
QVector< QgsPolygonXY > QgsMultiPolygonXY
A collection of QgsPolygons that share a common collection of attributes.
Definition: qgsgeometry.h:85
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:78
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:68
static std::unique_ptr< QgsMultiLineString > fromMultiPolylineXY(const QgsMultiPolylineXY &multiline)
Construct geometry from a multipolyline.
QgsWkbTypes::Type readHeader() const
readHeader
Definition: qgswkbptr.cpp:53
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
QString what() const
Definition: qgsexception.h:48
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition: qgsgeometry.h:44
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:956
static Type flatType(Type type)
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:565
static std::unique_ptr< QgsMultiPolygon > fromMultiPolygonXY(const QgsMultiPolygonXY &multipoly)
Construct geometry from a multipolygon.