-
Notifications
You must be signed in to change notification settings - Fork 13
/
kml22gx.go
165 lines (143 loc) · 4.7 KB
/
kml22gx.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
package kml
import (
"encoding/xml"
"io"
"strconv"
"strings"
)
// GxNamespace is the default namespace for Google Earth extensions.
const GxNamespace = "http://www.google.com/kml/ext/2.2"
// A GxOptionName is a gx:option name.
type GxOptionName string
func (e GxOptionName) String() string { return string(e) }
// GxOptionNames.
const (
GxOptionNameHistoricalImagery GxOptionName = "historicalimagery"
GxOptionNameStreetView GxOptionName = "streetview"
GxOptionNameSunlight GxOptionName = "sunlight"
)
// A GxAnglesElement is a gx:angles element.
type GxAnglesElement struct {
Heading float64
Tilt float64
Roll float64
}
// GxAngles returns a new GxAnglesElement.
func GxAngles(heading, tilt, roll float64) *GxAnglesElement {
return &GxAnglesElement{
Heading: heading,
Tilt: tilt,
Roll: roll,
}
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *GxAnglesElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{Name: xml.Name{Local: "gx:angles"}}
var builder strings.Builder
builder.Grow(3 * float64StringSize)
builder.WriteString(strconv.FormatFloat(e.Heading, 'f', -1, 64))
builder.WriteByte(' ')
builder.WriteString(strconv.FormatFloat(e.Tilt, 'f', -1, 64))
builder.WriteByte(' ')
builder.WriteString(strconv.FormatFloat(e.Roll, 'f', -1, 64))
charData := xml.CharData(builder.String())
return encodeElementWithCharData(encoder, startElement, charData)
}
// A GxCoordElement is a gx:coord element.
type GxCoordElement Coordinate
// GxCoord returns a new GxCoordElement.
func GxCoord(coordinate Coordinate) GxCoordElement {
return GxCoordElement(coordinate)
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e GxCoordElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{Name: xml.Name{Local: "gx:coord"}}
var builder strings.Builder
builder.Grow(3 * float64StringSize)
builder.WriteString(strconv.FormatFloat(e.Lon, 'f', -1, 64))
builder.WriteByte(' ')
builder.WriteString(strconv.FormatFloat(e.Lat, 'f', -1, 64))
builder.WriteByte(' ')
builder.WriteString(strconv.FormatFloat(e.Alt, 'f', -1, 64))
charData := xml.CharData(builder.String())
return encodeElementWithCharData(encoder, startElement, charData)
}
// A GxKMLElement is a kml element with gx: extensions.
type GxKMLElement struct {
Child Element
}
// GxKML returns a new GxKMLElement.
func GxKML(child Element) *GxKMLElement {
return &GxKMLElement{
Child: child,
}
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *GxKMLElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{
Name: xml.Name{Space: Namespace, Local: "kml"},
Attr: []xml.Attr{
{
Name: xml.Name{Local: "xmlns:gx"},
Value: GxNamespace,
},
},
}
return encodeElementWithChild(encoder, startElement, e.Child)
}
// Write writes e to w.
func (e *GxKMLElement) Write(w io.Writer) error {
return write(w, e)
}
// WriteIndent writes e to w with the given prefix and indent.
func (e *GxKMLElement) WriteIndent(w io.Writer, prefix, indent string) error {
return writeIndent(w, e, prefix, indent)
}
// A GxOptionElement is a gx:option element.
type GxOptionElement struct {
Name GxOptionName
Enabled bool
}
// GxOption returns a new gx:option element.
func GxOption(name GxOptionName, enabled bool) *GxOptionElement {
return &GxOptionElement{
Name: name,
Enabled: enabled,
}
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *GxOptionElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{
Name: xml.Name{Local: "gx:option"},
Attr: []xml.Attr{
{Name: xml.Name{Local: "name"}, Value: string(e.Name)},
{Name: xml.Name{Local: "enabled"}, Value: strconv.FormatBool(e.Enabled)},
},
}
return encodeElement(encoder, startElement)
}
// A GxSimpleArrayFieldElement is a gx:SimpleArrayField element.
type GxSimpleArrayFieldElement struct {
Name string
Type string
Children []Element
}
// GxSimpleArrayField returns a new GxSimpleArrayFieldElement.
func GxSimpleArrayField(name, _type string, children ...Element) *GxSimpleArrayFieldElement {
return &GxSimpleArrayFieldElement{
Name: name,
Type: _type,
Children: children,
}
}
// MarshalXML implements encoding/xml.Marshaler.MarshalXML.
func (e *GxSimpleArrayFieldElement) MarshalXML(encoder *xml.Encoder, _ xml.StartElement) error {
startElement := xml.StartElement{
Name: xml.Name{Local: "gx:SimpleArrayField"},
Attr: []xml.Attr{
{Name: xml.Name{Local: "name"}, Value: e.Name},
{Name: xml.Name{Local: "type"}, Value: e.Type},
},
}
return encodeElementWithChildren(encoder, startElement, e.Children)
}