diff --git a/gps.go b/gps.go index 052f28d..1750f46 100644 --- a/gps.go +++ b/gps.go @@ -1,54 +1,56 @@ package exif import ( - "fmt" - "time" + "errors" + "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 { - Orientation byte - Degrees, Minutes, Seconds int + Orientation byte + Degrees, Minutes, Seconds int } func (d GpsDegrees) String() string { - return fmt.Sprintf("Degrees", string([]byte { d.Orientation }), d.Degrees, d.Minutes, d.Seconds) + return fmt.Sprintf("Degrees", 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 - } else { - return decimal - } + if d.Orientation == 'S' || d.Orientation == 'W' { + return -decimal + } else { + return decimal + } } - type GpsInfo struct { - Latitude, Longitude GpsDegrees - Altitude int - Timestamp time.Time + Latitude, Longitude GpsDegrees + Altitude int + Timestamp time.Time } func (gi *GpsInfo) String() string { - return fmt.Sprintf("GpsInfo", gi.Latitude.Decimal(), gi.Longitude.Decimal(), gi.Altitude, gi.Timestamp) + return fmt.Sprintf("GpsInfo", gi.Latitude.Decimal(), gi.Longitude.Decimal(), gi.Altitude, gi.Timestamp) } func (gi *GpsInfo) S2CellId() s2.CellID { - latitude := gi.Latitude.Decimal() - longitude := gi.Longitude.Decimal() + 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) - ll.Normalized() + if cellId.IsValid() == false { + panic(ErrGpsCoordinatesNotValid) + } - return s2.CellIDFromLatLng(ll) + return cellId }