diff --git a/data.json.gz b/data.json.gz index 74c9068..4a293d2 100644 Binary files a/data.json.gz and b/data.json.gz differ diff --git a/go.mod b/go.mod index 90f45b3..c13a3df 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 1a6ae5e..2f909af 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= diff --git a/localtimezone.go b/localtimezone.go index 0e2dbdf..8da2b41 100644 --- a/localtimezone.go +++ b/localtimezone.go @@ -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" ) @@ -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) { diff --git a/localtimezone_test.go b/localtimezone_test.go index 0542f62..03fd36d 100644 --- a/localtimezone_test.go +++ b/localtimezone_test.go @@ -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 { @@ -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() @@ -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") - } -} diff --git a/tzshapefilegen/main.go b/tzshapefilegen/main.go index 77e62c6..0394ada 100644 --- a/tzshapefilegen/main.go +++ b/tzshapefilegen/main.go @@ -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" ) @@ -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