-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgeo.go
More file actions
206 lines (165 loc) · 4 KB
/
Copy pathgeo.go
File metadata and controls
206 lines (165 loc) · 4 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package gfx
import (
"image"
"image/draw"
"math"
)
// GeoPoint represents a geographic point with Lat/Lon.
type GeoPoint struct {
Lon float64
Lat float64
}
// GP creates a new GeoPoint
func GP(lat, lon float64) GeoPoint {
return GeoPoint{Lon: lon, Lat: lat}
}
// Vec returns a vector for the geo point based on the given tileSize and zoom level.
func (gp GeoPoint) Vec(tileSize, zoom int) Vec {
scale := math.Pow(2, float64(zoom))
fts := float64(tileSize)
return V(
((float64(gp.Lon)+180)/360)*scale*fts,
(fts/2)-(fts*math.Log(math.Tan((Pi/4)+((float64(gp.Lat)*Pi/180)/2)))/(2*Pi))*scale,
)
}
// In returns a Vec for the position of the GeoPoint in a GeoTile.
func (gp GeoPoint) In(gt GeoTile, tileSize int) Vec {
return gt.Vec(gp, tileSize)
}
// GeoTile for the GeoPoint at the given zoom level.
func (gp GeoPoint) GeoTile(zoom int) GeoTile {
latRad := Degrees(gp.Lat).Radians()
n := math.Pow(2, float64(zoom))
return GeoTile{
Zoom: zoom,
X: int(n * (float64(gp.Lon) + 180) / 360),
Y: int((1.0 - math.Log(math.Tan(latRad)+(1/math.Cos(latRad)))/Pi) / 2.0 * n),
}
}
// NewGeoPointFromTileNumbers creates a new GeoPoint based on the given tile numbers.
// https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Tile_numbers_to_lon..2Flat.
func NewGeoPointFromTileNumbers(zoom, x, y int) GeoPoint {
n := math.Pow(2, float64(zoom))
latRad := math.Atan(math.Sinh(Pi * (1 - (2 * float64(y) / n))))
return GP(latRad*180/Pi, (float64(x)/n*360)-180)
}
// GeoTiles is a slice of GeoTile.
type GeoTiles []GeoTile
// GeoTile consists of a Zoom level, X and Y values.
type GeoTile struct {
Zoom int
X int
Y int
}
// GT creates a new GeoTile.
func GT(zoom, x, y int) GeoTile {
return GeoTile{Zoom: zoom, X: x, Y: y}
}
// GeoPoint for the GeoTile.
func (gt GeoTile) GeoPoint() GeoPoint {
n := math.Pow(2, float64(gt.Zoom))
latRad := math.Atan(math.Sinh(Pi * (1 - (2 * float64(gt.Y) / n))))
return GP(latRad*180/Pi, (float64(gt.X)/n*360)-180)
}
// Vec returns the Vec for the GeoPoint in the GeoTile.
func (gt GeoTile) Vec(gp GeoPoint, tileSize int) Vec {
return gp.Vec(tileSize, gt.Zoom).Sub(gt.GeoPoint().Vec(tileSize, gt.Zoom))
}
// Rawurl formats a URL string with Zoom, X and Y.
func (gt GeoTile) Rawurl(format string) string {
return Sprintf(format, gt.Zoom, gt.X, gt.Y)
}
// AddXY adds x and y.
func (gt GeoTile) AddXY(x, y int) GeoTile {
return GT(gt.Zoom, gt.X+x, gt.Y+y)
}
// Neighbors returns the neighboring tiles.
func (gt GeoTile) Neighbors() GeoTiles {
return GeoTiles{
gt.N(),
gt.NE(),
gt.E(),
gt.SE(),
gt.S(),
gt.SW(),
gt.W(),
gt.NW(),
}
}
// N is the tile to the north.
func (gt GeoTile) N() GeoTile {
if gt.Zoom > 0 && gt.Y > 0 {
gt.Y--
}
return gt
}
// NE is the tile to the northeast.
func (gt GeoTile) NE() GeoTile {
if gt.Zoom > 0 {
if gt.Y > 0 {
gt.Y--
}
gt.X++
}
return gt
}
// E is the tile to the east.
func (gt GeoTile) E() GeoTile {
if gt.Zoom > 0 {
gt.X++
}
return gt
}
// SE is the tile to the southeast.
func (gt GeoTile) SE() GeoTile {
if gt.Zoom > 0 {
gt.X++
gt.Y++
}
return gt
}
// S is the tile to the south.
func (gt GeoTile) S() GeoTile {
if gt.Zoom > 0 {
gt.Y++
}
return gt
}
// SW is the tile to the southwest.
func (gt GeoTile) SW() GeoTile {
if gt.Zoom > 0 {
if gt.X > 0 {
gt.X--
}
gt.Y++
}
return gt
}
// W is the tile to the west.
func (gt GeoTile) W() GeoTile {
if gt.Zoom > 0 && gt.X > 0 {
gt.X--
}
return gt
}
// NW is the tile to the northwest.
func (gt GeoTile) NW() GeoTile {
if gt.Zoom > 0 {
if gt.Y > 0 {
gt.Y--
}
if gt.X > 0 {
gt.X--
}
}
return gt
}
// Draw the tile on dst.
func (gt GeoTile) Draw(dst draw.Image, gp GeoPoint, src image.Image) {
Draw(dst, gt.Bounds(dst, gp, src.Bounds().Dx()), src)
}
// Bounds returns an image.Rectangle for the GeoTile based on the dst, gp and tileSize.
func (gt GeoTile) Bounds(dst image.Image, gp GeoPoint, tileSize int) image.Rectangle {
c := BoundsCenter(dst.Bounds())
return dst.Bounds().Add(c.Pt()).Sub(gp.In(gt, tileSize).Pt())
}