find-stone-beer / files / coord-round_test.go
coord-round_test.go
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/jftuga/geodist" |
| 7 | "github.com/stretchr/testify/assert" |
| 8 | ) |
| 9 | |
| 10 | func TestCoordRound(t *testing.T) { |
| 11 | // 47.6254333,-122.1632314 |
| 12 | |
| 13 | assert.Equal(t, "1.06in", getPrecisionDistance(t, geodist.Coord{Lat: 47.6254333, Lon: -122.1632314}).ToString()) |
| 14 | assert.Equal(t, "10.57in", getPrecisionDistance(t, geodist.Coord{Lat: 47.625433, Lon: -122.163231}).ToString()) |
| 15 | assert.Equal(t, "5.92in", getPrecisionDistance(t, geodist.Coord{Lat: 47.625433111111, Lon: -122.163231}).ToString()) |
| 16 | assert.Equal(t, "880.62ft", getPrecisionDistance(t, geodist.Coord{Lat: 47.625, Lon: -122.163}).ToString()) |
| 17 | assert.Equal(t, "167.38mi", getPrecisionDistance(t, geodist.Coord{Lat: 47, Lon: -122}).ToString()) |
| 18 | |
| 19 | coord := geodist.Coord{Lat: 47.6254333, Lon: -122.1632314} |
| 20 | roundingDistance := ImperialDistance{Miles: 0.25} |
| 21 | roundedCoord, err := RoundCoord(coord, roundingDistance) |
| 22 | assert.Nil(t, err) |
| 23 | precisionDistance := getPrecisionDistance(t, roundedCoord) |
| 24 | assert.Greater(t, precisionDistance.Miles, roundingDistance.Miles) |
| 25 | } |
| 26 | |
| 27 | func getPrecisionDistance(t *testing.T, coord geodist.Coord) ImperialDistance { |
| 28 | latDelta := getDelta(coord.Lat) |
| 29 | minLat := coord.Lat - latDelta |
| 30 | maxLat := coord.Lat + latDelta |
| 31 | |
| 32 | lonDelta := getDelta(coord.Lon) |
| 33 | minLon := coord.Lon - lonDelta |
| 34 | maxLon := coord.Lon + lonDelta |
| 35 | |
| 36 | minCoord := geodist.Coord{Lat: minLat, Lon: minLon} |
| 37 | maxCoord := geodist.Coord{Lat: maxLat, Lon: maxLon} |
| 38 | distMiles, _, err := geodist.VincentyDistance(minCoord, maxCoord) |
| 39 | assert.Nil(t, err) |
| 40 | id := ImperialDistance{Miles: distMiles} |
| 41 | return id |
| 42 | } |