node.js - 从 postgis 格式化 geojson

标签 node.js postgis geojson leaflet

我正在尝试从 SQL 查询构建一个 GeoJSON 对象到 postgis postgresql 数据库中的一些 GIS 点数据。下面是我的 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
 }

`

最佳答案

这应该可以完成工作:

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

关于node.js - 从 postgis 格式化 geojson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12766289/

相关文章:

javascript - 从sequelize模型 'define'向sqlite添加约束

node.js - nodejs sqlite3 db.run 作为 Bluebird promise

django - Apache 错误日志中的未知 Django (Postgis) 异常,没有电子邮件报告

django - GeoDjango 序列化 GeoJSON 跳过 'id' 字段

javascript - 可以将 math.max 用于对象数组吗?

postgresql - 我如何删除postgis中的重叠线

postgresql - 查找与点相交的所有区域,反之亦然 - PostGIS

leaflet - 如何为 geoJson 图层设置 z 索引图层顺序?

javascript - OpenLayers 4 可变图标旋转

javascript - Mongoose 填充未定义的字段