comparison pkg/octree/loader.go @ 4650:f5fce22184da stree-experiment

Added a deserializer from STRTrees.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 14 Oct 2019 01:28:18 +0200
parents 86c7a023400e
children a2f16987911b
comparison
equal deleted inserted replaced
4649:8f745c353784 4650:f5fce22184da
18 "bytes" 18 "bytes"
19 "encoding/binary" 19 "encoding/binary"
20 "log" 20 "log"
21 21
22 "github.com/golang/snappy" 22 "github.com/golang/snappy"
23 "github.com/pierrec/lz4"
23 ) 24 )
24 25
25 func loadReader(r *bufio.Reader) (*Tree, error) { 26 func (s *STRTree) deserializeIndex(r *bufio.Reader) error {
26 tree := new(Tree) 27 var numIndex int32
27 28 if err := binary.Read(r, binary.LittleEndian, &numIndex); err != nil {
28 if err := binary.Read(r, binary.LittleEndian, &tree.EPSG); err != nil { 29 return err
29 return nil, err 30 }
30 } 31 index := make([]int32, numIndex)
31 32
32 log.Printf("info: EPSG: %d\n", tree.EPSG) 33 var last int32
33 34 for i := range index {
34 if err := tree.Min.Read(r); err != nil { 35 v, err := binary.ReadVarint(r)
35 return nil, err 36 if err != nil {
36 } 37 return err
37 38 }
38 if err := tree.Max.Read(r); err != nil { 39 value := int32(v) + last
39 return nil, err 40 index[i] = value
41 last = value
42 }
43
44 return nil
45 }
46
47 func (s *STRTree) deserializeBBoxes(r *bufio.Reader) error {
48
49 var numBBoxes int32
50 if err := binary.Read(r, binary.LittleEndian, &numBBoxes); err != nil {
51 return err
52 }
53
54 bboxes := make([]Box2D, numBBoxes)
55 s.bboxes = bboxes
56
57 var err error
58
59 read := func(v *float64) {
60 if err == nil {
61 err = binary.Read(r, binary.LittleEndian, v)
62 }
63 }
64
65 for i := range bboxes {
66 read(&bboxes[i].X1)
67 read(&bboxes[i].Y1)
68 read(&bboxes[i].X2)
69 read(&bboxes[i].Y2)
70 }
71
72 return err
73 }
74
75 func (s *STRTree) deserialize(r *bufio.Reader) error {
76 s.tin = new(Tin)
77
78 if err := s.tin.Deserialize(r); err != nil {
79 return err
80 }
81 var numEntries uint8
82 if err := binary.Read(r, binary.LittleEndian, &numEntries); err != nil {
83 return err
84 }
85 s.Entries = int(numEntries)
86
87 if err := s.deserializeIndex(r); err != nil {
88 return err
89 }
90
91 return s.deserializeBBoxes(r)
92 }
93
94 func (s *STRTree) FromBytes(data []byte) error {
95 return s.deserialize(
96 bufio.NewReader(
97 lz4.NewReader(
98 bytes.NewReader(data))))
99 }
100
101 func (t *Tin) Deserialize(r *bufio.Reader) error {
102
103 if err := binary.Read(r, binary.LittleEndian, &t.EPSG); err != nil {
104 return err
105 }
106
107 log.Printf("info: EPSG: %d\n", t.EPSG)
108
109 if err := t.Min.Read(r); err != nil {
110 return err
111 }
112
113 if err := t.Max.Read(r); err != nil {
114 return err
40 } 115 }
41 116
42 log.Printf("info: BBOX: [[%f, %f, %f], [%f, %f, %f]]\n", 117 log.Printf("info: BBOX: [[%f, %f, %f], [%f, %f, %f]]\n",
43 tree.Min.X, tree.Min.Y, tree.Min.Z, 118 t.Min.X, t.Min.Y, t.Min.Z,
44 tree.Max.X, tree.Max.Y, tree.Max.Z) 119 t.Max.X, t.Max.Y, t.Max.Z)
45 120
46 var numVertices uint32 121 var numVertices uint32
47 if err := binary.Read(r, binary.LittleEndian, &numVertices); err != nil { 122 if err := binary.Read(r, binary.LittleEndian, &numVertices); err != nil {
48 return nil, err 123 return err
49 } 124 }
50 125
51 log.Printf("info: vertices: %d\n", numVertices) 126 log.Printf("info: vertices: %d\n", numVertices)
52 127
53 vertices := make([]Vertex, numVertices) 128 vertices := make([]Vertex, numVertices)
54 tree.vertices = vertices 129 t.Vertices = vertices
55 130
56 for i := range vertices { 131 for i := range vertices {
57 if err := vertices[i].Read(r); err != nil { 132 if err := vertices[i].Read(r); err != nil {
58 return nil, err 133 return err
59 } 134 }
60 } 135 }
61 136
62 var numTriangles uint32 137 var numTriangles uint32
63 if err := binary.Read(r, binary.LittleEndian, &numTriangles); err != nil { 138 if err := binary.Read(r, binary.LittleEndian, &numTriangles); err != nil {
64 return nil, err 139 return err
65 } 140 }
66 141
67 log.Printf("info: triangles: %d\n", numTriangles) 142 log.Printf("info: triangles: %d\n", numTriangles)
68 143
69 indices := make([]int32, 3*numTriangles) 144 indices := make([]int32, 3*numTriangles)
70 triangles := make([][]int32, numTriangles) 145 triangles := make([][]int32, numTriangles)
71 tree.triangles = triangles 146 t.Triangles = triangles
72 147
73 var last int32 148 var last int32
74 149
75 for i := range triangles { 150 for i := range triangles {
76 tri := indices[:3] 151 tri := indices[:3]
77 indices = indices[3:] 152 indices = indices[3:]
78 triangles[i] = tri 153 triangles[i] = tri
79 for j := range tri { 154 for j := range tri {
80 v, err := binary.ReadVarint(r) 155 v, err := binary.ReadVarint(r)
81 if err != nil { 156 if err != nil {
82 return nil, err 157 return err
83 } 158 }
84 value := int32(v) + last 159 value := int32(v) + last
85 tri[j] = value 160 tri[j] = value
86 last = value 161 last = value
87 } 162 }
88 } 163 }
89 164
165 return nil
166 }
167
168 func loadReader(r *bufio.Reader) (*Tree, error) {
169 tree := new(Tree)
170
171 var tin Tin
172
173 if err := tin.Deserialize(r); err != nil {
174 return nil, err
175 }
176
177 tree.EPSG = tin.EPSG
178 tree.vertices = tin.Vertices
179 tree.triangles = tin.Triangles
180 tree.Min = tin.Min
181 tree.Max = tin.Max
182
90 var numNodes uint32 183 var numNodes uint32
91 if err := binary.Read(r, binary.LittleEndian, &numNodes); err != nil { 184 if err := binary.Read(r, binary.LittleEndian, &numNodes); err != nil {
92 return nil, err 185 return nil, err
93 } 186 }
94 187
95 log.Printf("info: num nodes: %d\n", numNodes) 188 log.Printf("info: num nodes: %d\n", numNodes)
96 189
97 tree.index = make([]int32, numNodes) 190 tree.index = make([]int32, numNodes)
98 entries := tree.index[1:] 191 entries := tree.index[1:]
99 192
100 last = 0 193 var last int32
101 for i := range entries { 194 for i := range entries {
102 v, err := binary.ReadVarint(r) 195 v, err := binary.ReadVarint(r)
103 if err != nil { 196 if err != nil {
104 return nil, err 197 return nil, err
105 } 198 }