Skip to content

Commit

Permalink
Merge pull request #93 from travisgrigsby/master
Browse files Browse the repository at this point in the history
encoding/mvt: stable marshalling
  • Loading branch information
paulmach authored Apr 2, 2022
2 parents 8add77b + 4408fe0 commit a24e53a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
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

0 comments on commit a24e53a

Please sign in to comment.