
var _ = require('underscore')
var pg  = require('./pg');

function geojson() {}

geojson.prototype = new pg('geojson');

var p = geojson.prototype;

p._contentType = "application/json; charset=utf-8";

p.getQuery = function(sql, options) {
    var gn = options.gn;
    var dp = options.dp;
    return 'SELECT *, ST_AsGeoJSON(' + gn + ',' + dp + ') as the_geom FROM (' + sql + ') as foo';
};

p.transform = function(result, options, callback) {
  _toGeoJSON(result, options.gn, callback);
};

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);
  }
}

module.exports = geojson;
module.exports.toGeoJSON = _toGeoJSON

@@ -65,29 +65,64 @@
 ion 
-geojson() %7B%7D%0A%0Ageoj
+GeoJsonFormat() %7B%0A    this.buffer = '';%0A%7D%0A%0AGeoJ
 son
+Format
 .pro
@@ -154,23 +154,21 @@
 );%0A%0A
-var p = geoj
+GeoJ
 son
+Format
 .pro
@@ -173,20 +173,16 @@
 rototype
-;%0A%0Ap
 ._conten
@@ -225,17 +225,39 @@
 tf-8%22;%0A%0A
-p
+GeoJsonFormat.prototype
 .getQuer
@@ -441,101 +441,1242 @@
 %7D;%0A%0A
-p.transform = function(result, options, callback) %7B%0A  _toGeoJSON(result, options.gn, 
+GeoJsonFormat.prototype.startStreaming = function() %7B%0A    this.total_rows = 0;%0A    if (this.opts.beforeSink) %7B%0A        this.opts.beforeSink();%0A    %7D%0A    if (this.opts.callback) %7B%0A        this.buffer += this.opts.callback + '(';%0A    %7D%0A    this.buffer += '%7B%22type%22: %22FeatureCollection%22, %22features%22: %5B';%0A    this._streamingStarted = true;%0A%7D;%0A%0AGeoJsonFormat.prototype.handleQueryRow = function(row) %7B%0A%0A    if ( ! this._streamingStarted ) %7B%0A        this.startStreaming();%0A    %7D%0A%0A    var geojson = %5B%0A        '%7B',%0A        '%22type%22:%22Feature%22,',%0A        '%22geometry%22:' + row%5Bthis.opts.gn%5D + ',',%0A        '%22properties%22:'%0A    %5D;%0A    delete row%5Bthis.opts.gn%5D;%0A    delete row%5B'the_geom_webmercator'%5D;%0A    geojson.push(JSON.stringify(row));%0A    geojson.push('%7D');%0A%0A    this.buffer += (this.total_rows++ ? ',' : '') + geojson.join('');%0A%0A    if (this.total_rows %25 (this.opts.bufferedRows %7C%7C 1000)) %7B%0A        this.opts.sink.write(this.buffer);%0A        this.buffer = '';%0A    %7D%0A%7D;%0A%0AGeoJsonFormat.prototype.handleQueryEnd = function(result) %7B%0A    this.buffer += '%5D%7D'; // end of features%0A    if (this.opts.callback) %7B%0A        this.buffer += ')';%0A    %7D%0A%0A    this.opts.sink.write(this.buffer);%0A    this.opts.sink.end();%0A    this.buffer = '';%0A%0A    this.
 callback
 );%0A%7D
@@ -1671,16 +1671,17 @@
 callback
+(
 );%0A%7D;%0A%0Af
@@ -2276,23 +2276,29 @@
 ports = 
-geoj
+GeoJ
 son
+Format
 ;%0Amodule
@@ -2328,9 +2328,10 @@
 oGeoJSON
+;
 %0A

Use streaming for geojson format