/
kml2csv.js
79 lines (73 loc) · 2.66 KB
/
kml2csv.js
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
import { XML } from "https://js.sabae.cc/XML.js";
import { CSV } from "https://js.sabae.cc/CSV.js";
import { GooglePlaceAPI } from "https://code4fukui.github.io/GooglePlaceAPI/GooglePlaceAPI.js";
const kml = await Deno.readTextFile("./doc.kml");
const json = XML.toJSON(kml);
//console.log(json);
const folders = json.kml.Document.Folder;
//console.log(folders);
const array = a => Array.isArray(a) ? a : [a];
/*
cosnt
<Document>
<name>RENEW/2023 参加企業一覧</name>
<description><![CDATA[タクシー乗降可能場所<br>・RENEW参加企業全94社<br>・うるしの里会館<br>・JR武生駅<br>・福井鉄道たけふ新駅(JR武生駅より徒歩5分)]]></description>
<Style id="icon-ci-1-normal">
<IconStyle>
<scale>1.1</scale>
<Icon>
<href>images/icon-1.png</href>
*/
//console.log(json.kml.Document.Style[0])
//Deno.exit(0);
//console.log(await GooglePlaceAPI.fetchPosFromName("福井県越前市千福町606-2"));
//console.log(await GooglePlaceAPI.fetchPosFromName("福井県鯖江市河和田町19-1-7"));
//console.log(await GooglePlaceAPI.fetchPosFromName("福井県鯖江市日の出町1−2"));
//Deno.exit(0);
const premap = {
"shifu.": { lat: 35.8879893, lng: 136.1553524, zoom: 15 },
"PARK": { lat: 35.9515016, lng: 136.2742195, zoom: 17 },
"リペア/鯖江駅眼鏡": { lat: 35.9431928, lng: 136.1887162, zoom: 17 },
};
const list = [];
for (const f of folders) {
const category = f.name["#text"];
for (const p of array(f.Placemark)) {
const d = {};
d.name = p.name["#text"];
d.category = category;
const st = p.styleUrl["#text"].substring(1);
console.log(st)
const smap = json.kml.Document.StyleMap.find(s => s.id == st).Pair[0].styleUrl["#text"].substring(1);
d.image = json.kml.Document.Style.find(s => s.id == smap).IconStyle.Icon.href["#text"];
if (p.Point) {
console.log(p.Point)
const ll = p.Point.coordinates["#text"].trim().split(",");
d.lat = ll[1];
d.lng = ll[0];
} else {
if (premap[d.name]) {
const pos = premap[d.name];
d.lat = pos.lat;
d.lng = pos.lng;
} else {
const pos = await GooglePlaceAPI.fetchPosFromName(d.name);
d.lat = pos.lat;
d.lng = pos.lng;
console.log("pos not included", p, "by api", pos);
}
}
d.address = p.name["#text"];
d.description = p.description["#text"];
//console.log(p.ExtendedData.Data)
for (const e of p.ExtendedData.Data) {
const k = e.name;
const v = e.value["#text"];
if (v) {
d[k] = v;
}
}
list.push(d);
}
}
await Deno.writeTextFile("renew-2023-map.csv", CSV.stringify(list));