forked from sams96/rgeo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rgeo_test.go
697 lines (634 loc) · 15.3 KB
/
rgeo_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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
Copyright 2020 Sam Smith
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
package rgeo
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"math/rand"
"testing"
"github.com/go-test/deep"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/geojson"
)
var testdata = []struct {
name string
in []float64
err error
expected Location
}{
{
name: "Algeria",
in: []float64{1.880273, 31.787305},
err: nil,
expected: Location{
Country: "Algeria",
CountryLong: "People's Democratic Republic of Algeria",
CountryCode2: "DZ",
CountryCode3: "DZA",
Continent: "Africa",
Region: "Africa",
SubRegion: "Northern Africa",
Province: "El Bayadh",
ProvinceCode: "DZ-32",
},
},
{
name: "Madagascar",
in: []float64{47.523836, -18.905691},
err: nil,
expected: Location{
Country: "Madagascar",
CountryLong: "Republic of Madagascar",
CountryCode2: "MG",
CountryCode3: "MDG",
Continent: "Africa",
Region: "Africa",
SubRegion: "Eastern Africa",
Province: "Analamanga",
ProvinceCode: "MG-T",
City: "Antananarivo",
},
},
{
name: "Zimbabwe",
in: []float64{29.832875, -19.948725},
err: nil,
expected: Location{
Country: "Zimbabwe",
CountryLong: "Republic of Zimbabwe",
CountryCode2: "ZW",
CountryCode3: "ZWE",
Continent: "Africa",
Region: "Africa",
SubRegion: "Eastern Africa",
Province: "Midlands",
ProvinceCode: "ZW-MI",
},
},
{
name: "Ocean",
in: []float64{0, 0},
err: ErrLocationNotFound,
expected: Location{},
},
{
name: "North Pole",
in: []float64{-135, 90},
err: ErrLocationNotFound,
expected: Location{},
},
{
name: "South Pole",
in: []float64{44.99, -89.99},
err: nil,
expected: Location{
Country: "Antarctica",
CountryLong: "",
CountryCode2: "AQ",
CountryCode3: "ATA",
Continent: "Antarctica",
Region: "Antarctica",
SubRegion: "Antarctica",
Province: "Antarctica",
ProvinceCode: "AQ-X01~",
},
},
{
name: "Alaska",
in: []float64{-149.901785, 61.199134},
err: nil,
expected: Location{
Country: "United States of America",
CountryLong: "United States of America",
CountryCode2: "US",
CountryCode3: "USA",
Continent: "North America",
Region: "Americas",
SubRegion: "Northern America",
Province: "Alaska",
ProvinceCode: "US-AK",
City: "Anchorage",
},
},
{
name: "UK",
in: []float64{0, 51.5045},
err: nil,
expected: Location{
Country: "United Kingdom",
CountryLong: "United Kingdom of Great Britain and Northern Ireland",
CountryCode2: "GB",
CountryCode3: "GBR",
Continent: "Europe",
Region: "Europe",
SubRegion: "Northern Europe",
Province: "Tower Hamlets",
ProvinceCode: "GB-TWH",
City: "London",
},
},
{
name: "Libya",
in: []float64{24.98, 25.86},
err: nil,
expected: Location{
Country: "Libya",
CountryLong: "Libya",
CountryCode2: "LY",
CountryCode3: "LBY",
Continent: "Africa",
Region: "Africa",
SubRegion: "Northern Africa",
Province: "Al Kufrah",
ProvinceCode: "LY-KF",
},
},
{
name: "Egypt",
in: []float64{25.005187, 25.855963},
err: nil,
expected: Location{
Country: "Egypt",
CountryLong: "Arab Republic of Egypt",
CountryCode2: "EG",
CountryCode3: "EGY",
Continent: "Africa",
Region: "Africa",
SubRegion: "Northern Africa",
Province: "Al Wadi at Jadid",
ProvinceCode: "EG-WAD",
},
},
{
name: "US Border",
in: []float64{-102.560616, 48.992073},
err: nil,
expected: Location{
Country: "United States of America",
CountryLong: "United States of America",
CountryCode2: "US",
CountryCode3: "USA",
Continent: "North America",
Region: "Americas",
SubRegion: "Northern America",
Province: "North Dakota",
ProvinceCode: "US-ND",
},
},
{
name: "Canada Border",
in: []float64{-102.560616, 49.02},
err: nil,
expected: Location{
Country: "Canada",
CountryLong: "Canada",
CountryCode2: "CA",
CountryCode3: "CAN",
Continent: "North America",
Region: "Americas",
SubRegion: "Northern America",
Province: "Saskatchewan",
ProvinceCode: "CA-SK",
},
},
}
func TestReverseGeocode(t *testing.T) {
testgeo := json.RawMessage(`{
"type":"FeatureCollection",
"features":[
{"type":"Feature",
"properties":{"ISO_A3_EH":"TST"},
"geometry":{"type":"Polygon",
"coordinates":[[[0,52],[1,52],[1,53],[0,53],[0,52]]]}}
]
}`)
var fc geojson.FeatureCollection
if err := json.Unmarshal(testgeo, &fc); err != nil {
t.Fatalf("decode GeoJSON: %s", err)
}
features, err := LoadGeoJSON(fc)
if err != nil {
t.Fatalf("features from GeoJSON: %s", err)
}
var testdata = []struct {
name string
in []float64
err error
expected Location
}{
{
name: "in",
in: []float64{0.5, 52.5},
err: nil,
expected: Location{CountryCode3: "TST"},
},
{
name: "out",
in: []float64{0, 0},
err: ErrLocationNotFound,
expected: Location{},
},
}
r, err := New(func() []Feature { return features })
if err != nil {
t.Error(err)
}
for _, test := range testdata {
test := test
t.Run(test.name, func(t *testing.T) {
result, err := r.ReverseGeocode(test.in)
if err != test.err {
t.Errorf("expected error: %s\n got: %s\n", test.err, err)
}
if diff := deep.Equal(test.expected, result); diff != nil {
t.Error(diff)
}
})
}
}
func TestReverseGeocode_Countries(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test (countries) for short mode")
}
t.Parallel()
for _, dataset := range []Dataset{Countries110, Countries10} {
r, err := New(dataset)
if err != nil {
t.Error(err)
}
for _, test := range testdata {
test := test
test.expected.Province = ""
test.expected.ProvinceCode = ""
test.expected.City = ""
t.Run(test.name, func(t *testing.T) {
t.Parallel()
result, err := r.ReverseGeocode(test.in)
if err != test.err {
t.Errorf("expected error: %s\n got: %s\n", test.err, err)
}
if diff := deep.Equal(test.expected, result); diff != nil {
t.Error(diff)
}
})
}
}
}
func TestReverseGeocode_Provinces(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integraion test (provinces) in short mode")
}
t.Parallel()
r, err := New(Provinces10)
if err != nil {
t.Error(err)
}
for _, test := range testdata {
test := test
test.expected.City = ""
t.Run(test.name, func(t *testing.T) {
t.Parallel()
result, err := r.ReverseGeocode(test.in)
if err != test.err {
t.Errorf("expected error: %s\n got: %s\n", test.err, err)
}
if diff := deep.Equal(test.expected, result); diff != nil {
t.Error(diff)
}
})
}
}
func TestReverseGeocode_Cities(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test (cities) in short mode.")
}
t.Parallel()
r, err := New(Provinces10, Cities10)
if err != nil {
t.Error(err)
}
for _, test := range testdata {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
result, err := r.ReverseGeocode(test.in)
if err != test.err {
t.Errorf("expected error: %s\n got: %s\n", test.err, err)
}
if diff := deep.Equal(test.expected, result); diff != nil {
t.Error(diff)
}
})
}
}
func TestLoadGeoJSON_BadData(t *testing.T) {
testdata := []struct {
name string
in string
err string
}{
{
name: "Wrong type",
in: `{"type":"FeatureCollection","features":
[{"type":"Feature","geometry":
{"type":"Point","coordinates":[0,0]}}]}`,
err: "bad polygon in geometry: needs Polygon or MultiPolygon",
},
{
name: "Small polygon",
in: `{"type":"FeatureCollection","features":
[{"type":"Feature","geometry":
{"type":"Polygon",
"coordinates":[[[1,2],[3,4],[1,2]]]}}]}`,
err: "bad polygon in geometry: can't convert ring with less than 4 points",
},
{
name: "No repeated end",
in: `{"type":"FeatureCollection","features":
[{"type":"Feature","geometry":
{"type":"Polygon",
"coordinates":[[[1,2],[3,4],[5,6],[7,8]]]}}]}`,
err: "bad polygon in geometry: " +
"last coordinate not same as first for polygon: [1 2 3 4 5 6 7 8]",
},
{
name: "Bad Multipolygon",
in: `{"type":"FeatureCollection","features":
[{"type":"Feature","geometry":
{"type":"MultiPolygon",
"coordinates":[[[[1,2],[3,4],[5,6],[7,8]]]]}}]}`,
err: "bad polygon in geometry: " +
"last coordinate not same as first for polygon: [1 2 3 4 5 6 7 8]",
},
}
for _, test := range testdata {
test := test
t.Run(test.name, func(t *testing.T) {
var fc geojson.FeatureCollection
if err := json.Unmarshal([]byte(test.in), &fc); err != nil {
t.Errorf("decode GeoJSON: %s", err)
} else if _, err := LoadGeoJSON(fc); err != nil && err.Error() != test.err {
t.Errorf("expected error: %s\n got: %s\n", test.err, err)
}
})
}
}
func TestString(t *testing.T) {
tests := []struct {
name string
in Location
expected string
}{
{
name: "Algeria",
in: Location{
Country: "Algeria",
CountryCode3: "DZA",
Continent: "Africa",
},
expected: "<Location> Algeria (DZA), Africa",
},
{
name: "Zimbabwe",
in: Location{
CountryLong: "Republic of Zimbabwe",
CountryCode2: "ZW",
Region: "Africa",
},
expected: "<Location> Republic of Zimbabwe (ZW), Africa",
},
{
name: "Northern America",
in: Location{
SubRegion: "Northern America",
},
expected: "<Location> Northern America",
},
{
name: "London",
in: Location{
Country: "United Kingdom",
CountryLong: "United Kingdom of Great Britain and Northern Ireland",
CountryCode3: "GBR",
Continent: "Europe",
City: "London",
},
expected: "<Location> London, United Kingdom (GBR), Europe",
},
{
name: "Empty",
in: Location{},
expected: "<Location> Empty Location",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
result := test.in.String()
if diff := deep.Equal(test.expected, result); diff != nil {
t.Error(diff)
}
})
}
}
func ExampleRgeo_ReverseGeocode() {
r, err := New(Countries110)
if err != nil {
// Handle error
}
loc, err := r.ReverseGeocode([]float64{0, 52})
if err != nil {
// Handle error
}
fmt.Printf("%s\n", loc.Country)
fmt.Printf("%s\n", loc.CountryLong)
fmt.Printf("%s\n", loc.CountryCode2)
fmt.Printf("%s\n", loc.CountryCode3)
fmt.Printf("%s\n", loc.Continent)
fmt.Printf("%s\n", loc.Region)
fmt.Printf("%s\n", loc.SubRegion)
// Output: United Kingdom
// United Kingdom of Great Britain and Northern Ireland
// GB
// GBR
// Europe
// Europe
// Northern Europe
}
func ExampleRgeo_ReverseGeocode_city() {
r, err := New(Provinces10, Cities10)
if err != nil {
// Handle error
}
loc, err := r.ReverseGeocode([]float64{141.35, 43.07})
if err != nil {
// Handle error
}
fmt.Println(loc)
// Output: <Location> Sapporo, Hokkaidō, Japan (JPN), Asia
}
func ExampleRgeo_ReverseGeocode_snapping() {
r, err := New(Provinces10)
if err != nil {
// Handle error
}
loc, err := r.ReverseGeocodeSnapping(geom.Coord{141.5439, 40.5588})
if err != nil {
// Handle error
}
fmt.Println(loc)
// Output: <Location> Aomori, Japan (JPN), Asia
}
func TestTable(t *testing.T) {
type LookupFunc func(geom.Coord) (Location, error)
coordsFar := geom.Coord{139.0385841, 40.894911} // somewhere in the japanese sea
coordsClose := geom.Coord{141.5350, 40.5658} // about 3km outside the country in the sea
coordsInside := geom.Coord{141.5317, 40.5205} // inside the country
locationEmpty := Location{}
locationFound := Location{Country: "Japan", Province: "Aomori"}
r, err := New(Provinces10)
if err != nil {
// Handle error
}
tests := map[string]struct {
input geom.Coord
expected Location
method LookupFunc
}{
"[reverse] far": {
input: coordsFar,
expected: locationEmpty,
method: r.ReverseGeocode,
},
"[reverse] close": {
input: coordsClose,
expected: locationEmpty,
method: r.ReverseGeocode,
},
"[reverse] inside": {
input: coordsInside,
expected: locationFound,
method: r.ReverseGeocode,
},
"[snapping] far": {
input: coordsFar,
expected: locationEmpty,
method: r.ReverseGeocodeSnapping,
},
"[snapping] close": {
input: coordsClose,
expected: locationFound,
method: r.ReverseGeocodeSnapping,
},
"[snapping] inside": {
input: coordsInside,
expected: locationFound,
method: r.ReverseGeocodeSnapping,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
actual, err := test.method(test.input)
if err != nil {
// Handle error
}
if actual.Country != test.expected.Country ||
actual.Province != test.expected.Province ||
(test.expected == locationEmpty && !errors.Is(err, ErrLocationNotFound)) {
t.Errorf("expected %q, got %q", test.expected, actual)
}
})
}
}
func BenchmarkReverseGeocode_110(b *testing.B) {
r, err := New(Countries110)
if err != nil {
b.Error(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = r.ReverseGeocode([]float64{
(rand.Float64() * 360) - 180,
(rand.Float64() * 180) - 90,
})
}
}
func BenchmarkReverseGeocode_10(b *testing.B) {
r, err := New(Countries10)
if err != nil {
b.Error(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = r.ReverseGeocode([]float64{
(rand.Float64() * 360) - 180,
(rand.Float64() * 180) - 90,
})
}
}
func BenchmarkReverseGeocode_Prov10(b *testing.B) {
r, err := New(Provinces10)
if err != nil {
b.Error(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = r.ReverseGeocode([]float64{
(rand.Float64() * 360) - 180,
(rand.Float64() * 180) - 90,
})
}
}
func BenchmarkReverseGeocode_City10(b *testing.B) {
r, err := New(Provinces10, Cities10)
if err != nil {
b.Error(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = r.ReverseGeocode([]float64{
(rand.Float64() * 360) - 180,
(rand.Float64() * 180) - 90,
})
}
}
func BenchmarkNew(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := New(Countries110)
if err != nil {
b.Error(err)
}
}
}
func BenchmarkNewCity(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := New(Cities10)
if err != nil {
b.Error(err)
}
}
}
func testDataset(t *testing.T, text string) Dataset {
var fc geojson.FeatureCollection
if err := json.NewDecoder(bytes.NewReader([]byte(text))).Decode(&fc); err != nil {
t.Fatalf("decode GeoJSON: %s", err)
}
features, err := LoadGeoJSON(fc)
if err != nil {
t.Fatalf("features from GeoJSON: %s", err)
}
return func() []Feature {
return features
}
}