postGIS's nearest neighbor with node.js
I,m using postgreSQL 9.1 , postGIS 2.0, nodejs 0.10.12 and the latest
version of node's pg module.
I use websockets on the server side. I have two functions. Depending on
the data came from the user I call the right function.
First function is a simple SELECT, works fine.
The second tries to find 5 nearest neighbors of a given geometry (it can
be point or line or polygon). Does not work. I tried different syntax for
the nearest neighbor query and still nothing.
What could be wrong? The query? I was thinking maybe the pg module does
not support the ST_DWithin function.
Here is the code of the nearest neighbor function
function checkMapIn(je){
var conString = "pg://username:password@localhost:5432/myDB";
var client = new pg.Client(conString);
client.connect();
//je came from client, is a geometry he just inserted in the map
var query = client.query('SELECT pins.p_name FROM pins ORDER BY
pins.p_geom <-> '+je+' LIMIT 5')
//alternative syntax I tried
//SELECT pins.p_name FROM pins INNER JOIN pins ON ST_DWithin('+je+',
pins.p_geom, 1000) LIMIT 5
//SELECT pins.p_name FROM pins WHERE ST_DWithin('+je+', pins.p_geom, 1000)
LIMIT 5
query.on("row", function (row, result) {
result.addRow(row);});
query.on("end", function (result) {
console.log(JSON.stringify(result.rows, null, " "));
for (var i=0; i<result.rows.length; i++){
connection.send(result.rows[i].p_name+'</br>')
}
client.end();
});
}
And this is the error I get , no matter how I edit the synatx of the query
events.js:72
throw er; // Unhandled 'error' event
^
error: syntax error at or near "["
at Connection.parsE (C:\Program Files
(x86)\nodejs\node_modules\pg\lib\connection.js:526:11)
at Connection.parseMessage (C:\Program Files
(x86)\nodejs\node_modules\pg\lib\connection.js:371:17)
at Socket.(anonymous) (C:\Program Files
(x86)\nodejs\node_modules\pg\lib\connection.js:86:20)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.(anonymous) (_stream_readable.js:736:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
at emitReadable (_stream_readable.js:404:5)
at readableAddChunk (_stream_readable.js:156:9)
at Spcket.Readable.push (_stream_readable.js:127:10)
Any advise? Hints?
Thanks in advance
slevin
No comments:
Post a Comment