This repository has been archived by the owner on Dec 26, 2017. It is now read-only.
forked from omniscale/imposm3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.go
125 lines (107 loc) · 2.94 KB
/
element.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package element
import (
"fmt"
"math"
"sort"
)
type Tags map[string]string
func (t *Tags) String() string {
return fmt.Sprintf("%v", (map[string]string)(*t))
}
type OSMElem struct {
Id int64 `json:"-"`
Tags Tags `json:"tags,omitempty"`
}
type Node struct {
OSMElem
Lat float64 `json:"lat"`
Long float64 `json:"lon"`
}
type Way struct {
OSMElem
Refs []int64 `json:"refs"`
Nodes []Node `json:"nodes,omitempty"`
}
func (w *Way) IsClosed() bool {
return len(w.Refs) >= 4 && w.Refs[0] == w.Refs[len(w.Refs)-1]
}
func (w *Way) TryClose(maxGap float64) bool {
return TryCloseWay(w.Refs, w.Nodes, maxGap)
}
// TryCloseWay closes the way if both end nodes are nearly identical.
// Returns true if it succeeds.
func TryCloseWay(refs []int64, nodes []Node, maxGap float64) bool {
if len(refs) < 4 {
return false
}
start, end := nodes[0], nodes[len(nodes)-1]
dist := math.Hypot(start.Lat-end.Lat, start.Long-end.Long)
if dist < maxGap {
refs[len(refs)-1] = refs[0]
nodes[len(nodes)-1] = nodes[0]
return true
}
return false
}
type MemberType int
const (
NODE MemberType = 0
WAY = 1
RELATION = 2
)
var MemberTypeValues = map[string]MemberType{
"node": NODE,
"way": WAY,
"relation": RELATION,
}
type Member struct {
Id int64 `json:"id"`
Type MemberType `json:"type"`
Role string `json:"role"`
Way *Way `json:"-"`
Node *Node `json:"-"`
Elem *OSMElem `json:"-"`
}
type Relation struct {
OSMElem
Members []Member `json:"members"`
}
type IdRefs struct {
Id int64
Refs []int64
}
func (idRefs *IdRefs) Add(ref int64) {
i := sort.Search(len(idRefs.Refs), func(i int) bool {
return idRefs.Refs[i] >= ref
})
if i < len(idRefs.Refs) && idRefs.Refs[i] >= ref {
if idRefs.Refs[i] > ref {
idRefs.Refs = append(idRefs.Refs, 0)
copy(idRefs.Refs[i+1:], idRefs.Refs[i:])
idRefs.Refs[i] = ref
} // else already inserted
} else {
idRefs.Refs = append(idRefs.Refs, ref)
}
}
func (idRefs *IdRefs) Delete(ref int64) {
i := sort.Search(len(idRefs.Refs), func(i int) bool {
return idRefs.Refs[i] >= ref
})
if i < len(idRefs.Refs) && idRefs.Refs[i] == ref {
idRefs.Refs = append(idRefs.Refs[:i], idRefs.Refs[i+1:]...)
}
}
// RelIdOffset is a constant we subtract from relation IDs
// to avoid conflicts with way and node IDs.
// Nodes, ways and relations have separate ID spaces in OSM, but
// we need unique IDs for updating and removing elements in diff mode.
// In a normal diff import relation IDs are negated to distinguish them
// from way IDs, because ways and relations can both be imported in the
// same polygon table.
// Nodes are only imported together with ways and relations in single table
// imports (see `type_mappings`). In this case we negate the way and
// relation IDs and aditionaly subtract RelIdOffset from the relation IDs.
// Ways will go from -0 to -100,000,000,000,000,000, relations from
// -100,000,000,000,000,000 down wards.
const RelIdOffset = -1e17