从PostGIS格式化GeoJSON

3
我正在尝试从postgis postgresql数据库中的一些GIS点数据的SQL查询构建一个GeoJSON对象。下面是我的node.js app.js代码片段。
目前为止,我了解如何构建类型和要素,但不知道如何将属性数组附加到每个GeoJSON记录中(在下面的代码中,所有内容都在最后呈现,与要素不相关)。
问题:我需要做什么才能使属性附加(汇总)到构建GeoJSON的循环中的每条记录上,以便它看起来更像这个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
 }

`


1
看起来你需要使用一个解决方法,请参考http://www.postgresonline.com/journal/archives/253-PostgreSQL-9.2-native-json-type-support.html。 - Mike T
是的,你可能是对的,看起来我得更新我的Postgres :( - roy
2个回答

3
这应该能胜任任务:
...
for(i=0; i<result.rows.length; i++){
    var feature = new Feature();
    feature.geometry = JSON.parse(result.rows[i].locale);
    feature.properties = {"TTL", result.rows[i].ttl};
    featureCollection.features.push(feature);
}
...

使用:

function FeatureCollection(){
    this.type = 'FeatureCollection';
    this.features = new Array();
}

function Feature(){
    this.type = 'Feature';
    this.geometry = new Object;
    this.properties = new Object;
} 

2

我最近为此编写了一个小助手模块。它非常易于使用 -

var postgeo = require("postgeo");

postgeo.connect("postgres://user@host:port/database");

postgeo.query("SELECT id, name ST_AsGeoJSON(geom) AS geometry FROM table", "geojson", function(data) {
    console.log(data);
});

你可以在这里找到仓库 - https://github.com/jczaplew/postgeo


新版本:https://github.com/jczaplew/dbgeo...非常好用,gg。 - Marek Bernád

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接