-
Notifications
You must be signed in to change notification settings - Fork 0
/
geomap.go
259 lines (235 loc) · 5.05 KB
/
geomap.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package charts
import (
"bytes"
"embed"
"encoding/xml"
"fmt"
"io"
"strconv"
"strings"
)
//go:embed maps/*/*.svg
var folder embed.FS
func GetAvailableMaps() []string {
return []string{
"australia",
"austria",
"brazil",
"cambodia",
"cameroon",
"canada",
"canada.lambert-projection",
"cape-verde",
"china",
"cli",
"colombia",
"denmark",
"france.departments",
"france.regions",
"germany",
"greece",
"honduras",
"hong-kong",
"india",
"indonesia",
"israel",
"italy",
"japan",
"kenya",
"mexico",
"moldova",
"netherlands",
"new-zealand",
"nigeria",
"pakistan.districts",
"puerto-rico",
"romania",
"saudi-arabia",
"south-korea",
"spain",
"sri-lanka",
"sweden",
"taiwan",
"taiwan.main",
"tanzania",
"thailand",
"tunisia",
"uae",
"ukraine",
"usa",
"usa.counties",
"usa.florida",
"usa.michigan",
"usa.states-territories",
"usa.utah",
"uzbekistan",
"world",
"world.capitals",
"zimbabwe",
}
}
type GeoMap struct {
Dimension
mapName string
data map[string]float64
numberFormat string
colorScheme *ColorScheme
showValues bool
isInteractive bool
}
func NewGeoMap(
mapName string,
data map[string]float64,
) *GeoMap {
return &GeoMap{
colorScheme: &DefaultColorScheme,
mapName: mapName,
data: data,
showValues: false,
isInteractive: false,
}
}
func (gm *GeoMap) SetColorDcheme(colorScheme *ColorScheme) *GeoMap {
gm.colorScheme = colorScheme
return gm
}
func (gm *GeoMap) SetNumberFormat(numberFormat string) *GeoMap {
gm.numberFormat = numberFormat
return gm
}
func (gm *GeoMap) SetInteractive(interactive bool) *GeoMap {
gm.isInteractive = interactive
return gm
}
func (gm *GeoMap) SetShowValue(showValues bool) *GeoMap {
gm.showValues = showValues
return gm
}
type Node struct {
XMLName xml.Name
Attrs []xml.Attr `xml:",any,attr"`
Content []byte `xml:",innerxml"`
Nodes []Node `xml:",any"`
}
func walk(nodes []Node, f func(Node) bool) {
for _, n := range nodes {
if f(n) {
walk(n.Nodes, f)
}
}
}
func (gm *GeoMap) RenderSVG(w io.Writer) error {
templateMap, err := folder.ReadFile(fmt.Sprintf("maps/%s/%s.svg", gm.mapName, gm.mapName))
if err != nil {
return err
}
buf := bytes.NewBuffer(templateMap)
dec := xml.NewDecoder(buf)
labelBuffer := new(bytes.Buffer)
var n Node
err = dec.Decode(&n)
if err != nil {
return err
}
walk([]Node{n}, func(n Node) bool {
if n.XMLName.Local == "svg" {
width, height := 1024, 1024
for _, attr := range n.Attrs {
if strings.ToUpper(attr.Name.Local) == "VIEWBOX" {
s := strings.Split(attr.Value, " ")
width, _ = strconv.Atoi(s[2])
height, _ = strconv.Atoi(s[3])
break
}
}
startSVG(w, width, height, gm.colorScheme)
writeDefsTxtBg(w, gm.colorScheme)
writeFontStyle(w, gm.isInteractive)
writeBackground(w, width, height, gm.colorScheme)
gm.styleSVG(w)
for _, node := range n.Nodes {
fmt.Fprint(w, "<path ")
var name, id string
var labelx, labely float64
for _, attr := range node.Attrs {
fmt.Fprintf(w, "%s=\"%s\" ", attr.Name.Local, attr.Value)
if attr.Name.Local == "name" {
name = attr.Value
}
if attr.Name.Local == "id" {
id = attr.Value
}
if attr.Name.Local == "d" {
attr.Value = strings.ReplaceAll(attr.Value, "m", "")
attr.Value = strings.Trim(attr.Value, " ")
arr := strings.FieldsFunc(attr.Value, splitfn)
labelx, _ = strconv.ParseFloat(arr[0], 64)
labely, _ = strconv.ParseFloat(arr[1], 64)
}
}
fmt.Fprint(w, "/>")
if gm.showValues || gm.isInteractive {
fmt.Fprint(labelBuffer, "<path ")
if gm.isInteractive {
fmt.Fprint(labelBuffer, "class='hovercircle' fill-opacity='0' ")
}
for _, attr := range node.Attrs {
if strings.ToUpper(attr.Name.Local) != "ID" {
fmt.Fprintf(labelBuffer, "%s=\"%s\" ", attr.Name.Local, attr.Value)
}
}
fmt.Fprint(labelBuffer, "/>")
fmt.Fprintf(
labelBuffer,
"<text style='paint-order:stroke fill' class='value' text-anchor='middle' alignment-baseline='middle' filter='url(#textbg)' x='%f' y='%f'>%s (%g)</text>",
labelx,
labely,
name,
gm.data[id],
)
}
}
}
return true
})
if gm.showValues || gm.isInteractive {
w.Write(labelBuffer.Bytes())
}
endSVG(w)
return nil
}
func (hm *GeoMap) styleSVG(w io.Writer) error {
var min, max float64
var start = true
for _, value := range hm.data {
if start {
min, max = value, value
start = false
continue
}
if value < min {
min = value
} else if value > max {
max = value
}
}
fmt.Fprint(w, "<style> ")
/*
fmt.Fprintf(w, " path { fill: %s; stroke: %s; stroke-width: 0.5; } \n",
hm.colorScheme.Background,
hm.colorScheme.LightAxisColor,
)
*/
for k, v := range hm.data {
fmt.Fprintf(w, " path[id='%s'] { fill: %s; fill-opacity: %f; } \n",
k,
hm.colorScheme.ColorPalette(0),
(v-min)/(max-min),
)
}
fmt.Fprint(w, " </style>")
return nil
}
func splitfn(r rune) bool {
return r == ',' || r == ' '
}