forked from paulmach/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
change.go
124 lines (96 loc) · 3.15 KB
/
change.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
package osm
import (
"encoding/xml"
)
// Change is the structure of a changeset to be
// uploaded or downloaded from the osm api server.
// See: http://wiki.openstreetmap.org/wiki/OsmChange
type Change struct {
Version string `xml:"version,attr,omitempty" json:"version,omitempty"`
Generator string `xml:"generator,attr,omitempty" json:"generator,omitempty"`
// to indicate the origin of the data
Copyright string `xml:"copyright,attr,omitempty" json:"copyright,omitempty"`
Attribution string `xml:"attribution,attr,omitempty" json:"attribution,omitempty"`
License string `xml:"license,attr,omitempty" json:"license,omitempty"`
Create *OSM `xml:"create" json:"create,omitempty"`
Modify *OSM `xml:"modify" json:"modify,omitempty"`
Delete *OSM `xml:"delete" json:"delete,omitempty"`
}
// AppendCreate will append the object to the Create OSM object.
func (c *Change) AppendCreate(o Object) {
if c.Create == nil {
c.Create = &OSM{}
}
c.Create.Append(o)
}
// AppendModify will append the object to the Modify OSM object.
func (c *Change) AppendModify(o Object) {
if c.Modify == nil {
c.Modify = &OSM{}
}
c.Modify.Append(o)
}
// AppendDelete will append the object to the Delete OSM object.
func (c *Change) AppendDelete(o Object) {
if c.Delete == nil {
c.Delete = &OSM{}
}
c.Delete.Append(o)
}
// HistoryDatasource converts the change object to a datasource accessible
// by feature id. All the creates, modifies and deletes will be added
// in that order.
func (c *Change) HistoryDatasource() *HistoryDatasource {
ds := &HistoryDatasource{}
ds.add(c.Create, true)
ds.add(c.Modify, true)
ds.add(c.Delete, false)
return ds
}
// MarshalXML implements the xml.Marshaller method to allow for the
// correct wrapper/start element case and attr data.
func (c Change) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = "osmChange"
start.Attr = []xml.Attr{}
if c.Version != "" {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "version"}, Value: c.Version})
}
if c.Generator != "" {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "generator"}, Value: c.Generator})
}
if c.Copyright != "" {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "copyright"}, Value: c.Copyright})
}
if c.Attribution != "" {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "attribution"}, Value: c.Attribution})
}
if c.License != "" {
start.Attr = append(start.Attr, xml.Attr{Name: xml.Name{Local: "license"}, Value: c.License})
}
if err := e.EncodeToken(start); err != nil {
return err
}
if err := marshalInnerChange(e, "create", c.Create); err != nil {
return err
}
if err := marshalInnerChange(e, "modify", c.Modify); err != nil {
return err
}
if err := marshalInnerChange(e, "delete", c.Delete); err != nil {
return err
}
return e.EncodeToken(start.End())
}
func marshalInnerChange(e *xml.Encoder, name string, o *OSM) error {
if o == nil {
return nil
}
t := xml.StartElement{Name: xml.Name{Local: name}}
if err := e.EncodeToken(t); err != nil {
return err
}
if err := o.marshalInnerXML(e); err != nil {
return err
}
return e.EncodeToken(t.End())
}