forked from rubenv/topojson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunpackobjects.go
88 lines (77 loc) · 1.81 KB
/
unpackobjects.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
package topojson
import (
"github.com/paulmach/orb/geojson"
)
type arcEntry struct {
Start int
End int
}
func (t *Topology) unpackObjects() {
for _, o := range t.objects {
obj := t.unpackObject(o)
t.Objects[obj.ID] = obj
}
t.objects = nil
t.deletedArcs = nil
t.shiftArcs = nil
t.arcIndexes = nil
}
func (t *Topology) unpackObject(o *topologyObject) *Geometry {
obj := &Geometry{
ID: o.ID,
Type: o.Type,
Properties: o.Properties,
BoundingBox: o.BoundingBox,
}
switch o.Type {
default:
for _, geom := range o.Geometries {
obj.Geometries = append(obj.Geometries, t.unpackObject(geom))
}
case geojson.TypeLineString:
obj.LineString = t.lookupArc(o.Arc)
case geojson.TypeMultiLineString:
obj.MultiLineString = t.lookupArcs(o.Arcs)
case geojson.TypePolygon:
obj.Polygon = t.lookupArcs(o.Arcs)
case geojson.TypeMultiPolygon:
obj.MultiPolygon = t.lookupMultiArcs(o.MultiArcs)
case geojson.TypePoint:
obj.Point = o.Point
case geojson.TypeMultiPoint:
obj.MultiPoint = o.MultiPoint
}
return obj
}
func (t *Topology) lookupArc(a *arc) []int {
result := make([]int, 0)
for a != nil {
if a.Start < a.End {
index := t.arcIndexes[arcEntry{a.Start, a.End}]
if !t.deletedArcs[index] {
result = append(result, index-t.shiftArcs[index])
}
} else {
index := t.arcIndexes[arcEntry{a.End, a.Start}]
if !t.deletedArcs[index] {
result = append(result, ^(index - t.shiftArcs[index]))
}
}
a = a.Next
}
return result
}
func (t *Topology) lookupArcs(a []*arc) [][]int {
result := make([][]int, 0)
for _, arc := range a {
result = append(result, t.lookupArc(arc))
}
return result
}
func (t *Topology) lookupMultiArcs(a [][]*arc) [][][]int {
result := make([][][]int, 0)
for _, s := range a {
result = append(result, t.lookupArcs(s))
}
return result
}