Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Implements the TopoJSON specification:
https://github.com/mbostock/topojson-specification

Uses the GeoJSON implementation of paulmach:
https://github.com/paulmach/go.geojson
https://github.com/paulmach/orb/geojson

Large parts are a port of the canonical JavaScript implementation, big chunks
of the test suite are ported as well:
Expand Down
58 changes: 39 additions & 19 deletions bounds.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package topojson
import (
"math"

"github.com/paulmach/go.geojson"
"github.com/paulmach/orb"
)

func (t *Topology) bounds() {
Expand All @@ -20,25 +20,45 @@ func (t *Topology) bounds() {

}

func (t *Topology) boundGeometry(g *geojson.Geometry) {
switch g.Type {
case geojson.GeometryCollection:
for _, geom := range g.Geometries {
t.boundGeometry(geom)
func (t *Topology) boundGeometry(g orb.Geometry) {
switch c := g.(type) {
case orb.Point:
t.BBox(c.Bound())
case orb.MultiPoint:
t.BBox(c.Bound())
case orb.LineString:
t.BBox(c.Bound())
case orb.MultiLineString:
t.BBox(c.Bound())
case orb.Polygon:
t.BBox(c.Bound())
case orb.MultiPolygon:
t.BBox(c.Bound())
case orb.Collection:
t.BBox(c.Bound())
// for _, geo := range c {
// t.boundGeometry(geo)
// }
}
}

func (t *Topology) BBox(b orb.Bound) {
xx := []float64{b.Min[0], b.Max[0]}
yy := []float64{b.Min[1], b.Max[1]}
for _, x := range xx {
if x < t.BoundingBox[0] {
t.BoundingBox[0] = x
}
if x > t.BoundingBox[2] {
t.BoundingBox[2] = x
}
}
for _, y := range yy {
if y < t.BoundingBox[1] {
t.BoundingBox[1] = y
}
case geojson.GeometryPoint:
t.boundPoint(g.Point)
case geojson.GeometryMultiPoint:
t.boundPoints(g.MultiPoint)
case geojson.GeometryLineString:
t.boundPoints(g.LineString)
case geojson.GeometryMultiLineString:
t.boundMultiPoints(g.MultiLineString)
case geojson.GeometryPolygon:
t.boundMultiPoints(g.Polygon)
case geojson.GeometryMultiPolygon:
for _, poly := range g.MultiPolygon {
t.boundMultiPoints(poly)
if y > t.BoundingBox[3] {
t.BoundingBox[3] = y
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions bounds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import (
"testing"

"github.com/cheekybits/is"
geojson "github.com/paulmach/go.geojson"
orb "github.com/paulmach/orb"
geojson "github.com/paulmach/orb/geojson"
)

func TestBoundingBox(t *testing.T) {
is := is.New(t)

in := []*geojson.Feature{
NewTestFeature("foo", geojson.NewLineStringGeometry([][]float64{
{0, 0}, {1, 0}, {2, 0},
})),
NewTestFeature("bar", geojson.NewLineStringGeometry([][]float64{
{-1, 0}, {1, 0}, {-2, 3},
})),
NewTestFeature("foo", orb.LineString{
orb.Point{0, 0}, orb.Point{1, 0}, orb.Point{2, 0},
}),
NewTestFeature("bar", orb.LineString{
orb.Point{-1, 0}, orb.Point{1, 0}, orb.Point{-2, 3},
}),
}

topo := &Topology{input: in}
Expand Down
Loading