QGIS API Documentation  3.2.0-Bonn (bc43194)
qgswkbptr.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgswkbptr.cpp
3  ---------------------
4  begin : May 2015
5  copyright : (C) 2015 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "qgswkbptr.h"
16 
17 QgsWkbPtr::QgsWkbPtr( QByteArray &wkb )
18 {
19  mP = reinterpret_cast<unsigned char *>( wkb.data() );
20  mStart = mP;
21  mEnd = mP + wkb.length();
22 }
23 
24 QgsWkbPtr::QgsWkbPtr( unsigned char *p, int size )
25 {
26  mP = p;
27  mStart = mP;
28  mEnd = mP + size;
29 }
30 
31 void QgsWkbPtr::verifyBound( int size ) const
32 {
33  if ( !mP || mP + size > mEnd )
34  throw QgsWkbException( QStringLiteral( "wkb access out of bounds" ) );
35 }
36 
37 QgsConstWkbPtr::QgsConstWkbPtr( const QByteArray &wkb )
38 {
39  mP = reinterpret_cast< unsigned char * >( const_cast<char *>( wkb.constData() ) );
40  mEnd = mP + wkb.length();
41  mEndianSwap = false;
42  mWkbType = QgsWkbTypes::Unknown;
43 }
44 
45 QgsConstWkbPtr::QgsConstWkbPtr( const unsigned char *p, int size )
46 {
47  mP = const_cast< unsigned char * >( p );
48  mEnd = mP + size;
49  mEndianSwap = false;
50  mWkbType = QgsWkbTypes::Unknown;
51 }
52 
54 {
55  if ( !mP )
56  return QgsWkbTypes::Unknown;
57 
58  char wkbEndian;
59  *this >> wkbEndian;
60  mEndianSwap = wkbEndian != QgsApplication::endian();
61 
62  int wkbType;
63  *this >> wkbType;
64  mWkbType = static_cast<QgsWkbTypes::Type>( wkbType );
65 
66  return mWkbType;
67 }
68 
70 {
71  if ( !mP || mP + size > mEnd )
72  throw QgsWkbException( QStringLiteral( "wkb access out of bounds" ) );
73 }
74 
75 const QgsConstWkbPtr &QgsConstWkbPtr::operator>>( QPointF &point ) const
76 {
77  read( point.rx() );
78  read( point.ry() );
79  return *this;
80 }
81 
82 const QgsConstWkbPtr &QgsConstWkbPtr::operator>>( QPolygonF &points ) const
83 {
84  int skipZM = ( QgsWkbTypes::coordDimensions( mWkbType ) - 2 ) * sizeof( double );
85  Q_ASSERT( skipZM >= 0 );
86 
87  unsigned int nPoints;
88  read( nPoints );
89 
90  points.resize( nPoints );
91  QPointF *ptr = points.data();
92 
93  for ( unsigned int i = 0; i < nPoints; ++i, ++ptr )
94  {
95  read( ptr->rx() );
96  read( ptr->ry() );
97  mP += skipZM;
98  }
99  return *this;
100 }
QgsWkbPtr(QByteArray &wkb)
Construct WKB pointer from QByteArray.
Definition: qgswkbptr.cpp:17
QgsConstWkbPtr(const QByteArray &wkb)
Construct WKB pointer from QByteArray.
Definition: qgswkbptr.cpp:37
static endian_t endian()
Returns whether this machine uses big or little endian.
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:67
const QgsConstWkbPtr & operator>>(double &v) const
Definition: qgswkbptr.h:166
Custom exception class for Wkb related exceptions.
Definition: qgswkbptr.h:31
void verifyBound(int size) const
Verify bounds.
Definition: qgswkbptr.cpp:69
int size() const
size
Definition: qgswkbptr.h:108
QgsWkbTypes::Type readHeader() const
readHeader
Definition: qgswkbptr.cpp:53
static int coordDimensions(Type type)
Returns the coordinate dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:650