Fix Circle type

This commit is contained in:
tidwall 2018-10-16 08:55:26 -07:00
parent 6257ddba78
commit 1544f2914d
8 changed files with 38 additions and 6 deletions

4
Gopkg.lock generated
View File

@ -227,7 +227,7 @@
[[projects]]
branch = "master"
digest = "1:157eb52179752d4d88f1049aa6c3e4954d6796af5f6bcd54b5ab40f8637805df"
digest = "1:62d3906c154a5fd594c51e434c8d38a3f5ae207f0396bf733fcfbe0ea4796430"
name = "github.com/tidwall/geojson"
packages = [
".",
@ -235,7 +235,7 @@
"geometry",
]
pruneopts = ""
revision = "8ff3ef500c61617c9f325603cf40863ca7086a1d"
revision = "996bb585989456cda9c8302e89c2fe6e82cc61bf"
[[projects]]
digest = "1:211773b67c5594aa92b1e8389c59558fa4927614507ea38237265e00c0ba6b81"

View File

@ -57,7 +57,7 @@ func (g *Circle) AppendJSON(dst []byte) []byte {
dst = strconv.AppendFloat(dst, g.center.Y, 'f', -1, 64)
dst = append(dst, `]},"properties":{"type":"Circle","radius":`...)
dst = strconv.AppendFloat(dst, g.meters, 'f', -1, 64)
dst = append(dst, `",radius_units":"m"}}`...)
dst = append(dst, `,"radius_units":"m"}}`...)
return dst
}

View File

@ -3,6 +3,10 @@ package geojson
import "testing"
func TestCircle(t *testing.T) {
expectJSON(t,
`{"type":"Feature","geometry":{"type":"Point","coordinates":[-112,33]},"properties":{"type":"Circle","radius":"5000"}}`,
`{"type":"Feature","geometry":{"type":"Point","coordinates":[-112,33]},"properties":{"type":"Circle","radius":5000,"radius_units":"m"}}`,
)
g, err := Parse(`{
"type":"Feature",
"geometry":{"type":"Point","coordinates":[-112.2693,33.5123]},
@ -14,5 +18,5 @@ func TestCircle(t *testing.T) {
if err != nil {
t.Fatal(err)
}
println(g.Contains(PO(-112.26, 33.51)))
expect(t, g.Contains(PO(-112.26, 33.51)))
}

View File

@ -97,7 +97,7 @@ func TestCollectionBostonSubset(t *testing.T) {
func TestCollectionUSASubset(t *testing.T) {
data, err := ioutil.ReadFile("test_files/usa.geojson")
if err == nil {
if err != nil {
fmt.Printf("test_files/usa.geojson missing\n")
return
}

View File

@ -17,6 +17,11 @@ func (rect Rect) Move(deltaX, deltaY float64) Rect {
}
}
// Index ...
func (rect Rect) Index() interface{} {
return nil
}
// Clockwise ...
func (rect Rect) Clockwise() bool {
return false

View File

@ -21,6 +21,10 @@ func TestRectMove(t *testing.T) {
expect(t, R(1, 2, 3, 4).Move(5, 6) == R(6, 8, 8, 10))
}
func TestRectIndex(t *testing.T) {
expect(t, (Rect{}).Index() == nil)
}
func TestRectNumPoints(t *testing.T) {
expect(t, R(0, 0, 10, 10).NumPoints() == 5)
}

View File

@ -24,6 +24,7 @@ type Series interface {
PointAt(index int) Point
SegmentAt(index int) Segment
Search(rect Rect, iter func(seg Segment, index int) bool)
Index() interface{}
}
func seriesCopyPoints(series Series) []Point {
@ -41,7 +42,7 @@ type baseSeries struct {
convex bool // points create a convex shape
rect Rect // minumum bounding rectangle
points []Point // original points
tree *d2.BoxTree // segment tree
tree *d2.BoxTree // segment tree.
}
// makeSeries returns a processed baseSeries.
@ -62,6 +63,14 @@ func makeSeries(points []Point, copyPoints, closed bool, index int) baseSeries {
return series
}
// Index ...
func (series *baseSeries) Index() interface{} {
if series.tree == nil {
return nil
}
return series.tree
}
// Clockwise ...
func (series *baseSeries) Clockwise() bool {
return series.clockwise

View File

@ -34,6 +34,16 @@ func TestSeriesEmpty(t *testing.T) {
expect(t, series2.Empty())
}
func TestSeriesIndex(t *testing.T) {
series := makeSeries(nil, false, false, 0)
expect(t, series.Index() == nil)
series = makeSeries([]Point{
P(0, 0), P(10, 0), P(10, 10), P(0, 10), P(0, 0),
}, true, true, 1)
expect(t, series.Index() != nil)
}
func TestSeriesClockwise(t *testing.T) {
var series baseSeries
series = makeSeries([]Point{