Skip to content

Instantly share code, notes, and snippets.

@springmeyer
Created August 10, 2011 23:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save springmeyer/1138576 to your computer and use it in GitHub Desktop.
Save springmeyer/1138576 to your computer and use it in GitHub Desktop.
create an sqlite rtree spatial index using node-sqlite3 and node-mapnik
/*
wget http://dl.dropbox.com/u/342088/10m-us-parks-area.sqlite
ogr2ogr -f SQLite parks.sqlite ParkPly_900913.shp
dane$ ogr2ogr -f SQLite parks.sqlite ~/Documents/MapBox/cache/2976a0c1-10m-us-parks-area/2976a0c1-10m-us-parks-area.shp
*/
var mapnik = require('./lib/mapnik');
var sqlite3 = require('sqlite3');
/*var options = {
type: 'sqlite',
file: '10m-us-parks-area.sqlite',
table: '"10m_us_parks_area"'
//extent: '-20037508.34,-20037508.34,20037508.34,20037508.34'
};
*/
var options = {
type: 'sqlite',
file: 'parks.sqlite',
table: '"2976a0c1_10m_us_parks_area"',
extent: '-20037508.34,-20037508.34,20037508.34,20037508.34',
use_spatial_index:false
};
var db = new sqlite3.Database(options.file);
var table = options.table.slice(1,-1);
var geom_col = 'GEOMETRY';
//db.run("drop virtual table \"" + table + "_" + geom_col + "\"");
var spatial_index_sql = "create virtual table idx_" + table + "_" + geom_col + " using rtree(pkid, xmin, xmax, ymin, ymax)";
var extent_insert = "insert into idx_" + table + "_" + geom_col + " values (?,?,?,?,?)";
db.serialize(function() {
db.run(spatial_index_sql);
var stmt = db.prepare(extent_insert);
var ds = new mapnik.Datasource(options);
// get one feature
var featureset = ds.featureset();
var feature;
while (feature = featureset.next()) {
var e = feature.extent();
stmt.run(feature.id(),e[0],e[1],e[2],e[3]);
}
stmt.finalize();
});
db.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment