2016-03-06 17:55:00 +03:00
|
|
|
package main
|
2016-03-05 02:08:16 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
|
2018-04-19 18:43:32 +03:00
|
|
|
"github.com/tidwall/tile38/pkg/geojson"
|
2016-03-05 02:08:16 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type pointT struct {
|
|
|
|
name string
|
|
|
|
point geojson.SimplePoint
|
|
|
|
}
|
|
|
|
|
2016-04-03 05:16:36 +03:00
|
|
|
// KML represents a KML object.
|
2016-03-05 02:08:16 +03:00
|
|
|
type KML struct {
|
|
|
|
points []pointT
|
|
|
|
}
|
|
|
|
|
2016-04-03 05:16:36 +03:00
|
|
|
// NewKML returns a new KML object.
|
2016-03-06 17:55:00 +03:00
|
|
|
func NewKML() *KML {
|
2016-03-05 02:08:16 +03:00
|
|
|
return &KML{}
|
|
|
|
}
|
|
|
|
|
2016-04-03 05:16:36 +03:00
|
|
|
// AddPoint adds a point to a KML object.
|
2016-03-05 02:08:16 +03:00
|
|
|
func (kml *KML) AddPoint(name string, lat, lon float64) {
|
|
|
|
kml.points = append(kml.points, pointT{name: name, point: geojson.SimplePoint{X: lon, Y: lat}})
|
|
|
|
}
|
|
|
|
|
2016-04-03 05:16:36 +03:00
|
|
|
// Bytes returns the xml of the KML.
|
2016-03-05 02:08:16 +03:00
|
|
|
func (kml *KML) Bytes() []byte {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
buf.WriteString(`<?xml version="1.0" encoding="UTF-8"?>` + "\n")
|
|
|
|
buf.WriteString(`<kml xmlns="http://www.opengis.net/kml/2.2">` + "\n")
|
|
|
|
buf.WriteString(`<Document>` + "\n")
|
|
|
|
buf.WriteString(`<Style id="yellow"><IconStyle><Icon><href>http://www.google.com/intl/en_us/mapfiles/ms/icons/yellow-dot.png</href></Icon></IconStyle></Style> ` + "\n")
|
|
|
|
buf.WriteString(`<Style id="blue"><IconStyle><Icon><href>http://www.google.com/intl/en_us/mapfiles/ms/icons/blue-dot.png</href></Icon></IconStyle></Style> ` + "\n")
|
|
|
|
for _, point := range kml.points {
|
|
|
|
buf.WriteString(fmt.Sprintf(`<Placemark><styleUrl>#yellow</styleUrl><name>%s</name><Point><coordinates>%f,%f,0</coordinates></Point></Placemark>`+"\n", point.name, point.point.X, point.point.Y))
|
|
|
|
}
|
|
|
|
buf.WriteString(`</Document></kml>` + "\n")
|
|
|
|
return buf.Bytes()
|
|
|
|
}
|