QGIS API Documentation  2.12.0-Lyon
qgspolygonv2.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspolygonv2.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 "qgspolygonv2.h"
19 #include "qgsapplication.h"
20 #include "qgsgeometryutils.h"
21 #include "qgslinestringv2.h"
22 #include "qgswkbptr.h"
23 
25 {
26  return new QgsPolygonV2( *this );
27 }
28 
29 bool QgsPolygonV2::fromWkb( const unsigned char* wkb )
30 {
31  clear();
32  if ( !wkb )
33  {
34  return false;
35  }
36 
37  QgsConstWkbPtr wkbPtr( wkb );
38  QgsWKBTypes::Type type = wkbPtr.readHeader();
40  {
41  return false;
42  }
43  mWkbType = type;
44 
45  int nRings;
46  wkbPtr >> nRings;
47  for ( int i = 0; i < nRings; ++i )
48  {
49  QgsLineStringV2* line = new QgsLineStringV2();
50  line->fromWkbPoints( mWkbType, wkbPtr );
51  /*if ( !line->isRing() )
52  {
53  delete line; continue;
54  }*/
55 
56  if ( !mExteriorRing )
57  {
58  mExteriorRing = line;
59  }
60  else
61  {
62  mInteriorRings.append( line );
63  }
64  }
65 
66  return true;
67 }
68 
70 {
71  int size = sizeof( char ) + sizeof( quint32 ) + sizeof( quint32 );
72  if ( mExteriorRing )
73  {
74  // Endianness and WkbType is not stored for LinearRings
75  size += mExteriorRing->wkbSize() - ( sizeof( char ) + sizeof( quint32 ) );
76  }
77  Q_FOREACH ( const QgsCurveV2* curve, mInteriorRings )
78  {
79  // Endianness and WkbType is not stored for LinearRings
80  size += curve->wkbSize() - ( sizeof( char ) + sizeof( quint32 ) );
81  }
82  return size;
83 }
84 
85 unsigned char* QgsPolygonV2::asWkb( int& binarySize ) const
86 {
87  binarySize = wkbSize();
88  unsigned char* geomPtr = new unsigned char[binarySize];
89  QgsWkbPtr wkb( geomPtr );
90  wkb << static_cast<char>( QgsApplication::endian() );
91  wkb << static_cast<quint32>( wkbType() );
92  wkb << static_cast<quint32>(( mExteriorRing != 0 ) + mInteriorRings.size() );
93  if ( mExteriorRing )
94  {
96  mExteriorRing->points( pts );
98  }
99  Q_FOREACH ( const QgsCurveV2* curve, mInteriorRings )
100  {
101  QList<QgsPointV2> pts;
102  curve->points( pts );
103  QgsGeometryUtils::pointsToWKB( wkb, pts, curve->is3D(), curve->isMeasure() );
104  }
105  return geomPtr;
106 }
107 
109 {
110  return clone();
111 }
QgsWKBTypes::Type wkbType() const
Returns the WKB type of the geometry.
void clear() override
Clears the geometry, ie reset it to a null geometry.
virtual void points(QList< QgsPointV2 > &pt) const =0
Returns a list of points within the curve.
unsigned char * asWkb(int &binarySize) const override
Returns a WKB representation of the geometry.
static endian_t endian()
Returns whether this machine uses big or little endian.
virtual int wkbSize() const =0
Returns the size of the WKB representation of the geometry.
int size() const
QgsWKBTypes::Type readHeader() const
Definition: qgswkbptr.cpp:8
Polygon geometry type.
Definition: qgspolygonv2.h:29
static Type flatType(Type type)
Definition: qgswkbtypes.cpp:46
bool is3D() const
Returns true if the geometry is 3D and contains a z-value.
void append(const T &value)
bool isMeasure() const
Returns true if the geometry contains m values.
Line string geometry type.
void fromWkbPoints(QgsWKBTypes::Type type, const QgsConstWkbPtr &wkb)
virtual QgsPolygonV2 * clone() const override
Clones the geometry by performing a deep copy.
QList< QgsCurveV2 * > mInteriorRings
QgsPolygonV2 * surfaceToPolygon() const override
Abstract base class for curved geometry type.
Definition: qgscurvev2.h:32
int wkbSize() const override
Returns the size of the WKB representation of the geometry.
static void pointsToWKB(QgsWkbPtr &wkb, const QList< QgsPointV2 > &points, bool is3D, bool isMeasure)
Returns a LinearRing { uint32 numPoints; Point points[numPoints]; }.
virtual bool fromWkb(const unsigned char *wkb) override
Sets the geometry from a WKB string.
QgsCurveV2 * mExteriorRing