-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconverter.html
More file actions
187 lines (172 loc) · 7.44 KB
/
Copy pathconverter.html
File metadata and controls
187 lines (172 loc) · 7.44 KB
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
<!--
~ Copyright (C) 2014-2015 Stichting Mapcode Foundation (http://www.mapcode.com)
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title>Simple Mapcode CSV Converter</title>
</head>
<body>
<h1>Simple Mapcode CSV Converter</h1>
<p>
This page allows you to upload a CSV file with coordinates and convert
them to mapcodes, also in CSV format. The input CSV file needs to have 3 columns:<br/>
<tt>latitude</tt> (in degrees), <tt>longitude</tt> (in degrees), <tt>territory</tt> (optional)<br/>
<i>Field delimiter:</i> <b>,</b> (comma)<br/>
<i>Decimal point:</i> <b>.</b> (dot)<br/>
</p>
<p>
The column <b>context</b> is used to specify the
<a href="http://www.mapcode.com/isos.html">territory code</a> for which the shortest mapcode should be displayed.
For example, for South Africa, this would be <b>ZAF</b>. Note that if you leave the field empty, the result might
not be what you expected. The system may choose a nearby territory instead.
</p>
<p>Example of an input CSV file:</p>
<p><tt>
<b>-28.700225, 28.602905, </b></tt><i> (note the territory is missing)</i><tt><br/>
<b>-28.700225, 28.602905, ZAF</b><br/>
<b>-28.433676, 19.986191, ZAF</b>
</tt></p>
<p>This produces the following output:</p>
<p><tt>
latitude,longitude,mapcode,international<br/>
-28.700225,28.602905,LSO MRG.8XX,8KYVG.4C8Y<br/>
-28.700225,28.602905,ZAF QNW2.Y72,8KYVG.4C8Y<br/>
-28.433676,19.986191,ZAF 6MQP.506,8K70Z.7PB4
</tt></p>
<p>
The output is formatted in CSV format. In some browsers this will automatically be saved to a file called
<b>mapcodes.csv</b> in your Downloads folder.
However, not all browsers support this functionality. Some will show the contents of the file on your screen
instead and you'll need to copy or save it to a CSV file yourself then.
(Note: We've tried this with Google Chrome and it seems to work fine.)
</p>
<p>
Troubleshooting: If you select a CSV file and nothing happens, the format of the input file is not OK.
It should contain exactly 3 columns, separated by comma's and the latitude/longitudes must use decimal points.
Try saving the example text from above in a file and use that first, in that case.<br/>
Note that this page is just an example. If you wish to have better error checking and more options, feel
free to use and adapt the source files to your needs.
They can be found <a href="https://github.com/mapcode-foundation/mapcode-js/tree/master/csv">here</a> on Github.
</p>
<p>
Useful links:<br/>
<a href="http://www.mapcode.com/isos.html">List of territory codes</a><br/>
<a href="https://github.com/mapcode-foundation/mapcode-js/tree/master/csv">Source files on Github</a>
</p>
<div id="inputs" class="clearfix">
<input type="file" id="files" name="files[]" multiple/>
</div>
<hr/>
<output id="list">
</output>
<hr/>
<table id="contents" style="width:100%; height:400px;" border>
</table>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="jquery.csv.js"></script>
<script src="FileSaver.js"></script>
<script src="../ndata.js" type="text/javascript"></script>
<script src="../mapcode.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
if (isAPIAvailable()) {
$('#files').bind('change', handleFileSelect);
}
});
function isAPIAvailable() {
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
// Great success! All the File APIs are supported.
return true;
} else {
// source: File API availability - http://caniuse.com/#feat=fileapi
// source: <output> availability - http://html5doctor.com/the-output-element/
document.writeln('The HTML5 APIs used in this form are only available in the following browsers:<br />');
// 6.0 File API & 13.0 <output>
document.writeln(' - Google Chrome: 13.0 or later<br />');
// 3.6 File API & 6.0 <output>
document.writeln(' - Mozilla Firefox: 6.0 or later<br />');
// 10.0 File API & 10.0 <output>
document.writeln(' - Internet Explorer: Not supported (partial support expected in 10.0)<br />');
// ? File API & 5.1 <output>
document.writeln(' - Safari: Not supported<br />');
// ? File API & 9.2 <output>
document.writeln(' - Opera: Not supported');
return false;
}
}
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
var file = files[0];
// read the file metadata
var output = ''
// read the file contents
printTable(file);
// post the results
$('#list').append(output);
}
function printTable(file) {
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function (event) {
var csv = event.target.result;
var data = $.csv.toArrays(csv);
var html = '';
var csv = 'latitude,longitude,territory,mapcode,international\r\n';
html += '<tr><td>latitude</td><td>longitude</td><td>territory</td><td>mapcode</td><td>international</td>/tr>';
for (var row in data) {
var lat = Number(data[row][0]);
var lon = Number(data[row][1]);
var context = data[row][2].trim();
html += '<tr>\r\n';
html += '<td>' + lat + '</td>\r\n';
html += '<td>' + lon + '</td>\r\n';
csv += lat + ',' + lon + ',';
var r;
if (context.length == 0) {
r = encodeShortest(lat, lon);
} else {
r = encodeShortest(lat, lon, context);
}
if (r.length > 0) {
var shortest = r[0];
html += '<td>' + shortest.territoryAlphaCode + '</td>\r\n';
html += '<td>' + shortest.mapcode + '</td>\r\n';
csv += shortest.territoryAlphaCode + ',' + shortest.mapcode + ',';
} else {
html += "<td></td>\r\n";
html += "<td></td>\r\n";
csv += ",,";
}
var international = encodeInternational(lat, lon)[0];
html += '<td>' + international.mapcode + '</td>\r\n';
csv += international.mapcode + '\r\n';
html += '</tr>\r\n';
}
$('#contents').html(html);
var blob = new Blob([csv], {
type: "text/csv;charset=utf-8;",
});
saveAs(blob, "mapcodes.csv");
};
reader.onerror = function () {
alert('Unable to read ' + file.fileName);
};
}
</script>
</body>
</html>