Skip to content

Commit

Permalink
uint -> int
Browse files Browse the repository at this point in the history
  • Loading branch information
buck heroux committed May 4, 2016
1 parent b25092d commit 3874eee
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 38 deletions.
6 changes: 3 additions & 3 deletions coordinates.go
Expand Up @@ -18,14 +18,14 @@ func (c Coords) Equals(that Coords) bool {
}

// ToPixel gets the Pixel of the coord at the zoom level
func (c Coords) ToPixel(zoom uint) Pixel {
func (c Coords) ToPixel(zoom int) Pixel {
x := (c.Lon + 180) / 360.0
sinLat := math.Sin(c.Lat * math.Pi / 180.0)
y := 0.5 - math.Log((1+sinLat)/(1-sinLat))/(4*math.Pi)
size := float64(mapDimensions(zoom))
return Pixel{
X: uint(clip(x*size+0.5, 0, size-1)),
Y: uint(clip(y*size+0.5, 0, size-1)),
X: int(clip(x*size+0.5, 0, size-1)),
Y: int(clip(y*size+0.5, 0, size-1)),
Z: zoom,
}

Expand Down
2 changes: 1 addition & 1 deletion helpers.go
@@ -1,7 +1,7 @@
package tiles

// CoordinateToTile take float lat/lons and a zoom and return a tile
func CoordinateToTile(lat, lon float64, zoom uint) (Tile, TilePixel) {
func CoordinateToTile(lat, lon float64, zoom int) (Tile, TilePixel) {
coord := ClippedCoords(lat, lon)
pixel := coord.ToPixel(zoom)
return pixel.ToTile()
Expand Down
4 changes: 2 additions & 2 deletions pixel.go
Expand Up @@ -6,7 +6,7 @@ import (

// Pixel in a WGS84 Mercator map projection with a NW origin (0,0) of the projection
type Pixel struct {
X, Y, Z uint
X, Y, Z int
}

func (p Pixel) floatX() float64 {
Expand Down Expand Up @@ -44,7 +44,7 @@ func (p Pixel) ToTile() (tile Tile, offset TilePixel) {

// TilePixel is a pixel whose origin (0,0) is NW corner of Tile referenced in to tile field
type TilePixel struct {
X, Y uint
X, Y int
Tile *Tile
}

Expand Down
31 changes: 8 additions & 23 deletions tile.go
Expand Up @@ -10,22 +10,7 @@ import (

// Tile is a simple struct for holding the XYZ coordinates for use in mapping
type Tile struct {
X, Y, Z uint
}

// IntX is a helper that casts the given field to an int. Should be removed when the field is changed to an int.
func (t Tile) IntX() int {
return int(t.X)
}

// IntY is a helper that casts the given field to an int. Should be removed when the field is changed to an int.
func (t Tile) IntY() int {
return int(t.Y)
}

// IntZ is a helper that casts the given field to an int. Should be removed when the field is changed to an int.
func (t Tile) IntZ() int {
return int(t.Z)
X, Y, Z int
}

// ToPixel return the NW pixel of this tile
Expand All @@ -51,11 +36,11 @@ func (t Tile) QuadKey() string {
var qk bytes.Buffer
for i := t.Z; i > 0; i-- {
quad := 0
mask := 1 << (i - 1)
if (t.IntX() & mask) != 0 {
mask := 1 << uint(i-1)
if (t.X & mask) != 0 {
quad++
}
if (t.IntY() & mask) != 0 {
if (t.Y & mask) != 0 {
quad += 2
}
digit := strconv.Itoa(quad)
Expand All @@ -67,13 +52,13 @@ func (t Tile) QuadKey() string {
// TileFromQuadKey returns a tile that represents the given quadkey
// Panics on invalid keys
func TileFromQuadKey(quadkey string) (tile Tile) {
tile.Z = uint(len(quadkey))
tile.Z = len(quadkey)
for i := tile.Z; i > 0; i-- {
mask := uint(1 << (i - 1))
cur := len(quadkey) - int(i)
mask := 1 << uint(i-1)
cur := len(quadkey) - i
quad, err := strconv.Atoi(string(quadkey[cur]))
check(err)
switch uint(quad) {
switch quad {
case 0:
break
case 1:
Expand Down
10 changes: 5 additions & 5 deletions util.go
Expand Up @@ -15,7 +15,7 @@ const (
)

// TileSize is the size in pixels of each tile. It can be tuned at the package level.
var TileSize uint = 256
var TileSize = 256

// if val is outside of min-max range, clip it to min or max
// panic if min > max
Expand All @@ -28,15 +28,15 @@ func clip(val, min, max float64) float64 {

// Gets the size of the x, y dimensions in pixels at the given zoom level
// x == y since the map is a square
func mapDimensions(zoom uint) uint {
func mapDimensions(zoom int) int {
//TODO panic outside of zoom bounds
return TileSize << zoom
return TileSize << uint(zoom)
}

// Gets the ground resoultion (meters/pixel) of the map at the lat and zoom
// TODO remove if unused
/*
func grndRes(lat float64, zoom uint) float64 {
func grndRes(lat float64, zoom int) float64 {
lat = clip(lat, MinLat, MaxLat)
dim := float64(mapDimensions(zoom))
return math.Cos(lat*math.Pi/180) * 2 * math.Pi * EarthRadiusM / dim
Expand All @@ -46,7 +46,7 @@ func grndRes(lat float64, zoom uint) float64 {
// Gets the map scale at the lat, zoom & screen DPI expressed as the denominator N of the ratio 1 : N.
// TODO remove if unused
/*
func mapScale(lat float64, zoom, dpi uint) float64 {
func mapScale(lat float64, zoom, dpi int) float64 {
d := float64(dpi)
return grndRes(lat, zoom) * d / 0.0254
}
Expand Down
8 changes: 4 additions & 4 deletions util_test.go
Expand Up @@ -23,7 +23,7 @@ func TestClip(t *testing.T) {

func TestMapDimensions(t *testing.T) {
mapDimTests := []struct {
zoom, out uint
zoom, out int
}{
{0, 256},
{1, 512},
Expand All @@ -42,7 +42,7 @@ func TestMapDimensions(t *testing.T) {
//TODO assert neither of these are used and remove
func TestGroundRes(t *testing.T) {
lat := 40.0
var zoom uint = 7
var zoom int = 7
res := 936.86657226219847
if out := grndRes(lat, zoom); !floatEquals(out, res) {
t.Errorf("grndRes(%v, %v) -> %v not %v", lat, zoom, res, out)
Expand All @@ -51,8 +51,8 @@ func TestGroundRes(t *testing.T) {
func TestMapScale(t *testing.T) {
lat := 40.0
var zoom uint = 7
var dpi uint = 96
var zoom int = 7
var dpi int = 96
scale := 3540913.0290224836
if out := mapScale(lat, zoom, dpi); !floatEquals(out, scale) {
t.Errorf("mapScale(%v, %v, %v) -> %v not %v", lat, zoom, dpi, scale, out)
Expand Down

0 comments on commit 3874eee

Please sign in to comment.