Skip to content

Instantly share code, notes, and snippets.

@stoolrossa
Created October 25, 2011 15:29
Show Gist options
  • Save stoolrossa/1313139 to your computer and use it in GitHub Desktop.
Save stoolrossa/1313139 to your computer and use it in GitHub Desktop.
Loading data from PostGIS into CouchDB
var pg = require('pg'),
cradle = require('cradle');
var pgConnString = 'tcp://spatial:spatial@localhost/spatial';
var couchServer = 'localhost';
var couchPort = 5984;
var couchDatabase = 'spatial';
var couchConn = new cradle.Connection(couchServer, couchPort);
var couchDb = couchConn.database(couchDatabase);
var now = new Date();
console.log("Start " + now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds());
// call the main function, telling it the total number of records, the start point and the record block size
loadPostgisRecords(2583598, 0, 1000);
// retrieve blocks of PostGIS records and save them in CouchDB
function loadPostgisRecords(total, lower, number) {
// make a connection to PostGIS
pg.connect(pgConnString, function(err, client) {
var upper = lower + number;
if (upper > total) {
upper = total + 1;
}
// build up the sql to select the range of records from PostGIS
var sql = 'select gid, objectid, lot, plan, lot_area, acc_code, shire_name, segpar, shape_area, shape_len, ST_AsGeoJSON(geog) as shape ';
sql += 'from spatialschema.state_1 ';
sql += 'where gid >= ' + lower.toString() + ' and gid < ' + upper.toString();
// execute the query
client.query(sql, function(err, result) {
var resultsLength = result.rows.length;
var features = [];
// loop through the resulting records and build a collection of features
for (var i = 0; i < resultsLength; i += 1)
{
var record = result.rows[i];
var feature = { "_id": record.gid.toString(),
"type": "Feature",
"geometry": JSON.parse(record.shape),
"properties": { "gid": record.gid,
"objectid": record.objectid,
"lot": record.lot,
"plan": record.plan,
"lot_area": record.lot_area,
"acc_code": record.acc_code,
"shire_name": record.shire_name,
"segpar": record.segpar,
"shape_area": record.shape_area,
"shape_len": record.shape_len
}
};
features.push(feature);
}
client = null;
// save the collection of features in CouchDB
couchDb.save(features, function(err, ok) {
if (err) {
console.log(err.message);
}
else {
features = null;
// if there are more records to load, call the main function recursively
if (upper < total) {
loadPostgisRecords(total, upper, number);
} else {
now = new Date();
console.log("End " + now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds());
}
}
});
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment