-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocoordinate.go
56 lines (49 loc) · 1.35 KB
/
geocoordinate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package jma
import (
"fmt"
"math"
)
type GeoCoordinate struct {
Lat float64 `validate:"gte=-85.0511287798,lte=85.0511287798,required"`
Lon float64 `validate:"required"`
}
func (g GeoCoordinate) String() string {
return fmt.Sprintf("{Lat: %f, Lon: %f}", g.Lat, g.Lon)
}
func (g GeoCoordinate) IsValid() bool {
err := validate.Struct(g)
if err != nil {
// fmt.Printf("Err(s):\n%+v\n", err)
return false
}
return true
}
func (g GeoCoordinate) Normalize() GeoCoordinate {
latmax := 85.0511287798
return GeoCoordinate{
Lat: math.Max(math.Min(g.Lat, latmax), -latmax),
Lon: math.Mod(g.Lon+180, 360) - 180,
}
}
func (g GeoCoordinate) GetTile(zoom uint) Tile {
// reference for conversion formula: https://standardization.at.webry.info/201401/article_1.html
n := math.Pow(2, float64(zoom))
r := math.Pi / 180
lat := g.Lat * r
return Tile{
Zoom: zoom,
X: uint((g.Lon + 180) / 360 * n),
Y: uint((1.0 - math.Log(math.Tan(math.Pi/4+lat/2))/math.Pi) / 2 * n),
}
}
func (g GeoCoordinate) GetTileCoordinate(zoom uint) TileCoordinate {
// reference for conversion formula: https://standardization.at.webry.info/201401/article_1.html
n := math.Pow(2, float64(zoom))
r := math.Pi / 180
lat := g.Lat * r
return TileCoordinate{
Zoom: zoom,
X: (g.Lon + 180) / 360 * n,
Y: (1.0 - math.Log(math.Tan(math.Pi/4+lat/2))/math.Pi) / 2 * n,
}
}