2013-05-16 17:24:52 +08:00
|
|
|
|
|
|
|
var _ = require('underscore')
|
2013-05-27 17:21:56 +08:00
|
|
|
var pg = require('./pg');
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
function geojson() {}
|
|
|
|
|
|
|
|
geojson.prototype = new pg('geojson');
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
var p = geojson.prototype;
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
p._contentType = "application/json; charset=utf-8";
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
p.getQuery = function(sql, options) {
|
2013-05-16 17:24:52 +08:00
|
|
|
var gn = options.gn;
|
|
|
|
var dp = options.dp;
|
|
|
|
return 'SELECT *, ST_AsGeoJSON(' + gn + ',' + dp + ') as the_geom FROM (' + sql + ') as foo';
|
2013-05-27 17:21:56 +08:00
|
|
|
};
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
p.transform = function(result, options, callback) {
|
|
|
|
_toGeoJSON(result, options.gn, callback);
|
2013-05-16 17:24:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
function _toGeoJSON(data, gn, callback){
|
|
|
|
try {
|
|
|
|
var out = {
|
|
|
|
type: "FeatureCollection",
|
|
|
|
features: []
|
|
|
|
};
|
|
|
|
|
|
|
|
_.each(data.rows, function(ele){
|
|
|
|
var _geojson = {
|
|
|
|
type: "Feature",
|
|
|
|
properties: { },
|
|
|
|
geometry: { }
|
|
|
|
};
|
|
|
|
_geojson.geometry = JSON.parse(ele[gn]);
|
|
|
|
delete ele[gn];
|
|
|
|
delete ele["the_geom_webmercator"]; // TODO: use skipfields
|
|
|
|
_geojson.properties = ele;
|
|
|
|
out.features.push(_geojson);
|
|
|
|
});
|
|
|
|
|
|
|
|
// return payload
|
|
|
|
callback(null, out);
|
|
|
|
} catch (err) {
|
|
|
|
callback(err,null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-27 17:21:56 +08:00
|
|
|
module.exports = geojson;
|
2013-05-16 17:24:52 +08:00
|
|
|
module.exports.toGeoJSON = _toGeoJSON
|