Skip to content

Instantly share code, notes, and snippets.

@isc-rsingh
Last active August 29, 2015 14:07
Show Gist options
  • Save isc-rsingh/ec0d9c17c544663e7b02 to your computer and use it in GitHub Desktop.
Save isc-rsingh/ec0d9c17c544663e7b02 to your computer and use it in GitHub Desktop.
Converts an on-disk Shapefile into GeoJSON and imports the data into your Cloudant database using node.js.
/**
* Converts an on-disk Shapefile into GeoJSON and imports the data into your Cloudant database
* requires Node.js and the 'cloudant' and 'shapefile' modules.
* - installation: save this code to a file called index.js
* - at the shell: npm install cloudant
* - at the shell: npm install shapefile
* - at the shell, run the program: node index.js
*/
var Cloudant = require('cloudant'); // npm install cloudant
var shapefile = require('shapefile'); // npm install shapefile
//-- variables you must set
var dbname = 'testshp'; // ex: testshapefile (TODO: or null to use shapefile name)
var username = 'rajsingh';
var pw = process.env.cloudant_password; // at the shell type: export cloudant_password="yourpwhere"
var shpfile = "./sf/schools_private_pts/schools_private_pts.zip";
//-- end variables you must set
Cloudant({account:username, password:pw}, function(er, cloudant) {
if (er) return console.log('Error connecting to Cloudant account %s: %s', me, er.message);
// specify the database we are going to use
cloudant.db.create(dbname, function(err, body){
if (err) console.error(err);
else console.log(body);
})
var ldb = cloudant.db.use(dbname);
// specify the shapefile we are going to use
shapefile.read(shpfile, function(er, data){
if (er) console.log(er);
else {
for (var i = 0; i < data.features.length; i++) {
ldb.insert(data.features[i], function(er, body){
if (er) console.error(er);
else console.log(body);
});
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment