-
Notifications
You must be signed in to change notification settings - Fork 13
/
kml.go
213 lines (187 loc) · 5.28 KB
/
kml.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//go:generate go run ./internal/generate -f -o kml22gx.gen.go -n gx: xsd/kml22gx.xsd
//go:generate go run ./internal/generate -f -o ogckml22.gen.go xsd/ogckml22.xsd
// Package kml provides convenience methods for creating and writing KML documents.
//
// See https://developers.google.com/kml/
//
// Goals
//
// - Convenient API for creating both simple and complex KML documents.
// - 1:1 mapping between functions and KML elements.
//
// Non-goals
//
// - Protection against generating invalid documents.
// - Concealment of KML complexity.
// - Fine-grained control over generated XML.
package kml
import (
"encoding/xml"
"fmt"
"image/color"
"io"
"strconv"
"time"
)
// An Element represents an abstract KML element.
type Element interface {
xml.Marshaler
Write(io.Writer) error
WriteIndent(io.Writer, string, string) error
}
// A SimpleElement is an Element with a single value.
type SimpleElement struct {
xml.StartElement
value string
}
// A CompoundElement is an Element with children.
type CompoundElement struct {
xml.StartElement
children []Element
}
// A SharedElement is an element with an id.
type SharedElement struct {
CompoundElement
id string
}
// MarshalXML marshals se to e. start is ignored.
func (se *SimpleElement) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(xml.CharData(se.value), se.StartElement)
}
// Write writes an XML header and se to w.
func (se *SimpleElement) Write(w io.Writer) error {
return write(w, "", "", se)
}
// WriteIndent writes an XML header and se to w.
func (se *SimpleElement) WriteIndent(w io.Writer, prefix, indent string) error {
return write(w, prefix, indent, se)
}
// Add adds children to ce.
func (ce *CompoundElement) Add(children ...Element) *CompoundElement {
ce.children = append(ce.children, children...)
return ce
}
// MarshalXML marshals ce to e. start is ignored.
func (ce *CompoundElement) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if err := e.EncodeToken(ce.StartElement); err != nil {
return err
}
for _, c := range ce.children {
if err := e.EncodeElement(c, ce.StartElement); err != nil {
return err
}
}
return e.EncodeToken(ce.End())
}
// Write writes an XML header and ce to w.
func (ce *CompoundElement) Write(w io.Writer) error {
return write(w, "", "", ce)
}
// WriteIndent writes an XML header and ce to w.
func (ce *CompoundElement) WriteIndent(w io.Writer, prefix, indent string) error {
return write(w, prefix, indent, ce)
}
// ID returns se's id.
func (se *SharedElement) ID() string {
return se.id
}
// URL returns se's URL.
func (se *SharedElement) URL() string {
return "#" + se.ID()
}
func write(w io.Writer, prefix, indent string, m xml.Marshaler) error {
if _, err := w.Write([]byte(xml.Header)); err != nil {
return err
}
e := xml.NewEncoder(w)
e.Indent(prefix, indent)
return e.Encode(m)
}
func newSEBool(name string, value bool) *SimpleElement {
var v string
if value {
v = "1"
} else {
v = "0"
}
return &SimpleElement{
StartElement: xml.StartElement{Name: xml.Name{Local: name}},
value: v,
}
}
func newSEColor(name string, value color.Color) *SimpleElement {
r, g, b, a := value.RGBA()
return &SimpleElement{
StartElement: xml.StartElement{Name: xml.Name{Local: name}},
value: fmt.Sprintf("%02x%02x%02x%02x", a/256, b/256, g/256, r/256),
}
}
func newSEElement(name string, value Element) *CompoundElement {
return &CompoundElement{
StartElement: xml.StartElement{
Name: xml.Name{Local: name},
},
children: []Element{value},
}
}
func newSEFloat(name string, value float64) *SimpleElement {
return &SimpleElement{
StartElement: xml.StartElement{Name: xml.Name{Local: name}},
value: strconv.FormatFloat(value, 'f', -1, 64),
}
}
func newSEInt(name string, value int) *SimpleElement {
return &SimpleElement{
StartElement: xml.StartElement{Name: xml.Name{Local: name}},
value: strconv.Itoa(value),
}
}
func newSEVec2(name string, value Vec2) *SimpleElement {
return &SimpleElement{
StartElement: xml.StartElement{
Name: xml.Name{Local: name},
Attr: []xml.Attr{
{Name: xml.Name{Local: "x"}, Value: strconv.FormatFloat(value.X, 'f', -1, 64)},
{Name: xml.Name{Local: "y"}, Value: strconv.FormatFloat(value.Y, 'f', -1, 64)},
{Name: xml.Name{Local: "xunits"}, Value: string(value.XUnits)},
{Name: xml.Name{Local: "yunits"}, Value: string(value.YUnits)},
},
},
}
}
func newSEString(name, value string) *SimpleElement {
return &SimpleElement{
StartElement: xml.StartElement{Name: xml.Name{Local: name}},
value: value,
}
}
func newSETime(name string, value time.Time) *SimpleElement {
return &SimpleElement{
StartElement: xml.StartElement{Name: xml.Name{Local: name}},
value: value.Format(time.RFC3339),
}
}
func newCE(name string, children []Element) *CompoundElement {
return &CompoundElement{
StartElement: xml.StartElement{
Name: xml.Name{Local: name},
},
children: children,
}
}
func newSharedE(name, id string, children []Element) *SharedElement {
var attr []xml.Attr
if id != "" {
attr = append(attr, xml.Attr{Name: xml.Name{Local: "id"}, Value: id})
}
return &SharedElement{
CompoundElement: CompoundElement{
StartElement: xml.StartElement{
Name: xml.Name{Local: name},
Attr: attr,
},
children: children,
},
id: id,
}
}