[GIS] How to convert a String to OpenLayers.Geometry.Polygon using javascript

convertjqueryopenlayers-2polygonpostgis

I'm using Javascript, jQuery, Java, GeoServer, OpenLayers and PostGIS.

Using java class ResultSet I return this String:

POLYGON((516000 2135000,515039.264020162 2125245.48389919,512193.976625564 2115865.82838175,507573.480615127 2107221.48834902,501355.339059327 2099644.66094067,493778.51165098 2093426.51938487,485134.171618255 2088806.02337444,475754.516100806 2085960.73597984,466000 2085000,456245.483899194 2085960.73597984,446865.828381746 2088806.02337444,438221.48834902 2093426.51938487,430644.660940673 2099644.66094067,424426.519384873 2107221.48834902,419806.023374436 2115865.82838175,416960.735979839 2125245.48389919,416000 2135000,416960.735979838 2144754.51610081,419806.023374436 2154134.17161825,424426.519384873 2162778.51165098,430644.660940673 2170355.33905933,438221.48834902 2176573.48061513,446865.828381745 2181193.97662556,456245.483899193 2184039.26402016,466000 2185000,475754.516100806 2184039.26402016,485134.171618254 2181193.97662556,493778.51165098 2176573.48061513,501355.339059327 2170355.33905933,507573.480615127 2162778.51165098,512193.976625564 2154134.17161825,515039.264020161 2144754.51610081,516000 2135000))

Quite long isn't it? So using JQuery's ajax function I catch this String and now I want to convert it to a OpenLayers.Geometry.Polygon, this is the line where I use the ajax function:

$.ajax({
    url:"Catcher",
    data:query,
    type:"POST",
    dataType:"text",
    success:function(data){
        var fea=new OpenLayers.Feature.Vector(data); //this doesn't work
        vectorLayer.addFeatures([fea]); // here I add the feature to the vector layer I created as a global variable
        map.zoomToExtent(bounds);
    }
});   

How can I make the conversion?

Best Answer

Use the OpenLayers.Format.WKT class.

Try replacing this line:

var fea=new OpenLayers.Feature.Vector(data); //this doesn't work

For this line(if data.d = "POLYGON((516000 ..."):

var fea = new OpenLayers.Format.WKT().read(data.d);

Here's an example on how to use it: http://dev.openlayers.org/examples/vector-formats.html

enter image description here