/
csv2geojson.js
203 lines (185 loc) · 7.49 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
var expect = require('expect.js'),
csv2geojson = require('../'),
fs = require('fs');
function textFile(f) {
return fs.readFileSync('./test/data/' + f, 'utf8');
}
function jsonFile(f) {
return JSON.parse(fs.readFileSync('./test/data/' + f, 'utf8'));
}
describe('csv2geojson', function() {
describe('#isLat', function() {
it('detects latitude fields', function() {
expect(csv2geojson.isLat('latitude')).to.eql(true);
expect(csv2geojson.isLat('lat')).to.eql(true);
expect(csv2geojson.isLat('Lat')).to.eql(true);
expect(csv2geojson.isLat('Latitude')).to.eql(true);
});
it('prefixed and postfixed', function() {
expect(csv2geojson.isLat('is_lat')).to.eql(true);
expect(csv2geojson.isLat('is_lat_field')).to.eql(true);
});
it('does not accept false positives', function() {
expect(csv2geojson.isLat('nothingoftheabove')).to.eql(false);
expect(csv2geojson.isLat('some other thing')).to.eql(false);
});
});
describe('#csv', function() {
it('handles empty input', function() {
expect(csv2geojson.csv('')).to.eql([]);
});
it('handles simple fields', function() {
expect(csv2geojson.csv('a,b\n1,2')).to.eql([{a: '1', b: '2'}]);
});
});
describe('#sexagesimal', function() {
it('degrees', function(done) {
csv2geojson.csv2geojson(textFile('degrees.csv'), function(err, data) {
expect(data).to.eql(jsonFile('degrees.geojson'));
done();
});
});
it('minutes', function(done) {
csv2geojson.csv2geojson(textFile('minutes.csv'), function(err, data) {
expect(data).to.eql(jsonFile('minutes.geojson'));
done();
});
});
});
describe('#csv2geojson', function() {
it('handles empty input', function() {
csv2geojson.csv2geojson('', function(err, data) {
expect(data).to.eql({
type: 'FeatureCollection',
features: []
});
});
});
it('detects lat and lon', function(done) {
csv2geojson.csv2geojson(textFile('simple.csv'), function(err, data) {
expect(data).to.eql(jsonFile('simple.geojson'));
done();
});
});
it('with space before columns', function(done) {
csv2geojson.csv2geojson(textFile('space_column.csv'), function(err, data) {
expect(data).to.eql(jsonFile('simple_space.geojson'));
done();
});
});
it('with lng instead of lon', function(done) {
csv2geojson.csv2geojson(textFile('lng.csv'), function(err, data) {
expect(data).to.eql(jsonFile('lng.geojson'));
done();
});
});
it('with includeLatLon option', function(done) {
csv2geojson.csv2geojson(textFile('includeLatLon.csv'), { includeLatLon: true }, function(err, data) {
expect(data).to.eql(jsonFile('includeLatLon.geojson'));
done();
});
});
describe('delimiters', function() {
it('|', function(done) {
csv2geojson.csv2geojson(textFile('simple.pipe.dsv'), { delimiter: '|' },
function(err, data) {
expect(data).to.eql(jsonFile('simple.geojson'));
done();
});
});
it(',', function(done) {
csv2geojson.csv2geojson(textFile('simple.csv'), { delimiter: ',' },
function(err, data) {
expect(data).to.eql(jsonFile('simple.geojson'));
done();
});
});
it(';', function(done) {
csv2geojson.csv2geojson(textFile('simple.semicolon.dsv'), { delimiter: ';' },
function(err, data) {
expect(data).to.eql(jsonFile('simple.geojson'));
done();
});
});
it('tab', function(done) {
csv2geojson.csv2geojson(textFile('simple.tsv'), { delimiter: '\t' },
function(err, data) {
expect(data).to.eql(jsonFile('simple.geojson'));
done();
});
});
describe('auto', function() {
['simple.tsv', 'simple.semicolon.dsv', 'simple.csv', 'simple.pipe.dsv'].forEach(function(f) {
it('auto with ' + f, function(done) {
csv2geojson.csv2geojson(textFile(f), { delimiter: 'auto' },
function(err, data) {
expect(data).to.eql(jsonFile('simple.geojson'));
done();
});
});
});
});
});
describe('#auto', function() {
['simple.tsv', 'simple.semicolon.dsv', 'simple.csv', 'simple.pipe.dsv'].forEach(function(f) {
it('auto with ' + f, function() {
expect(csv2geojson.auto(textFile(f))).to.eql(jsonFile('simple.json'));
});
});
});
describe('#auto->csv2geojson', function() {
['simple.tsv', 'simple.semicolon.dsv', 'simple.csv', 'simple.pipe.dsv'].forEach(function(f) {
it('auto and then csv2 with ' + f, function(done) {
csv2geojson.csv2geojson(csv2geojson.auto(textFile(f)), function(err, data) {
expect(data).to.eql(jsonFile('simple.geojson'));
done();
});
});
});
});
it('custom field names', function(done) {
csv2geojson.csv2geojson('y|x|name\n1|2|3', {
delimiter: '|',
latfield: 'y',
lonfield: 'x'
}, function(err, data) {
expect(data).to.eql({
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: { name: '3' },
geometry: {
type: 'Point',
coordinates: [2, 1]
}
}]
});
done();
});
});
it('accepts a parsed object', function() {
csv2geojson.csv2geojson(csv2geojson.csv(textFile('simple.csv')), function(err, data) {
expect(data).to.eql(jsonFile('simple.geojson'));
});
});
it('reports bad coordinates', function() {
csv2geojson.csv2geojson(csv2geojson.csv(textFile('bad_coord.csv')), function(err, data) {
expect(data).to.eql({
type: 'FeatureCollection',
features: []
});
expect(err).to.eql(jsonFile('bad_coord.error.json'));
});
});
it('returns an error on not finding fields', function() {
csv2geojson.csv2geojson(csv2geojson.csv('name\nfoo'), function(err, data) {
expect(err).to.eql({
type: 'Error',
message: 'Latitude and longitude fields not present',
data: [{name:'foo'}],
fields: ['name']
});
});
});
});
});