-
Notifications
You must be signed in to change notification settings - Fork 3
/
kml.go
190 lines (180 loc) · 4.67 KB
/
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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package chile
import (
"encoding/xml"
"errors"
"fmt"
"strconv"
"strings"
"github.com/whitewater-guide/gorge/core"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type kmlPmDesc struct {
codigo string
nombre string
altitud float64
vigencia bool
inicio string
suspension string
parametro string
}
type kmlPmSnippet struct {
MaxLines string `xml:"maxLines,attr"`
}
type kmlPmPoint struct {
Coordinates core.Location
}
type kmlPlacemark struct {
Name string `xml:"name"`
Snippet kmlPmSnippet `xml:"Snippet"`
Description kmlPmDesc `xml:"description"`
StyleURL string `xml:"styleUrl"`
Point kmlPmPoint `xml:"Point"`
}
func (desc *kmlPmPoint) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var raw struct {
Coordinates string `xml:"coordinates"`
}
err := d.DecodeElement(&raw, &start)
if err != nil {
return err
}
lla := strings.Split(raw.Coordinates, ",")
if len(lla) != 3 {
return errors.New("failed to split coordinate")
}
lon, err := strconv.ParseFloat(lla[0], 64)
if err != nil {
return fmt.Errorf("failed to parse longitude: %w", err)
}
lat, err := strconv.ParseFloat(lla[1], 64)
if err != nil {
return fmt.Errorf("failed to parse latitude: %w", err)
}
desc.Coordinates.Longitude = core.TruncCoord(lon)
desc.Coordinates.Latitude = core.TruncCoord(lat)
return nil
}
func (desc *kmlPmDesc) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var s string
err := d.DecodeElement(&s, &start)
if err != nil {
return err
}
lines := strings.Split(s, "<br />")
for _, l := range lines {
kv := strings.Split(l, " = ")
if len(kv) != 2 {
continue
}
k := strings.TrimSpace(kv[0])
val := strings.TrimSpace(kv[1])
switch k {
case "CODIGO BNA":
desc.codigo = val
case "NOMBRE":
desc.nombre = cases.Title(language.Spanish).String(val)
case "ALTITUD":
alt, err := strconv.ParseFloat(val, 64)
if err != nil {
return fmt.Errorf("failed to parse altitude: %w", err)
}
if alt == -9999 {
alt = 0
}
desc.altitud = alt
case "VIGENCIA":
desc.vigencia = val == "VIGENTE"
case "INICIO":
desc.inicio = val
case "SUSPENSION":
desc.suspension = val
case "PARAMETRO":
desc.parametro = val // everything is CAUDAL (m3/s)
}
}
return nil
}
// generated with https://www.onlinetool.io/xmltogo/
type chileKml struct {
XMLName xml.Name `xml:"kml"`
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
Gx string `xml:"gx,attr"`
Kml string `xml:"kml,attr"`
Atom string `xml:"atom,attr"`
Document struct {
Text string `xml:",chardata"`
Name string `xml:"name"`
Open string `xml:"open"`
Snippet struct {
Text string `xml:",chardata"`
MaxLines string `xml:"maxLines,attr"`
} `xml:"Snippet"`
Description string `xml:"description"`
Style []struct {
Text string `xml:",chardata"`
ID string `xml:"id,attr"`
IconStyle struct {
Text string `xml:",chardata"`
Color string `xml:"color"`
Scale string `xml:"scale"`
Icon struct {
Text string `xml:",chardata"`
Href string `xml:"href"`
X string `xml:"x"`
Y string `xml:"y"`
W string `xml:"w"`
H string `xml:"h"`
} `xml:"Icon"`
} `xml:"IconStyle"`
LabelStyle struct {
Text string `xml:",chardata"`
Color string `xml:"color"`
} `xml:"LabelStyle"`
} `xml:"Style"`
StyleMap []struct {
Text string `xml:",chardata"`
ID string `xml:"id,attr"`
Pair []struct {
Text string `xml:",chardata"`
Key string `xml:"key"`
StyleURL string `xml:"styleUrl"`
} `xml:"Pair"`
} `xml:"StyleMap"`
Folder struct {
Text string `xml:",chardata"`
Name string `xml:"name"`
Placemark []kmlPlacemark `xml:"Placemark"`
} `xml:"Folder"`
} `xml:"Document"`
}
func (s *scriptChile) getKMLGauges() (map[string]core.Gauge, error) {
var data chileKml
err := core.Client.GetAsXML("http://documentos.dga.cl/KML/01_Red_Hidrometrica/Fluviometricas_001.kml", &data, nil)
if err != nil {
return nil, err
}
result := make(map[string]core.Gauge)
for _, pm := range data.Document.Folder.Placemark {
if pm.Name != "FLUVIOMETRICAS" || !pm.Description.vigencia {
continue
}
result[pm.Description.codigo] = core.Gauge{
GaugeID: core.GaugeID{
Script: s.name,
Code: pm.Description.codigo,
},
Name: pm.Description.nombre,
FlowUnit: "m3/s",
LevelUnit: "m",
Location: &core.Location{
Latitude: pm.Point.Coordinates.Latitude,
Longitude: pm.Point.Coordinates.Longitude,
Altitude: pm.Description.altitud,
},
Timezone: "America/Santiago",
}
}
return result, nil
}