-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkml-to-geojson.html
104 lines (47 loc) · 2.06 KB
/
kml-to-geojson.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>KML to GeoJSON Converter</title>
<!-- Load toGeoJSON library from unpkg CDN -->
<script src=https://unpkg.com/@tmcw/togeojson></script>
</head>
<body>
<h1>KML to GeoJSON Converter</h1>
<input type="file" id="fileInput" accept=".kml">
<button id="convertButton">Convert to GeoJSON</button>
<button id="downloadButton" style="display: none;">Download GeoJSON</button>
<pre id="result"></pre>
<script>
document.getElementById('convertButton').addEventListener('click', function () {
const fileInput = document.getElementById('fileInput');
if (fileInput.files.length === 0) {
alert('Please select a KML file first.');
return;
}
const file = fileInput.files[0];
const reader = new FileReader();
reader.onload = function (e) {
const kmlText = e.target.result;
const parser = new DOMParser();
const kml = parser.parseFromString(kmlText, 'text/xml');
const converted = toGeoJSON.kml(kml);
const geojsonStr = JSON.stringify(converted, null, 2);
document.getElementById('result').textContent = geojsonStr;
const downloadButton = document.getElementById('downloadButton');
downloadButton.style.display = 'inline';
downloadButton.onclick = function () {
const blob = new Blob([geojsonStr], {type: 'application/json'});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.setAttribute('href', url);
a.setAttribute('download', 'converted.geojson');
a.click();
URL.revokeObjectURL(url); // Clean up the URL object
};
};
reader.readAsText(file);
});
</script>
</body>
</html>