Skip to content

Commit

Permalink
add AnyInteresting function to Tags object
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach committed Oct 6, 2018
1 parent 2bdd701 commit 7a428b3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 13 deletions.
14 changes: 1 addition & 13 deletions osmgeojson/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ import (
"github.com/paulmach/osm/internal/mputil"
)

var uninterestingTags = map[string]bool{
"source": true,
"source_ref": true,
"source:ref": true,
"history": true,
"attribution": true,
"created_by": true,
"tiger:county": true,
"tiger:tlid": true,
"tiger:upload_uuid": true,
}

type context struct {
noID bool
noMeta bool
Expand Down Expand Up @@ -405,7 +393,7 @@ func hasInterestingTags(tags osm.Tags, ignore map[string]string) bool {

for _, tag := range tags {
k, v := tag.Key, tag.Value
if !uninterestingTags[k] &&
if !osm.UninterestingTags[k] &&
(ignore == nil || !(ignore[k] == "true" || ignore[k] == v)) {
return true
}
Expand Down
27 changes: 27 additions & 0 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ import (
"sync"
)

// UninterestingTags are boring tags. If an element only has
// these tags it does not usually need to be displayed.
// For example, if a node with just these tags is part of a way, it
// probably does not need its own icon along the way.
var UninterestingTags = map[string]bool{
"source": true,
"source_ref": true,
"source:ref": true,
"history": true,
"attribution": true,
"created_by": true,
"tiger:county": true,
"tiger:tlid": true,
"tiger:upload_uuid": true,
}

// Tag is a key+value item attached to osm nodes, ways and relations.
type Tag struct {
Key string `xml:"k,attr"`
Expand Down Expand Up @@ -38,6 +54,17 @@ func (ts Tags) Map() map[string]string {
return result
}

// AnyInteresting will return true if there is at last one interesting tag.
func (ts Tags) AnyInteresting() bool {
for _, t := range ts {
if !UninterestingTags[t.Key] {
return true
}
}

return false
}

// MarshalJSON allows the tags to be marshalled as a key/value object,
// as defined by the overpass osmjson.
func (ts Tags) MarshalJSON() ([]byte, error) {
Expand Down
38 changes: 38 additions & 0 deletions tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,44 @@ import (
"testing"
)

func TestTags_AnyInteresting(t *testing.T) {
cases := []struct {
name string
tags Tags
interesting bool
}{
{
name: "has interesting",
tags: Tags{
{Key: "building", Value: "yes"},
},
interesting: true,
},
{
name: "no tags",
tags: Tags{},
interesting: false,
},
{
name: "no interesting tags",
tags: Tags{
{Key: "source", Value: "whatever"},
{Key: "history", Value: "lots"},
},
interesting: false,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
v := tc.tags.AnyInteresting()
if v != tc.interesting {
t.Errorf("incorrect interesting: %v != %v", v, tc.interesting)
}
})
}
}

func TestTags_MarshalJSON(t *testing.T) {
data, err := Tags{}.MarshalJSON()
if err != nil {
Expand Down

0 comments on commit 7a428b3

Please sign in to comment.