From 3874eee38efecb882e1c19e68bed5a148bf508ef Mon Sep 17 00:00:00 2001 From: buck heroux Date: Wed, 4 May 2016 11:51:23 -0400 Subject: [PATCH] uint -> int --- coordinates.go | 6 +++--- helpers.go | 2 +- pixel.go | 4 ++-- tile.go | 31 ++++++++----------------------- util.go | 10 +++++----- util_test.go | 8 ++++---- 6 files changed, 23 insertions(+), 38 deletions(-) diff --git a/coordinates.go b/coordinates.go index 722ea09..b36897b 100644 --- a/coordinates.go +++ b/coordinates.go @@ -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, } diff --git a/helpers.go b/helpers.go index a4add5a..b005900 100644 --- a/helpers.go +++ b/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() diff --git a/pixel.go b/pixel.go index 1219470..9d77c76 100644 --- a/pixel.go +++ b/pixel.go @@ -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 { @@ -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 } diff --git a/tile.go b/tile.go index 297ee9d..b2ad60d 100644 --- a/tile.go +++ b/tile.go @@ -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 @@ -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) @@ -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: diff --git a/util.go b/util.go index c9ba928..01d084e 100644 --- a/util.go +++ b/util.go @@ -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 @@ -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 @@ -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 } diff --git a/util_test.go b/util_test.go index 691ff57..868736c 100644 --- a/util_test.go +++ b/util_test.go @@ -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}, @@ -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) @@ -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)