-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
directory.go
348 lines (303 loc) · 9.36 KB
/
directory.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package pmtiles
import (
"bufio"
"bytes"
"compress/gzip"
"encoding/binary"
"fmt"
)
// Compression is the compression algorithm applied to individual tiles (or none)
type Compression uint8
const (
UnknownCompression Compression = 0
NoCompression = 1
Gzip = 2
Brotli = 3
Zstd = 4
)
// TileType is the format of individual tile contents in the archive.
type TileType uint8
const (
UnknownTileType TileType = 0
Mvt = 1
Png = 2
Jpeg = 3
Webp = 4
Avif = 5
)
// HeaderV3LenBytes is the fixed-size binary header size.
const HeaderV3LenBytes = 127
// HeaderV3 is a binary header for PMTiles specification version 3.
type HeaderV3 struct {
SpecVersion uint8
RootOffset uint64
RootLength uint64
MetadataOffset uint64
MetadataLength uint64
LeafDirectoryOffset uint64
LeafDirectoryLength uint64
TileDataOffset uint64
TileDataLength uint64
AddressedTilesCount uint64
TileEntriesCount uint64
TileContentsCount uint64
Clustered bool
InternalCompression Compression
TileCompression Compression
TileType TileType
MinZoom uint8
MaxZoom uint8
MinLonE7 int32
MinLatE7 int32
MaxLonE7 int32
MaxLatE7 int32
CenterZoom uint8
CenterLonE7 int32
CenterLatE7 int32
}
func headerContentType(header HeaderV3) (string, bool) {
switch header.TileType {
case Mvt:
return "application/x-protobuf", true
case Png:
return "image/png", true
case Jpeg:
return "image/jpeg", true
case Webp:
return "image/webp", true
case Avif:
return "image/avif", true
default:
return "", false
}
}
func headerExt(header HeaderV3) string {
switch header.TileType {
case Mvt:
return ".mvt"
case Png:
return ".png"
case Jpeg:
return ".jpg"
case Webp:
return ".webp"
case Avif:
return ".avif"
default:
return ""
}
}
func headerContentEncoding(compression Compression) (string, bool) {
switch compression {
case Gzip:
return "gzip", true
case Brotli:
return "br", true
default:
return "", false
}
}
// EntryV3 is an entry in a PMTiles spec version 3 directory.
type EntryV3 struct {
TileID uint64
Offset uint64
Length uint32
RunLength uint32
}
func serializeEntries(entries []EntryV3) []byte {
var b bytes.Buffer
tmp := make([]byte, binary.MaxVarintLen64)
w, _ := gzip.NewWriterLevel(&b, gzip.BestCompression)
var n int
n = binary.PutUvarint(tmp, uint64(len(entries)))
w.Write(tmp[:n])
lastID := uint64(0)
for _, entry := range entries {
n = binary.PutUvarint(tmp, uint64(entry.TileID)-lastID)
w.Write(tmp[:n])
lastID = uint64(entry.TileID)
}
for _, entry := range entries {
n := binary.PutUvarint(tmp, uint64(entry.RunLength))
w.Write(tmp[:n])
}
for _, entry := range entries {
n := binary.PutUvarint(tmp, uint64(entry.Length))
w.Write(tmp[:n])
}
for i, entry := range entries {
var n int
if i > 0 && entry.Offset == entries[i-1].Offset+uint64(entries[i-1].Length) {
n = binary.PutUvarint(tmp, 0)
} else {
n = binary.PutUvarint(tmp, uint64(entry.Offset+1)) // add 1 to not conflict with 0
}
w.Write(tmp[:n])
}
w.Close()
return b.Bytes()
}
func deserializeEntries(data *bytes.Buffer) []EntryV3 {
entries := make([]EntryV3, 0)
reader, _ := gzip.NewReader(data)
byteReader := bufio.NewReader(reader)
numEntries, _ := binary.ReadUvarint(byteReader)
lastID := uint64(0)
for i := uint64(0); i < numEntries; i++ {
tmp, _ := binary.ReadUvarint(byteReader)
entries = append(entries, EntryV3{lastID + tmp, 0, 0, 0})
lastID = lastID + tmp
}
for i := uint64(0); i < numEntries; i++ {
runLength, _ := binary.ReadUvarint(byteReader)
entries[i].RunLength = uint32(runLength)
}
for i := uint64(0); i < numEntries; i++ {
length, _ := binary.ReadUvarint(byteReader)
entries[i].Length = uint32(length)
}
for i := uint64(0); i < numEntries; i++ {
tmp, _ := binary.ReadUvarint(byteReader)
if i > 0 && tmp == 0 {
entries[i].Offset = entries[i-1].Offset + uint64(entries[i-1].Length)
} else {
entries[i].Offset = tmp - 1
}
}
return entries
}
func findTile(entries []EntryV3, tileID uint64) (EntryV3, bool) {
m := 0
n := len(entries) - 1
for m <= n {
k := (n + m) >> 1
cmp := int64(tileID) - int64(entries[k].TileID)
if cmp > 0 {
m = k + 1
} else if cmp < 0 {
n = k - 1
} else {
return entries[k], true
}
}
// at this point, m > n
if n >= 0 {
if entries[n].RunLength == 0 {
return entries[n], true
}
if tileID-entries[n].TileID < uint64(entries[n].RunLength) {
return entries[n], true
}
}
return EntryV3{}, false
}
func serializeHeader(header HeaderV3) []byte {
b := make([]byte, HeaderV3LenBytes)
copy(b[0:7], "PMTiles")
b[7] = 3
binary.LittleEndian.PutUint64(b[8:8+8], header.RootOffset)
binary.LittleEndian.PutUint64(b[16:16+8], header.RootLength)
binary.LittleEndian.PutUint64(b[24:24+8], header.MetadataOffset)
binary.LittleEndian.PutUint64(b[32:32+8], header.MetadataLength)
binary.LittleEndian.PutUint64(b[40:40+8], header.LeafDirectoryOffset)
binary.LittleEndian.PutUint64(b[48:48+8], header.LeafDirectoryLength)
binary.LittleEndian.PutUint64(b[56:56+8], header.TileDataOffset)
binary.LittleEndian.PutUint64(b[64:64+8], header.TileDataLength)
binary.LittleEndian.PutUint64(b[72:72+8], header.AddressedTilesCount)
binary.LittleEndian.PutUint64(b[80:80+8], header.TileEntriesCount)
binary.LittleEndian.PutUint64(b[88:88+8], header.TileContentsCount)
if header.Clustered {
b[96] = 0x1
}
b[97] = uint8(header.InternalCompression)
b[98] = uint8(header.TileCompression)
b[99] = uint8(header.TileType)
b[100] = header.MinZoom
b[101] = header.MaxZoom
binary.LittleEndian.PutUint32(b[102:102+4], uint32(header.MinLonE7))
binary.LittleEndian.PutUint32(b[106:106+4], uint32(header.MinLatE7))
binary.LittleEndian.PutUint32(b[110:110+4], uint32(header.MaxLonE7))
binary.LittleEndian.PutUint32(b[114:114+4], uint32(header.MaxLatE7))
b[118] = header.CenterZoom
binary.LittleEndian.PutUint32(b[119:119+4], uint32(header.CenterLonE7))
binary.LittleEndian.PutUint32(b[123:123+4], uint32(header.CenterLatE7))
return b
}
func deserializeHeader(d []byte) (HeaderV3, error) {
h := HeaderV3{}
magicNumber := d[0:7]
if string(magicNumber) != "PMTiles" {
return h, fmt.Errorf("magic number not detected. confirm this is a PMTiles archive")
}
specVersion := d[7]
if specVersion > uint8(3) {
return h, fmt.Errorf("archive is spec version %d, but this program only supports version 3: upgrade your pmtiles program", specVersion)
}
h.SpecVersion = specVersion
h.RootOffset = binary.LittleEndian.Uint64(d[8 : 8+8])
h.RootLength = binary.LittleEndian.Uint64(d[16 : 16+8])
h.MetadataOffset = binary.LittleEndian.Uint64(d[24 : 24+8])
h.MetadataLength = binary.LittleEndian.Uint64(d[32 : 32+8])
h.LeafDirectoryOffset = binary.LittleEndian.Uint64(d[40 : 40+8])
h.LeafDirectoryLength = binary.LittleEndian.Uint64(d[48 : 48+8])
h.TileDataOffset = binary.LittleEndian.Uint64(d[56 : 56+8])
h.TileDataLength = binary.LittleEndian.Uint64(d[64 : 64+8])
h.AddressedTilesCount = binary.LittleEndian.Uint64(d[72 : 72+8])
h.TileEntriesCount = binary.LittleEndian.Uint64(d[80 : 80+8])
h.TileContentsCount = binary.LittleEndian.Uint64(d[88 : 88+8])
h.Clustered = (d[96] == 0x1)
h.InternalCompression = Compression(d[97])
h.TileCompression = Compression(d[98])
h.TileType = TileType(d[99])
h.MinZoom = d[100]
h.MaxZoom = d[101]
h.MinLonE7 = int32(binary.LittleEndian.Uint32(d[102 : 102+4]))
h.MinLatE7 = int32(binary.LittleEndian.Uint32(d[106 : 106+4]))
h.MaxLonE7 = int32(binary.LittleEndian.Uint32(d[110 : 110+4]))
h.MaxLatE7 = int32(binary.LittleEndian.Uint32(d[114 : 114+4]))
h.CenterZoom = d[118]
h.CenterLonE7 = int32(binary.LittleEndian.Uint32(d[119 : 119+4]))
h.CenterLatE7 = int32(binary.LittleEndian.Uint32(d[123 : 123+4]))
return h, nil
}
func buildRootsLeaves(entries []EntryV3, leafSize int) ([]byte, []byte, int) {
rootEntries := make([]EntryV3, 0)
leavesBytes := make([]byte, 0)
numLeaves := 0
for idx := 0; idx < len(entries); idx += leafSize {
numLeaves++
end := idx + leafSize
if idx+leafSize > len(entries) {
end = len(entries)
}
serialized := serializeEntries(entries[idx:end])
rootEntries = append(rootEntries, EntryV3{entries[idx].TileID, uint64(len(leavesBytes)), uint32(len(serialized)), 0})
leavesBytes = append(leavesBytes, serialized...)
}
rootBytes := serializeEntries(rootEntries)
return rootBytes, leavesBytes, numLeaves
}
func optimizeDirectories(entries []EntryV3, targetRootLen int) ([]byte, []byte, int) {
if len(entries) < 16384 {
testRootBytes := serializeEntries(entries)
// Case1: the entire directory fits into the target len
if len(testRootBytes) <= targetRootLen {
return testRootBytes, make([]byte, 0), 0
}
}
// TODO: case 2: mixed tile entries/directory entries in root
// case 3: root directory is leaf pointers only
// use an iterative method, increasing the size of the leaf directory until the root fits
var leafSize float32
leafSize = float32(len(entries)) / 3500
if leafSize < 4096 {
leafSize = 4096
}
for {
rootBytes, leavesBytes, numLeaves := buildRootsLeaves(entries, int(leafSize))
if len(rootBytes) <= targetRootLen {
return rootBytes, leavesBytes, numLeaves
}
leafSize *= 1.2
}
}