forked from sams96/rgeo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
embed.go
71 lines (54 loc) · 1.42 KB
/
embed.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
//go:build !datagen
package rgeo
// data is sourced from: https://github.com/nvkelso/natural-earth-vector/tree/master/geojson
// and bundled via regen.sh in the repository's root
// embedding files individually here to allow the linker to strip out unused ones
import (
"bytes"
_ "embed"
"errors"
"fmt"
"github.com/klauspost/compress/zstd"
)
//go:embed data/Cities10.zst
var cities10 []byte
func Cities10() []Feature {
return must(embeddedFeatureCollection(cities10))
}
//go:embed data/Countries10.zst
var countries10 []byte
func Countries10() []Feature {
return must(embeddedFeatureCollection(countries10))
}
//go:embed data/Countries110.zst
var countries110 []byte
func Countries110() []Feature {
return must(embeddedFeatureCollection(countries110))
}
//go:embed data/Provinces10.zst
var provinces10 []byte
func Provinces10() []Feature {
return must(embeddedFeatureCollection(provinces10))
}
func must(features []Feature, err error) []Feature {
if err != nil {
panic("rgeo embed.go: " + err.Error())
}
return features
}
func embeddedFeatureCollection(data []byte) ([]Feature, error) {
br := bytes.NewReader(data)
if br.Len() == 0 {
return nil, errors.New("empty dataset")
}
zr, err := zstd.NewReader(br)
if err != nil {
return nil, fmt.Errorf("zstd reader setup: %w", err)
}
defer zr.Close()
result, err := LoadEncoded(zr)
if err != nil {
return nil, fmt.Errorf("decode: %w", err)
}
return result, nil
}