From 9926b06ec7fc96a3ffdbddb9fe25967245e9fa66 Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Wed, 11 Sep 2013 12:14:23 -0400 Subject: [PATCH 1/6] add latLngProperties option, default to removing parsed lat/lng from feature properties, closes #11 --- csv2geojson.js | 23 ++++++++++++++--------- index.js | 5 +++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/csv2geojson.js b/csv2geojson.js index b74931c..501a9de 100644 --- a/csv2geojson.js +++ b/csv2geojson.js @@ -1,5 +1,10 @@ (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){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s= 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":3}],3:[function(require,module,exports){ -// nothing to see here... no file methods for the browser - -},{}],2:[function(require,module,exports){ +},{"fs":1}],5:[function(require,module,exports){ module.exports = function(x, dims) { if (!dims) dims = 'NSEW'; if (typeof x !== 'string') return null; @@ -205,6 +210,6 @@ module.exports = function(x, dims) { ((m[4] && m[4] === 'S' || m[4] === 'W') ? -1 : 1); }; -},{}]},{},[1])(1) +},{}]},{},[3])(3) }); ; \ No newline at end of file diff --git a/index.js b/index.js index b188199..064ee06 100644 --- a/index.js +++ b/index.js @@ -111,6 +111,11 @@ function csv2geojson(x, options, callback) { row: parsed[i] }); } else { + if (!options.latLngProperties) { + delete parsed[i][lonfield]; + delete parsed[i][latfield]; + } + features.push({ type: 'Feature', properties: parsed[i], From 3f4a0422f0652275ab0580f84dced7e3fc4a96b8 Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Wed, 11 Sep 2013 12:22:30 -0400 Subject: [PATCH 2/6] 3.5.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index be2a2b7..1324024 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "csv2geojson", - "version": "3.4.1", + "version": "3.5.0", "description": "convert CSV files to GeoJSON", "main": "index.js", "repository": { From 3674b27a5782a8e2ee1323021402f001ef3f9ce2 Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Wed, 11 Sep 2013 12:48:50 -0400 Subject: [PATCH 3/6] updating tests --- test/csv2geojson.js | 2 +- test/data/degrees.geojson | 2 +- test/data/lng.geojson | 4 +--- test/data/minutes.geojson | 2 +- test/data/simple.geojson | 4 +--- test/data/simple_space.geojson | 2 -- 6 files changed, 5 insertions(+), 11 deletions(-) diff --git a/test/csv2geojson.js b/test/csv2geojson.js index ad36ef5..52cf8e0 100644 --- a/test/csv2geojson.js +++ b/test/csv2geojson.js @@ -155,7 +155,7 @@ describe('csv2geojson', function() { type: 'FeatureCollection', features: [{ type: 'Feature', - properties: { name: '3', y: '1', x: '2' }, + properties: { name: '3' }, geometry: { type: 'Point', coordinates: [2, 1] diff --git a/test/data/degrees.geojson b/test/data/degrees.geojson index 3bc74db..0a3a718 100644 --- a/test/data/degrees.geojson +++ b/test/data/degrees.geojson @@ -1 +1 @@ -{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"lat":"23°N","lon":"26°W","name":"Chester"},"geometry":{"type":"Point","coordinates":[-26,23]}}]} +{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"name":"Chester"},"geometry":{"type":"Point","coordinates":[-26,23]}}]} diff --git a/test/data/lng.geojson b/test/data/lng.geojson index 5bf2be0..5dae6b0 100644 --- a/test/data/lng.geojson +++ b/test/data/lng.geojson @@ -3,9 +3,7 @@ "features": [{ "type": "Feature", "properties": { - "name": "3", - "lat": "1", - "lng": "2" + "name": "3" }, "geometry": { "type": "Point", diff --git a/test/data/minutes.geojson b/test/data/minutes.geojson index 04109cd..75585b9 100644 --- a/test/data/minutes.geojson +++ b/test/data/minutes.geojson @@ -1 +1 @@ -{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"lat":"40° 11′","lon":"40° 11' 15\"","name":"Chester"},"geometry":{"type":"Point","coordinates":[40.1875,40.18333333333333]}}]} +{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"name":"Chester"},"geometry":{"type":"Point","coordinates":[40.1875,40.18333333333333]}}]} diff --git a/test/data/simple.geojson b/test/data/simple.geojson index 32626f5..5dae6b0 100644 --- a/test/data/simple.geojson +++ b/test/data/simple.geojson @@ -3,9 +3,7 @@ "features": [{ "type": "Feature", "properties": { - "name": "3", - "lat": "1", - "lon": "2" + "name": "3" }, "geometry": { "type": "Point", diff --git a/test/data/simple_space.geojson b/test/data/simple_space.geojson index 2db7307..7f6d3d9 100644 --- a/test/data/simple_space.geojson +++ b/test/data/simple_space.geojson @@ -3,8 +3,6 @@ "features": [{ "type": "Feature", "properties": { - " lat": "1", - " lon": "2", " name": "3" }, "geometry": { From 5430c4ff7c89f77c3c7804a3da0a2692bb2f67ad Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Wed, 11 Sep 2013 12:55:27 -0400 Subject: [PATCH 4/6] add test for latLngProperties option --- test/csv2geojson.js | 7 +++++++ test/data/latLngProperties.csv | 2 ++ test/data/latLngProperties.geojson | 15 +++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 test/data/latLngProperties.csv create mode 100644 test/data/latLngProperties.geojson diff --git a/test/csv2geojson.js b/test/csv2geojson.js index 52cf8e0..36f9a60 100644 --- a/test/csv2geojson.js +++ b/test/csv2geojson.js @@ -84,6 +84,13 @@ describe('csv2geojson', function() { }); }); + it('with latLngProperties option', function(done) { + csv2geojson.csv2geojson(textFile('latLngProperties.csv'), { latLngProperties: true }, function(err, data) { + expect(data).to.eql(jsonFile('latLngProperties.geojson')); + done(); + }); + }); + describe('delimiters', function() { it('|', function(done) { csv2geojson.csv2geojson(textFile('simple.pipe.dsv'), { delimiter: '|' }, diff --git a/test/data/latLngProperties.csv b/test/data/latLngProperties.csv new file mode 100644 index 0000000..7ed3a65 --- /dev/null +++ b/test/data/latLngProperties.csv @@ -0,0 +1,2 @@ +lat,lng,name +1,2,3 diff --git a/test/data/latLngProperties.geojson b/test/data/latLngProperties.geojson new file mode 100644 index 0000000..0d8de71 --- /dev/null +++ b/test/data/latLngProperties.geojson @@ -0,0 +1,15 @@ +{ + "type": "FeatureCollection", + "features": [{ + "type": "Feature", + "properties": { + "lat": "1", + "lng": "2", + "name": "3" + }, + "geometry": { + "type": "Point", + "coordinates": [2, 1] + } + }] +} From 8e932f396f18847f026db31f2f9e90727f69f7b2 Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Wed, 11 Sep 2013 12:59:29 -0400 Subject: [PATCH 5/6] switch latLngProperties to includeLatLon --- csv2geojson.js | 2 +- index.js | 2 +- test/csv2geojson.js | 6 +++--- test/data/{latLngProperties.csv => includeLatLon.csv} | 0 .../{latLngProperties.geojson => includeLatLon.geojson} | 0 5 files changed, 5 insertions(+), 5 deletions(-) rename test/data/{latLngProperties.csv => includeLatLon.csv} (100%) rename test/data/{latLngProperties.geojson => includeLatLon.geojson} (100%) diff --git a/csv2geojson.js b/csv2geojson.js index 501a9de..acd6cba 100644 --- a/csv2geojson.js +++ b/csv2geojson.js @@ -118,7 +118,7 @@ function csv2geojson(x, options, callback) { row: parsed[i] }); } else { - if (!options.latLngProperties) { + if (!options.includeLatLon) { delete parsed[i][lonfield]; delete parsed[i][latfield]; } diff --git a/index.js b/index.js index 064ee06..025bd61 100644 --- a/index.js +++ b/index.js @@ -111,7 +111,7 @@ function csv2geojson(x, options, callback) { row: parsed[i] }); } else { - if (!options.latLngProperties) { + if (!options.includeLatLon) { delete parsed[i][lonfield]; delete parsed[i][latfield]; } diff --git a/test/csv2geojson.js b/test/csv2geojson.js index 36f9a60..58b72f4 100644 --- a/test/csv2geojson.js +++ b/test/csv2geojson.js @@ -84,9 +84,9 @@ describe('csv2geojson', function() { }); }); - it('with latLngProperties option', function(done) { - csv2geojson.csv2geojson(textFile('latLngProperties.csv'), { latLngProperties: true }, function(err, data) { - expect(data).to.eql(jsonFile('latLngProperties.geojson')); + it('with includeLatLon option', function(done) { + csv2geojson.csv2geojson(textFile('includeLatLon.csv'), { includeLatLon: true }, function(err, data) { + expect(data).to.eql(jsonFile('includeLatLon.geojson')); done(); }); }); diff --git a/test/data/latLngProperties.csv b/test/data/includeLatLon.csv similarity index 100% rename from test/data/latLngProperties.csv rename to test/data/includeLatLon.csv diff --git a/test/data/latLngProperties.geojson b/test/data/includeLatLon.geojson similarity index 100% rename from test/data/latLngProperties.geojson rename to test/data/includeLatLon.geojson From 81c6f9dbd44a22820a549f5ca89db2d521842bb2 Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Wed, 11 Sep 2013 13:01:50 -0400 Subject: [PATCH 6/6] 3.5.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1324024..b078088 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "csv2geojson", - "version": "3.5.0", + "version": "3.5.1", "description": "convert CSV files to GeoJSON", "main": "index.js", "repository": {