diff --git a/index.js b/index.js index 6924e92..f203735 100644 --- a/index.js +++ b/index.js @@ -80,6 +80,8 @@ function csv2geojson(x, options, callback) { } options.delimiter = options.delimiter || ','; + options.parseLatLon = options.parseLatLon || parseFloat; + options.sexagesimal = options.sexagesimal !== false; var latfield = options.latfield || '', lonfield = options.lonfield || '', @@ -128,6 +130,7 @@ function csv2geojson(x, options, callback) { if (!latfield) latfield = guessLatHeader(parsed[0]); if (!lonfield) lonfield = guessLonHeader(parsed[0]); + var noGeometry = (!latfield || !lonfield); if (noGeometry) { @@ -151,13 +154,15 @@ function csv2geojson(x, options, callback) { lonf, latf, a; - a = sexagesimal(lonk, 'EW'); - if (a) lonk = a; - a = sexagesimal(latk, 'NS'); - if (a) latk = a; + if (options.sexagesimal) { + a = sexagesimal(lonk, 'EW'); + if (a) lonk = a; + a = sexagesimal(latk, 'NS'); + if (a) latk = a; + } - lonf = parseFloat(lonk); - latf = parseFloat(latk); + lonf = options.parseLatLon(lonk); + latf = options.parseLatLon(latk); if (isNaN(lonf) || isNaN(latf)) { @@ -178,8 +183,8 @@ function csv2geojson(x, options, callback) { geometry: { type: 'Point', coordinates: [ - parseFloat(lonf), - parseFloat(latf) + lonf, + latf ] } }); diff --git a/test/csv2geojson.js b/test/csv2geojson.js index 5436da2..a134ee7 100644 --- a/test/csv2geojson.js +++ b/test/csv2geojson.js @@ -258,5 +258,26 @@ describe('csv2geojson', function () { expect(data).to.eql(jsonFile('geometry_null.geojson')); }); }); + + it('allow custom parseLatLng function', function (done) { + csv2geojson.csv2geojson('Lat;Long;name\n1,234;2,345;foobar', { + delimiter: ';', + sexagesimal: false, + parseLatLon: (raw) => parseFloat(raw.replace(',', '.')), + }, function (err, data) { + expect(data).to.eql({ + type: 'FeatureCollection', + features: [{ + type: 'Feature', + properties: {name: 'foobar'}, + geometry: { + type: 'Point', + coordinates: [2.345, 1.234] + } + }] + }); + done(); + }); + }); }); });