| 1 |
//go:build !tinygo |
| 2 |
// +build !tinygo |
| 3 |
|
| 4 |
package gfx |
| 5 |
|
| 6 |
import ( |
| 7 |
"image" |
| 8 |
"image/draw" |
| 9 |
"math" |
| 10 |
) |
| 11 |
|
| 12 |
// GeoPoint represents a geographic point with Lat/Lon. |
| 13 |
type GeoPoint struct { |
| 14 |
Lon float64 |
| 15 |
Lat float64 |
| 16 |
} |
| 17 |
|
| 18 |
// GP creates a new GeoPoint |
| 19 |
func GP(lat, lon float64) GeoPoint { |
| 20 |
return GeoPoint{Lon: lon, Lat: lat} |
| 21 |
} |
| 22 |
|
| 23 |
// Vec returns a vector for the geo point based on the given tileSize and zoom level. |
| 24 |
func (gp GeoPoint) Vec(tileSize, zoom int) Vec { |
| 25 |
scale := math.Pow(2, float64(zoom)) |
| 26 |
fts := float64(tileSize) |
| 27 |
|
| 28 |
return V( |
| 29 |
((float64(gp.Lon)+180)/360)*scale*fts, |
| 30 |
(fts/2)-(fts*math.Log(math.Tan((Pi/4)+((float64(gp.Lat)*Pi/180)/2)))/(2*Pi))*scale, |
| 31 |
) |
| 32 |
} |
| 33 |
|
| 34 |
// In returns a Vec for the position of the GeoPoint in a GeoTile. |
| 35 |
func (gp GeoPoint) In(gt GeoTile, tileSize int) Vec { |
| 36 |
return gt.Vec(gp, tileSize) |
| 37 |
} |
| 38 |
|
| 39 |
// GeoTile for the GeoPoint at the given zoom level. |
| 40 |
func (gp GeoPoint) GeoTile(zoom int) GeoTile { |
| 41 |
latRad := Degrees(gp.Lat).Radians() |
| 42 |
n := math.Pow(2, float64(zoom)) |
| 43 |
|
| 44 |
return GeoTile{ |
| 45 |
Zoom: zoom, |
| 46 |
X: int(n * (float64(gp.Lon) + 180) / 360), |
| 47 |
Y: int((1.0 - math.Log(math.Tan(latRad)+(1/math.Cos(latRad)))/Pi) / 2.0 * n), |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// NewGeoPointFromTileNumbers creates a new GeoPoint based on the given tile numbers. |
| 52 |
// https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Tile_numbers_to_lon..2Flat. |
| 53 |
func NewGeoPointFromTileNumbers(zoom, x, y int) GeoPoint { |
| 54 |
n := math.Pow(2, float64(zoom)) |
| 55 |
latRad := math.Atan(math.Sinh(Pi * (1 - (2 * float64(y) / n)))) |
| 56 |
|
| 57 |
return GP(latRad*180/Pi, (float64(x)/n*360)-180) |
| 58 |
} |
| 59 |
|
| 60 |
// GeoTiles is a slice of GeoTile. |
| 61 |
type GeoTiles []GeoTile |
| 62 |
|
| 63 |
// GeoTile consists of a Zoom level, X and Y values. |
| 64 |
type GeoTile struct { |
| 65 |
Zoom int |
| 66 |
X int |
| 67 |
Y int |
| 68 |
} |
| 69 |
|
| 70 |
// GT creates a new GeoTile. |
| 71 |
func GT(zoom, x, y int) GeoTile { |
| 72 |
return GeoTile{Zoom: zoom, X: x, Y: y} |
| 73 |
} |
| 74 |
|
| 75 |
// GeoPoint for the GeoTile. |
| 76 |
func (gt GeoTile) GeoPoint() GeoPoint { |
| 77 |
n := math.Pow(2, float64(gt.Zoom)) |
| 78 |
latRad := math.Atan(math.Sinh(Pi * (1 - (2 * float64(gt.Y) / n)))) |
| 79 |
|
| 80 |
return GP(latRad*180/Pi, (float64(gt.X)/n*360)-180) |
| 81 |
} |
| 82 |
|
| 83 |
// Vec returns the Vec for the GeoPoint in the GeoTile. |
| 84 |
func (gt GeoTile) Vec(gp GeoPoint, tileSize int) Vec { |
| 85 |
return gp.Vec(tileSize, gt.Zoom).Sub(gt.GeoPoint().Vec(tileSize, gt.Zoom)) |
| 86 |
} |
| 87 |
|
| 88 |
// Rawurl formats a URL string with Zoom, X and Y. |
| 89 |
func (gt GeoTile) Rawurl(format string) string { |
| 90 |
return Sprintf(format, gt.Zoom, gt.X, gt.Y) |
| 91 |
} |
| 92 |
|
| 93 |
// AddXY adds x and y. |
| 94 |
func (gt GeoTile) AddXY(x, y int) GeoTile { |
| 95 |
return GT(gt.Zoom, gt.X+x, gt.Y+y) |
| 96 |
} |
| 97 |
|
| 98 |
// Neighbors returns the neighboring tiles. |
| 99 |
func (gt GeoTile) Neighbors() GeoTiles { |
| 100 |
return GeoTiles{ |
| 101 |
gt.N(), |
| 102 |
gt.NE(), |
| 103 |
gt.E(), |
| 104 |
gt.SE(), |
| 105 |
gt.S(), |
| 106 |
gt.SW(), |
| 107 |
gt.W(), |
| 108 |
gt.NW(), |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// N is the tile to the north. |
| 113 |
func (gt GeoTile) N() GeoTile { |
| 114 |
if gt.Zoom > 0 && gt.Y > 0 { |
| 115 |
gt.Y-- |
| 116 |
} |
| 117 |
|
| 118 |
return gt |
| 119 |
} |
| 120 |
|
| 121 |
// NE is the tile to the northeast. |
| 122 |
func (gt GeoTile) NE() GeoTile { |
| 123 |
if gt.Zoom > 0 { |
| 124 |
if gt.Y > 0 { |
| 125 |
gt.Y-- |
| 126 |
} |
| 127 |
|
| 128 |
gt.X++ |
| 129 |
} |
| 130 |
|
| 131 |
return gt |
| 132 |
} |
| 133 |
|
| 134 |
// E is the tile to the east. |
| 135 |
func (gt GeoTile) E() GeoTile { |
| 136 |
if gt.Zoom > 0 { |
| 137 |
gt.X++ |
| 138 |
} |
| 139 |
|
| 140 |
return gt |
| 141 |
} |
| 142 |
|
| 143 |
// SE is the tile to the southeast. |
| 144 |
func (gt GeoTile) SE() GeoTile { |
| 145 |
if gt.Zoom > 0 { |
| 146 |
gt.X++ |
| 147 |
gt.Y++ |
| 148 |
} |
| 149 |
|
| 150 |
return gt |
| 151 |
} |
| 152 |
|
| 153 |
// S is the tile to the south. |
| 154 |
func (gt GeoTile) S() GeoTile { |
| 155 |
if gt.Zoom > 0 { |
| 156 |
gt.Y++ |
| 157 |
} |
| 158 |
|
| 159 |
return gt |
| 160 |
} |
| 161 |
|
| 162 |
// SW is the tile to the southwest. |
| 163 |
func (gt GeoTile) SW() GeoTile { |
| 164 |
if gt.Zoom > 0 { |
| 165 |
if gt.X > 0 { |
| 166 |
gt.X-- |
| 167 |
} |
| 168 |
|
| 169 |
gt.Y++ |
| 170 |
} |
| 171 |
|
| 172 |
return gt |
| 173 |
} |
| 174 |
|
| 175 |
// W is the tile to the west. |
| 176 |
func (gt GeoTile) W() GeoTile { |
| 177 |
if gt.Zoom > 0 && gt.X > 0 { |
| 178 |
gt.X-- |
| 179 |
} |
| 180 |
|
| 181 |
return gt |
| 182 |
} |
| 183 |
|
| 184 |
// NW is the tile to the northwest. |
| 185 |
func (gt GeoTile) NW() GeoTile { |
| 186 |
if gt.Zoom > 0 { |
| 187 |
if gt.Y > 0 { |
| 188 |
gt.Y-- |
| 189 |
} |
| 190 |
|
| 191 |
if gt.X > 0 { |
| 192 |
gt.X-- |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
return gt |
| 197 |
} |
| 198 |
|
| 199 |
// GetImage for the tile. |
| 200 |
func (gt GeoTile) GetImage(format string) (image.Image, error) { |
| 201 |
return GetImage(gt.Rawurl(format)) |
| 202 |
} |
| 203 |
|
| 204 |
// Draw the tile on dst. |
| 205 |
func (gt GeoTile) Draw(dst draw.Image, gp GeoPoint, src image.Image) { |
| 206 |
|
| 207 |
Draw(dst, gt.Bounds(dst, gp, src.Bounds().Dx()), src) |
| 208 |
} |
| 209 |
|
| 210 |
// Bounds returns an image.Rectangle for the GeoTile based on the dst, gp and tileSize. |
| 211 |
func (gt GeoTile) Bounds(dst image.Image, gp GeoPoint, tileSize int) image.Rectangle { |
| 212 |
c := BoundsCenter(dst.Bounds()) |
| 213 |
|
| 214 |
return dst.Bounds().Add(c.Pt()).Sub(gp.In(gt, tileSize).Pt()) |
| 215 |
} |
| 216 |
|
| 217 |
// GeoTileServer represents a tile server. |
| 218 |
type GeoTileServer struct { |
| 219 |
Format string |
| 220 |
} |
| 221 |
|
| 222 |
// GTS creates a GeoTileServer. |
| 223 |
func GTS(format string) GeoTileServer { |
| 224 |
return GeoTileServer{Format: format} |
| 225 |
} |
| 226 |
|
| 227 |
// GetImage for the given GeoTile from the tile server. |
| 228 |
func (gts GeoTileServer) GetImage(gt GeoTile) (image.Image, error) { |
| 229 |
return gt.GetImage(gts.Format) |
| 230 |
} |
| 231 |
|
| 232 |
// DrawTileAndNeighbors on dst. |
| 233 |
func (gts GeoTileServer) DrawTileAndNeighbors(dst draw.Image, gt GeoTile, gp GeoPoint) error { |
| 234 |
if err := gts.DrawTile(dst, gt, gp); err != nil { |
| 235 |
return nil |
| 236 |
} |
| 237 |
|
| 238 |
return gts.DrawNeighbors(dst, gt, gp) |
| 239 |
} |
| 240 |
|
| 241 |
// DrawTile on dst. |
| 242 |
func (gts GeoTileServer) DrawTile(dst draw.Image, gt GeoTile, gp GeoPoint) error { |
| 243 |
src, err := gts.GetImage(gt) |
| 244 |
if err != nil { |
| 245 |
return err |
| 246 |
} |
| 247 |
|
| 248 |
gt.Draw(dst, gp, src) |
| 249 |
|
| 250 |
return nil |
| 251 |
} |
| 252 |
|
| 253 |
// DrawNeighbors on dst. |
| 254 |
func (gts GeoTileServer) DrawNeighbors(dst draw.Image, gt GeoTile, gp GeoPoint) error { |
| 255 |
for _, n := range gt.Neighbors() { |
| 256 |
if err := gts.DrawTile(dst, n, gp); err != nil { |
| 257 |
return err |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
return nil |
| 262 |
} |