Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 http://www.geojson.org/geojson-spec.html#examples?

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);
      featureCollection.properties[i] = JSON.parse(result.rows[i].ttl); //this is wrong
   }

   res.send(featureCollection);
   });

});
}

 function FeatureCollection(){
   this.type = 'FeatureCollection';
   this.features = new Array();
   this.properties = new Object;  //this is wrong
 }
share|improve this question

1 Answer 1

up vote 2 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
      }

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

Your Answer

 
discard

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.