I try to save features created on a vector layer (openlayers) to postgreSQL 9.1/postGIS 2.0 via websockets.
As server I use nodejs 0.10.12 along with the pg module
I cannot fix it. Its getting so overwhelming I decided to ask a new question.
The problem : I cannot pass the geometry to the server. If I pass it like WKT (a format like POINT(122544 588788)) client side complains about syntax error : Converting circular structure to JSON. And this is because I'm using JSON Stringify to send multiple data to the server via websockets.
So I convert feature's geometry to a string, I strip it from the letters and the parentheses and I send to the server only the numbers. I send only this 122544 588788. In that case, JSON does not complain, but I get syntax errors on the server side, about the geometry.
Seriously, any help, suggestion or alternative would be great. I apologize for the big question. Here is the code
client side
//create websockets
var so = new WebSocket("ws://localhost:8000");
//error report for websockets
so.onerror=function (evt)
{saveMSG.textContent = evt;}
//get the geometry of the feature
var jak= map.layers[2].features[0].geometry;
//turn it to a string
var amir=new String(jak);
//get the first five letters to check if it is line,point or polygon
var sara=amir.substring(0,5);
//if its lines....
if(sara=='LINES'){
//get just the numbers
var hul=amir.substring(11,amir.length-1);
//open websockets connection
so.onopen = function(){
//send the geometry's numbers, the type of the geometry and other things from a form
so.send(JSON.stringify({
name: document.getElementById("name").value,
geo: hul,
geoT:'line',
descr: document.getElementById("descr").value
}));}
}
//same ifs for point or polygons....
and the server side
function insertEm(name,descr,geo,geoT){
//if line....
if(geoT=='line'){
//create the "WKT"...
var ste="LINESTRING("+geo+")";
//and put it in place...
var met="ST_GeomFromWKT('LINESTRING("+ste+")',900913)";
}
//same ifs for point or polygon
//and now do the insert...
var conString = "pg://username:password@localhost:5432/myDB";
var client = new pg.Client(conString);
client.connect();
var query = client.query({name:"inser", text:"INSERT INTO pins(p_name, p_descr,p_geom) values($1,$2,$3)", values:[name, descr, met]});
query.on("error", function (error) {console.log(error)});
query.on("end", function (result) {console.log(result); client.end();});
What I get from the server side is
[error: parse error - invalid geometry]
hint: '"ST"<-- parse error a position 2 within geometry'
I've tried different syntaxes but I cannot get it work.
I'm still trying. I tried a lot of things and different syntaxes. Now I have, on the server side (snippet)
//open websockets
so.onopen = function(){
//get geometry
var jak=map.layers[2].features[0].geometry;
//make it a string
var as=new String(jak);
//keep the numbers
var hul=as.substring(11,as.length-1);
//make it WKT
var god=hul.toString();
//send it with stringify
so.send(JSON.stringify({command: 'insertAll',
geo: god,
//send other things from the form....
Server side (snippet)
//get data from client, put it in place and create a string from it
var met="ST_GeomFromText('LINESTRING("+geo+")',900913)";
var pra=new String(met);
//connect and query
var conString = "pg://user:user@localhost:5432/myDB";
var client = new pg.Client(conString);
client.connect();
var query = client.query({name:"inser", text:"INSERT INTO pins(p_geom) values($1)", values:[pra]});
And I STILL get
[error: parse error - invalid geometry]
hint: '"ST"<-- parse error a position 2 within geometry'
I change it to a simple query
var query = client.query("INSERT INTO pins (p_geom) values('"+pra+"')")
and get [error: syntax error at or near "LINESTRING"].
At the end of the query I put query.on("end", function (result) {console.log(result);connection.send(pra); client.end();});
So I can see what the clients send to the server.
I get ST_GeomFromText('LINESTRING(2335859.0225 4725430.1340625,2378933.155 4741356.7040625)',900913) that looks fine...
Any suggestions?
I used jQuery/AJAX and INSERT of PHP. Amazingly , still nothing.
client side
//get geometry
var jak=map.layers[2].features[0].geometry;
//send
jQuery.post("insertPPB.php", { geo, d}, function(data) {
alert(data);
$('#response').html(data);
});
server side
//get geometry
$g=$_POST['g'];
//prepared statement for the insert...
$st = $dbh->prepare("INSERT INTO pins (p_geom) VALUES (:geo)");
$st->bindParam(':geo', $geo, PDO::PARAM_STR);
//put the geometry in place
$geo = "ST_GeomFromText('".$g.")', 900913)";
$st->execute();
$last_insert_id = $dbh->lastInsertId('pins_p_id_seq');
echo $last_insert_id;
If I alert the jak in the client side , before I send it, I get something like LINESTRING(2328981.64 4722534.3940625,2399203.335 4706607.8240625,2405356.7825 4645435.3165625)
That SHOULD work. Right? But it does not. The INSERT never happens.
I dont know what to do, I though using AJAX/PHP would solve this.
If this does not work can I use WFS-T? But, how can I get the newly id created in the db from the point just added? I need it to add form text data and relate them with that point. All the ids are serial (grow automatically +1 with every insert). On Geoserver I dont see the "id" in the Feature Type Details of the vector layer.
Why?