-
Notifications
You must be signed in to change notification settings - Fork 2
/
TheKarte-2kml.html
executable file
·301 lines (248 loc) · 11.3 KB
/
TheKarte-2kml.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<!DOCTYPE html>
<!--
Converts CSV and JSON-files files to KML and vice versa.
Interaction is done via drag and drop while data is exported via local download.
The output file name is the original name concatenated with ".kml".
-->
<html>
<head>
<meta charset="UTF-8">
<title>TheKarte-csv2kml</title>
<link rel="icon" type="image/svg+xml" href="img/TheKarte-logo.svg" />
<link rel="icon" type="image/png" href="img/TheKarte-logo.png" sizes="96x96">
<script src="dependencies/papaparse.min.js"></script>
<script src="dependencies/ol.js"></script>
<style>
.dropArea {
height: 30em;
width: 30em;
margin-top: 1em;
margin-left: auto;
margin-right: auto;
border-style: dotted;
display: flex;
flex-direction: column;
justify-content: center;
}
</style>
<script src="src/HelperExport.js"></script>
<script>
/**
Allow drop events.
*/
function loadAllowDrop(event) {
event.stopPropagation();
event.preventDefault();
event.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
/**
Handles drop events.
*/
function loadDrop(event) {
loadAllowDrop(event);
const columnLatitude = document.getElementById('choice-latitude').value;
const columnLongitude = document.getElementById('choice-longitude').value;
if (!columnLatitude || columnLatitude.length == 0 || !columnLatitude || columnLatitude.length == 0) {
console.error("TheKarte-csv2kml: no columns defined for coordinates.");
return;
}
console.log("TheKarte-csv2kml: column latitude: " + columnLatitude + " and column longitude: " + columnLongitude);
if (event.dataTransfer.types.indexOf("Files") >= 0) {
const files = event.dataTransfer.files;
for (let i = 0; i < files.length; i++) {
console.log("TheKarte-csv2kml: got file (" + files[i].name + ").");
let fileSuffix = files[i].name.split('.').pop();
if (fileSuffix === undefined)
fileSuffix = "";
let format = null;
let filenameFormat = null;
switch (fileSuffix.toLowerCase()) {
case "csv":
format = convertCSV2KML;
filenameFormat = function(filename) {
return filename + ".kml"
};
break;
case "json":
format = convertJSON2KML;
filenameFormat = function(filename) {
return filename + ".kml"
};
break;
case "kml":
format = convertKML2CSV;
filenameFormat = function(filename) {
return filename + ".csv"
};
break;
default:
console.error("TheKarte-csv2kml: don't know how to read file with suffix: " + fileSuffix);
break
}
let reader = new FileReader();
reader.onload = function() {
let contentFormatted = format(reader.result, columnLatitude, columnLongitude);
if (contentFormatted === undefined) {
console.error("TheKarte-csv2kml: could not convert data.");
return;
}
console.log("TheKarte-csv2kml: content of " + reader.filename);
console.log(contentFormatted);
TheKarteHelper_ExportString(filenameFormat(reader.filename), contentFormatted);
};
reader.onerror = function() {
console.log("TheKarte-csv2kml: error during reading: " + reader.error);
};
reader.filename = files[i].name;
reader.readAsText(files[i]);
}
}
}
/**
Converts loaded CSV data into KML string.
Uses PapaParse to parse CSV.
@param {string} csvString CSV data as string.
@param {string} columnLatitude The CSV column containing the latitude.
@param {string} columnLongitude The CSV column containing the longitude.
@return {string} The KML data OR undefined.
*/
function convertCSV2KML(csvString, columnLatitude, columnLongitude) {
const csvPapa = Papa.parse(csvString, {
header: true,
trimHeader: true,
dynamicTyping: true,
skipEmptyLines: 'greedy'
});
const csvData = csvPapa.data;
return converArray2KML(csvData, columnLatitude, columnLongitude);
}
/**
Converts loaded JSON data into KML string.
@param {string} JSONString JSON data as string.
@param {string} columnLatitude The JSON field containing the latitude.
@param {string} columnLongitude The JSON field containing the longitude.
@return {string} The KML data OR undefined.
*/
function convertJSON2KML(jsonString, columnLatitude, columnLongitude) {
const jsonData = JSON.parse(jsonString);
return converArray2KML(jsonData, columnLatitude, columnLongitude);
}
/**
Converts JavaScript Array<Map> data into KML string.
@param {string} arrayData Array<Map>.
@param {string} columnLatitude The Array field containing the latitude.
@param {string} columnLongitude The Array field containing the longitude.
@return {string} The KML data OR undefined.
*/
function converArray2KML(arrayData, columnLatitude, columnLongitude) {
console.log("TheKarte-csv2kml: got " + arrayData.length + " rows to convert into KML.");
//Converting to Openlayers features.
const features = new Array();
for (let i = 0; i < arrayData.length; i++) {
if (arrayData[i][columnLatitude] && arrayData[i][columnLongitude]) {
let latitude = string2number(arrayData[i][columnLatitude]);
let longitude = string2number(arrayData[i][columnLongitude]);
console.info("csv2kml: got (" + latitude + ", " + longitude + ")");
let point = new ol.geom.Point([latitude, longitude]);
let feature = new ol.Feature(point);
delete arrayData[i][columnLatitude];
delete arrayData[i][columnLongitude];
feature.setProperties(arrayData[i]);
features.push(feature);
}
}
return new ol.format.KML().writeFeatures(features);
}
/**
Converts loaded KML data into CSV string.
Uses PapaParse to unparse CSV.
@param {string} kmlString KML data as string.
@param {string} columnLatitude The CSV column containing the latitude.
@param {string} columnLongitude The CSV column containing the longitude.
@return {string} The CSV data OR undefined.
*/
function convertKML2CSV(kmlString, columnLatitude, columnLongitude) {
const features = new ol.format.KML().readFeatures(kmlString);
console.log("TheKarte-csv2kml: got " + features.length + " features to convert into CSV.");
//Converting to Openlayers features.
const featuresCSV = new Array();
for (let i = 0; i < features.length; i++) {
let coordinates = features[i].getGeometry().getCoordinates();
//Get coordinates
let featureData = {};
featureData[columnLatitude] = coordinates[0];
featureData[columnLongitude] = coordinates[1];
//Get data
featureProperties = features[i].getProperties();
for (let key in featureProperties) {
if (key != features[i].getGeometryName())
featureData[key] = featureProperties[key];
}
featuresCSV.push(featureData);
}
const exportString = Papa.unparse(featuresCSV, {
quotes: false,
quoteChar: '"',
escapeChar: '"',
delimiter: ",",
header: true,
newline: "\r\n"
});
return exportString;
}
/**
Localization: parses stringified number.
Accounts for european decimal separator before parsing.
@param {string} str One stringified number.
*/
function string2number(str) {
if (typeof str === 'string' && str.includes(',')) {
return str.replace(',', '.');
}
return str;
}
</script>
</head>
<body style="height:100%; margin: 0;">
<div style="position: fixed; height: 100%; width: 100%; text-align: center; vertical-align: center">
<h1>TheKarte: CSV2KML</h1>
<p>
How to use:
<ol>
<li>Set the columns for longitude and latitude</li>
<li>Drag and drop the <i>CSV files</i> or <i>KML files</i> to be converted onto the drop area.</li>
<li>Each file is processed individually and automatically downloaded to your download-folder.</li>
</ol>
<b>NOTE:</b> If something went wrong, no file is downloaded.
</p>
<table style="margin-left: auto; margin-right:auto;">
<tr>
<td align="right"><label for="choice-latitude">Latitude column:</label></td>
<td align="left">
<input list="latitude-flavors" id="choice-latitude" />
<datalist id="latitude-flavors">
<option value="y">
<option value="Latitude">
<option value="lat">
</datalist>
</td>
</tr>
<tr>
<td align="right"><label for="choice-longitude">Longitude column:</label></td>
<td align="left">
<input list="longitude-flavors" id="choice-longitude" />
<datalist id="longitude-flavors">
<option value="x">
<option value="longitude">
<option value="lon">
<option value="lng">
</datalist>
</td>
</tr>
</table>
<div class="dropArea" ondragover="loadAllowDrop(event)" ondrop="loadDrop(event)">
<div>DROP AREA</div>
</div>
</div>
</body>
</html>