-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkml.go
44 lines (37 loc) · 868 Bytes
/
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
package kml
import (
"encoding/xml"
"os"
)
type KML struct {
XMLName xml.Name `xml:"kml"`
Xmlns string `xml:"xmlns,attr"`
Document []any `xml:"Document"`
}
// Initialize a new KML instance
func New() *KML {
return &KML{
Xmlns: "http://www.opengis.net/kml/2.2",
}
}
// Append all Document element child
func (kml *KML) AddToDocument(child any) {
kml.Document = append(kml.Document, child)
}
// Marshal input to KML
func (kml *KML) MarshalIndent(prefix string, indent string) ([]byte, error) {
return xml.MarshalIndent(kml, prefix, indent)
}
// Save the output as a KML file
func (kml *KML) WriteOutput(marshaled []byte, filename string) (string, error) {
file, err := os.Create(filename)
if err != nil {
return "", err
}
defer file.Close()
_, err = file.Write(marshaled)
if err != nil {
return "", err
}
return file.Name(), err
}