bookshelf-geojson
v1.0.0
Published
Automatically parse and format PostGIS geometry columns as GeoJSON in your models
Downloads
5
Readme
bookshelf-geojson
This Bookshelf.js plugin automatically parses and formats PostGIS geometry columns as GeoJSON in your models.
Developed at the Media Engineering Institute (HEIG-VD).
Usage
const bookshelf = require('bookshelf');
const bookshelfGeojson = require('bookshelf-geojson');
const knex = require('knex');
const db = knex({
client: 'postgresql',
connection: 'postgres://localhost/bookshelf-geojson'
});
const Bookshelf = bookshelf(knex);
Bookshelf.plugin(bookshelfGeojson(db));
const Address = Bookshelf.Model.extend({
tableName: 'addresses',
// Indicate the column containing a PostGIS geometry type.
geojson: 'location'
});
const geojson = {
type: 'Point',
coordinates: [ 24, 42 ]
};
// The GeoJSON is automatically serialized for PostGIS.
const address = new Address();
address.set('location', geojson);
address.save();
// The GeoJSON is automatically parsed with PostGIS's ST_AsGeoJSON function.
const anotherAddress = new Address({ id: 1 }).fetch();
console.log(anotherAddress.get('location'));
// { id: 1, location: { type: 'Point', coordinates: [ 33, 66 ] } }