format geojson from postgis

自闭症网瘾萝莉.ら 提交于 2019-12-05 21:55:55

This should do the job:

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

Using:

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

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

I recently wrote a little helper module for this purpose. It's very straightforward to use -

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

You can find the repo here - https://github.com/jczaplew/postgeo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!