Skip to content

Commit 49a206d

Browse files
authored
Create kml-to-geojson.html
1 parent fc92540 commit 49a206d

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

kml-to-geojson.html

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
<!DOCTYPE html>
3+
4+
<html lang="en">
5+
6+
<head>
7+
8+
<meta charset="UTF-8">
9+
10+
<title>KML to GeoJSON Converter</title>
11+
12+
<!-- Load toGeoJSON library from unpkg CDN -->
13+
14+
<script src=https://unpkg.com/@tmcw/togeojson></script>
15+
16+
</head>
17+
18+
<body>
19+
20+
21+
22+
<h1>KML to GeoJSON Converter</h1>
23+
24+
<input type="file" id="fileInput" accept=".kml">
25+
26+
<button id="convertButton">Convert to GeoJSON</button>
27+
28+
<button id="downloadButton" style="display: none;">Download GeoJSON</button>
29+
30+
<pre id="result"></pre>
31+
32+
33+
34+
<script>
35+
36+
document.getElementById('convertButton').addEventListener('click', function () {
37+
38+
const fileInput = document.getElementById('fileInput');
39+
40+
if (fileInput.files.length === 0) {
41+
42+
alert('Please select a KML file first.');
43+
44+
return;
45+
46+
}
47+
48+
49+
50+
const file = fileInput.files[0];
51+
52+
const reader = new FileReader();
53+
54+
reader.onload = function (e) {
55+
56+
const kmlText = e.target.result;
57+
58+
const parser = new DOMParser();
59+
60+
const kml = parser.parseFromString(kmlText, 'text/xml');
61+
62+
const converted = toGeoJSON.kml(kml);
63+
64+
const geojsonStr = JSON.stringify(converted, null, 2);
65+
66+
document.getElementById('result').textContent = geojsonStr;
67+
68+
69+
70+
const downloadButton = document.getElementById('downloadButton');
71+
72+
downloadButton.style.display = 'inline';
73+
74+
downloadButton.onclick = function () {
75+
76+
const blob = new Blob([geojsonStr], {type: 'application/json'});
77+
78+
const url = URL.createObjectURL(blob);
79+
80+
const a = document.createElement('a');
81+
82+
a.setAttribute('href', url);
83+
84+
a.setAttribute('download', 'converted.geojson');
85+
86+
a.click();
87+
88+
URL.revokeObjectURL(url); // Clean up the URL object
89+
90+
};
91+
92+
};
93+
94+
reader.readAsText(file);
95+
96+
});
97+
98+
</script>
99+
100+
101+
102+
</body>
103+
104+
</html>

0 commit comments

Comments
 (0)