2020-01-12 14:00:56 +01:00
|
|
|
/*
|
|
|
|
Package s2 encapsulates Google's S2 library.
|
|
|
|
|
|
|
|
Additional information can be found in our Developer Guide:
|
|
|
|
|
|
|
|
https://github.com/photoprism/photoprism/wiki
|
|
|
|
|
|
|
|
...and in the Google S2 documentation:
|
|
|
|
|
|
|
|
https://s2geometry.io/
|
|
|
|
|
|
|
|
*/
|
2019-12-31 07:16:11 +01:00
|
|
|
package s2
|
|
|
|
|
|
|
|
import (
|
|
|
|
gs2 "github.com/golang/geo/s2"
|
|
|
|
)
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
// Default cell level, see https://s2geometry.io/resources/s2cell_statistics.html.
|
|
|
|
var DefaultLevel = 21
|
2019-12-31 07:16:11 +01:00
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
// Token returns the S2 cell token for coordinates using the default level.
|
2019-12-31 07:16:11 +01:00
|
|
|
func Token(lat, lng float64) string {
|
2020-01-12 14:00:56 +01:00
|
|
|
return TokenLevel(lat, lng, DefaultLevel)
|
2019-12-31 07:16:11 +01:00
|
|
|
}
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
// Token returns the S2 cell token for coordinates.
|
2019-12-31 07:16:11 +01:00
|
|
|
func TokenLevel(lat, lng float64, level int) string {
|
|
|
|
if lat == 0.0 && lng == 0.0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if lat < -90 || lat > 90 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if lng < -180 || lng > 180 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
l := gs2.LatLngFromDegrees(lat, lng)
|
|
|
|
return gs2.CellIDFromLatLng(l).Parent(level).ToToken()
|
|
|
|
}
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
// LatLng returns the coordinates for a S2 cell token.
|
2019-12-31 07:16:11 +01:00
|
|
|
func LatLng(token string) (lat, lng float64) {
|
|
|
|
if token == "" || token == "-" {
|
|
|
|
return 0.0, 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
c := gs2.CellIDFromToken(token)
|
2020-01-15 04:04:33 +01:00
|
|
|
|
|
|
|
if !c.IsValid() {
|
|
|
|
return 0.0, 0.0
|
|
|
|
}
|
|
|
|
|
2019-12-31 07:16:11 +01:00
|
|
|
l := c.LatLng()
|
|
|
|
return l.Lat.Degrees(), l.Lng.Degrees()
|
|
|
|
}
|
2020-01-15 04:04:33 +01:00
|
|
|
|
|
|
|
// IsZero returns true if the coordinates are both empty.
|
|
|
|
func IsZero(lat, lng float64) bool {
|
|
|
|
return lat == 0.0 && lng == 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Range returns a token range for finding nearby locations.
|
|
|
|
func Range(token string, levelUp int) (min, max string) {
|
|
|
|
c := gs2.CellIDFromToken(token)
|
|
|
|
|
|
|
|
if !c.IsValid() {
|
|
|
|
return min, max
|
|
|
|
}
|
|
|
|
|
2020-01-15 12:30:50 +01:00
|
|
|
// See https://s2geometry.io/resources/s2cell_statistics.html
|
|
|
|
lvl := c.Level()
|
2020-01-15 04:04:33 +01:00
|
|
|
|
2020-01-15 12:30:50 +01:00
|
|
|
parent := c.Parent(lvl - levelUp)
|
|
|
|
|
|
|
|
return parent.Prev().ChildBeginAtLevel(lvl).ToToken(), parent.Next().ChildBeginAtLevel(lvl).ToToken()
|
2020-01-15 04:04:33 +01:00
|
|
|
}
|