From 7f1634482152452a87b2709d30093cbb348e7139 Mon Sep 17 00:00:00 2001 From: Will Sewell Date: Tue, 1 Oct 2019 20:37:43 +0100 Subject: [PATCH] Make Bounds implement Object with a dummy ObjectID method Resolves #6 --- bounds.go | 6 ++++++ object.go | 1 + osmxml/scanner.go | 5 +++++ osmxml/scanner_test.go | 24 ++++++++++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/bounds.go b/bounds.go index b4c0e91..575f427 100644 --- a/bounds.go +++ b/bounds.go @@ -46,3 +46,9 @@ func (b *Bounds) ContainsNode(n *Node) bool { return true } + +// ObjectID always returns 0 because bounds do not have an id. +// It exists to implement the Object interface. +func (b *Bounds) ObjectID() ObjectID { + return 0 +} diff --git a/object.go b/object.go index 4bc2862..c400847 100644 --- a/object.go +++ b/object.go @@ -92,6 +92,7 @@ type Object interface { private() } +func (n *Bounds) private() {} func (n *Node) private() {} func (w *Way) private() {} func (r *Relation) private() {} diff --git a/osmxml/scanner.go b/osmxml/scanner.go index 3242a63..a93f969 100644 --- a/osmxml/scanner.go +++ b/osmxml/scanner.go @@ -83,6 +83,10 @@ Loop: s.next = nil switch strings.ToLower(se.Name.Local) { + case "bounds": + bounds := &osm.Bounds{} + err = s.decoder.DecodeElement(&bounds, &se) + s.next = bounds case "node": node := &osm.Node{} err = s.decoder.DecodeElement(&node, &se) @@ -122,6 +126,7 @@ Loop: // Object returns the most recent token generated by a call to Scan // as a new osm.Object. This interface is implemented by: +// *osm.Bounds // *osm.Node // *osm.Way // *osm.Relation diff --git a/osmxml/scanner_test.go b/osmxml/scanner_test.go index 6f96783..fd85aa7 100644 --- a/osmxml/scanner_test.go +++ b/osmxml/scanner_test.go @@ -144,6 +144,21 @@ func TestScanner_userNote(t *testing.T) { } } +func TestScanner_bounds(t *testing.T) { + r := boundsReader() + scanner := New(context.Background(), r) + defer scanner.Close() + + if v := scanner.Scan(); !v { + t.Fatalf("should read first scan: %v", scanner.Err()) + } + + b := scanner.Object().(*osm.Bounds) + if b.MinLat != 1 || b.MinLon != 2 || b.MaxLat != 3 || b.MaxLon != 4 { + t.Fatalf("did not scan correctly, got: %v", b) + } +} + func TestAndorra(t *testing.T) { f, err := os.Open("../testdata/andorra-latest.osm.bz2") if err != nil { @@ -232,6 +247,15 @@ func userNoteReader() io.Reader { return bytes.NewReader(data) } +func boundsReader() io.Reader { + data := []byte(` + + +`) + + return bytes.NewReader(data) +} + func changesetReader() io.Reader { data := []byte(`