Created
July 13, 2015 15:42
-
-
Save ismyrnow/0f55be962c3f0f318384 to your computer and use it in GitHub Desktop.
Stream GeoJSON into CartoDB
This file contains hidden or 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
/** | |
* 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