-
-
Notifications
You must be signed in to change notification settings - Fork 198
/
binary_header.go
216 lines (189 loc) · 5.07 KB
/
binary_header.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package gpkg
import (
"encoding/binary"
"errors"
"fmt"
"math"
)
type envelopeType uint8
// Magic is the magic number encode in the header. It should be 0x4750
var Magic = [2]byte{0x47, 0x50}
const (
EnvelopeTypeNone = envelopeType(0)
EnvelopeTypeXY = envelopeType(1)
EnvelopeTypeXYZ = envelopeType(2)
EnvelopeTypeXYM = envelopeType(3)
EnvelopeTypeXYZM = envelopeType(4)
EnvelopeTypeInvalid = envelopeType(5)
)
// NumberOfElements that the particular Evnelope Type will have.
func (et envelopeType) NumberOfElements() int {
switch et {
case EnvelopeTypeNone:
return 0
case EnvelopeTypeXY:
return 4
case EnvelopeTypeXYZ:
return 6
case EnvelopeTypeXYM:
return 6
case EnvelopeTypeXYZM:
return 8
default:
return -1
}
}
func (et envelopeType) String() string {
str := "NONEXYZMXYMINVALID"
switch et {
case EnvelopeTypeNone:
return str[0:4]
case EnvelopeTypeXY:
return str[4 : 4+2]
case EnvelopeTypeXYZ:
return str[4 : 4+3]
case EnvelopeTypeXYM:
return str[8 : 8+3]
case EnvelopeTypeXYZM:
return str[4 : 4+4]
default:
return str[11:]
}
}
// HEADER FLAG LAYOUT
// 7 6 5 4 3 2 1 0
// R R X Y E E E B
// R Reserved for future use. (should be set to 0)
// X GeoPackageBinary type
// Y empty geometry
// E Envelope type
// B ByteOrder
const (
maskByteOrder = 1 << 0
maskEnvelopeType = 1<<3 | 1<<2 | 1<<1
maskEmptyGeometry = 1 << 4
maskGeoPackageBinary = 1 << 5
)
type headerFlags byte
func (hf headerFlags) String() string { return fmt.Sprintf("0x%02x", uint8(hf)) }
// Endian will return the encoded Endianess
func (hf headerFlags) Endian() binary.ByteOrder {
if hf&maskByteOrder == 0 {
return binary.BigEndian
}
return binary.LittleEndian
}
// EnvelopeType returns the type of the envelope.
func (hf headerFlags) Envelope() envelopeType {
et := uint8((hf & maskEnvelopeType) >> 1)
if et >= uint8(EnvelopeTypeInvalid) {
return EnvelopeTypeInvalid
}
return envelopeType(et)
}
// IsEmpty returns whether or not the geometry is empty.
func (hf headerFlags) IsEmpty() bool { return ((hf & maskEmptyGeometry) >> 4) == 1 }
// IsStandard returns weather or not the geometry is a standard GeoPackage geometry type.
func (hf headerFlags) IsStandard() bool { return ((hf & maskGeoPackageBinary) >> 5) == 0 }
// BinaryHeader is the gpkg header that accompainies every feature.
type BinaryHeader struct {
// See: http://www.geopackage.org/spec/
magic [2]byte // should be 0x47 0x50 (GP in ASCII)
version uint8
flags headerFlags
srsid int32
envelope []float64
headerSize int // total bytes in header
}
// NewBinaryHeader decodes the data into the BinaryHeader
func NewBinaryHeader(data []byte) (*BinaryHeader, error) {
if len(data) < 8 {
return nil, errors.New("not enough bytes to decode header")
}
var bh BinaryHeader
bh.magic[0] = data[0]
bh.magic[1] = data[1]
bh.version = data[2]
bh.flags = headerFlags(data[3])
en := bh.flags.Endian()
bh.srsid = int32(en.Uint32(data[4 : 4+4]))
bytes := data[8:]
et := bh.flags.Envelope()
if et == EnvelopeTypeInvalid {
return nil, errors.New("invalid envelope type")
}
if et == EnvelopeTypeNone {
return &bh, nil
}
num := et.NumberOfElements()
// there are 8 bytes per float64 value and we need num of them.
if len(bytes) < (num * 8) {
return nil, errors.New("not enough bytes to decode header")
}
bh.envelope = make([]float64, 0, num)
for i := 0; i < num; i++ {
bits := en.Uint64(bytes[i*8 : (i*8)+8])
bh.envelope = append(bh.envelope, math.Float64frombits(bits))
}
if bh.magic[0] != Magic[0] || bh.magic[1] != Magic[1] {
return &bh, errors.New("invalid magic number")
}
return &bh, nil
}
// Magic is the magic number encode in the header. It should be 0x4750
func (h *BinaryHeader) Magic() [2]byte {
if h == nil {
return Magic
}
return h.magic
}
// Version is the version number encode in the header.
func (h *BinaryHeader) Version() uint8 {
if h == nil {
return 0
}
return h.version
}
// EnvelopeType is the type of the envelope that is provided.
func (h *BinaryHeader) EnvelopeType() envelopeType {
if h == nil {
return EnvelopeTypeInvalid
}
return h.flags.Envelope()
}
// SRSId is the SRS id of the feature.
func (h *BinaryHeader) SRSId() int32 {
if h == nil {
return 0
}
return h.srsid
}
// Envelope is the bounding box of the feature, used for searching. If the EnvelopeType is EvelopeTypeNone, then there isn't a envelope encoded
// and a search without an index will need to be preformed. This is to save space.
func (h *BinaryHeader) Envelope() []float64 {
if h == nil {
return nil
}
return h.envelope
}
// IsGeometryEmpty tells us if the geometry should be considered empty.
func (h *BinaryHeader) IsGeometryEmpty() bool {
if h == nil {
return true
}
return h.flags.IsEmpty()
}
// IsStandardGeometery is the geometry a core/extended geometry type, or a user defined geometry type.
func (h *BinaryHeader) IsStandardGeometry() bool {
if h == nil {
return true
}
return h.flags.IsStandard()
}
// Size is the size of the header in bytes.
func (h *BinaryHeader) Size() int {
if h == nil {
return 0
}
return (len(h.envelope) * 8) + 8
}