2013-01-28 10:55:34 +04:00
|
|
|
package element
|
|
|
|
|
2013-05-15 11:49:38 +04:00
|
|
|
import (
|
2014-08-19 15:17:25 +04:00
|
|
|
"fmt"
|
2014-10-22 15:36:06 +04:00
|
|
|
"math"
|
2013-08-06 19:22:31 +04:00
|
|
|
"sort"
|
2013-05-15 11:49:38 +04:00
|
|
|
)
|
|
|
|
|
2013-01-28 10:55:34 +04:00
|
|
|
type Tags map[string]string
|
|
|
|
|
2014-08-19 15:17:25 +04:00
|
|
|
func (t *Tags) String() string {
|
|
|
|
return fmt.Sprintf("%v", (map[string]string)(*t))
|
|
|
|
}
|
|
|
|
|
2013-01-28 10:55:34 +04:00
|
|
|
type OSMElem struct {
|
2015-04-30 10:42:49 +03:00
|
|
|
Id int64 `json:"-"`
|
|
|
|
Tags Tags `json:"tags,omitempty"`
|
2013-01-28 10:55:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Node struct {
|
2013-04-03 00:07:27 +04:00
|
|
|
OSMElem
|
2013-08-02 12:08:01 +04:00
|
|
|
Lat float64 `json:"lat"`
|
|
|
|
Long float64 `json:"lon"`
|
2013-01-28 10:55:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Way struct {
|
2013-04-03 00:07:27 +04:00
|
|
|
OSMElem
|
2013-08-02 12:08:01 +04:00
|
|
|
Refs []int64 `json:"refs"`
|
|
|
|
Nodes []Node `json:"nodes,omitempty"`
|
2013-05-15 11:49:38 +04:00
|
|
|
}
|
|
|
|
|
2013-05-13 17:19:39 +04:00
|
|
|
func (w *Way) IsClosed() bool {
|
|
|
|
return len(w.Refs) >= 4 && w.Refs[0] == w.Refs[len(w.Refs)-1]
|
|
|
|
}
|
|
|
|
|
2014-10-22 15:36:06 +04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2013-01-28 10:55:34 +04:00
|
|
|
type MemberType int
|
|
|
|
|
|
|
|
const (
|
2013-04-03 00:07:27 +04:00
|
|
|
NODE MemberType = 0
|
|
|
|
WAY = 1
|
|
|
|
RELATION = 2
|
2013-01-28 10:55:34 +04:00
|
|
|
)
|
|
|
|
|
2013-05-31 17:49:22 +04:00
|
|
|
var MemberTypeValues = map[string]MemberType{
|
|
|
|
"node": NODE,
|
|
|
|
"way": WAY,
|
|
|
|
"relation": RELATION,
|
|
|
|
}
|
|
|
|
|
2013-01-28 10:55:34 +04:00
|
|
|
type Member struct {
|
2013-08-02 12:08:01 +04:00
|
|
|
Id int64 `json:"id"`
|
|
|
|
Type MemberType `json:"type"`
|
|
|
|
Role string `json:"role"`
|
|
|
|
Way *Way `json:"-"`
|
2013-01-28 10:55:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Relation struct {
|
2013-04-03 00:07:27 +04:00
|
|
|
OSMElem
|
2013-08-02 12:08:01 +04:00
|
|
|
Members []Member `json:"members"`
|
2013-01-28 10:55:34 +04:00
|
|
|
}
|
2013-08-06 18:14:29 +04:00
|
|
|
|
|
|
|
type IdRefs struct {
|
|
|
|
Id int64
|
|
|
|
Refs []int64
|
|
|
|
}
|
2013-08-06 19:22:31 +04:00
|
|
|
|
|
|
|
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:]...)
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 12:18:08 +04:00
|
|
|
|
|
|
|
// 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
|