forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry.go
300 lines (254 loc) · 5.82 KB
/
geometry.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package mvt
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/paulmach/orb"
"github.com/paulmach/orb/encoding/mvt/vectortile"
)
const (
moveTo = 1
lineTo = 2
closePath = 7
)
func encodeGeometry(g orb.Geometry) (vectortile.Tile_GeomType, []uint32, error) {
switch g := g.(type) {
case orb.Point:
e := newGeomEncoder(3)
e.MoveTo([]orb.Point{g})
return vectortile.Tile_POINT, e.Data, nil
case orb.MultiPoint:
e := newGeomEncoder(1 + 2*len(g))
e.MoveTo([]orb.Point(g))
return vectortile.Tile_POINT, e.Data, nil
case orb.LineString:
e := newGeomEncoder(2 + 2*len(g))
e.MoveTo([]orb.Point{g[0]})
e.LineTo([]orb.Point(g[1:]))
return vectortile.Tile_LINESTRING, e.Data, nil
case orb.MultiLineString:
e := newGeomEncoder(elMLS(g))
for _, ls := range g {
e.MoveTo([]orb.Point{ls[0]})
e.LineTo([]orb.Point(ls[1:]))
}
return vectortile.Tile_LINESTRING, e.Data, nil
case orb.Ring:
e := newGeomEncoder(3 + 2*len(g))
e.MoveTo([]orb.Point{g[0]})
if g.Closed() {
e.LineTo([]orb.Point(g[1 : len(g)-1]))
} else {
e.LineTo([]orb.Point(g[1:]))
}
e.ClosePath()
return vectortile.Tile_POLYGON, e.Data, nil
case orb.Polygon:
e := newGeomEncoder(elP(g))
for _, r := range g {
e.MoveTo([]orb.Point{r[0]})
if r.Closed() {
e.LineTo([]orb.Point(r[1 : len(r)-1]))
} else {
e.LineTo([]orb.Point(r[1:]))
}
e.ClosePath()
}
return vectortile.Tile_POLYGON, e.Data, nil
case orb.MultiPolygon:
e := newGeomEncoder(elMP(g))
for _, p := range g {
for _, r := range p {
e.MoveTo([]orb.Point{r[0]})
if r.Closed() {
e.LineTo([]orb.Point(r[1 : len(r)-1]))
} else {
e.LineTo([]orb.Point(r[1:]))
}
e.ClosePath()
}
}
return vectortile.Tile_POLYGON, e.Data, nil
case orb.Collection:
return 0, nil, errors.New("geometry collections are not supported")
case orb.Bound:
return encodeGeometry(g.ToPolygon())
}
panic(fmt.Sprintf("geometry type not supported: %T", g))
}
type geomEncoder struct {
prevX, prevY int32
Data []uint32
}
func newGeomEncoder(l int) *geomEncoder {
return &geomEncoder{
Data: make([]uint32, 0, l),
}
}
func (ge *geomEncoder) MoveTo(points []orb.Point) {
l := uint32(len(points))
ge.Data = append(ge.Data, (l<<3)|moveTo)
ge.addPoints(points)
}
func (ge *geomEncoder) LineTo(points []orb.Point) {
l := uint32(len(points))
ge.Data = append(ge.Data, (l<<3)|lineTo)
ge.addPoints(points)
}
func (ge *geomEncoder) addPoints(points []orb.Point) {
for i := range points {
x := int32(points[i][0]) - ge.prevX
y := int32(points[i][1]) - ge.prevY
ge.prevX = int32(points[i][0])
ge.prevY = int32(points[i][1])
ge.Data = append(ge.Data,
uint32((x<<1)^(x>>31)),
uint32((y<<1)^(y>>31)),
)
}
}
func (ge *geomEncoder) ClosePath() {
ge.Data = append(ge.Data, (1<<3)|closePath)
}
type keyValueEncoder struct {
Keys []string
keyMap map[string]uint32
Values []*vectortile.Tile_Value
valueMap map[interface{}]uint32
keySortBuffer []string
}
func newKeyValueEncoder() *keyValueEncoder {
return &keyValueEncoder{
keyMap: make(map[string]uint32),
valueMap: make(map[interface{}]uint32),
}
}
func (kve *keyValueEncoder) Key(s string) uint32 {
if i, ok := kve.keyMap[s]; ok {
return i
}
i := uint32(len(kve.Keys))
kve.Keys = append(kve.Keys, s)
kve.keyMap[s] = i
return i
}
func (kve *keyValueEncoder) Value(v interface{}) (uint32, error) {
// If a type is not comparable we can't figure out uniqueness in the hash,
// we also can't encode it into a vectortile.Tile_Value.
// So we encoded it as a json string, which is what other encoders
// also do.
if v == nil || !reflect.TypeOf(v).Comparable() {
data, err := json.Marshal(v)
if err != nil {
return 0, fmt.Errorf("uncomparable: %T", v)
}
v = string(data)
}
if i, ok := kve.valueMap[v]; ok {
return i, nil
}
tv, err := encodeValue(v)
if err != nil {
return 0, err
}
i := uint32(len(kve.Values))
kve.Values = append(kve.Values, tv)
kve.valueMap[v] = i
return i, nil
}
func encodeValue(v interface{}) (*vectortile.Tile_Value, error) {
tv := &vectortile.Tile_Value{}
switch t := v.(type) {
case string:
tv.StringValue = &t
case fmt.Stringer:
s := t.String()
tv.StringValue = &s
case int:
i := int64(t)
tv.SintValue = &i
case int8:
i := int64(t)
tv.SintValue = &i
case int16:
i := int64(t)
tv.SintValue = &i
case int32:
i := int64(t)
tv.SintValue = &i
case int64:
i := int64(t)
tv.SintValue = &i
case uint:
i := uint64(t)
tv.UintValue = &i
case uint8:
i := uint64(t)
tv.UintValue = &i
case uint16:
i := uint64(t)
tv.UintValue = &i
case uint32:
i := uint64(t)
tv.UintValue = &i
case uint64:
i := uint64(t)
tv.UintValue = &i
case float32:
tv.FloatValue = &t
case float64:
tv.DoubleValue = &t
case bool:
tv.BoolValue = &t
default:
return nil, fmt.Errorf("unable to encode value of type %T: %v", v, v)
}
return tv, nil
}
func decodeValue(v *vectortile.Tile_Value) interface{} {
if v == nil {
return nil
}
if v.StringValue != nil {
return *v.StringValue
} else if v.FloatValue != nil {
return float64(*v.FloatValue)
} else if v.DoubleValue != nil {
return *v.DoubleValue
} else if v.IntValue != nil {
return float64(*v.IntValue)
} else if v.UintValue != nil {
return float64(*v.UintValue)
} else if v.SintValue != nil {
return float64(*v.SintValue)
} else if v.BoolValue != nil {
return *v.BoolValue
}
return nil
}
// functions to estimate encoded length
func elMLS(mls orb.MultiLineString) int {
c := 0
for _, ls := range mls {
c += 2 + 2*len(ls)
}
return c
}
func elP(p orb.Polygon) int {
c := 0
for _, r := range p {
c += 3 + 2*len(r)
}
return c
}
func elMP(mp orb.MultiPolygon) int {
c := 0
for _, p := range mp {
c += elP(p)
}
return c
}
func unzigzag(v uint32) float64 {
return float64(int32(((v >> 1) & ((1 << 32) - 1)) ^ -(v & 1)))
}