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