2012-10-07 9 views
2

Я пытаюсь построить объект GeoJSON из SQL-запроса в некоторые данные точек GIS в базе данных postgis postgresql. Ниже приведен фрагмент моего node.js app.js.format geojson from postgis

Как я понял, я строил тип и функции, но не знаю, как прикрепить массив свойств к каждой записи 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 
} 

`

+1

Похоже, что вам нужно использовать обходные; см. http://www.postgresonline.com/journal/archives/253-PostgreSQL-9.2-native-json-type-support.html –

+0

да, возможно, вы правы, хотя похоже, что мне нужно будет обновить мои postgres :( – roy

ответ

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

Недавно я написал небольшой вспомогательный модуль для этой цели. Это очень простой в использовании -

var postgeo = require("postgeo"); 

postgeo.connect("postgres://[email protected]: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