-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathdecode_test.go
370 lines (328 loc) · 8.5 KB
/
decode_test.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
package osmpbf
import (
"context"
"fmt"
"io"
"math"
"net/http"
"os"
"reflect"
"runtime"
"testing"
"time"
"github.com/paulmach/osm"
)
const (
// Originally downloaded from http://download.geofabrik.de/europe/great-britain/england/greater-london.html
London = "greater-london-140324.osm.pbf"
LondonURL = "https://gist.githubusercontent.com/paulmach/853d57b83d408480d3b148b07954c110/raw/853f33f4dbe4246915134f1cde8edb30241ecc10/greater-london-140324.osm.pbf"
// Created based on the above file, by running `osmium add-locations-to-ways`.
LondonLocations = "greater-london-140324-low.osm.pbf"
LondonLocationsURL = "https://gist.github.com/paulmach/853d57b83d408480d3b148b07954c110/raw/d3dd351fcb202e3db1c77b44313c1ba0d71b43b3/greater-london-140324-low.osm.pbf"
coordinatesPrecision = 1e7
)
func parseTime(s string) time.Time {
t, err := time.Parse(time.RFC3339, s)
if err != nil {
panic(err)
}
return t
}
func stripCoordinates(w *osm.Way) *osm.Way {
if w == nil {
return nil
}
ws := new(osm.Way)
*ws = *w
ws.Nodes = make(osm.WayNodes, len(w.Nodes))
for i, n := range w.Nodes {
n.Lat, n.Lon = 0, 0
ws.Nodes[i] = n
}
return ws
}
func roundCoordinates(w *osm.Way) {
if w == nil {
return
}
for i := range w.Nodes {
w.Nodes[i].Lat = math.Round(w.Nodes[i].Lat*coordinatesPrecision) / coordinatesPrecision
w.Nodes[i].Lon = math.Round(w.Nodes[i].Lon*coordinatesPrecision) / coordinatesPrecision
}
}
type OSMFileTest struct {
*testing.T
FileName string
FileURL string
ExpNode *osm.Node
ExpWay *osm.Way
ExpRel *osm.Relation
ExpNodeCount, ExpWayCount, ExpRelCount uint64
IDsExpOrder []string
}
var (
idsExpectedOrderNodes = []string{
"node/44", "node/47", "node/52", "node/58", "node/60",
"node/79", // Just because way/79 is already there
"node/2740703694", "node/2740703695", "node/2740703697",
"node/2740703699", "node/2740703701",
}
idsExpectedOrderWays = []string{
"way/73", "way/74", "way/75", "way/79", "way/482",
"way/268745428", "way/268745431", "way/268745434", "way/268745436",
"way/268745439",
}
idsExpectedOrderRelations = []string{
"relation/69", "relation/94", "relation/152", "relation/245",
"relation/332", "relation/3593436", "relation/3595575",
"relation/3595798", "relation/3599126", "relation/3599127",
}
IDsExpectedOrderNoNodes = append(idsExpectedOrderWays, idsExpectedOrderRelations...)
IDsExpectedOrder = append(idsExpectedOrderNodes, IDsExpectedOrderNoNodes...)
IDs map[string]bool
enc uint64 = 2729006
encl uint64 = 244523
ewc uint64 = 459055
erc uint64 = 12833
en = &osm.Node{
ID: 18088578,
Lat: 51.5442632,
Lon: -0.2010027,
Tags: osm.Tags([]osm.Tag{
{Key: "alt_name", Value: "The King's Head"},
{Key: "amenity", Value: "pub"},
{Key: "created_by", Value: "JOSM"},
{Key: "name", Value: "The Luminaire"},
{Key: "note", Value: "Live music venue too"},
}),
Version: 2,
Timestamp: parseTime("2009-05-20T10:28:54Z"),
ChangesetID: 1260468,
UserID: 508,
User: "Welshie",
Visible: true,
}
ewl = &osm.Way{
ID: 4257116,
Nodes: osm.WayNodes{
{ID: 21544864, Lat: 51.5230531, Lon: -0.1408525},
{ID: 333731851, Lat: 51.5224309, Lon: -0.1402297},
{ID: 333731852, Lat: 51.5224107, Lon: -0.1401878},
{ID: 333731850, Lat: 51.522422, Lon: -0.1401375},
{ID: 333731855, Lat: 51.522792, Lon: -0.1392477},
{ID: 333731858, Lat: 51.5228209, Lon: -0.1392124},
{ID: 333731854, Lat: 51.5228579, Lon: -0.1392339},
{ID: 108047, Lat: 51.5234407, Lon: -0.1398771},
{ID: 769984352, Lat: 51.5232469, Lon: -0.1403648},
{ID: 21544864, Lat: 51.5230531, Lon: -0.1408525},
},
Tags: osm.Tags([]osm.Tag{
{Key: "area", Value: "yes"},
{Key: "highway", Value: "pedestrian"},
{Key: "name", Value: "Fitzroy Square"},
}),
Version: 7,
Timestamp: parseTime("2013-08-07T12:08:39Z"),
ChangesetID: 17253164,
UserID: 1016290,
User: "Amaroussi",
Visible: true,
}
ew = stripCoordinates(ewl)
er = &osm.Relation{
ID: 7677,
Members: osm.Members{
{Ref: 4875932, Type: osm.TypeWay, Role: "outer"},
{Ref: 4894305, Type: osm.TypeWay, Role: "inner"},
},
Tags: osm.Tags([]osm.Tag{
{Key: "created_by", Value: "Potlatch 0.9c"},
{Key: "type", Value: "multipolygon"},
}),
Version: 4,
Timestamp: parseTime("2008-07-19T15:04:03Z"),
ChangesetID: 540201,
UserID: 3876,
User: "Edgemaster",
Visible: true,
}
)
func init() {
IDs = make(map[string]bool)
for _, id := range IDsExpectedOrder {
IDs[id] = false
}
}
func (ft *OSMFileTest) downloadTestOSMFile() {
if _, err := os.Stat(ft.FileName); os.IsNotExist(err) {
out, err := os.Create(ft.FileName)
if err != nil {
ft.Fatal(err)
}
defer out.Close()
resp, err := http.Get(ft.FileURL)
if err != nil {
ft.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
ft.Fatalf("test status code invalid: %v", resp.StatusCode)
}
if _, err := io.Copy(out, resp.Body); err != nil {
ft.Fatal(err)
}
} else if err != nil {
ft.Fatal(err)
}
}
func (ft *OSMFileTest) testDecode() {
ft.downloadTestOSMFile()
f, err := os.Open(ft.FileName)
if err != nil {
ft.Fatal(err)
}
defer f.Close()
d := newDecoder(context.Background(), &Scanner{}, f)
err = d.Start(runtime.GOMAXPROCS(-1))
if err != nil {
ft.Fatal(err)
}
var n *osm.Node
var w *osm.Way
var r *osm.Relation
var nc, wc, rc uint64
idsOrder := make([]string, 0, len(IDsExpectedOrder))
for {
e, err := d.Next()
if err == io.EOF {
break
} else if err != nil {
ft.Fatal(err)
}
switch v := e.(type) {
case *osm.Node:
nc++
if v.ID == ft.ExpNode.ID {
n = v
}
id := fmt.Sprintf("node/%d", v.ID)
if _, ok := IDs[id]; ok {
idsOrder = append(idsOrder, id)
}
case *osm.Way:
wc++
if v.ID == ft.ExpWay.ID {
w = v
}
id := fmt.Sprintf("way/%d", v.ID)
if _, ok := IDs[id]; ok {
idsOrder = append(idsOrder, id)
}
case *osm.Relation:
rc++
if v.ID == ft.ExpRel.ID {
r = v
}
id := fmt.Sprintf("relation/%d", v.ID)
if _, ok := IDs[id]; ok {
idsOrder = append(idsOrder, id)
}
}
}
d.Close()
if !reflect.DeepEqual(ft.ExpNode, n) {
ft.Errorf("\nExpected: %#v\nActual: %#v", ft.ExpNode, n)
}
roundCoordinates(w)
if !reflect.DeepEqual(ft.ExpWay, w) {
ft.Errorf("\nExpected: %#v\nActual: %#v", ft.ExpWay, w)
}
if !reflect.DeepEqual(ft.ExpRel, r) {
ft.Errorf("\nExpected: %#v\nActual: %#v", ft.ExpRel, r)
}
if ft.ExpNodeCount != nc || ft.ExpWayCount != wc || ft.ExpRelCount != rc {
ft.Errorf("\nExpected %7d nodes, %7d ways, %7d relations\nGot %7d nodes, %7d ways, %7d relations.",
ft.ExpNodeCount, ft.ExpWayCount, ft.ExpRelCount, nc, wc, rc)
}
if !reflect.DeepEqual(ft.IDsExpOrder, idsOrder) {
ft.Errorf("\nExpected: %v\nGot: %v", ft.IDsExpOrder, idsOrder)
}
}
func TestDecode(t *testing.T) {
ft := &OSMFileTest{
T: t,
FileName: London,
FileURL: LondonURL,
ExpNode: en,
ExpWay: ew,
ExpRel: er,
ExpNodeCount: enc,
ExpWayCount: ewc,
ExpRelCount: erc,
IDsExpOrder: IDsExpectedOrder,
}
ft.testDecode()
}
func TestDecodeLocations(t *testing.T) {
ft := &OSMFileTest{
T: t,
FileName: LondonLocations,
FileURL: LondonLocationsURL,
ExpNode: en,
ExpWay: ewl,
ExpRel: er,
ExpNodeCount: encl,
ExpWayCount: ewc,
ExpRelCount: erc,
IDsExpOrder: IDsExpectedOrderNoNodes,
}
ft.testDecode()
}
func TestDecode_Close(t *testing.T) {
f, err := os.Open(Delaware)
if err != nil {
t.Fatal(err)
}
defer f.Close()
// should close at start
f.Seek(0, 0)
d := newDecoder(context.Background(), &Scanner{}, f)
d.Start(5)
err = d.Close()
if err != nil {
t.Errorf("close error: %v", err)
}
// should close after partial read
f.Seek(0, 0)
d = newDecoder(context.Background(), &Scanner{}, f)
d.Start(5)
d.Next()
d.Next()
err = d.Close()
if err != nil {
t.Errorf("close error: %v", err)
}
// should close after full read
f.Seek(0, 0)
d = newDecoder(context.Background(), &Scanner{}, f)
d.Start(5)
elements := 0
for {
_, err := d.Next()
if err == io.EOF {
break
}
if err != nil {
t.Errorf("next error: %v", err)
}
elements++
}
if elements < 2 {
t.Errorf("did not read enough elements: %v", elements)
}
// should close at end of read
err = d.Close()
if err != nil {
t.Errorf("close error: %v", err)
}
}