我正在尝试从 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/