unmarshall kml into structure

This commit is contained in:
Lars Hoogestraat 2021-06-16 11:36:52 +02:00
parent 9e6ff4dace
commit 33cca3e473
7 changed files with 108 additions and 5 deletions

2
go.mod
View File

@ -1,3 +1,5 @@
module git.hoogi.eu/snafu/dwd
go 1.16
require golang.org/x/text v0.3.6

3
go.sum Normal file
View File

@ -0,0 +1,3 @@
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

74
parser/kml.go Normal file
View File

@ -0,0 +1,74 @@
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 nil, 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)
}

25
parser/kml_test.go Normal file
View File

@ -0,0 +1,25 @@
package parser_test
import (
"git.hoogi.eu/snafu/dwd/parser"
"os"
"testing"
)
func TestParseMosmix(t *testing.T) {
f, err := os.Open("../sample/MOSMIX_L_2021061509_EW006.kml")
if err != nil {
t.Fatal(err)
}
defer f.Close()
_, err = parser.ParseKML(f)
if err != nil {
t.Fatal(err)
}
// TODO: add some test
}

View File

@ -1 +0,0 @@
package parser

View File

@ -56,7 +56,7 @@ type columnSize struct {
end int
}
func Parse(file *os.File) ([]Station, error) {
func ParseStation(file *os.File) ([]Station, error) {
scanner := bufio.NewScanner(file)
var stations []Station

View File

@ -6,8 +6,8 @@ import (
"testing"
)
func TestParser(t *testing.T) {
file, err := os.Open("mosmix_stationskatalog.cfg")
func TestParseStation(t *testing.T) {
file, err := os.Open("../sample/mosmix_stationskatalog.cfg")
if err != nil {
t.Fatal(err)
@ -15,7 +15,7 @@ func TestParser(t *testing.T) {
defer file.Close()
st, err := parser.Parse(file)
st, err := parser.ParseStation(file)
if err != nil {
t.Fatal(err)