Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to build a GeoJSON object from a SQL query to some GIS point data in a postgis postgresql database. A snippet of my node.js app.js is below.

As it stands I understand building the type and features, but don't know how to attach a properties properties to each GeoJSON record (in the below, it all renders at the end, separate from (not collated with) the features).

THE QUESTION: What do I need to do so that the properties attach (collate) for each record in the loop that builds the GeoJSON So that it looks more like this

function GrabData(bounds, res){

  pg.connect(conn, function(err, client){

  var moisql = 'SELECT ttl, (ST_AsGeoJSON(the_geom)) as locale from cpag;'

  client.query(moisql, function(err, result){
    var featureCollection = new FeatureCollection();

    for(i=0; i<result.rows.length; i++){
      featureCollection.features[i] = JSON.parse(result.rows[i].locale);[i] = JSON.parse(result.rows[i].ttl); //this is wrong



 function FeatureCollection(){
   this.type = 'FeatureCollection';
   this.features = new Array(); = new Object;  //this is wrong
share|improve this question
up vote 3 down vote accepted

This function should work for you; just pass the result.rows object as the argument;

  toGeoJson: function(rows){
    var obj, i;

    obj = {
      type: "FeatureCollection",
      features: []

    for (i = 0; i < rows.length; i++) {
      var item, feature, geometry;
      item = rows[i];

      geometry = JSON.parse(item.geometry);
      delete item.geometry;

      feature = {
        type: "Feature",
        properties: item,
        geometry: geometry

    return obj;
share|improve this answer
yes thank you, that helps clear it up. – Roybert Soylend Oct 18 '12 at 3:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.