-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2geojson.js
217 lines (185 loc) · 9.86 KB
/
csv2geojson.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
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
(function(e){if("function"==typeof bootstrap)bootstrap("csv2geojson",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeCsv2geojson=e}else"undefined"!=typeof window?window.csv2geojson=e():global.csv2geojson=e()})(function(){var define,ses,bootstrap,module,exports;
return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var dsv = require('dsv'),
sexagesimal = require('sexagesimal');
function isLat(f) { return !!f.match(/(Lat)(itude)?/gi); }
function isLon(f) { return !!f.match(/(L)(on|ng)(gitude)?/i); }
function keyCount(o) {
return (typeof o == 'object') ? Object.keys(o).length : 0;
}
function autoDelimiter(x) {
var delimiters = [',', ';', '\t', '|'];
var results = [];
delimiters.forEach(function(delimiter) {
var res = dsv(delimiter).parse(x);
if (res.length >= 1) {
var count = keyCount(res[0]);
for (var i = 0; i < res.length; i++) {
if (keyCount(res[i]) !== count) return;
}
}
results.push({
delimiter: delimiter,
arity: Object.keys(res[0]).length,
});
});
if (results.length) {
return results.sort(function(a, b) {
return b.arity - a.arity;
})[0].delimiter;
} else {
return null;
}
}
function auto(x) {
var delimiter = autoDelimiter(x);
if (!delimiter) return null;
return dsv(delimiter).parse(x);
}
function csv2geojson(x, options, callback) {
if (!callback) {
callback = options;
options = {};
}
options.delimiter = options.delimiter || ',';
var latfield = options.latfield || '',
lonfield = options.lonfield || '';
var features = [],
featurecollection = { type: 'FeatureCollection', features: features };
if (options.delimiter === 'auto' && typeof x == 'string') {
options.delimiter = autoDelimiter(x);
if (!options.delimiter) return callback({
type: 'Error',
message: 'Could not autodetect delimiter'
});
}
var parsed = (typeof x == 'string') ? dsv(options.delimiter).parse(x) : x;
if (!parsed.length) return callback(null, featurecollection);
if (!latfield || !lonfield) {
for (var f in parsed[0]) {
if (!latfield && isLat(f)) latfield = f;
if (!lonfield && isLon(f)) lonfield = f;
}
if (!latfield || !lonfield) {
var fields = [];
for (var k in parsed[0]) fields.push(k);
return callback({
type: 'Error',
message: 'Latitude and longitude fields not present',
data: parsed,
fields: fields
});
}
}
var errors = [];
for (var i = 0; i < parsed.length; i++) {
if (parsed[i][lonfield] !== undefined &&
parsed[i][lonfield] !== undefined) {
var lonk = parsed[i][lonfield],
latk = parsed[i][latfield],
lonf, latf,
a;
a = sexagesimal(lonk, 'EW');
if (a) lonk = a;
a = sexagesimal(latk, 'NS');
if (a) latk = a;
lonf = parseFloat(lonk);
latf = parseFloat(latk);
if (isNaN(lonf) ||
isNaN(latf)) {
errors.push({
message: 'A row contained an invalid value for latitude or longitude',
row: parsed[i]
});
} else {
if (!options.includeLatLon) {
delete parsed[i][lonfield];
delete parsed[i][latfield];
}
features.push({
type: 'Feature',
properties: parsed[i],
geometry: {
type: 'Point',
coordinates: [
parseFloat(lonf),
parseFloat(latf)
]
}
});
}
}
}
callback(errors.length ? errors: null, featurecollection);
}
function toLine(gj) {
var features = gj.features;
var line = {
type: 'Feature',
geometry: {
type: 'LineString',
coordinates: []
}
};
for (var i = 0; i < features.length; i++) {
line.geometry.coordinates.push(features[i].geometry.coordinates);
}
line.properties = features[0].properties;
return {
type: 'FeatureSet',
features: [line]
};
}
function toPolygon(gj) {
var features = gj.features;
var poly = {
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [[]]
}
};
for (var i = 0; i < features.length; i++) {
poly.geometry.coordinates[0].push(features[i].geometry.coordinates);
}
poly.properties = features[0].properties;
return {
type: 'FeatureSet',
features: [poly]
};
}
module.exports = {
isLon: isLon,
isLat: isLat,
csv: dsv.csv.parse,
tsv: dsv.tsv.parse,
dsv: dsv,
auto: auto,
csv2geojson: csv2geojson,
toLine: toLine,
toPolygon: toPolygon
};
},{"dsv":2,"sexagesimal":3}],2:[function(require,module,exports){
var fs = require("fs");
module.exports = new Function("dsv.version = \"0.0.3\";\n\ndsv.tsv = dsv(\"\\t\");\ndsv.csv = dsv(\",\");\n\nfunction dsv(delimiter) {\n var dsv = {},\n reFormat = new RegExp(\"[\\\"\" + delimiter + \"\\n]\"),\n delimiterCode = delimiter.charCodeAt(0);\n\n dsv.parse = function(text, f) {\n var o;\n return dsv.parseRows(text, function(row, i) {\n if (o) return o(row, i - 1);\n var a = new Function(\"d\", \"return {\" + row.map(function(name, i) {\n return JSON.stringify(name) + \": d[\" + i + \"]\";\n }).join(\",\") + \"}\");\n o = f ? function(row, i) { return f(a(row), i); } : a;\n });\n };\n\n dsv.parseRows = function(text, f) {\n var EOL = {}, // sentinel value for end-of-line\n EOF = {}, // sentinel value for end-of-file\n rows = [], // output rows\n N = text.length,\n I = 0, // current character index\n n = 0, // the current line number\n t, // the current token\n eol; // is the current token followed by EOL?\n\n function token() {\n if (I >= N) return EOF; // special case: end of file\n if (eol) return eol = false, EOL; // special case: end of line\n\n // special case: quotes\n var j = I;\n if (text.charCodeAt(j) === 34) {\n var i = j;\n while (i++ < N) {\n if (text.charCodeAt(i) === 34) {\n if (text.charCodeAt(i + 1) !== 34) break;\n ++i;\n }\n }\n I = i + 2;\n var c = text.charCodeAt(i + 1);\n if (c === 13) {\n eol = true;\n if (text.charCodeAt(i + 2) === 10) ++I;\n } else if (c === 10) {\n eol = true;\n }\n return text.substring(j + 1, i).replace(/\"\"/g, \"\\\"\");\n }\n\n // common case: find next delimiter or newline\n while (I < N) {\n var c = text.charCodeAt(I++), k = 1;\n if (c === 10) eol = true; // \\n\n else if (c === 13) { eol = true; if (text.charCodeAt(I) === 10) ++I, ++k; } // \\r|\\r\\n\n else if (c !== delimiterCode) continue;\n return text.substring(j, I - k);\n }\n\n // special case: last token before EOF\n return text.substring(j);\n }\n\n while ((t = token()) !== EOF) {\n var a = [];\n while (t !== EOL && t !== EOF) {\n a.push(t);\n t = token();\n }\n if (f && !(a = f(a, n++))) continue;\n rows.push(a);\n }\n\n return rows;\n };\n\n dsv.format = function(rows) {\n if (Array.isArray(rows[0])) return dsv.formatRows(rows); // deprecated; use formatRows\n var fieldSet = {}, fields = [];\n\n // Compute unique fields in order of discovery.\n rows.forEach(function(row) {\n for (var field in row) {\n if (!(field in fieldSet)) {\n fields.push(fieldSet[field] = field);\n }\n }\n });\n\n return [fields.map(formatValue).join(delimiter)].concat(rows.map(function(row) {\n return fields.map(function(field) {\n return formatValue(row[field]);\n }).join(delimiter);\n })).join(\"\\n\");\n };\n\n dsv.formatRows = function(rows) {\n return rows.map(formatRow).join(\"\\n\");\n };\n\n function formatRow(row) {\n return row.map(formatValue).join(delimiter);\n }\n\n function formatValue(text) {\n return reFormat.test(text) ? \"\\\"\" + text.replace(/\\\"/g, \"\\\"\\\"\") + \"\\\"\" : text;\n }\n\n return dsv;\n}\n" + ";return dsv")();
},{"fs":4}],3:[function(require,module,exports){
module.exports = function(x, dims) {
if (!dims) dims = 'NSEW';
if (typeof x !== 'string') return null;
var r = /^([0-9.]+)°? *(?:([0-9.]+)['’′‘] *)?(?:([0-9.]+)(?:''|"|”|″) *)?([NSEW])?/,
m = x.match(r);
if (!m) return null;
else if (m[4] && dims.indexOf(m[4]) === -1) return null;
else return (((m[1]) ? parseFloat(m[1]) : 0) +
((m[2] ? parseFloat(m[2]) / 60 : 0)) +
((m[3] ? parseFloat(m[3]) / 3600 : 0))) *
((m[4] && m[4] === 'S' || m[4] === 'W') ? -1 : 1);
};
},{}],4:[function(require,module,exports){
// not implemented
// The reason for having an empty file and not throwing is to allow
// untraditional implementation of this module.
},{}]},{},[1])
(1)
});
;