Permalink
Browse files

use vincenty formula for distance

Signed-off-by: Peter Edge <peter.edge@gmail.com>
  • Loading branch information...
peter-edge committed Jan 26, 2016
1 parent 80f9e6e commit 597076061efeabbb307365701bb982d7fe921870
Showing with 16 additions and 19 deletions.
  1. +8 −9 README.md
  2. +8 −10 util.go
@@ -55,15 +55,14 @@ airport codes. The flag `--min` will use a minimum miles for a segment, useful f

```
$ openflights miles --min 500 vie-bru-ewr-mem-ord-ams-vie
VIE-BRU 574mi
BRU-EWR 3670mi
EWR-MEM 944mi
MEM-ORD 500mi (492mi actual)
ORD-AMS 4108mi
AMS-VIE 596mi
10392mi (10384mi actual)
20784mi round trip (20768mi actual)
VIE-BRU 576mi
BRU-EWR 3681mi
EWR-MEM 946mi
MEM-ORD 500mi (491mi actual)
ORD-AMS 4120mi
AMS-VIE 598mi
10421mi (10412mi actual)
```

## Future Work
18 util.go
@@ -5,10 +5,16 @@ import (
"strconv"
"strings"

"github.com/StefanSchroeder/Golang-Ellipsoid/ellipsoid"

"google.golang.org/grpc"
"google.golang.org/grpc/codes"
)

var (
geo = ellipsoid.Init("WGS84", ellipsoid.Degrees, ellipsoid.Mile, ellipsoid.LongitudeIsSymmetric, ellipsoid.BearingIsSymmetric)
)

func getAirportByKey(m map[string]*Airport, key string) (*Airport, error) {
airport, ok := m[strings.ToLower(key)]
if !ok {
@@ -59,16 +65,8 @@ func getDistanceForAirports(airport1 *Airport, airport2 *Airport) uint32 {
}

func getDistance(lat1 float64, lng1 float64, lat2 float64, lng2 float64) uint32 {
dLat := (lat2 - lat1) * (math.Pi / 180.0)
dLon := (lng2 - lng1) * (math.Pi / 180.0)
lat1 = lat1 * (math.Pi / 180.0)
lat2 = lat2 * (math.Pi / 180.0)
a1 := math.Sin(dLat/2) * math.Sin(dLat/2)
a2 := math.Sin(dLon/2) * math.Sin(dLon/2) * math.Cos(lat1) * math.Cos(lat2)
a := a1 + a2
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
miles := 3959 * c
return uint32(round(math.Floor(miles)))
distance, _ := geo.To(lat1, lng1, lat2, lng2)
return uint32(round(distance))
}

// expensive

0 comments on commit 5970760

Please sign in to comment.