/
qgspolygonv2.cpp
111 lines (100 loc) · 3.17 KB
/
qgspolygonv2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/***************************************************************************
qgspolygonv2.cpp
----------------
begin : September 2014
copyright : (C) 2014 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgspolygonv2.h"
#include "qgsapplication.h"
#include "qgsgeometryutils.h"
#include "qgslinestringv2.h"
#include "qgswkbptr.h"
QgsPolygonV2* QgsPolygonV2::clone() const
{
return new QgsPolygonV2( *this );
}
bool QgsPolygonV2::fromWkb( const unsigned char* wkb )
{
clear();
if ( !wkb )
{
return false;
}
QgsConstWkbPtr wkbPtr( wkb );
QgsWKBTypes::Type type = wkbPtr.readHeader();
if ( QgsWKBTypes::flatType( type ) != QgsWKBTypes::Polygon )
{
return false;
}
mWkbType = type;
int nRings;
wkbPtr >> nRings;
for ( int i = 0; i < nRings; ++i )
{
QgsLineStringV2* line = new QgsLineStringV2();
line->fromWkbPoints( mWkbType, wkbPtr );
/*if ( !line->isRing() )
{
delete line; continue;
}*/
if ( !mExteriorRing )
{
mExteriorRing = line;
}
else
{
mInteriorRings.append( line );
}
}
return true;
}
int QgsPolygonV2::wkbSize() const
{
int size = sizeof( char ) + sizeof( quint32 ) + sizeof( quint32 );
if ( mExteriorRing )
{
// Endianness and WkbType is not stored for LinearRings
size += mExteriorRing->wkbSize() - ( sizeof( char ) + sizeof( quint32 ) );
}
Q_FOREACH ( const QgsCurveV2* curve, mInteriorRings )
{
// Endianness and WkbType is not stored for LinearRings
size += curve->wkbSize() - ( sizeof( char ) + sizeof( quint32 ) );
}
return size;
}
unsigned char* QgsPolygonV2::asWkb( int& binarySize ) const
{
binarySize = wkbSize();
unsigned char* geomPtr = new unsigned char[binarySize];
QgsWkbPtr wkb( geomPtr );
wkb << static_cast<char>( QgsApplication::endian() );
wkb << static_cast<quint32>( wkbType() );
wkb << static_cast<quint32>(( mExteriorRing != 0 ) + mInteriorRings.size() );
if ( mExteriorRing )
{
QList<QgsPointV2> pts;
mExteriorRing->points( pts );
QgsGeometryUtils::pointsToWKB( wkb, pts, mExteriorRing->is3D(), mExteriorRing->isMeasure() );
}
Q_FOREACH ( const QgsCurveV2* curve, mInteriorRings )
{
QList<QgsPointV2> pts;
curve->points( pts );
QgsGeometryUtils::pointsToWKB( wkb, pts, curve->is3D(), curve->isMeasure() );
}
return geomPtr;
}
QgsPolygonV2* QgsPolygonV2::surfaceToPolygon() const
{
return clone();
}