Skip to content

Instantly share code, notes, and snippets.

@ismyrnow
Created July 13, 2015 15:42
Show Gist options
  • Save ismyrnow/0f55be962c3f0f318384 to your computer and use it in GitHub Desktop.
Save ismyrnow/0f55be962c3f0f318384 to your computer and use it in GitHub Desktop.
Stream GeoJSON into CartoDB
/**
* Streams a geojson file to an existing cartodb dataset.
* This requires that the dataset is created in CartoDB
* initially using a subset (1st record) of geojson data.
*/
var fs = require('fs');
var through2 = require('through2');
var geojsonStream = require('geojson-stream');
var cartodbTools = require('cartodb-tools');
// CartoDB settings
var username = '';
var apikey = '';
var dataset = '';
var cartodb = cartodbTools(username, apikey);
var i = 0;
fs.createReadStream(__dirname + '/example.geojson')
.pipe(geojsonStream.parse())
.pipe(through2.obj(function (feature, enc, callback) {
//console.log(++i, feature.properties);
feature.properties = lowercase(feature.properties);
this.push(feature);
return callback();
}))
.pipe(cartodb.createWriteStream(dataset))
.on('finish', function () {
console.log('done');
});
function lowercase(obj) {
var key, keys = Object.keys(obj);
var n = keys.length;
var newobj={}
while (n--) {
key = keys[n];
newobj[key.toLowerCase()] = obj[key];
}
return newobj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment