Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从postgis格式化geojson_Node.js_Postgis_Geojson_Leaflet - Fatal编程技术网

Node.js 从postgis格式化geojson

Node.js 从postgis格式化geojson,node.js,postgis,geojson,leaflet,Node.js,Postgis,Geojson,Leaflet,我正在尝试从一个SQL查询到postgis postgresql数据库中的一些GIS点数据构建一个GeoJSON对象。下面是my node.js app.js的一个片段 就目前而言,我理解构建类型和功能,但不知道如何将属性数组附加到每个GeoJSON记录(在下文中,它都是在最后呈现的,与功能分开(而不是对比) 问题是:我需要做些什么,以便在构建GeoJSON的循环中为每个记录附加(整理)属性,使其看起来更像这样 函数抓取数据(边界,res){ pg.connect(连接,功能(错误,客户端){

我正在尝试从一个SQL查询到postgis postgresql数据库中的一些GIS点数据构建一个GeoJSON对象。下面是my node.js app.js的一个片段

就目前而言,我理解构建类型和功能,但不知道如何将属性数组附加到每个GeoJSON记录(在下文中,它都是在最后呈现的,与功能分开(而不是对比)

问题是:我需要做些什么,以便在构建GeoJSON的循环中为每个记录附加(整理)属性,使其看起来更像这样

函数抓取数据(边界,res){ pg.connect(连接,功能(错误,客户端){ var moisql='从cpag中选择ttl,(ST_AsGeoJSON(the_geom))作为区域设置;' 查询(moisql,函数(err,result){ var featureCollection=新featureCollection();
对于(i=0;i这应该可以完成以下工作:

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

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

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

你可以在这里找到回购协议-

看起来你需要使用一种变通方法;看,是的,你可能是对的,不过看起来我必须更新我的postgres:(新版本:…工作得很好,gg
function FeatureCollection(){
    this.type = 'FeatureCollection';
    this.features = new Array();
}

function Feature(){
    this.type = 'Feature';
    this.geometry = new Object;
    this.properties = new Object;
} 
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);
});