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

encoding/mvt: stable marshalling #93

Merged
merged 2 commits into from
Apr 2, 2022
Merged
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
2 changes: 2 additions & 0 deletions encoding/mvt/geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ type keyValueEncoder struct {

Values []*vectortile.Tile_Value
valueMap map[interface{}]uint32

keySortBuffer []string
}

func newKeyValueEncoder() *keyValueEncoder {
Expand Down
12 changes: 10 additions & 2 deletions encoding/mvt/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"compress/gzip"
"fmt"
"sort"
"strconv"

"github.com/paulmach/orb/encoding/mvt/vectortile"
Expand Down Expand Up @@ -84,9 +85,16 @@ func Marshal(layers Layers) ([]byte, error) {

func encodeProperties(kve *keyValueEncoder, properties geojson.Properties) ([]uint32, error) {
tags := make([]uint32, 0, 2*len(properties))
for k, v := range properties {

kve.keySortBuffer = kve.keySortBuffer[:0]
for k := range properties {
kve.keySortBuffer = append(kve.keySortBuffer, k)
}
sort.Strings(kve.keySortBuffer)

for _, k := range kve.keySortBuffer {
ki := kve.Key(k)
vi, err := kve.Value(v)
vi, err := kve.Value(properties[k])
if err != nil {
return nil, fmt.Errorf("property %s: %v", k, err)
}
Expand Down
18 changes: 18 additions & 0 deletions encoding/mvt/marshal_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mvt

import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -469,6 +471,22 @@ func TestMarshal_ID(t *testing.T) {
})
}

func TestStableMarshalling(t *testing.T) {
layers := NewLayers(loadGeoJSON(t, maptile.New(17896, 24449, 16)))
values := make(map[string]bool)

for i := 0; i < 100; i++ {
marshal, _ := Marshal(layers)
checksum := md5.Sum(marshal)
sum := hex.EncodeToString(checksum[:])
values[sum] = true
}

if len(values) != 1 {
t.Errorf("multiple values (%d) for marshalled bytes", len(values))
}
}

func BenchmarkMarshal(b *testing.B) {
layers := NewLayers(loadGeoJSON(b, maptile.New(17896, 24449, 16)))

Expand Down