2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2015-05-13 19:00:01 +08:00
|
|
|
var _ = require('underscore');
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
var Pg = require('./../pg');
|
|
|
|
const errorHandlerFactory = require('../../../services/error-handler-factory');
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
function GeoJsonFormat () {
|
2014-07-31 09:17:14 +08:00
|
|
|
this.buffer = '';
|
|
|
|
}
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2019-01-16 22:58:11 +08:00
|
|
|
GeoJsonFormat.prototype = new Pg('geojson');
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
GeoJsonFormat.prototype._contentType = 'application/json; charset=utf-8';
|
2013-05-16 17:24:52 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
GeoJsonFormat.prototype.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
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
GeoJsonFormat.prototype.startStreaming = function () {
|
2014-07-31 09:17:14 +08:00
|
|
|
this.total_rows = 0;
|
|
|
|
if (this.opts.beforeSink) {
|
|
|
|
this.opts.beforeSink();
|
|
|
|
}
|
|
|
|
if (this.opts.callback) {
|
|
|
|
this.buffer += this.opts.callback + '(';
|
|
|
|
}
|
|
|
|
this.buffer += '{"type": "FeatureCollection", "features": [';
|
|
|
|
this._streamingStarted = true;
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
GeoJsonFormat.prototype.handleQueryRow = function (row) {
|
|
|
|
if (!this._streamingStarted) {
|
2014-07-31 09:17:14 +08:00
|
|
|
this.startStreaming();
|
|
|
|
}
|
|
|
|
|
|
|
|
var geojson = [
|
|
|
|
'{',
|
|
|
|
'"type":"Feature",',
|
|
|
|
'"geometry":' + row[this.opts.gn] + ',',
|
|
|
|
'"properties":'
|
|
|
|
];
|
|
|
|
delete row[this.opts.gn];
|
2015-05-13 19:00:01 +08:00
|
|
|
delete row.the_geom_webmercator;
|
2014-07-31 09:17:14 +08:00
|
|
|
geojson.push(JSON.stringify(row));
|
|
|
|
geojson.push('}');
|
|
|
|
|
|
|
|
this.buffer += (this.total_rows++ ? ',' : '') + geojson.join('');
|
|
|
|
|
|
|
|
if (this.total_rows % (this.opts.bufferedRows || 1000)) {
|
|
|
|
this.opts.sink.write(this.buffer);
|
|
|
|
this.buffer = '';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
GeoJsonFormat.prototype.handleQueryEnd = function (/* result */) {
|
2015-05-14 00:15:38 +08:00
|
|
|
if (this.error && !this._streamingStarted) {
|
2014-08-05 06:47:14 +08:00
|
|
|
this.callback(this.error);
|
|
|
|
return;
|
|
|
|
}
|
2014-08-05 06:47:49 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
if (this.opts.profiler) {
|
2015-05-13 19:00:01 +08:00
|
|
|
this.opts.profiler.done('gotRows');
|
|
|
|
}
|
2014-08-19 01:45:17 +08:00
|
|
|
|
2019-12-24 01:19:08 +08:00
|
|
|
if (!this._streamingStarted) {
|
2014-08-05 06:47:49 +08:00
|
|
|
this.startStreaming();
|
|
|
|
}
|
|
|
|
|
2015-05-14 00:15:38 +08:00
|
|
|
this.buffer += ']'; // end of features
|
|
|
|
|
|
|
|
if (this.error) {
|
2019-01-02 23:15:28 +08:00
|
|
|
this.buffer += ',"error":' + JSON.stringify(errorHandlerFactory(this.error).getResponse().error);
|
2015-05-14 00:15:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
this.buffer += '}'; // end of root object
|
|
|
|
|
2014-07-31 09:17:14 +08:00
|
|
|
if (this.opts.callback) {
|
|
|
|
this.buffer += ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.opts.sink.write(this.buffer);
|
|
|
|
this.opts.sink.end();
|
|
|
|
this.buffer = '';
|
|
|
|
|
|
|
|
this.callback();
|
2013-05-16 17:24:52 +08:00
|
|
|
};
|
|
|
|
|
2019-12-24 01:19:08 +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-16 17:24:52 +08:00
|
|
|
}
|
|
|
|
|
2014-07-31 09:17:14 +08:00
|
|
|
module.exports = GeoJsonFormat;
|
|
|
|
module.exports.toGeoJSON = _toGeoJSON;
|