从postgis格式化geojson [英] format geojson from postgis
问题描述
我正尝试从SQL查询到Postgis postgresql数据库中的某些GIS点数据构建GeoJSON对象.我的node.js app.js的片段如下.
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.
按现状,我了解构建类型和功能,但不知道如何将属性数组附加到每个GeoJSON记录(在下面,所有这些都最后呈现,与功能分开(不与功能对照) ).
As it stands I understand building the type and features, but don't know how to attach a properties array to each GeoJSON record (in the below, it all renders at the end, separate from (not collated with) the features).
问题:我需要做些什么,以便属性为构建GeoJSON的循环中的每条记录附加(整理),使它看起来更像这样
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
}
`
推荐答案
这应该可以完成:
...
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;
}
这篇关于从postgis格式化geojson的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!