Skip to content

Commit

Permalink
Allow geometry to be null as by spec
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanw committed Jun 25, 2016
1 parent 1dc88ac commit ee8d854
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
24 changes: 15 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,30 @@ function csv2geojson(x, options, callback) {

if (!parsed.length) return callback(null, featurecollection);

var errors = [];

var noGeometry = false;

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: deleteColumns(parsed),
fields: fields
});
noGeometry = true;
}
}

var errors = [];
if (noGeometry) {
for (var i = 0; i < parsed.length; i++) {
features.push({
type: 'Feature',
properties: parsed[i],
geometry: null
});
}
callback(errors.length ? errors: null, featurecollection);
}

for (var i = 0; i < parsed.length; i++) {
if (parsed[i][lonfield] !== undefined &&
Expand Down
11 changes: 3 additions & 8 deletions test/csv2geojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,9 @@ describe('csv2geojson', function() {
});

it('returns an error on not finding fields', function() {
csv2geojson.csv2geojson('name\nfoo', function(err, data) {
expect(err).to.eql({
type: 'Error',
message: 'Latitude and longitude fields not present',
data: [{name:'foo'}],
fields: ['name']
});
});
csv2geojson.csv2geojson(textFile('geometry_null.csv'), function(err, data) {
expect(data).to.eql(jsonFile('geometry_null.geojson'));
});
});
});
});
2 changes: 2 additions & 0 deletions test/data/geometry_null.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name
foo
10 changes: 10 additions & 0 deletions test/data/geometry_null.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"name": "foo"
},
"geometry": null
}]
}

0 comments on commit ee8d854

Please sign in to comment.