Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add latLngProperties option, default to removing parsed lat/lng from feature properties #15

Merged
merged 6 commits into from
Sep 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions csv2geojson.js
Original file line number Diff line number Diff line change
@@ -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.length;s++)i(n[s]);return i})({1:[function(require,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){
// nothing to see here... no file methods for the browser

},{}],"dsv":[function(require,module,exports){
module.exports=require('dvu7iV');
},{}],3:[function(require,module,exports){
var dsv = require('dsv'),
sexagesimal = require('sexagesimal');

Expand Down Expand Up @@ -113,6 +118,11 @@ function csv2geojson(x, options, callback) {
row: parsed[i]
});
} else {
if (!options.includeLatLon) {
delete parsed[i][lonfield];
delete parsed[i][latfield];
}

features.push({
type: 'Feature',
properties: parsed[i],
Expand Down Expand Up @@ -181,17 +191,12 @@ module.exports = {
toPolygon: toPolygon
};

},{"dsv":"kPifln","sexagesimal":2}],"dsv":[function(require,module,exports){
module.exports=require('kPifln');
},{}],"kPifln":[function(require,module,exports){
},{"dsv":"dvu7iV","sexagesimal":5}],"dvu7iV":[function(require,module,exports){
var fs = require("fs");

module.exports = new Function("dsv.version = \"0.0.2\";\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":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;
Expand All @@ -205,6 +210,6 @@ module.exports = function(x, dims) {
((m[4] && m[4] === 'S' || m[4] === 'W') ? -1 : 1);
};

},{}]},{},[1])(1)
},{}]},{},[3])(3)
});
;
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ function csv2geojson(x, options, callback) {
row: parsed[i]
});
} else {
if (!options.includeLatLon) {
delete parsed[i][lonfield];
delete parsed[i][latfield];
}

features.push({
type: 'Feature',
properties: parsed[i],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "csv2geojson",
"version": "3.4.1",
"version": "3.5.1",
"description": "convert CSV files to GeoJSON",
"main": "index.js",
"repository": {
Expand Down
9 changes: 8 additions & 1 deletion test/csv2geojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ describe('csv2geojson', function() {
});
});

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: '|' },
Expand Down Expand Up @@ -155,7 +162,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]
Expand Down
2 changes: 1 addition & 1 deletion test/data/degrees.geojson
Original file line number Diff line number Diff line change
@@ -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]}}]}
2 changes: 2 additions & 0 deletions test/data/includeLatLon.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lat,lng,name
1,2,3
15 changes: 15 additions & 0 deletions test/data/includeLatLon.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"lat": "1",
"lng": "2",
"name": "3"
},
"geometry": {
"type": "Point",
"coordinates": [2, 1]
}
}]
}
4 changes: 1 addition & 3 deletions test/data/lng.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
"features": [{
"type": "Feature",
"properties": {
"name": "3",
"lat": "1",
"lng": "2"
"name": "3"
},
"geometry": {
"type": "Point",
Expand Down
2 changes: 1 addition & 1 deletion test/data/minutes.geojson
Original file line number Diff line number Diff line change
@@ -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]}}]}
4 changes: 1 addition & 3 deletions test/data/simple.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
"features": [{
"type": "Feature",
"properties": {
"name": "3",
"lat": "1",
"lon": "2"
"name": "3"
},
"geometry": {
"type": "Point",
Expand Down
2 changes: 0 additions & 2 deletions test/data/simple_space.geojson
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"features": [{
"type": "Feature",
"properties": {
" lat": "1",
" lon": "2",
" name": "3"
},
"geometry": {
Expand Down