Skip to content

Commit

Permalink
When appropriate changed []float64 to Coord in order to provide some …
Browse files Browse the repository at this point in the history
…semantics to the type.

An example of where this is useful when distinguishing between flatcoords and a coordinate since both types were []float64
  • Loading branch information
Jesse Eichar authored and Jesse Eichar committed Dec 8, 2015
1 parent 89c140a commit 72ef2cf
Show file tree
Hide file tree
Showing 31 changed files with 292 additions and 290 deletions.
24 changes: 12 additions & 12 deletions area_test.go
Expand Up @@ -24,19 +24,19 @@ func TestArea(t *testing.T) {
want: 0,
},
{
g: NewLinearRing(XY).MustSetCoords([][]float64{
g: NewLinearRing(XY).MustSetCoords([]Coord{
{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0},
}),
want: 1,
},
{
g: NewLinearRing(XY).MustSetCoords([][]float64{
g: NewLinearRing(XY).MustSetCoords([]Coord{
{0, 0}, {1, 1}, {1, 0}, {0, 0},
}),
want: -0.5,
},
{
g: NewLinearRing(XY).MustSetCoords([][]float64{
g: NewLinearRing(XY).MustSetCoords([]Coord{
{-3, -2}, {-1, 4}, {6, 1}, {3, 10}, {-4, 9}, {-3, -2},
}),
want: 60,
Expand All @@ -50,25 +50,25 @@ func TestArea(t *testing.T) {
want: 0,
},
{
g: NewPolygon(XY).MustSetCoords([][][]float64{
g: NewPolygon(XY).MustSetCoords([][]Coord{
{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}},
}),
want: 1,
},
{
g: NewPolygon(XY).MustSetCoords([][][]float64{
g: NewPolygon(XY).MustSetCoords([][]Coord{
{{0, 0}, {1, 1}, {1, 0}, {0, 0}},
}),
want: -0.5,
},
{
g: NewPolygon(XY).MustSetCoords([][][]float64{
g: NewPolygon(XY).MustSetCoords([][]Coord{
{{-3, -2}, {-1, 4}, {6, 1}, {3, 10}, {-4, 9}, {-3, -2}},
}),
want: 60,
},
{
g: NewPolygon(XY).MustSetCoords([][][]float64{
g: NewPolygon(XY).MustSetCoords([][]Coord{
{{-3, -2}, {-1, 4}, {6, 1}, {3, 10}, {-4, 9}, {-3, -2}},
{{0, 6}, {2, 6}, {2, 8}, {0, 8}, {0, 6}},
}),
Expand All @@ -79,31 +79,31 @@ func TestArea(t *testing.T) {
want: 0,
},
{
g: NewMultiPolygon(XY).MustSetCoords([][][][]float64{
g: NewMultiPolygon(XY).MustSetCoords([][][]Coord{
{
{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}},
},
}),
want: 1,
},
{
g: NewMultiPolygon(XY).MustSetCoords([][][][]float64{
g: NewMultiPolygon(XY).MustSetCoords([][][]Coord{
{
{{0, 0}, {1, 1}, {1, 0}, {0, 0}},
},
}),
want: -0.5,
},
{
g: NewMultiPolygon(XY).MustSetCoords([][][][]float64{
g: NewMultiPolygon(XY).MustSetCoords([][][]Coord{
{
{{-3, -2}, {-1, 4}, {6, 1}, {3, 10}, {-4, 9}, {-3, -2}},
},
}),
want: 60,
},
{
g: NewMultiPolygon(XY).MustSetCoords([][][][]float64{
g: NewMultiPolygon(XY).MustSetCoords([][][]Coord{
{
{{-3, -2}, {-1, 4}, {6, 1}, {3, 10}, {-4, 9}, {-3, -2}},
{{0, 6}, {2, 6}, {2, 8}, {0, 8}, {0, 6}},
Expand All @@ -112,7 +112,7 @@ func TestArea(t *testing.T) {
want: 56,
},
{
g: NewMultiPolygon(XY).MustSetCoords([][][][]float64{
g: NewMultiPolygon(XY).MustSetCoords([][][]Coord{
{
{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}},
},
Expand Down
6 changes: 3 additions & 3 deletions bounds.go
Expand Up @@ -9,14 +9,14 @@ import (

type Bounds struct {
layout Layout
min []float64
max []float64
min Coord
max Coord
}

// NewBounds creates a new Bounds.
func NewBounds(layout Layout) *Bounds {
stride := layout.Stride()
min, max := make([]float64, stride), make([]float64, stride)
min, max := make(Coord, stride), make(Coord, stride)
for i := 0; i < stride; i++ {
min[i], max[i] = math.Inf(1), math.Inf(-1)
}
Expand Down
20 changes: 10 additions & 10 deletions encoding/geojson/geojson.go
Expand Up @@ -119,7 +119,7 @@ func Decode(g *Geometry) (geom.T, error) {
}

func decodePoint(g *Geometry) (*geom.Point, error) {
coordinates, ok := g.Coordinates.([]float64)
coordinates, ok := g.Coordinates.(geom.Coord)
if !ok {
return nil, errors.New("geojson: coordinates is not a []float64")
}
Expand All @@ -131,7 +131,7 @@ func decodePoint(g *Geometry) (*geom.Point, error) {
}

func decodeLineString(g *Geometry) (*geom.LineString, error) {
coordinates, ok := g.Coordinates.([][]float64)
coordinates, ok := g.Coordinates.([]geom.Coord)
if !ok {
return nil, errors.New("geojson: coordinates is not a [][]float64")
}
Expand All @@ -143,7 +143,7 @@ func decodeLineString(g *Geometry) (*geom.LineString, error) {
}

func decodePolygon(g *Geometry) (*geom.Polygon, error) {
coordinates, ok := g.Coordinates.([][][]float64)
coordinates, ok := g.Coordinates.([][]geom.Coord)
if !ok {
return nil, errors.New("geojson: coordinates is not a [][][]float64")
}
Expand All @@ -155,7 +155,7 @@ func decodePolygon(g *Geometry) (*geom.Polygon, error) {
}

func decodeMultiPoint(g *Geometry) (*geom.MultiPoint, error) {
coordinates, ok := g.Coordinates.([][]float64)
coordinates, ok := g.Coordinates.([]geom.Coord)
if !ok {
return nil, errors.New("geojson: coordinates is not a [][]float64")
}
Expand All @@ -167,7 +167,7 @@ func decodeMultiPoint(g *Geometry) (*geom.MultiPoint, error) {
}

func decodeMultiLineString(g *Geometry) (*geom.MultiLineString, error) {
coordinates, ok := g.Coordinates.([][][]float64)
coordinates, ok := g.Coordinates.([][]geom.Coord)
if !ok {
return nil, errors.New("geojson: coordinates is not a [][][]float64")
}
Expand All @@ -179,7 +179,7 @@ func decodeMultiLineString(g *Geometry) (*geom.MultiLineString, error) {
}

func decodeMultiPolygon(g *Geometry) (*geom.MultiPolygon, error) {
coordinates, ok := g.Coordinates.([][][][]float64)
coordinates, ok := g.Coordinates.([][][]geom.Coord)
if !ok {
return nil, errors.New("geojson: coordinates is not a [][][][]float64")
}
Expand All @@ -190,7 +190,7 @@ func decodeMultiPolygon(g *Geometry) (*geom.MultiPolygon, error) {
return geom.NewMultiPolygon(layout).SetCoords(coordinates)
}

func guessLayout0(coords0 []float64) (geom.Layout, error) {
func guessLayout0(coords0 geom.Coord) (geom.Layout, error) {
switch n := len(coords0); n {
case 0, 1:
return geom.NoLayout, ErrDimensionalityTooLow(len(coords0))
Expand All @@ -205,23 +205,23 @@ func guessLayout0(coords0 []float64) (geom.Layout, error) {
}
}

func guessLayout1(coords1 [][]float64) (geom.Layout, error) {
func guessLayout1(coords1 []geom.Coord) (geom.Layout, error) {
if len(coords1) == 0 {
return DefaultLayout, nil
} else {
return guessLayout0(coords1[0])
}
}

func guessLayout2(coords2 [][][]float64) (geom.Layout, error) {
func guessLayout2(coords2 [][]geom.Coord) (geom.Layout, error) {
if len(coords2) == 0 {
return DefaultLayout, nil
} else {
return guessLayout1(coords2[0])
}
}

func guessLayout3(coords3 [][][][]float64) (geom.Layout, error) {
func guessLayout3(coords3 [][][]geom.Coord) (geom.Layout, error) {
if len(coords3) == 0 {
return DefaultLayout, nil
} else {
Expand Down
38 changes: 19 additions & 19 deletions encoding/geojson/geojson_test.go
Expand Up @@ -16,77 +16,77 @@ func TestGeometry(t *testing.T) {
g: geom.NewPoint(DefaultLayout),
geometry: &Geometry{
Type: "Point",
Coordinates: []float64{0, 0},
Coordinates: geom.Coord{0, 0},
},
},
{
g: geom.NewPoint(geom.XY).MustSetCoords([]float64{1, 2}),
g: geom.NewPoint(geom.XY).MustSetCoords(geom.Coord{1, 2}),
geometry: &Geometry{
Type: "Point",
Coordinates: []float64{1, 2},
Coordinates: geom.Coord{1, 2},
},
},
{
g: geom.NewPoint(geom.XYZ).MustSetCoords([]float64{1, 2, 3}),
g: geom.NewPoint(geom.XYZ).MustSetCoords(geom.Coord{1, 2, 3}),
geometry: &Geometry{
Type: "Point",
Coordinates: []float64{1, 2, 3},
Coordinates: geom.Coord{1, 2, 3},
},
},
{
g: geom.NewPoint(geom.XYZM).MustSetCoords([]float64{1, 2, 3, 4}),
g: geom.NewPoint(geom.XYZM).MustSetCoords(geom.Coord{1, 2, 3, 4}),
geometry: &Geometry{
Type: "Point",
Coordinates: []float64{1, 2, 3, 4},
Coordinates: geom.Coord{1, 2, 3, 4},
},
},
{
g: geom.NewLineString(DefaultLayout),
geometry: &Geometry{
Type: "LineString",
Coordinates: [][]float64{},
Coordinates: []geom.Coord{},
},
},
{
g: geom.NewLineString(geom.XY).MustSetCoords([][]float64{{1, 2}, {3, 4}}),
g: geom.NewLineString(geom.XY).MustSetCoords([]geom.Coord{{1, 2}, {3, 4}}),
geometry: &Geometry{
Type: "LineString",
Coordinates: [][]float64{{1, 2}, {3, 4}},
Coordinates: []geom.Coord{{1, 2}, {3, 4}},
},
},
{
g: geom.NewLineString(geom.XYZ).MustSetCoords([][]float64{{1, 2, 3}, {4, 5, 6}}),
g: geom.NewLineString(geom.XYZ).MustSetCoords([]geom.Coord{{1, 2, 3}, {4, 5, 6}}),
geometry: &Geometry{
Type: "LineString",
Coordinates: [][]float64{{1, 2, 3}, {4, 5, 6}},
Coordinates: []geom.Coord{{1, 2, 3}, {4, 5, 6}},
},
},
{
g: geom.NewLineString(geom.XYZM).MustSetCoords([][]float64{{1, 2, 3, 4}, {5, 6, 7, 8}}),
g: geom.NewLineString(geom.XYZM).MustSetCoords([]geom.Coord{{1, 2, 3, 4}, {5, 6, 7, 8}}),
geometry: &Geometry{
Type: "LineString",
Coordinates: [][]float64{{1, 2, 3, 4}, {5, 6, 7, 8}},
Coordinates: []geom.Coord{{1, 2, 3, 4}, {5, 6, 7, 8}},
},
},
{
g: geom.NewPolygon(DefaultLayout),
geometry: &Geometry{
Type: "Polygon",
Coordinates: [][][]float64{},
Coordinates: [][]geom.Coord{},
},
},
{
g: geom.NewPolygon(geom.XY).MustSetCoords([][][]float64{{{1, 2}, {3, 4}, {5, 6}, {1, 2}}}),
g: geom.NewPolygon(geom.XY).MustSetCoords([][]geom.Coord{{{1, 2}, {3, 4}, {5, 6}, {1, 2}}}),
geometry: &Geometry{
Type: "Polygon",
Coordinates: [][][]float64{{{1, 2}, {3, 4}, {5, 6}, {1, 2}}},
Coordinates: [][]geom.Coord{{{1, 2}, {3, 4}, {5, 6}, {1, 2}}},
},
},
{
g: geom.NewPolygon(geom.XYZ).MustSetCoords([][][]float64{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 2, 3}}}),
g: geom.NewPolygon(geom.XYZ).MustSetCoords([][]geom.Coord{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 2, 3}}}),
geometry: &Geometry{
Type: "Polygon",
Coordinates: [][][]float64{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 2, 3}}},
Coordinates: [][]geom.Coord{{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 2, 3}}},
},
},
// FIXME Add MultiPoint tests
Expand Down
12 changes: 6 additions & 6 deletions encoding/hex/hex_test.go
Expand Up @@ -15,27 +15,27 @@ func Test(t *testing.T) {
xdr string
}{
{
g: geom.NewPoint(geom.XY).MustSetCoords([]float64{1, 2}),
g: geom.NewPoint(geom.XY).MustSetCoords(geom.Coord{1, 2}),
ndr: "0101000000000000000000f03f0000000000000040",
},
{
g: geom.NewPoint(geom.XY).MustSetCoords([]float64{2, 4}),
g: geom.NewPoint(geom.XY).MustSetCoords(geom.Coord{2, 4}),
xdr: "000000000140000000000000004010000000000000",
},
{
g: geom.NewLineString(geom.XY).MustSetCoords([][]float64{
g: geom.NewLineString(geom.XY).MustSetCoords([]geom.Coord{
{-71.160281, 42.258729}, {-71.160837, 42.259113}, {-71.161144, 42.25932},
}),
ndr: "010200000003000000e44a3d0b42ca51c06ec328081e21454027bf45274bca51c0f67b629d2a214540957cec2e50ca51c07099d36531214540",
},
{
g: geom.NewMultiLineString(geom.XY).MustSetCoords([][][]float64{
g: geom.NewMultiLineString(geom.XY).MustSetCoords([][]geom.Coord{
{{-71.160281, 42.258729}, {-71.160837, 42.259113}, {-71.161144, 42.25932}},
}),
ndr: "010500000001000000010200000003000000e44a3d0b42ca51c06ec328081e21454027bf45274bca51c0f67b629d2a214540957cec2e50ca51c07099d36531214540",
},
{
g: geom.NewPolygon(geom.XY).MustSetCoords([][][]float64{
g: geom.NewPolygon(geom.XY).MustSetCoords([][]geom.Coord{
{
{-71.1776585052917, 42.3902909739571},
{-71.1776820268866, 42.3903701743239},
Expand All @@ -47,7 +47,7 @@ func Test(t *testing.T) {
ndr: "010300000001000000050000006285c7c15ecb51c0ed88fc0df531454028a46f245fcb51c009075ea6f731454047ded1e65dcb51c0781c510ef83145404871a7835dcb51c0ebdaee75f53145406285c7c15ecb51c0ed88fc0df5314540",
},
{
g: geom.NewMultiPolygon(geom.XY).MustSetCoords([][][][]float64{
g: geom.NewMultiPolygon(geom.XY).MustSetCoords([][][]geom.Coord{
{
{
{-71.1031880899493, 42.3152774590236},
Expand Down

0 comments on commit 72ef2cf

Please sign in to comment.