Skip to content

Commit

Permalink
Tests: Refactor OS Grid and Reproject to use common test data.
Browse files Browse the repository at this point in the history
  • Loading branch information
geofurlong committed Apr 23, 2024
1 parent ba4d8ab commit 18e3184
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 108 deletions.
55 changes: 4 additions & 51 deletions pkg/geocode/osgrid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,10 @@ import (
"github.com/paulmach/orb"
)

type testValue struct {
easting int
northing int
osgr string
location string
}

func getTestCases() []testValue {
return []testValue{
{485_771, 92_079, "SZ8577192079", "Selsey Bill"},
{480_479, 96_317, "SZ8047996317", "Bracklesham Bay"},
{529_740, 180_773, "TQ2974080773", "Wardour St, London"},
{496_685, 177_398, "SU9668577398", "Eton Guns, Eton"},
{502_925, 160_554, "TQ0292560554", "Sheerwater, Woking"},
{528_132, 184_415, "TQ2813284415", "Chalk Farm Tube Station"},
{147_092, 30_263, "SW4709230263", "Penzance"},
{224_726, 382_330, "SH2472682330", "Holyhead"},
{150_552, 755_297, "NM5055255297", "Tobermory"},
{175_405, 225_295, "SM7540525295", "St Davids"},
{176_346, 827_263, "NG7634627263", "Kyle of Lochalsh"},
{182_648, 44_827, "SW8264844827", "Truro"},
{218_570, 597_931, "NX1857097931", "Girvan"},
{266_617, 845_493, "NH6661745493", "Inverness"},
{268_577, 551_148, "NX6857751148", "Kirkcudbright"},
{293_994, 758_112, "NN9399458112", "Pitlochry"},
{303_277, 827_846, "NJ0327727846", "Grantown-on-Spey"},
{311_687, 968_450, "ND1168768450", "Thurso"},
{317_814, 745_296, "NO1781445296", "Blairgowrie and Rattray"},
{330_872, 436_578, "SD3087236578", "Blackpool"},
{350_897, 239_976, "SO5089739976", "Hereford"},
{390_129, 426_184, "SD9012926184", "Portsmouth"},
{394_426, 806_556, "NJ9442606556", "Aberdeen"},
{402_281, 202_031, "SP0228102031", "Cirencester"},
{419_799, 585_952, "NZ1979985952", "Morpeth"},
{424_572, 403_285, "SE2457203285", "Penistone"},
{435_749, 316_723, "SK3574916723", "Ashby-de-la-Zouch"},
{447_467, 1_141_434, "HU4746741434", "Lerwick"},
{460_218, 452_158, "SE6021852158", "York"},
{519_204, 298_638, "TL1920498638", "Peterborough"},
{567_377, 194_515, "TQ6737794515", "Billericay"},
{579_941, 183_634, "TQ7994183634", "Canvey Island"},
{622_977, 308_549, "TG2297708549", "Norwich"},
{631_587, 141_745, "TR3158741745", "Dover"},
{654_779, 293_216, "TM5477993216", "Lowestoft"},
}
}

func TestPointToOSGR(t *testing.T) {
testCases := getTestCases()
testPlaces := getTestPlaces()

for _, tc := range testCases {
for _, tc := range testPlaces {
res := PointToOSGR(orb.Point{float64(tc.easting), float64(tc.northing)})

if res != tc.osgr {
Expand All @@ -67,9 +20,9 @@ func TestPointToOSGR(t *testing.T) {
}

func TestOSGRToPoint(t *testing.T) {
testCases := getTestCases()
testPlaces := getTestPlaces()

for _, tc := range testCases {
for _, tc := range testPlaces {
pt, err := OSGRToPoint(tc.osgr)
if err != nil {
t.Errorf("OSGRToPoint(%q) returned error: %v", tc.osgr, err)
Expand Down
72 changes: 15 additions & 57 deletions pkg/geocode/reproject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,76 +8,34 @@ import (
)

func TestReproject(t *testing.T) {
cases := []struct {
easting float64
northing float64
expected orb.Point
location string
}{
{485_771, 92_079, orb.Point{-0.78628983, 50.722016}, "Selsey Bill"},
{480_479, 96_317, orb.Point{-0.86031823, 50.760875}, "Bracklesham Bay"},
{529_740, 180_773, orb.Point{-0.13181379, 51.510997}, "Wardour St, London"},
{496_685, 177_398, orb.Point{-0.60888972, 51.487277}, "Eton Guns, Eton"},
{502_925, 160_554, orb.Point{-0.52393303, 51.334773}, "Sheerwater, Woking"},
{528_132, 184_415, orb.Point{-0.15364227, 51.544097}, "Chalk Farm Tube Station"},
{147_092, 30_263, orb.Point{-5.5392674101162, 50.1184449113554}, "Penzance"},
{224_726, 382_330, orb.Point{-4.6321875, 53.308828}, "Holyhead"},
{150_552, 755_298, orb.Point{-6.06809695014501, 56.622901648826}, "Tobermory"},
{175_405, 225_296, orb.Point{-5.26464006525205, 51.8808734514259}, "St Davids"},
{176_346, 827_264, orb.Point{-5.71229896759099, 57.2810797079495}, "Kyle of Lochalsh"},
{182_648, 44_827, orb.Point{-5.05101265950598, 50.2633300534275}, "Truro"},
{218_570, 597_931, orb.Point{-4.85531770044002, 55.2421123945225}, "Girvan"},
{266_617, 845_493, orb.Point{-4.22622129478818, 57.4799977149705}, "Inverness"},
{268_577, 551_148, orb.Point{-4.04786239360251, 54.8378524191877}, "Kirkcudbright"},
{293_994, 758_112, orb.Point{-3.73289806042956, 56.7026446631663}, "Pitlochry"},
{303_277, 827_846, orb.Point{-3.60817223914678, 57.3308520860594}, "Grantown-on-Spey"},
{311_687, 968_450, orb.Point{-3.52110010140828, 58.5950124669737}, "Thurso"},
{317_814, 745_297, orb.Point{-3.33991695114065, 56.5923527415861}, "Blairgowrie and Rattray"},
{330_873, 436_578, orb.Point{-3.05154404779973, 53.8208602230914}, "Blackpool"},
{350_897, 239_976, orb.Point{-2.71754978850477, 52.0559792724345}, "Hereford"},
{390_129, 426_184, orb.Point{-2.15110286235426, 53.7319540311404}, "Portsmouth"},
{394_426, 806_556, orb.Point{-2.09375976250924, 57.1498507970959}, "Aberdeen"},
{402_281, 202_031, orb.Point{-1.96837565467488, 51.7170067510382}, "Cirencester"},
{419_799, 585_952, orb.Point{-1.69074152555053, 55.1674893825777}, "Morpeth"},
{424_572, 403_286, orb.Point{-1.63081305258923, 53.5256667596675}, "Penistone"},
{435_749, 316_724, orb.Point{-1.47184910059996, 52.7469578946588}, "Ashby-de-la-Zouch"},
{447_467, 1_141_434, orb.Point{-1.14681809802032, 60.1546418281584}, "Lerwick"},
{460_218, 452_158, orb.Point{-1.0836748355092, 53.9620087173741}, "York"},
{519_204, 298_638, orb.Point{-0.242497599724569, 52.5725093308881}, "Peterborough"},
{567_377, 194_515, orb.Point{0.416622675810784, 51.6245616474664}, "Billericay"},
{579_941, 183_634, orb.Point{0.592366890161814, 51.5229534756215}, "Canvey Island"},
{622_977, 308_549, orb.Point{1.29330728438099, 52.6288686480942}, "Norwich"},
{631_587, 141_745, orb.Point{1.30842698233744, 51.1281377122637}, "Dover"},
{654_779, 293_216, orb.Point{1.75043311819206, 52.4773484258696}, "Lowestoft"},
}

const Epsilon = 1e-5

en := []orb.Point{}
for _, cm := range cases {
en = append(en, orb.Point{cm.easting, cm.northing})
testPlaces := getTestPlaces()

planarPoints := []orb.Point{}
for _, testPlace := range testPlaces {
planarPoints = append(planarPoints, orb.Point{float64(testPlace.easting), float64(testPlace.northing)})
}

ll := ReprojectMulti(en)
for i, cm := range cases {
deltaX := ll[i].Point().X() - cm.expected.X()
deltaY := ll[i].Point().Y() - cm.expected.Y()
geoPoints := ReprojectMulti(planarPoints)
for i, testPlace := range testPlaces {
deltaX := geoPoints[i].Point().X() - testPlace.lonLat.X()
deltaY := geoPoints[i].Point().Y() - testPlace.lonLat.Y()

if math.Abs(deltaX) > Epsilon || math.Abs(deltaY) > Epsilon {
t.Log("ReprojectMulti error, should be:", cm.expected, "but got:", ll[i])
t.Log("ReprojectMulti error, should be:", testPlace.lonLat, "but got:", geoPoints[i])
t.Fail()
}
}

pj := OSGBtoLongLat()
for _, c := range cases {
ll := Reproject(orb.Point{c.easting, c.northing}, pj)
deltaX := ll.X() - c.expected.X()
deltaY := ll.Y() - c.expected.Y()
t.Logf("%.6f %.6f", deltaX, deltaY)
for _, testPlace := range testPlaces {
geoPoint := Reproject(orb.Point{float64(testPlace.easting), float64(testPlace.northing)}, pj)
deltaX := geoPoint.X() - testPlace.lonLat.X()
deltaY := geoPoint.Y() - testPlace.lonLat.Y()

if math.Abs(deltaX) > Epsilon || math.Abs(deltaY) > Epsilon {
t.Log("Reproject error, should be:", c.expected, "but got:", ll, "for location:", c.location)
t.Log("Reproject error, should be:", testPlace.lonLat, "but got:", geoPoint, "for location:", testPlace.lonLat, " - ", testPlace.name)
t.Fail()
}
}
Expand Down
53 changes: 53 additions & 0 deletions pkg/geocode/test_data_geo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package geocode

import (
"github.com/paulmach/orb"
)

type testPlace struct {
easting int
northing int
name string
osgr string
lonLat orb.Point
}

func getTestPlaces() []testPlace {
return []testPlace{
{485_771, 92_079, "Selsey Bill", "SZ8577192079", orb.Point{-0.78628983, 50.722016}},
{480_479, 96_317, "Bracklesham Bay", "SZ8047996317", orb.Point{-0.86031823, 50.760875}},
{529_740, 180_773, "Wardour St, London", "TQ2974080773", orb.Point{-0.13181379, 51.510997}},
{496_685, 177_398, "Eton Guns, Eton", "SU9668577398", orb.Point{-0.60888972, 51.487277}},
{502_925, 160_554, "Sheerwater, Woking", "TQ0292560554", orb.Point{-0.52393303, 51.334773}},
{528_132, 184_415, "Chalk Farm Tube Station", "TQ2813284415", orb.Point{-0.15364227, 51.544097}},
{147_092, 30_263, "Penzance", "SW4709230263", orb.Point{-5.5392674101162, 50.1184449113554}},
{224_726, 382_330, "Holyhead", "SH2472682330", orb.Point{-4.6321875, 53.308828}},
{150_552, 755_297, "Tobermory", "NM5055255297", orb.Point{-6.06809695014501, 56.622901648826}},
{175_405, 225_295, "St Davids", "SM7540525295", orb.Point{-5.26464006525205, 51.8808734514259}},
{176_346, 827_263, "Kyle of Lochalsh", "NG7634627263", orb.Point{-5.71229896759099, 57.2810797079495}},
{182_648, 44_827, "Truro", "SW8264844827", orb.Point{-5.05101265950598, 50.2633300534275}},
{218_570, 597_931, "Girvan", "NX1857097931", orb.Point{-4.85531770044002, 55.2421123945225}},
{266_617, 845_493, "Inverness", "NH6661745493", orb.Point{-4.22622129478818, 57.4799977149705}},
{268_577, 551_148, "Kirkcudbright", "NX6857751148", orb.Point{-4.04786239360251, 54.8378524191877}},
{293_994, 758_112, "Pitlochry", "NN9399458112", orb.Point{-3.73289806042956, 56.7026446631663}},
{303_277, 827_846, "Grantown-on-Spey", "NJ0327727846", orb.Point{-3.60817223914678, 57.3308520860594}},
{311_687, 968_450, "Thurso", "ND1168768450", orb.Point{-3.52110010140828, 58.5950124669737}},
{317_814, 745_296, "Blairgowrie and Rattray", "NO1781445296", orb.Point{-3.33991695114065, 56.5923527415861}},
{330_873, 436_578, "Blackpool", "SD3087336578", orb.Point{-3.05154404779973, 53.8208602230914}},
{350_897, 239_976, "Hereford", "SO5089739976", orb.Point{-2.71754978850477, 52.0559792724345}},
{390_129, 426_184, "Portsmouth", "SD9012926184", orb.Point{-2.15110286235426, 53.7319540311404}},
{394_426, 806_556, "Aberdeen", "NJ9442606556", orb.Point{-2.09375976250924, 57.1498507970959}},
{402_281, 202_031, "Cirencester", "SP0228102031", orb.Point{-1.96837565467488, 51.7170067510382}},
{419_799, 585_952, "Morpeth", "NZ1979985952", orb.Point{-1.69074152555053, 55.1674893825777}},
{424_572, 403_285, "Penistone", "SE2457203285", orb.Point{-1.63081305258923, 53.5256667596675}},
{435_749, 316_723, "Ashby-de-la-Zouch", "SK3574916723", orb.Point{-1.47184910059996, 52.7469578946588}},
{447_467, 1_141_434, "Lerwick", "HU4746741434", orb.Point{-1.14681809802032, 60.1546418281584}},
{460_218, 452_158, "York", "SE6021852158", orb.Point{-1.0836748355092, 53.9620087173741}},
{519_204, 298_638, "Peterborough", "TL1920498638", orb.Point{-0.242497599724569, 52.5725093308881}},
{567_377, 194_515, "Billericay", "TQ6737794515", orb.Point{0.416622675810784, 51.6245616474664}},
{579_941, 183_634, "Canvey Island", "TQ7994183634", orb.Point{0.592366890161814, 51.5229534756215}},
{622_977, 308_549, "Norwich", "TG2297708549", orb.Point{1.29330728438099, 52.6288686480942}},
{631_587, 141_745, "Dover", "TR3158741745", orb.Point{1.30842698233744, 51.1281377122637}},
{654_779, 293_216, "Lowestoft", "TM5477993216", orb.Point{1.75043311819206, 52.4773484258696}},
}
}

0 comments on commit 18e3184

Please sign in to comment.