QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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( "Triangle" ), Qt::CaseInsensitive ) )
97  {
98  geom = qgis::make_unique< QgsTriangle >();
99  }
100  else if ( trimmed.startsWith( QLatin1String( "CurvePolygon" ), Qt::CaseInsensitive ) )
101  {
102  geom = qgis::make_unique< QgsCurvePolygon >();
103  }
104  else if ( trimmed.startsWith( QLatin1String( "MultiPoint" ), Qt::CaseInsensitive ) )
105  {
106  geom = qgis::make_unique< QgsMultiPoint >();
107  }
108  else if ( trimmed.startsWith( QLatin1String( "MultiCurve" ), Qt::CaseInsensitive ) )
109  {
110  geom = qgis::make_unique< QgsMultiCurve >();
111  }
112  else if ( trimmed.startsWith( QLatin1String( "MultiLineString" ), Qt::CaseInsensitive ) )
113  {
114  geom = qgis::make_unique< QgsMultiLineString >();
115  }
116  else if ( trimmed.startsWith( QLatin1String( "MultiSurface" ), Qt::CaseInsensitive ) )
117  {
118  geom = qgis::make_unique< QgsMultiSurface >();
119  }
120  else if ( trimmed.startsWith( QLatin1String( "MultiPolygon" ), Qt::CaseInsensitive ) )
121  {
122  geom = qgis::make_unique< QgsMultiPolygon >();
123  }
124  else if ( trimmed.startsWith( QLatin1String( "GeometryCollection" ), Qt::CaseInsensitive ) )
125  {
126  geom = qgis::make_unique< QgsGeometryCollection >();
127  }
128 
129  if ( geom )
130  {
131  if ( !geom->fromWkt( text ) )
132  {
133  return nullptr;
134  }
135  }
136  return geom;
137 }
138 
139 std::unique_ptr< QgsAbstractGeometry > QgsGeometryFactory::fromPointXY( const QgsPointXY &point )
140 {
141  return qgis::make_unique< QgsPoint >( point.x(), point.y() );
142 }
143 
144 std::unique_ptr<QgsMultiPoint> QgsGeometryFactory::fromMultiPointXY( const QgsMultiPointXY &multipoint )
145 {
146  std::unique_ptr< QgsMultiPoint > mp = qgis::make_unique< QgsMultiPoint >();
147  QgsMultiPointXY::const_iterator ptIt = multipoint.constBegin();
148  mp->reserve( multipoint.size() );
149  for ( ; ptIt != multipoint.constEnd(); ++ptIt )
150  {
151  QgsPoint *pt = new QgsPoint( ptIt->x(), ptIt->y() );
152  mp->addGeometry( pt );
153  }
154  return mp;
155 }
156 
157 std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::fromPolylineXY( const QgsPolylineXY &polyline )
158 {
159  return linestringFromPolyline( polyline );
160 }
161 
162 std::unique_ptr<QgsMultiLineString> QgsGeometryFactory::fromMultiPolylineXY( const QgsMultiPolylineXY &multiline )
163 {
164  std::unique_ptr< QgsMultiLineString > mLine = qgis::make_unique< QgsMultiLineString >();
165  mLine->reserve( multiline.size() );
166  for ( int i = 0; i < multiline.size(); ++i )
167  {
168  mLine->addGeometry( fromPolylineXY( multiline.at( i ) ).release() );
169  }
170  return mLine;
171 }
172 
173 std::unique_ptr<QgsPolygon> QgsGeometryFactory::fromPolygonXY( const QgsPolygonXY &polygon )
174 {
175  std::unique_ptr< QgsPolygon > poly = qgis::make_unique< QgsPolygon >();
176 
177  QVector<QgsCurve *> holes;
178  holes.reserve( polygon.size() );
179  for ( int i = 0; i < polygon.size(); ++i )
180  {
181  std::unique_ptr< QgsLineString > l = linestringFromPolyline( polygon.at( i ) );
182  l->close();
183 
184  if ( i == 0 )
185  {
186  poly->setExteriorRing( l.release() );
187  }
188  else
189  {
190  holes.push_back( l.release() );
191  }
192  }
193  poly->setInteriorRings( holes );
194  return poly;
195 }
196 
197 std::unique_ptr< QgsMultiPolygon > QgsGeometryFactory::fromMultiPolygonXY( const QgsMultiPolygonXY &multipoly )
198 {
199  std::unique_ptr< QgsMultiPolygon > mp = qgis::make_unique< QgsMultiPolygon >();
200  mp->reserve( multipoly.size() );
201  for ( int i = 0; i < multipoly.size(); ++i )
202  {
203  mp->addGeometry( fromPolygonXY( multipoly.at( i ) ).release() );
204  }
205  return mp;
206 }
207 
208 std::unique_ptr<QgsLineString> QgsGeometryFactory::linestringFromPolyline( const QgsPolylineXY &polyline )
209 {
210  const int size = polyline.size();
211  QVector< double > x;
212  x.resize( size );
213  QVector< double > y;
214  y.resize( size );
215  double *destX = x.data();
216  double *destY = y.data();
217  const QgsPointXY *src = polyline.data();
218  for ( int i = 0; i < size; ++i )
219  {
220  *destX++ = src->x();
221  *destY++ = src->y();
222  src++;
223  }
224  std::unique_ptr< QgsLineString > line = qgis::make_unique< QgsLineString >( x, y );
225  return line;
226 }
227 
228 std::unique_ptr<QgsAbstractGeometry> QgsGeometryFactory::geomFromWkbType( QgsWkbTypes::Type t )
229 {
231  switch ( type )
232  {
233  case QgsWkbTypes::Point:
234  return qgis::make_unique< QgsPoint >();
236  return qgis::make_unique< QgsLineString >();
238  return qgis::make_unique< QgsCircularString >();
240  return qgis::make_unique< QgsCompoundCurve >();
242  return qgis::make_unique< QgsPolygon >();
244  return qgis::make_unique< QgsCurvePolygon >();
246  return qgis::make_unique< QgsMultiLineString >();
248  return qgis::make_unique< QgsMultiPolygon >();
250  return qgis::make_unique< QgsMultiPoint >();
252  return qgis::make_unique< QgsMultiCurve >();
254  return qgis::make_unique< QgsMultiSurface >();
256  return qgis::make_unique< QgsGeometryCollection >();
258  return qgis::make_unique< QgsTriangle >();
259  default:
260  return nullptr;
261  }
262 }
263 
264 std::unique_ptr<QgsGeometryCollection> QgsGeometryFactory::createCollectionOfType( QgsWkbTypes::Type t )
265 {
267  std::unique_ptr< QgsGeometryCollection > collect;
268  switch ( type )
269  {
271  collect = qgis::make_unique< QgsMultiPoint >();
272  break;
274  collect = qgis::make_unique< QgsMultiLineString >();
275  break;
277  collect = qgis::make_unique< QgsMultiCurve >();
278  break;
280  collect = qgis::make_unique< QgsMultiPolygon >();
281  break;
283  collect = qgis::make_unique< QgsMultiSurface >();
284  break;
286  collect = qgis::make_unique< QgsGeometryCollection >();
287  break;
288  default:
289  // should not be possible
290  return nullptr;
291  }
292  if ( QgsWkbTypes::hasM( t ) )
293  collect->addMValue();
294  if ( QgsWkbTypes::hasZ( t ) )
295  collect->addZValue();
296 
297  return collect;
298 }
qgspolygon.h
QgsPointXY::y
double y
Definition: qgspointxy.h:48
QgsWkbTypes::Triangle
@ Triangle
Definition: qgswkbtypes.h:75
QgsWkbTypes::Point
@ Point
Definition: qgswkbtypes.h:72
qgslinestring.h
QgsPolygonXY
QVector< QgsPolylineXY > QgsPolygonXY
Polygon: first item of the list is outer ring, inner rings (if any) start from second item.
Definition: qgsgeometry.h:75
QgsWkbTypes::MultiPolygon
@ MultiPolygon
Definition: qgswkbtypes.h:78
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:38
qgsgeometryfactory.h
QgsGeometryFactory::fromPolygonXY
static std::unique_ptr< QgsPolygon > fromPolygonXY(const QgsPolygonXY &polygon)
Construct geometry from a polygon.
Definition: qgsgeometryfactory.cpp:173
QgsPointXY::x
Q_GADGET double x
Definition: qgspointxy.h:47
qgscompoundcurve.h
QgsWkbTypes::flatType
static Type flatType(Type type) SIP_HOLDGIL
Returns the flat type for a WKB type.
Definition: qgswkbtypes.h:702
QgsGeometryFactory::geomFromWkb
static std::unique_ptr< QgsAbstractGeometry > geomFromWkb(QgsConstWkbPtr &wkb)
Construct geometry from a WKB string.
Definition: qgsgeometryfactory.cpp:34
QgsPolylineXY
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
Definition: qgsgeometry.h:51
QgsWkbTypes::LineString
@ LineString
Definition: qgswkbtypes.h:73
QgsGeometryFactory::fromMultiPolygonXY
static std::unique_ptr< QgsMultiPolygon > fromMultiPolygonXY(const QgsMultiPolygonXY &multipoly)
Construct geometry from a multipolygon.
Definition: qgsgeometryfactory.cpp:197
qgsmultipoint.h
QgsMultiPolygonXY
QVector< QgsPolygonXY > QgsMultiPolygonXY
A collection of QgsPolygons that share a common collection of attributes.
Definition: qgsgeometry.h:92
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
QgsWkbTypes::MultiCurve
@ MultiCurve
Definition: qgswkbtypes.h:83
qgspoint.h
QgsDebugMsg
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QgsWkbTypes::multiType
static Type multiType(Type type) SIP_HOLDGIL
Returns the multi type for a WKB type.
Definition: qgswkbtypes.h:302
QgsMultiPointXY
QVector< QgsPointXY > QgsMultiPointXY
A collection of QgsPoints that share a common collection of attributes.
Definition: qgsgeometry.h:81
QgsWkbTypes::MultiLineString
@ MultiLineString
Definition: qgswkbtypes.h:77
QgsGeometryFactory::fromMultiPolylineXY
static std::unique_ptr< QgsMultiLineString > fromMultiPolylineXY(const QgsMultiPolylineXY &multiline)
Construct geometry from a multipolyline.
Definition: qgsgeometryfactory.cpp:162
QgsGeometryFactory::createCollectionOfType
static std::unique_ptr< QgsGeometryCollection > createCollectionOfType(QgsWkbTypes::Type type)
Returns a new geometry collection matching a specified WKB type.
Definition: qgsgeometryfactory.cpp:264
qgsmultipolygon.h
QgsWkbTypes::Unknown
@ Unknown
Definition: qgswkbtypes.h:71
QgsConstWkbPtr
Definition: qgswkbptr.h:128
QgsWkbTypes::GeometryCollection
@ GeometryCollection
Definition: qgswkbtypes.h:79
QgsException::what
QString what() const
Definition: qgsexception.h:48
QgsWkbException
Custom exception class for Wkb related exceptions.
Definition: qgswkbptr.h:31
QgsGeometryFactory::fromMultiPointXY
static std::unique_ptr< QgsMultiPoint > fromMultiPointXY(const QgsMultiPointXY &multipoint)
Construct geometry from a multipoint.
Definition: qgsgeometryfactory.cpp:144
QgsWkbTypes::CurvePolygon
@ CurvePolygon
Definition: qgswkbtypes.h:82
qgstriangle.h
QgsWkbTypes::hasM
static bool hasM(Type type) SIP_HOLDGIL
Tests whether a WKB type contains m values.
Definition: qgswkbtypes.h:1093
QgsGeometryFactory::geomFromWkt
static std::unique_ptr< QgsAbstractGeometry > geomFromWkt(const QString &text)
Construct geometry from a WKT string.
Definition: qgsgeometryfactory.cpp:72
QgsMultiPolylineXY
QVector< QgsPolylineXY > QgsMultiPolylineXY
A collection of QgsPolylines that share a common collection of attributes.
Definition: qgsgeometry.h:85
qgscurvepolygon.h
qgscircularstring.h
QgsPointXY
A class to represent a 2D point.
Definition: qgspointxy.h:44
QgsWkbTypes::CircularString
@ CircularString
Definition: qgswkbtypes.h:80
QgsWkbTypes::MultiSurface
@ MultiSurface
Definition: qgswkbtypes.h:84
QgsWkbTypes::Polygon
@ Polygon
Definition: qgswkbtypes.h:74
QgsWkbTypes::MultiPoint
@ MultiPoint
Definition: qgswkbtypes.h:76
QgsWkbTypes::hasZ
static bool hasZ(Type type) SIP_HOLDGIL
Tests whether a WKB type contains the z-dimension.
Definition: qgswkbtypes.h:1043
qgsmulticurve.h
QgsConstWkbPtr::readHeader
QgsWkbTypes::Type readHeader() const
readHeader
Definition: qgswkbptr.cpp:54
qgslogger.h
QgsGeometryFactory::geomFromWkbType
static std::unique_ptr< QgsAbstractGeometry > geomFromWkbType(QgsWkbTypes::Type t)
Returns empty geometry from wkb type.
Definition: qgsgeometryfactory.cpp:228
qgswkbtypes.h
qgsmultisurface.h
QgsGeometryFactory::fromPolylineXY
static std::unique_ptr< QgsAbstractGeometry > fromPolylineXY(const QgsPolylineXY &polyline)
Construct geometry from a polyline.
Definition: qgsgeometryfactory.cpp:157
qgsmultilinestring.h
QgsWkbTypes::CompoundCurve
@ CompoundCurve
Definition: qgswkbtypes.h:81
QgsGeometryFactory::fromPointXY
static std::unique_ptr< QgsAbstractGeometry > fromPointXY(const QgsPointXY &point)
Construct geometry from a point.
Definition: qgsgeometryfactory.cpp:139