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

This commit is contained in:
Dustin Oprea 2019-02-26 12:15:29 -05:00
parent 8d79b03fc5
commit a30f27f681

54
gps.go
View File

@ -1,54 +1,56 @@
package exif package exif
import ( import (
"fmt" "errors"
"time" "fmt"
"time"
"github.com/golang/geo/s1" "github.com/golang/geo/s2"
"github.com/golang/geo/s2"
) )
var (
ErrGpsCoordinatesNotValid = errors.New("GPS coordinates not valid")
)
type GpsDegrees struct { type GpsDegrees struct {
Orientation byte Orientation byte
Degrees, Minutes, Seconds int Degrees, Minutes, Seconds int
} }
func (d GpsDegrees) String() string { 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 { 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' { if d.Orientation == 'S' || d.Orientation == 'W' {
return -decimal return -decimal
} else { } else {
return decimal return decimal
} }
} }
type GpsInfo struct { type GpsInfo struct {
Latitude, Longitude GpsDegrees Latitude, Longitude GpsDegrees
Altitude int Altitude int
Timestamp time.Time Timestamp time.Time
} }
func (gi *GpsInfo) String() string { func (gi *GpsInfo) String() string {
return fmt.Sprintf("GpsInfo<LAT=(%.05f) LON=(%.05f) ALT=(%d) TIME=[%s]>", gi.Latitude.Decimal(), gi.Longitude.Decimal(), gi.Altitude, gi.Timestamp) return fmt.Sprintf("GpsInfo<LAT=(%.05f) LON=(%.05f) ALT=(%d) TIME=[%s]>", gi.Latitude.Decimal(), gi.Longitude.Decimal(), gi.Altitude, gi.Timestamp)
} }
func (gi *GpsInfo) S2CellId() s2.CellID { func (gi *GpsInfo) S2CellId() s2.CellID {
latitude := gi.Latitude.Decimal() latitude := gi.Latitude.Decimal()
longitude := gi.Longitude.Decimal() longitude := gi.Longitude.Decimal()
ll := s2.LatLng{ ll := s2.LatLngFromDegrees(latitude, longitude)
s1.Angle(latitude), cellId := s2.CellIDFromLatLng(ll)
s1.Angle(longitude),
}
ll.Normalized() if cellId.IsValid() == false {
panic(ErrGpsCoordinatesNotValid)
}
return s2.CellIDFromLatLng(ll) return cellId
} }