Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try using MVT for encoding #25

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified data.json.gz
Binary file not shown.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require (
)

require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/paulmach/protoscan v0.2.1 // indirect
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand All @@ -16,6 +17,7 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/paulmach/orb v0.8.0 h1:W5XAt5yNPNnhaMNEf0xNSkBMJ1LzOzdk2MRlB6EN0Vs=
github.com/paulmach/orb v0.8.0/go.mod h1:FWRlTgl88VI1RBx/MkrwWDRhQ96ctqMCh8boXhmqB/A=
github.com/paulmach/protoscan v0.2.1 h1:rM0FpcTjUMvPUNk2BhPJrreDKetq43ChnL+x1sRg8O8=
github.com/paulmach/protoscan v0.2.1/go.mod h1:SpcSwydNLrxUGSDvXvO0P7g7AuhJ7lcKfDlhJCDw2gY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down Expand Up @@ -54,5 +56,6 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
11 changes: 10 additions & 1 deletion localtimezone.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

json "github.com/json-iterator/go"
"github.com/paulmach/orb"
"github.com/paulmach/orb/encoding/mvt"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/planar"
)
Expand Down Expand Up @@ -275,13 +276,21 @@ func (z *localTimeZone) LoadGeoJSON(r io.Reader) error {
if err != nil {
return err
}
orbData, err := geojson.UnmarshalFeatureCollection(buf.Bytes())
layers, err := mvt.Unmarshal(buf.Bytes())
if err != nil {
z.tzData = make(map[string]tzData)
z.tzids = []string{}
z.mu.Unlock()
return err
}
fcs := layers.ToFeatureCollections()
orbData, ok := fcs["data"]
if !ok {
z.tzData = make(map[string]tzData)
z.tzids = []string{}
z.mu.Unlock()
return errors.New("asdf")
}
z.tzData = make(map[string]tzData, TZCount) // Possibly the incorrect length in case of Mock or custom data
z.tzids = []string{} // Cannot set a length or else array will be full of empty strings
go func(features []*geojson.Feature) {
Expand Down
71 changes: 0 additions & 71 deletions localtimezone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,41 +229,6 @@ func TestGetOneZone(t *testing.T) {
}
}

func TestMockLocalTimeZone(t *testing.T) {
z := NewMockLocalTimeZone()
for _, tc := range tt {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
tzids, err := z.GetZone(tc.point)
if tc.err != nil {
if err != tc.err {
t.Errorf("expected err %v; got %v", tc.err, err)
}
return
}
if len(tzids) != 1 {
t.Errorf("expected 1 zone; got %d", len(tzids))
}
if tzids[0] != MockTimeZone {
t.Errorf("expected zone America/Los_Angeles; got %s", tzids[0])
}
})
}
}

func TestMockLocalTimeZonePanic(t *testing.T) {
tempMockTZShapeFile := MockTZShapeFile
MockTZShapeFile = []byte("asdf")
defer func() {
MockTZShapeFile = tempMockTZShapeFile
if r := recover(); r == nil {
t.Errorf("expected a panic; got no panic")
}
}()
NewMockLocalTimeZone()
}

func BenchmarkZones(b *testing.B) {
zInterface, err := NewLocalTimeZone()
if err != nil {
Expand Down Expand Up @@ -329,18 +294,6 @@ func BenchmarkClientInit(b *testing.B) {
n++
}
})
b.Run("mock client", func(b *testing.B) {
for n := 0; n < b.N; {
c := NewMockLocalTimeZone()
cStruct, ok := c.(*localTimeZone)
if !ok {
b.Errorf("cannot initialize timezone client")
}
cStruct.mu.RLock()
cStruct.mu.RUnlock() //lint:ignore SA2001 Wait for the client to load
n++
}
})
}
func TestNautical(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -428,27 +381,3 @@ func TestLoadGeoJSONMalformed(t *testing.T) {
t.Errorf("tzData not reset")
}
}

func TestLoadOverwrite(t *testing.T) {
client, err := NewLocalTimeZone()
if err != nil {
t.Errorf("cannot initialize client, got %v", err)
}
c, ok := client.(*localTimeZone)
if !ok {
t.Errorf("cannot initialize client")
}
c.mu.RLock()
lenTzData := len(c.tzData)
c.mu.RUnlock()

err = c.load(MockTZShapeFile)
c.mu.RLock()
defer c.mu.RUnlock()
if err != nil {
t.Errorf("cannot switch client to mock data, got %v", err)
}
if len(c.tzData) >= lenTzData {
t.Errorf("boundCache not overwritten by loading new data")
}
}
8 changes: 7 additions & 1 deletion tzshapefilegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"

json "github.com/json-iterator/go"
"github.com/paulmach/orb/encoding/mvt"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/simplify"
)
Expand Down Expand Up @@ -136,7 +137,12 @@ func orbExec(combinedJSON []byte) ([]byte, int, error) {
}
fc.Features = features
tzCount := len(fc.Features)
reducedJSON, err := fc.MarshalJSON()
data := map[string]*geojson.FeatureCollection{
"data": fc,
}
layers := mvt.NewLayers(data)
// layers.Simplify(simplify.VisvalingamThreshold(0.0001))
reducedJSON, err := mvt.Marshal(layers)
if err != nil {
log.Printf("Error: could not marshal reduced.json: %v\n", err)
return nil, 0, err
Expand Down