gps.go: Fix known S2 mishandling that can lead to an invalid cell

pull/12/head
Dustin Oprea 2019-02-26 12:15:29 -05:00
parent 8d79b03fc5
commit a30f27f681
1 changed files with 28 additions and 26 deletions

22
gps.go
View File

@ -1,13 +1,16 @@
package exif
import (
"errors"
"fmt"
"time"
"github.com/golang/geo/s1"
"github.com/golang/geo/s2"
)
var (
ErrGpsCoordinatesNotValid = errors.New("GPS coordinates not valid")
)
type GpsDegrees struct {
Orientation byte
@ -15,11 +18,11 @@ type GpsDegrees struct {
}
func (d GpsDegrees) String() string {
return fmt.Sprintf("Degrees<O=[%s] D=(%d) M=(%d) S=(%d)>", string([]byte { d.Orientation }), d.Degrees, d.Minutes, d.Seconds)
return fmt.Sprintf("Degrees<O=[%s] D=(%d) M=(%d) S=(%d)>", string([]byte{d.Orientation}), d.Degrees, d.Minutes, d.Seconds)
}
func (d GpsDegrees) Decimal() float64 {
decimal := float64(d.Degrees) + float64(d.Minutes) / 60.0 + float64(d.Seconds) / 3600.0
decimal := float64(d.Degrees) + float64(d.Minutes)/60.0 + float64(d.Seconds)/3600.0
if d.Orientation == 'S' || d.Orientation == 'W' {
return -decimal
@ -28,7 +31,6 @@ func (d GpsDegrees) Decimal() float64 {
}
}
type GpsInfo struct {
Latitude, Longitude GpsDegrees
Altitude int
@ -43,12 +45,12 @@ func (gi *GpsInfo) S2CellId() s2.CellID {
latitude := gi.Latitude.Decimal()
longitude := gi.Longitude.Decimal()
ll := s2.LatLng{
s1.Angle(latitude),
s1.Angle(longitude),
ll := s2.LatLngFromDegrees(latitude, longitude)
cellId := s2.CellIDFromLatLng(ll)
if cellId.IsValid() == false {
panic(ErrGpsCoordinatesNotValid)
}
ll.Normalized()
return s2.CellIDFromLatLng(ll)
return cellId
}