Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in point calculations that can cause negative bearings. #74

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions geo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package geo
import (
_ "database/sql"
"fmt"
"github.com/erikstmartin/go-testdb"
"os"
"strconv"
"testing"

"github.com/erikstmartin/go-testdb"
)

// TODO This paticular test is just one big integration for using the entire library.
Expand All @@ -24,7 +25,7 @@ func TestPointsWithinRadiusIntegration(t *testing.T) {
s, sqlErr := HandleWithSQL()

if sqlErr != nil {
t.Error("ERROR: %s", sqlErr)
t.Errorf("ERROR: %s", sqlErr)
}

// SFO
Expand Down
4 changes: 2 additions & 2 deletions mapquest_geocoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@ func TestMapquestReverseGeocoderQueryStr(t *testing.T) {
func TestMapQuestGeocodeFromRequest(t *testing.T) {
data, err := GetMockResponse("test/data/mapquest_geocode_success.json")
if err != nil {
t.Error("%v\n", err)
t.Errorf("%v\n", err)
}

res := []*mapQuestGeocodeResponse{}

err = json.Unmarshal(data, &res)
if err != nil {
t.Error("%v\n", err)
t.Errorf("%v\n", err)
}

if len(res) <= 0 {
Expand Down
7 changes: 5 additions & 2 deletions point.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (p *Point) GreatCircleDistance(p2 *Point) float64 {
return EARTH_RADIUS * c
}

// BearingTo: Calculates the initial bearing (sometimes referred to as forward azimuth)
// BearingTo: Calculates the initial bearing in degrees (sometimes referred to as forward azimuth)
// Original Implementation from: http://www.movable-type.co.uk/scripts/latlong.html
func (p *Point) BearingTo(p2 *Point) float64 {

Expand All @@ -97,7 +97,10 @@ func (p *Point) BearingTo(p2 *Point) float64 {
x := math.Cos(lat1)*math.Sin(lat2) -
math.Sin(lat1)*math.Cos(lat2)*math.Cos(dLon)
brng := math.Atan2(y, x) * 180.0 / math.Pi

// Account for math.Atan2 returning negative values.
if brng < 0 {
brng = 360 + brng
}
return brng
}

Expand Down
42 changes: 30 additions & 12 deletions point_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"log"
"math"
"testing"
)

Expand Down Expand Up @@ -33,7 +34,7 @@ func TestLat(t *testing.T) {
lat := p.Lat()

if lat != 40.5 {
t.Error("Expected a call to GetLat() to return the same lat value as was set before, but got %f instead", lat)
t.Errorf("Expected a call to GetLat() to return the same lat value as was set before, but got %f instead", lat)
}
}

Expand All @@ -44,7 +45,7 @@ func TestLng(t *testing.T) {
lng := p.Lng()

if lng != 120.5 {
t.Error("Expected a call to GetLng() to return the same lat value as was set before, but got %f instead", lng)
t.Errorf("Expected a call to GetLng() to return the same lat value as was set before, but got %f instead", lng)
}
}

Expand Down Expand Up @@ -79,16 +80,33 @@ func TestPointAtDistanceAndBearing(t *testing.T) {
}

func TestBearingTo(t *testing.T) {
p1 := &Point{lat: 40.7486, lng: -73.9864}
p2 := &Point{lat: 0.0, lng: 0.0}
bearing := p1.BearingTo(p2)

// Expected bearing 60 degrees
resultBearing := 100.610833

withinBearingBounds := bearing < resultBearing+0.001 && bearing > resultBearing-0.001
if !withinBearingBounds {
t.Error("Unnacceptable result.", fmt.Sprintf("%f", bearing))
for _, tt := range []struct {
name string
p1 *Point
p2 *Point
want float64
}{
{
name: "Basic",
p1: &Point{lat: 40.7486, lng: -73.9864},
p2: &Point{lat: 0.0, lng: 0.0},
want: 100.610833,
},
{
name: "NegativeArctan",
p1: &Point{lat: 33.9271, lng: -118.2438},
p2: &Point{lat: 33.9225, lng: -118.3434},
want: 266.842019,
},
} {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got := tt.p1.BearingTo(tt.p2)
if diff := math.Abs(got - tt.want); diff > 0.001 {
t.Errorf("p1.BearingTo(p2) = %f want %f", got, tt.want)
}
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion polygon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestEquatorGreenwichContains(t *testing.T) {
polygon, err := polygonFromFile("test/data/equator_greenwich.json")

if err != nil {
t.Errorf("error parsing polygon", err)
t.Errorf("error parsing polygon: %v", err)
}

if !polygon.Contains(point1) {
Expand Down