forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojection.go
68 lines (59 loc) · 1.56 KB
/
projection.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
57
58
59
60
61
62
63
64
65
66
67
68
package mvt
import (
"math"
"math/bits"
"github.com/Smadarl/orb"
"github.com/Smadarl/orb/internal/mercator"
"github.com/Smadarl/orb/maptile"
)
type projection struct {
ToTile orb.Projection
ToWGS84 orb.Projection
}
func newProjection(tile maptile.Tile, extent uint32) *projection {
if isPowerOfTwo(extent) {
// powers of two extents allows for some more simplicity
n := uint32(bits.TrailingZeros32(extent))
z := uint32(tile.Z) + n
minx := float64(tile.X << n)
miny := float64(tile.Y << n)
return &projection{
ToTile: func(p orb.Point) orb.Point {
x, y := mercator.ToPlanar(p[0], p[1], z)
return orb.Point{
math.Floor(x - minx),
math.Floor(y - miny),
}
},
ToWGS84: func(p orb.Point) orb.Point {
lon, lat := mercator.ToGeo(p[0]+minx+0.5, p[1]+miny+0.5, z)
return orb.Point{lon, lat}
},
}
}
return nonPowerOfTwoProjection(tile, extent)
}
func nonPowerOfTwoProjection(tile maptile.Tile, extent uint32) *projection {
// I really don't know why anyone would use a non-power of two extent,
// but technically it is supported.
e := float64(extent)
z := uint32(tile.Z)
minx := float64(tile.X)
miny := float64(tile.Y)
return &projection{
ToTile: func(p orb.Point) orb.Point {
x, y := mercator.ToPlanar(p[0], p[1], z)
return orb.Point{
math.Floor((x - minx) * e),
math.Floor((y - miny) * e),
}
},
ToWGS84: func(p orb.Point) orb.Point {
lon, lat := mercator.ToGeo((p[0]/e)+minx, (p[1]/e)+miny, z)
return orb.Point{lon, lat}
},
}
}
func isPowerOfTwo(n uint32) bool {
return (n & (n - 1)) == 0
}