Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Created April 5, 2012 15:50
Show Gist options
  • Save springmeyer/2312067 to your computer and use it in GitHub Desktop.
Save springmeyer/2312067 to your computer and use it in GitHub Desktop.
convert point coordinate pair into wkb in javascript/node.js and insert into sqlite db for rendering in TileMill
var sqlite3 = require('sqlite3')
var db = new sqlite3.Database('test.db');
var make_wkb_point = function(x,y) {
var byteorder = 1;
var len = 1 + 4 + 8*2 ;
var wkb = new Buffer(len);
wkb.fill(0);
wkb[0] = byteorder;
wkb.writeUInt8(1,1)
wkb.writeDoubleLE(x,1+4)
wkb.writeDoubleLE(y,1+4+8)
return wkb;
}
var add_point = function(x,y,name) {
var stmt = db.prepare("INSERT into \"point_table\" (id,geometry,name) values (?,?,?)");
stmt.run(null,make_wkb_point(x,y),name);
stmt.finalize();
}
db.serialize(function() {
db.run("CREATE TABLE IF NOT EXISTS \"point_table\" (id INTEGER PRIMARY KEY AUTOINCREMENT, geometry BLOB, \"name\" varchar)");
add_point(-122,48,'Seattle');
add_point(-77,39,'DC');
add_point(0,51,'London');
});
@springmeyer
Copy link
Author

/cc @Vertice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment