Created
September 25, 2013 21:33
-
-
Save tmcw/6706363 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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,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){ | |
var dsv = require('dsv'), | |
sexagesimal = require('sexagesimal'); | |
function isLat(f) { return !!f.match(/(Lat)(itude)?/gi); } | |
function isLon(f) { return !!f.match(/(L)(on|ng)(gitude)?/i); } | |
function keyCount(o) { | |
return (typeof o == 'object') ? Object.keys(o).length : 0; | |
} | |
function autoDelimiter(x) { | |
var delimiters = [',', ';', '\t', '|']; | |
var results = []; | |
delimiters.forEach(function(delimiter) { | |
var res = dsv(delimiter).parse(x); | |
if (res.length >= 1) { | |
var count = keyCount(res[0]); | |
for (var i = 0; i < res.length; i++) { | |
if (keyCount(res[i]) !== count) return; | |
} | |
} | |
results.push({ | |
delimiter: delimiter, | |
arity: Object.keys(res[0]).length, | |
}); | |
}); | |
if (results.length) { | |
return results.sort(function(a, b) { | |
return b.arity - a.arity; | |
})[0].delimiter; | |
} else { | |
return null; | |
} | |
} | |
function auto(x) { | |
var delimiter = autoDelimiter(x); | |
if (!delimiter) return null; | |
return dsv(delimiter).parse(x); | |
} | |
function csv2geojson(x, options, callback) { | |
if (!callback) { | |
callback = options; | |
options = {}; | |
} | |
options.delimiter = options.delimiter || ','; | |
var latfield = options.latfield || '', | |
lonfield = options.lonfield || ''; | |
var features = [], | |
featurecollection = { type: 'FeatureCollection', features: features }; | |
if (options.delimiter === 'auto' && typeof x == 'string') { | |
options.delimiter = autoDelimiter(x); | |
if (!options.delimiter) return callback({ | |
type: 'Error', | |
message: 'Could not autodetect delimiter' | |
}); | |
} | |
var parsed = (typeof x == 'string') ? dsv(options.delimiter).parse(x) : x; | |
if (!parsed.length) return callback(null, featurecollection); | |
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: parsed, | |
fields: fields | |
}); | |
} | |
} | |
var errors = []; | |
for (var i = 0; i < parsed.length; i++) { | |
if (parsed[i][lonfield] !== undefined && | |
parsed[i][lonfield] !== undefined) { | |
var lonk = parsed[i][lonfield], | |
latk = parsed[i][latfield], | |
lonf, latf, | |
a; | |
a = sexagesimal(lonk, 'EW'); | |
if (a) lonk = a; | |
a = sexagesimal(latk, 'NS'); | |
if (a) latk = a; | |
lonf = parseFloat(lonk); | |
latf = parseFloat(latk); | |
if (isNaN(lonf) || | |
isNaN(latf)) { | |
errors.push({ | |
message: 'A row contained an invalid value for latitude or longitude', | |
row: parsed[i] | |
}); | |
} else { | |
if (!options.includeLatLon) { | |
delete parsed[i][lonfield]; | |
delete parsed[i][latfield]; | |
} | |
features.push({ | |
type: 'Feature', | |
properties: parsed[i], | |
geometry: { | |
type: 'Point', | |
coordinates: [ | |
parseFloat(lonf), | |
parseFloat(latf) | |
] | |
} | |
}); | |
} | |
} | |
} | |
callback(errors.length ? errors: null, featurecollection); | |
} | |
function toLine(gj) { | |
var features = gj.features; | |
var line = { | |
type: 'Feature', | |
geometry: { | |
type: 'LineString', | |
coordinates: [] | |
} | |
}; | |
for (var i = 0; i < features.length; i++) { | |
line.geometry.coordinates.push(features[i].geometry.coordinates); | |
} | |
line.properties = features[0].properties; | |
return { | |
type: 'FeatureSet', | |
features: [line] | |
}; | |
} | |
function toPolygon(gj) { | |
var features = gj.features; | |
var poly = { | |
type: 'Feature', | |
geometry: { | |
type: 'Polygon', | |
coordinates: [[]] | |
} | |
}; | |
for (var i = 0; i < features.length; i++) { | |
poly.geometry.coordinates[0].push(features[i].geometry.coordinates); | |
} | |
poly.properties = features[0].properties; | |
return { | |
type: 'FeatureSet', | |
features: [poly] | |
}; | |
} | |
module.exports = { | |
isLon: isLon, | |
isLat: isLat, | |
csv: dsv.csv.parse, | |
tsv: dsv.tsv.parse, | |
dsv: dsv, | |
auto: auto, | |
csv2geojson: csv2geojson, | |
toLine: toLine, | |
toPolygon: toPolygon | |
}; | |
},{"dsv":2,"sexagesimal":3}],2:[function(require,module,exports){ | |
var fs = require("fs"); | |
module.exports = new Function("dsv.version = \"0.0.3\";\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":4}],3:[function(require,module,exports){ | |
module.exports = function(x, dims) { | |
if (!dims) dims = 'NSEW'; | |
if (typeof x !== 'string') return null; | |
var r = /^([0-9.]+)°? *(?:([0-9.]+)['’′‘] *)?(?:([0-9.]+)(?:''|"|”|″) *)?([NSEW])?/, | |
m = x.match(r); | |
if (!m) return null; | |
else if (m[4] && dims.indexOf(m[4]) === -1) return null; | |
else return (((m[1]) ? parseFloat(m[1]) : 0) + | |
((m[2] ? parseFloat(m[2]) / 60 : 0)) + | |
((m[3] ? parseFloat(m[3]) / 3600 : 0))) * | |
((m[4] && m[4] === 'S' || m[4] === 'W') ? -1 : 1); | |
}; | |
},{}],4:[function(require,module,exports){ | |
// not implemented | |
// The reason for having an empty file and not throwing is to allow | |
// untraditional implementation of this module. | |
},{}]},{},[1]) | |
(1) | |
}); | |
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title></title> | |
<script src='//api.tiles.mapbox.com/mapbox.js/v1.3.1/mapbox.js'></script> | |
<script src='http://code.jquery.com/jquery-1.8.3.min.js'></script> | |
<script src='csv2geojson.js'></script> | |
<link href='//api.tiles.mapbox.com/mapbox.js/v1.3.1/mapbox.css' rel='stylesheet' /> | |
<!--[if lte IE 8]> | |
<link href='//api.tiles.mapbox.com/mapbox.js/v1.3.1/mapbox.ie.css' rel='stylesheet'> | |
<![endif]--> | |
<style> | |
body { margin:0; padding:0; } | |
#map { position:absolute; top:0; bottom:0; width:100%; } | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script type='text/javascript'> | |
var map = L.mapbox.map('map', 'examples.map-9ijuk24y') | |
.setView([0, 0], 9); | |
var markerLayer = L.mapbox.markerLayer().addTo(map); | |
$.ajax({ | |
url: 'points.csv', | |
success: function csvLoad(csv) { | |
csv2geojson.csv2geojson(csv, function(err, data) { | |
markerLayer.setGeoJSON(data); | |
markerLayer.eachLayer(function(layer) { | |
// here you call `bindPopup` with a string of HTML you create - the feature | |
// properties declared above are available under `layer.feature.properties` | |
var content = '<h1>size: ' + layer.feature.properties.size + '<\/h1>' + | |
'<h2>population: ' + layer.feature.properties.population + '<\/h2>'; | |
layer.bindPopup(content); | |
}); | |
}); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lat | lon | size | population | |
---|---|---|---|---|
0 | 0 | 10 | 1000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment