-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVG_map.core.mjs
195 lines (167 loc) · 5.29 KB
/
SVG_map.core.mjs
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
/*
2021/5/18 14:48:50
@see
Pixel Map Generator | amCharts
https://pixelmap.amcharts.com/
*/
function SVG_data_toString() {
return JSON.to_XML(this.SVG_JSON);
}
export function convert_to_SVG_data(SVG_content, options) {
const SVG_JSON = JSON.from_XML(SVG_content.trim());
// groups[id] = [ nodes ]
const groups = Object.create(null);
const index_of_group = Object.create(null);
SVG_JSON.svg.forEach((node, index) => {
if (!node.id || Object.keys(node)[0] !== 'g')
return;
groups[node.id] = node.g;
index_of_group[node.id] = index;
});
if (options?.remove_town && index_of_group.town >= 0) {
// Remove town data
SVG_JSON.svg[index_of_group.town] = '';
}
//console.log(Object.keys(groups));
key_groups.forEach(id => {
const group = groups[id];
// assert: Array.isArray(group);
const index_of = group.index_of = Object.create(null);
function set_index_of(value, index) {
if (value) {
index_of[value.toString().trim().replace(/\s[\s\S]+/, '')] = index;
return true;
}
}
group.forEach((node, index) => {
set_index_of(node.title, index);
if (node.text) {
set_index_of(node.text.tspan, index);
set_index_of(node.text.title, index);
} else if (node.path) {
(Array.isArray(node.path) ? node.path : [node.path]).forEach(n => {
set_index_of(n.title, index);
});
}
});
});
//console.log(groups.county);
return { SVG_JSON, groups, index_of_group, toString: SVG_data_toString };
}
// ----------------------------------------------------------------------------
// @see [[臺灣行政區劃]], original from [[File:Blank Taiwan map.svg]]
export const default_SVG_file_name = 'resources/Taiwan map labeled - no town.svg';
// default value
export const KEY_default = '*';
const key_groups = ['county', 'county_label', 'information_label'];
const node_properties = ['fill', 'stroke', 'stroke-width'];
function normalize_sub_map_options(sub_map_options, default_options) {
if (!sub_map_options)
return;
sub_map_options = Object.clone(sub_map_options);
key_groups.forEach(id => sub_map_options[id] = { ...sub_map_options[id] });
if (sub_map_options.fill && !sub_map_options.county.fill) {
sub_map_options.county.fill = sub_map_options.fill;
}
if (sub_map_options.color && !sub_map_options.information_label.fill) {
sub_map_options.information_label.fill = sub_map_options.color;
}
if (sub_map_options.label && !sub_map_options.information_label.text) {
sub_map_options.information_label.text = sub_map_options.label;
}
if (default_options) {
sub_map_options = { ...default_options, ...sub_map_options };
key_groups.forEach(id => sub_map_options[id] = { ...default_options[id], ...sub_map_options[id] });
}
return sub_map_options;
}
function convert_map_options(array_map_options) {
const map_options = Object.create(null);
const header = ['county', 'label', 'fill', 'color'];
array_map_options.forEach(line => {
const county = line[0];
if (!county) {
return;
}
if (!map_options[county])
map_options[county] = Object.create(null);
line.forEach((item, index) => {
if (header[index] !== 'county' && line[index])
map_options[county][header[index]] = line[index];
});
});
return map_options;
}
function set_node_text(node, text) {
if (text === 0)
text = String(text);
if (text || text === '') {
if (node.text) {
node.text.tspan = text;
} else if (node.path) {
node.path.forEach(n => {
if (n.title)
n.title = text;
});
}
return true;
}
}
export function adapt_map_options(SVG_data, map_options) {
if (Array.isArray(map_options)) {
map_options = convert_map_options(map_options);
}
map_options = map_options || Object.create(null);
const { groups } = SVG_data;
const default_options = normalize_sub_map_options(map_options[KEY_default]);
if (default_options) {
key_groups.forEach(id => {
node_properties.forEach(key => {
if (default_options[id][key])
groups[id][key] = default_options[id][key];
});
});
}
// 沒輸入的就不顯示 label文字。
groups.information_label.forEach(node => {
set_node_text(node, '');
});
for (let county in map_options) {
// 注意: 同縣市可能設定兩次,例如設定了 "新北" 與 "新北市"。以後出現者為主。
if (county === KEY_default) continue;
const sub_map_options = normalize_sub_map_options(map_options[county], default_options);
//console.log([county, sub_map_options]);
if (!(county in groups.county_label.index_of)
&& county.length > 1
&& !Object.keys(groups.county_label.index_of).some(_county => {
if (_county.includes(county)
|| _county.includes(county.replace(/台/g, '臺'))) {
county = _county;
return true;
}
})) {
CeL.error(`Cannot find county: ${county}!`);
continue;
}
key_groups.forEach(id => {
const group_index = groups[id].index_of[county];
if (!(group_index >= 0)) {
if (id !== 'county') {
// Should not go to here: Filtered above.
// 除非 SVG 圖片本身設定有問題。
CeL.error(`Cannot find county: ${id}.${county}!`);
}
return;
}
const _sub_map_options = sub_map_options[id];
if (!_sub_map_options)
return;
const node = groups[id][group_index];
set_node_text(node, _sub_map_options.text);
node_properties.forEach(key => {
if (_sub_map_options[key])
node[key] = _sub_map_options[key];
});
});
}
}