QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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 #include "qgsapplication.h"
17 
18 QgsWkbPtr::QgsWkbPtr( QByteArray &wkb )
19 {
20  mP = reinterpret_cast<unsigned char *>( wkb.data() );
21  mStart = mP;
22  mEnd = mP + wkb.length();
23 }
24 
25 QgsWkbPtr::QgsWkbPtr( unsigned char *p, int size )
26 {
27  mP = p;
28  mStart = mP;
29  mEnd = mP + size;
30 }
31 
32 void QgsWkbPtr::verifyBound( int size ) const
33 {
34  if ( !mP || mP + size > mEnd )
35  throw QgsWkbException( QStringLiteral( "wkb access out of bounds" ) );
36 }
37 
38 QgsConstWkbPtr::QgsConstWkbPtr( const QByteArray &wkb )
39 {
40  mP = reinterpret_cast< unsigned char * >( const_cast<char *>( wkb.constData() ) );
41  mEnd = mP + wkb.length();
42  mEndianSwap = false;
44 }
45 
46 QgsConstWkbPtr::QgsConstWkbPtr( const unsigned char *p, int size )
47 {
48  mP = const_cast< unsigned char * >( p );
49  mEnd = mP + size;
50  mEndianSwap = false;
52 }
53 
55 {
56  if ( !mP )
57  return QgsWkbTypes::Unknown;
58 
59  char wkbEndian;
60  *this >> wkbEndian;
61  mEndianSwap = wkbEndian != QgsApplication::endian();
62 
63  int wkbType;
64  *this >> wkbType;
65  mWkbType = static_cast<QgsWkbTypes::Type>( wkbType );
66 
67  return mWkbType;
68 }
69 
70 void QgsConstWkbPtr::verifyBound( int size ) const
71 {
72  if ( !mP || mP + size > mEnd )
73  throw QgsWkbException( QStringLiteral( "wkb access out of bounds" ) );
74 }
75 
76 const QgsConstWkbPtr &QgsConstWkbPtr::operator>>( QPointF &point ) const
77 {
78  read( point.rx() );
79  read( point.ry() );
80  return *this;
81 }
82 
83 const QgsConstWkbPtr &QgsConstWkbPtr::operator>>( QPolygonF &points ) const
84 {
85  int skipZM = ( QgsWkbTypes::coordDimensions( mWkbType ) - 2 ) * sizeof( double );
86  Q_ASSERT( skipZM >= 0 );
87 
88  unsigned int nPoints;
89  read( nPoints );
90 
91  points.resize( nPoints );
92  QPointF *ptr = points.data();
93 
94  for ( unsigned int i = 0; i < nPoints; ++i, ++ptr )
95  {
96  read( ptr->rx() );
97  read( ptr->ry() );
98  mP += skipZM;
99  }
100  return *this;
101 }
QgsConstWkbPtr::mP
unsigned char * mP
Definition: qgswkbptr.h:130
qgswkbptr.h
QgsConstWkbPtr::QgsConstWkbPtr
QgsConstWkbPtr(const QByteArray &wkb)
Construct WKB pointer from QByteArray.
Definition: qgswkbptr.cpp:38
QgsWkbTypes::Type
Type
The WKB type describes the number of dimensions a geometry has.
Definition: qgswkbtypes.h:70
QgsWkbPtr::size
int size() const
size
Definition: qgswkbptr.h:107
QgsWkbTypes::coordDimensions
static int coordDimensions(Type type) SIP_HOLDGIL
Returns the coordinate dimension of the geometry type as an integer.
Definition: qgswkbtypes.h:925
QgsConstWkbPtr::mWkbType
QgsWkbTypes::Type mWkbType
Definition: qgswkbptr.h:133
qgsapplication.h
QgsWkbTypes::Unknown
@ Unknown
Definition: qgswkbtypes.h:71
QgsConstWkbPtr::mEnd
unsigned char * mEnd
Definition: qgswkbptr.h:131
QgsConstWkbPtr
Definition: qgswkbptr.h:128
QgsWkbException
Custom exception class for Wkb related exceptions.
Definition: qgswkbptr.h:31
QgsConstWkbPtr::operator>>
const QgsConstWkbPtr & operator>>(double &v) const
Definition: qgswkbptr.h:165
QgsApplication::endian
static endian_t endian()
Returns whether this machine uses big or little endian.
Definition: qgsapplication.cpp:1250
QgsWkbPtr::QgsWkbPtr
QgsWkbPtr(QByteArray &wkb)
Construct WKB pointer from QByteArray.
Definition: qgswkbptr.cpp:18
QgsConstWkbPtr::verifyBound
void verifyBound(int size) const
Verify bounds.
Definition: qgswkbptr.cpp:70
QgsConstWkbPtr::readHeader
QgsWkbTypes::Type readHeader() const
readHeader
Definition: qgswkbptr.cpp:54
QgsConstWkbPtr::read
void read(T &v) const
Read a value.
Definition: qgswkbptr.h:145
QgsConstWkbPtr::mEndianSwap
bool mEndianSwap
Definition: qgswkbptr.h:132