3

SQLクエリからpostgis postgresqlデータベースのGISポイントデータへのGeoJSONオブジェクトを構築しようとしています。私の node.js app.js のスニペットを以下に示します。

現状では、タイプと機能の構築は理解していますが、プロパティ配列を各 GeoJSON レコードにアタッチする方法がわかりません (以下では、機能とは別に (照合せずに) 最後にすべてレンダリングします)。

質問: GeoJSON を構築するループ内の各レコードにプロパティがアタッチ (照合) されるようにするには、どうすればよいですか?例

`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
 }

`

4

2 に答える 2

3

これでうまくいくはずです:

...
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;
} 
于 2013-01-09T23:04:40.080 に答える
2

私は最近、この目的のために小さなヘルパー モジュールを書きました。使い方はとても簡単です -

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

ここでレポを見つけることができます - https://github.com/jczaplew/postgeo

于 2014-04-01T19:01:01.523 に答える