Skip to content

Commit 54c5cd3

Browse files
author
buck heroux
committed
coords -> coordinate
1 parent d125e11 commit 54c5cd3

File tree

8 files changed

+44
-40
lines changed

8 files changed

+44
-40
lines changed

coordinates.go renamed to coordinate.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ import (
55
"math"
66
)
77

8-
// Coords is a simple struct for hold WGS-84 Lat Lon coordinates in degrees
9-
type Coords struct {
8+
// Coordinate is a simple struct for hold WGS-84 Lat Lon coordinates in degrees
9+
type Coordinate struct {
1010
Lat, Lon float64
1111
}
1212

1313
// Equals checks if these coords are equal avoiding some float precision
14-
func (c Coords) Equals(that Coords) bool {
14+
func (c Coordinate) Equals(that Coordinate) bool {
1515
eq := floatEquals(c.Lat, that.Lat)
1616
eq = eq && floatEquals(c.Lon, that.Lon)
1717
return eq
1818
}
1919

2020
// ToPixel gets the Pixel of the coord at the zoom level
21-
func (c Coords) ToPixel(zoom int) Pixel {
21+
func (c Coordinate) ToPixel(zoom int) Pixel {
2222
x := (c.Lon + 180) / 360.0
2323
sinLat := math.Sin(c.Lat * math.Pi / 180.0)
2424
y := 0.5 - math.Log((1+sinLat)/(1-sinLat))/(4*math.Pi)
@@ -31,13 +31,14 @@ func (c Coords) ToPixel(zoom int) Pixel {
3131

3232
}
3333

34-
func (c Coords) String() string {
34+
func (c Coordinate) String() string {
3535
return fmt.Sprintf("(%v, %v)", c.Lat, c.Lon)
3636
}
3737

3838
// ClippedCoords that have been clipped to Max/Min Lat/Lon
39-
func ClippedCoords(lat, lon float64) Coords {
40-
return Coords{
39+
// This can be used as a constructor to assert bad values will be clipped
40+
func ClippedCoords(lat, lon float64) Coordinate {
41+
return Coordinate{
4142
Lat: clip(lat, MinLat, MaxLat),
4243
Lon: clip(lon, MinLon, MaxLon),
4344
}

coordinates_test.go renamed to coordinate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66

77
func TestToPixel(t *testing.T) {
88
pixelTests := []struct {
9-
coords Coords
9+
coords Coordinate
1010
pixel Pixel
1111
}{
1212
{ClippedCoords(40.0, -105.0), Pixel{6827, 12405, 7}},

index.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ func (idx *TileIndex) TileRange(zmin, zmax int) <-chan Tile {
3232
n := idx.keys[i+1].qk
3333
for z := zmin; z <= zmax && z <= len(q); z++ {
3434
if !strings.HasPrefix(n, q[:z]) {
35-
tiles <- TileFromQuadKey(q[:z])
35+
tiles <- FromQuadKey(q[:z])
3636
}
3737
}
3838
}
3939
q := idx.keys[len(idx.keys)-1].qk
4040
for z := zmin; z <= zmax && z <= len(q); z++ {
41-
tiles <- TileFromQuadKey(q[:z])
41+
tiles <- FromQuadKey(q[:z])
4242
}
4343
}()
4444
return tiles

index_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestTileIndexSimple(t *testing.T) {
1818
}
1919
idx := &TileIndex{}
2020
for i, qk := range qks {
21-
tile := TileFromQuadKey(qk)
21+
tile := FromQuadKey(qk)
2222
idx.Add(tile, i)
2323
}
2424
for tile := range idx.TileRange(1, 2) {
@@ -28,9 +28,9 @@ func TestTileIndexSimple(t *testing.T) {
2828

2929
func TestTileIndex(t *testing.T) {
3030
idx := &TileIndex{}
31-
esb := CoordinateToTile(40.7484, -73.9857, 18)
32-
sol := CoordinateToTile(40.6892, -74.0445, 18)
33-
bbn := CoordinateToTile(51.5007, -0.1246, 18)
31+
esb := FromCoordinate(40.7484, -73.9857, 18)
32+
sol := FromCoordinate(40.6892, -74.0445, 18)
33+
bbn := FromCoordinate(51.5007, -0.1246, 18)
3434
idx.Add(esb, "EmpireStateBuilding")
3535
idx.Add(sol, "StatueOfLiberty")
3636
idx.Add(bbn, "BigBen")
@@ -51,9 +51,9 @@ func TestTileIndex(t *testing.T) {
5151

5252
func ExampleTileIndex() {
5353
idx := &TileIndex{}
54-
esb := CoordinateToTile(40.7484, -73.9857, 18)
55-
sol := CoordinateToTile(40.6892, -74.0445, 18)
56-
bbn := CoordinateToTile(51.5007, -0.1246, 18)
54+
esb := FromCoordinate(40.7484, -73.9857, 18)
55+
sol := FromCoordinate(40.6892, -74.0445, 18)
56+
bbn := FromCoordinate(51.5007, -0.1246, 18)
5757
idx.Add(esb, "EmpireStateBuilding")
5858
idx.Add(sol, "StatueOfLiberty")
5959
idx.Add(bbn, "BigBen")

pixel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (p Pixel) floatY() float64 {
1818
}
1919

2020
// ToCoords converts to WGS84 coordaintes
21-
func (p Pixel) ToCoords() Coords {
21+
func (p Pixel) ToCoords() Coordinate {
2222
size := float64(mapDimensions(p.Z))
2323
x := (clip(p.floatX(), 0, size-1) / size) - 0.5
2424
y := 0.5 - (clip(p.floatY(), 0, size-1) / size)
@@ -48,7 +48,7 @@ type TilePixel struct {
4848
Tile *Tile
4949
}
5050

51-
func (p TilePixel) toCoords() Coords {
51+
func (p TilePixel) toCoords() Coordinate {
5252
panic("TilePixel.ToCoords() Not Implemented")
5353
//return Coords{}
5454
}

pixel_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
func TestPixelToCoords(t *testing.T) {
88
coordTests := []struct {
99
pixel Pixel
10-
coords Coords
10+
coords Coordinate
1111
}{
12-
{Pixel{6827, 12405, 7}, Coords{40.002372, -104.996338}},
12+
{Pixel{6827, 12405, 7}, Coordinate{40.002372, -104.996338}},
1313
}
1414
errf := "Pixel%+v: %+v -> %+v"
1515
for _, test := range coordTests {

tile.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ func (t Tile) QuadKey() string {
4949
return qk.String()
5050
}
5151

52-
// TileFromQuadKey returns a tile that represents the given quadkey
52+
// FromQuadKey returns a tile that represents the given quadkey
5353
// Panics on invalid keys
54-
func TileFromQuadKey(quadkey string) (tile Tile) {
54+
func FromQuadKey(quadkey string) (tile Tile) {
5555
tile.Z = len(quadkey)
5656
for i := tile.Z; i > 0; i-- {
5757
mask := 1 << uint(i-1)
@@ -78,10 +78,11 @@ func TileFromQuadKey(quadkey string) (tile Tile) {
7878
return
7979
}
8080

81-
// CoordinateToTile take float lat/lons and a zoom and return a tile
82-
func CoordinateToTile(lat, lon float64, zoom int) Tile {
83-
coord := ClippedCoords(lat, lon)
84-
pixel := coord.ToPixel(zoom)
85-
tile, _ := pixel.ToTile()
86-
return tile
81+
// FromCoordinate take float lat/lons and a zoom and return a tile
82+
// Clips the coordinates if they are outside of Min/MaxLat/Lon
83+
func FromCoordinate(lat, lon float64, zoom int) Tile {
84+
c := ClippedCoords(lat, lon)
85+
p := c.ToPixel(zoom)
86+
t, _ := p.ToTile()
87+
return t
8788
}

tile_test.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
package tiles
1+
package tiles_test
22

33
import (
44
"fmt"
55
"testing"
6+
7+
"github.com/buckhx/tiles"
68
)
79

810
func TestTileToPixel(t *testing.T) {
911
tileTests := []struct {
10-
tile Tile
11-
pixel Pixel
12+
tile tiles.Tile
13+
pixel tiles.Pixel
1214
}{
13-
{Tile{26, 48, 7}, Pixel{6656, 12288, 7}},
15+
{tiles.Tile{X: 26, Y: 48, Z: 7}, tiles.Pixel{X: 6656, Y: 12288, Z: 7}},
1416
//{Tile{26, 48, 7}, Pixel{6827, 12405, 7}},
1517
}
1618
errf := "Tile%+v: %+v -> %+v"
@@ -24,10 +26,10 @@ func TestTileToPixel(t *testing.T) {
2426

2527
func TestTileToQuadkey(t *testing.T) {
2628
tileTests := []struct {
27-
tile Tile
29+
tile tiles.Tile
2830
quadkey string
2931
}{
30-
{Tile{26, 48, 7}, "0231010"},
32+
{tiles.Tile{X: 26, Y: 48, Z: 7}, "0231010"},
3133
}
3234
errf := "Tile%+v: %+v -> %+v"
3335
for _, test := range tileTests {
@@ -41,22 +43,22 @@ func TestTileToQuadkey(t *testing.T) {
4143
func TestTileFromQuadkey(t *testing.T) {
4244
tileTests := []struct {
4345
quadkey string
44-
tile Tile
46+
tile tiles.Tile
4547
}{
46-
{"0231010", Tile{26, 48, 7}},
48+
{"0231010", Tile{X: 26, Y: 48, Z: 7}},
4749
}
4850
errf := "QuadKey%+v: %+v -> %+v"
4951
for _, test := range tileTests {
50-
tile := TileFromQuadKey(test.quadkey)
52+
tile := tiles.FromQuadKey(test.quadkey)
5153
if tile != test.tile {
5254
t.Errorf(errf, test.quadkey, test.tile, tile)
5355
}
5456
}
5557
}
5658

57-
func ExampleCoordinateToTile() {
59+
func ExampleFromCoordinate() {
5860
esbLat := 40.7484
5961
esbLon := -73.9857
60-
tile := CoordinateToTile(esbLat, esbLon, 18)
62+
tile := tiles.FromCoordinate(esbLat, esbLon, 18)
6163
fmt.Println(tile)
6264
}

0 commit comments

Comments
 (0)