This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.go
462 lines (382 loc) · 8.81 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package mvt
import (
"encoding/json"
"fmt"
"reflect"
"github.com/planetfederal/orb"
"github.com/planetfederal/orb/encoding/mvt/vectortile"
"github.com/pkg/errors"
)
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
}
// A geomDecoder holds state for geometry decoding.
type geomDecoder struct {
geom []uint32
i int
prev orb.Point
}
func decodeGeometry(geomType vectortile.Tile_GeomType, geom []uint32) (orb.Geometry, error) {
if len(geom) < 2 {
return nil, errors.Errorf("geom is not long enough: %v", geom)
}
gd := &geomDecoder{geom: geom}
switch geomType {
case vectortile.Tile_POINT:
return gd.decodePoint()
case vectortile.Tile_LINESTRING:
return gd.decodeLineString()
case vectortile.Tile_POLYGON:
return gd.decodePolygon()
}
return nil, errors.Errorf("unknown geometry type: %v", geomType)
}
func (gd *geomDecoder) decodePoint() (orb.Geometry, error) {
_, count, err := gd.cmdAndCount()
if err != nil {
return nil, err
}
if count == 1 {
return gd.NextPoint(), nil
}
mp := make(orb.MultiPoint, 0, count)
for i := uint32(0); i < count; i++ {
mp = append(mp, gd.NextPoint())
}
return mp, nil
}
func (gd *geomDecoder) decodeLine() (orb.LineString, error) {
cmd, count, err := gd.cmdAndCount()
if err != nil {
return nil, err
}
if cmd != moveTo || count != 1 {
return nil, errors.New("first command not one moveTo")
}
first := gd.NextPoint()
cmd, count, err = gd.cmdAndCount()
if err != nil {
return nil, err
}
if cmd != lineTo {
return nil, errors.New("second command not a lineTo")
}
ls := make(orb.LineString, 0, count+1)
ls = append(ls, first)
for i := uint32(0); i < count; i++ {
ls = append(ls, gd.NextPoint())
}
return ls, nil
}
func (gd *geomDecoder) decodeLineString() (orb.Geometry, error) {
var mls orb.MultiLineString
for !gd.done() {
ls, err := gd.decodeLine()
if err != nil {
return nil, err
}
if gd.done() && len(mls) == 0 {
return ls, nil
}
mls = append(mls, ls)
}
return mls, nil
}
func (gd *geomDecoder) decodePolygon() (orb.Geometry, error) {
var mp orb.MultiPolygon
var p orb.Polygon
for !gd.done() {
ls, err := gd.decodeLine()
if err != nil {
return nil, err
}
r := orb.Ring(ls)
cmd, _, err := gd.cmdAndCount()
if err != nil {
return nil, err
}
if cmd == closePath && !r.Closed() {
r = append(r, r[0])
}
// figure out if new polygon
if len(mp) == 0 && len(p) == 0 {
p = append(p, r)
} else {
if r.Orientation() == orb.CCW {
mp = append(mp, p)
p = orb.Polygon{r}
} else {
p = append(p, r)
}
}
}
if len(mp) == 0 {
return p, nil
}
return append(mp, p), nil
}
func (gd *geomDecoder) cmdAndCount() (uint32, uint32, error) {
if gd.i >= len(gd.geom) {
return 0, 0, errors.New("no more data")
}
v := gd.geom[gd.i]
cmd := v & 0x07
count := v >> 3
gd.i++
if cmd != closePath {
if v := gd.i + int(2*count); len(gd.geom) < v {
return 0, 0, errors.Errorf("data cut short: needed %d, have %d", v, len(gd.geom))
}
}
return cmd, count, nil
}
func (gd *geomDecoder) NextPoint() orb.Point {
gd.i += 2
gd.prev[0] += unzigzag(gd.geom[gd.i-2])
gd.prev[1] += unzigzag(gd.geom[gd.i-1])
return gd.prev
}
func (gd *geomDecoder) done() bool {
return gd.i >= len(gd.geom)
}
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 !reflect.TypeOf(v).Comparable() {
data, err := json.Marshal(v)
if err != nil {
return 0, errors.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, errors.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)))
}