Use a proven library to calculate distance
Also rounding distance at 2 decimals instead of round to 5
This commit is contained in:
parent
01347b8514
commit
b138d9b6bc
1
go.mod
1
go.mod
@ -20,6 +20,7 @@ require (
|
|||||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
github.com/spf13/viper v1.7.1
|
github.com/spf13/viper v1.7.1
|
||||||
|
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26
|
||||||
go.etcd.io/bbolt v1.3.5
|
go.etcd.io/bbolt v1.3.5
|
||||||
golang.org/x/image v0.0.0-20200801110659-972c09e46d76
|
golang.org/x/image v0.0.0-20200801110659-972c09e46d76
|
||||||
golang.org/x/sys v0.0.0-20200819035508-9a32b3aa38f5 // indirect
|
golang.org/x/sys v0.0.0-20200819035508-9a32b3aa38f5 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -212,6 +212,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
|
|||||||
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
|
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
|
||||||
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
|
||||||
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
|
||||||
|
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26 h1:UFHFmFfixpmfRBcxuu+LA9l8MdURWVdVNUHxO5n1d2w=
|
||||||
|
github.com/umahmood/haversine v0.0.0-20151105152445-808ab04add26/go.mod h1:IGhd0qMDsUa9acVjsbsT7bu3ktadtGOHI79+idTew/M=
|
||||||
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
|
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
|
||||||
go.etcd.io/bbolt v1.3.2 h1:Z/90sZLPOeCy2PwprqkFa25PdkusRzaj9P8zm/KNyvk=
|
go.etcd.io/bbolt v1.3.2 h1:Z/90sZLPOeCy2PwprqkFa25PdkusRzaj9P8zm/KNyvk=
|
||||||
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
|
||||||
|
@ -5,11 +5,12 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"math"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/umahmood/haversine"
|
||||||
|
|
||||||
"github.com/librespeed/speedtest/config"
|
"github.com/librespeed/speedtest/config"
|
||||||
"github.com/librespeed/speedtest/results"
|
"github.com/librespeed/speedtest/results"
|
||||||
|
|
||||||
@ -17,7 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
serverLat, serverLng float64
|
serverCoord haversine.Coord
|
||||||
)
|
)
|
||||||
|
|
||||||
func getRandomData(length int) []byte {
|
func getRandomData(length int) []byte {
|
||||||
@ -67,95 +68,89 @@ func getIPInfo(addr string) results.IPInfoResponse {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetServerLocation(conf *config.Config) (float64, float64) {
|
func SetServerLocation(conf *config.Config) {
|
||||||
if conf.ServerLat > 0 && conf.ServerLng > 0 {
|
if conf.ServerLat > 0 && conf.ServerLng > 0 {
|
||||||
log.Infof("Configured server coordinates: %.6f, %.6f", conf.ServerLat, conf.ServerLng)
|
log.Infof("Configured server coordinates: %.6f, %.6f", conf.ServerLat, conf.ServerLng)
|
||||||
return conf.ServerLat, conf.ServerLng
|
serverCoord.Lat = conf.ServerLat
|
||||||
|
serverCoord.Lon = conf.ServerLng
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var ret results.IPInfoResponse
|
var ret results.IPInfoResponse
|
||||||
resp, err := http.DefaultClient.Get(getIPInfoURL(""))
|
resp, err := http.DefaultClient.Get(getIPInfoURL(""))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error getting repsonse from ipinfo.io: %s", err)
|
log.Errorf("Error getting repsonse from ipinfo.io: %s", err)
|
||||||
return 0, 0
|
return
|
||||||
}
|
}
|
||||||
raw, err := ioutil.ReadAll(resp.Body)
|
raw, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error reading response from ipinfo.io: %s", err)
|
log.Errorf("Error reading response from ipinfo.io: %s", err)
|
||||||
return 0, 0
|
return
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if err := json.Unmarshal(raw, &ret); err != nil {
|
if err := json.Unmarshal(raw, &ret); err != nil {
|
||||||
log.Errorf("Error parsing response from ipinfo.io: %s", err)
|
log.Errorf("Error parsing response from ipinfo.io: %s", err)
|
||||||
return 0, 0
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var lat, lng float64
|
|
||||||
if ret.Location != "" {
|
if ret.Location != "" {
|
||||||
lat, lng = parseLocationString(ret.Location)
|
serverCoord, err = parseLocationString(ret.Location)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Cannot get server coordinates: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Fetched server coordinates: %.6f, %.6f", lat, lng)
|
log.Infof("Fetched server coordinates: %.6f, %.6f", serverCoord.Lat, serverCoord.Lon)
|
||||||
|
|
||||||
return lat, lng
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseLocationString(location string) (float64, float64) {
|
func parseLocationString(location string) (haversine.Coord, error) {
|
||||||
|
var coord haversine.Coord
|
||||||
|
|
||||||
parts := strings.Split(location, ",")
|
parts := strings.Split(location, ",")
|
||||||
if len(parts) != 2 {
|
if len(parts) != 2 {
|
||||||
log.Errorf("Unknown location format: %s", location)
|
err := fmt.Errorf("unknown location format: %s", location)
|
||||||
return 0, 0
|
log.Error(err)
|
||||||
|
return coord, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lat, err := strconv.ParseFloat(parts[0], 64)
|
lat, err := strconv.ParseFloat(parts[0], 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error parsing latitude: %s", parts[0])
|
log.Errorf("Error parsing latitude: %s", parts[0])
|
||||||
return 0, 0
|
return coord, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lng, err := strconv.ParseFloat(parts[1], 64)
|
lng, err := strconv.ParseFloat(parts[1], 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error parsing longitude: %s", parts[0])
|
log.Errorf("Error parsing longitude: %s", parts[0])
|
||||||
return 0, 0
|
return coord, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return lat, lng
|
coord.Lat = lat
|
||||||
|
coord.Lon = lng
|
||||||
|
|
||||||
|
return coord, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateDistance(clientLocation string, unit string) string {
|
func calculateDistance(clientLocation string, unit string) string {
|
||||||
clientLat, clientLng := parseLocationString(clientLocation)
|
clientCoord, err := parseLocationString(clientLocation)
|
||||||
|
if err != nil {
|
||||||
radlat1 := float64(math.Pi * serverLat / 180)
|
log.Errorf("Error parsing client coordinates: %s", err)
|
||||||
radlat2 := float64(math.Pi * clientLat / 180)
|
return ""
|
||||||
|
|
||||||
theta := float64(serverLng - clientLng)
|
|
||||||
radtheta := float64(math.Pi * theta / 180)
|
|
||||||
|
|
||||||
dist := math.Sin(radlat1)*math.Sin(radlat2) + math.Cos(radlat1)*math.Cos(radlat2)*math.Cos(radtheta)
|
|
||||||
|
|
||||||
if dist > 1 {
|
|
||||||
dist = 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dist = math.Acos(dist)
|
dist, km := haversine.Distance(clientCoord, serverCoord)
|
||||||
dist = dist * 180 / math.Pi
|
|
||||||
dist = dist * 60 * 1.1515
|
|
||||||
|
|
||||||
unitString := " mi"
|
unitString := " mi"
|
||||||
|
|
||||||
switch unit {
|
switch unit {
|
||||||
case "km":
|
case "km":
|
||||||
dist = dist * 1.609344
|
dist = km
|
||||||
unitString = " km"
|
unitString = " km"
|
||||||
case "NM":
|
case "NM":
|
||||||
dist = dist * 0.8684
|
dist = km * 0.539957
|
||||||
unitString = " NM"
|
unitString = " NM"
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf("%d%s", round(dist), unitString)
|
return fmt.Sprintf("%.2f%s", dist, unitString)
|
||||||
}
|
|
||||||
|
|
||||||
func round(v float64) int {
|
|
||||||
r := int(math.Round(v))
|
|
||||||
return 10 * ((r + 9) / 10)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user