forked from qedus/osmpbf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gosmonaut_test.go
278 lines (244 loc) · 5.82 KB
/
gosmonaut_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
package gosmonaut
import (
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"reflect"
"testing"
"time"
)
const (
// Test files are stored at https://gist.github.com/AlekSi/d4369aa13cf1fc5ddfac3e91b67b2f7b
// 8604f36a7357adfbd6b5292c2ea4972d9d0bfd3d is the latest commit.
GistURL = "https://gist.githubusercontent.com/AlekSi/d4369aa13cf1fc5ddfac3e91b67b2f7b/raw/8604f36a7357adfbd6b5292c2ea4972d9d0bfd3d/"
// Originally downloaded from http://download.geofabrik.de/europe/great-britain/england/greater-london.html
London = "greater-london-140324.osm.pbf"
// Same file as above, but without 'DenseNodes'. This has been generated using the below command (using osmium-tool http://osmcode.org/osmium-tool/)
// "osmium cat -o greater-london-140324-nondense.osm.pbf greater-london-140324.osm.pbf -f osm.pbf,pbf_dense_nodes=false"
LondonNonDense = "greater-london-140324-nondense.osm.pbf"
)
func TestGoDecoder(t *testing.T) {
testGosmonautWithFile(t, London, GoDecoder)
}
func TestGoDecoderNonDense(t *testing.T) {
testGosmonautWithFile(t, LondonNonDense, GoDecoder)
}
func TestFastDecoder(t *testing.T) {
testGosmonautWithFile(t, London, FastDecoder)
}
func TestFastDecoderNonDense(t *testing.T) {
testGosmonautWithFile(t, LondonNonDense, FastDecoder)
}
// Test results have been verified by Osmonaut v1.1
func testGosmonautWithFile(t *testing.T, filename string, decoder DecoderType) {
if testing.Short() {
t.Skip("Skipping decoding tests in short mode")
}
downloadTestOSMFile(filename, t)
// Test addresses
testGosmonaut(t, filename,
NewOSMTypeSet(true, true, true),
func(t OSMType, tags OSMTags) bool {
return tags.Has("addr:housenumber")
},
decoder,
428593, 63528, 97,
loadTestdata(t, "addr_node.json"),
loadTestdata(t, "addr_way.json"),
loadTestdata(t, "addr_relation.json"),
)
// Test restrictions
testGosmonaut(t, filename,
NewOSMTypeSet(false, false, true),
func(t OSMType, tags OSMTags) bool {
return tags.HasValue("type", "restriction")
},
decoder,
18143, 3181, 1517,
"",
"",
loadTestdata(t, "restriction.json"),
)
testGosmonaut(t, filename,
NewOSMTypeSet(false, false, false),
func(t OSMType, tags OSMTags) bool {
return true
},
decoder,
0, 0, 0,
"", "", "",
)
testGosmonaut(t, filename,
NewOSMTypeSet(true, true, true),
func(t OSMType, tags OSMTags) bool {
return false
},
decoder,
0, 0, 0,
"", "", "",
)
}
func testGosmonaut(
t *testing.T,
filename string,
types OSMTypeSet,
f func(OSMType, OSMTags) bool,
decoder DecoderType,
nc, wc, rc int, // Number of total entities per type
ns, ws, rs string, // JSON string of the first entity per type
) {
// Open file
file, err := os.Open(filepath.Join("testdata", filename))
if err != nil {
t.Fatal(err)
}
defer file.Close()
g, _ := NewGosmonaut(file, Config{
Decoder: decoder,
})
// Run test twice in order to test repeated runs
for i := 0; i < 2; i++ {
g.Start(types, f)
var nh, wh, rh bool
var rnc, rwc, rrc int
highestType := NodeType
for {
if e, err := g.Next(); err == io.EOF {
break
} else if err != nil {
t.Fatal(err)
} else {
// Check type order (nodes, then ways, then relations)
if e.GetType() < highestType {
t.Fatal("Type order is wrong")
}
highestType = e.GetType()
switch e := e.(type) {
case Node:
// Check first entity
if !nh {
if e.String() != ns {
t.Fatal("Node test failed")
}
nh = true
}
// Count entities
rnc++
case Way:
// Check first entity
if !wh {
if e.String() != ws {
t.Fatal("Way test failed")
}
wh = true
}
// Count entities
rwc++
rnc += len(e.Nodes)
case Relation:
// Check first entity
if !rh {
if e.String() != rs {
t.Fatal("Relation test failed")
}
rh = true
}
// Count entities
rrc++
for _, m := range e.Members {
switch mi := m.Entity.(type) {
case Node:
rnc++
case Way:
rwc++
rnc += len(mi.Nodes)
default:
t.Fatalf("Invalid member entity type: %T", mi)
}
}
default:
t.Fatalf("Invalid entity type: %T", i)
}
}
}
// Check type counts
if rnc != nc {
t.Fatalf("Wrong number of nodes")
}
if rwc != wc {
t.Fatalf("Wrong number of ways")
}
if rrc != rc {
t.Fatalf("Wrong number of relations")
}
}
}
func TestHeader(t *testing.T) {
if testing.Short() {
t.Skip("Skipping decoding tests in short mode")
}
downloadTestOSMFile(London, t)
header := Header{
BoundingBox: &BoundingBox{
-0.511482,
0.33543700000000004,
51.69344,
51.285540000000005,
},
RequiredFeatures: []string{
"OsmSchema-V0.6",
"DenseNodes",
},
WritingProgram: "Osmium (http://wiki.openstreetmap.org/wiki/Osmium)",
OsmosisReplicationTimestamp: time.Unix(1395698102, 0),
}
// Open file
file, err := os.Open(filepath.Join("testdata", London))
if err != nil {
t.Fatal(err)
}
defer file.Close()
// Compare headers
g, err := NewGosmonaut(file)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(header, g.Header()) {
t.Fatal("Header test failed")
}
}
func downloadTestOSMFile(fileName string, t *testing.T) {
path := filepath.Join("testdata", fileName)
_, err := os.Stat(path)
if err == nil {
return
}
if !os.IsNotExist(err) {
t.Fatal(err)
}
resp, err := http.Get(GistURL + fileName)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
t.Fatalf("expected 200, got %d", resp.StatusCode)
}
out, err := os.Create(path)
if err != nil {
t.Fatal(err)
}
defer out.Close()
if _, err = io.Copy(out, resp.Body); err != nil {
t.Fatal(err)
}
}
func loadTestdata(t *testing.T, filename string) string {
b, err := ioutil.ReadFile(filepath.Join("testdata", filename))
if err != nil {
t.Fatal(err)
}
return string(b)
}