75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
package parser
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"time"
|
|
|
|
"golang.org/x/text/encoding/charmap"
|
|
)
|
|
|
|
type KML struct {
|
|
Document Document
|
|
}
|
|
|
|
type Document struct {
|
|
ExtendedData ExtendedData
|
|
Placemark Placemark
|
|
}
|
|
|
|
type ExtendedData struct {
|
|
ProductDefinition ProductDefinition
|
|
Forecast []Forecast
|
|
}
|
|
|
|
type ProductDefinition struct {
|
|
Issuer string
|
|
ProductID string
|
|
GeneratingProcess string
|
|
IssueTime time.Time
|
|
ForecastTimeSteps ForecastTimeSteps
|
|
}
|
|
|
|
type ForecastTimeSteps struct {
|
|
TimeStep []time.Time
|
|
}
|
|
|
|
type Placemark struct {
|
|
Name string `xml:"name"`
|
|
Description string `xml:"description"`
|
|
ExtendedData ExtendedData
|
|
Point Point
|
|
}
|
|
|
|
type Forecast struct {
|
|
Name string `xml:"elementName,attr"`
|
|
Value string `xml:"value"`
|
|
}
|
|
|
|
type Point struct {
|
|
Coordinates string `xml:"coordinates"`
|
|
}
|
|
|
|
func ParseKML(file *os.File) (*KML, error) {
|
|
var kml *KML
|
|
|
|
decoder := xml.NewDecoder(file)
|
|
decoder.CharsetReader = makeCharsetReader
|
|
|
|
if err := decoder.Decode(&kml); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return kml, nil
|
|
}
|
|
|
|
func makeCharsetReader(charset string, input io.Reader) (io.Reader, error) {
|
|
if charset == "ISO-8859-1" {
|
|
// Windows-1252 is a superset of ISO-8859-1, so should do here
|
|
return charmap.Windows1252.NewDecoder().Reader(input), nil
|
|
}
|
|
return nil, fmt.Errorf("Unknown charset: %s", charset)
|
|
}
|