Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

First let me say that 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:

        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

How can I make the conversion? Thanks in advance for any help you can give me.

share|improve this question
does console.log(data.d) give you your WKT polygon string? (POLYGON((516000 )? – CaptDragon Jul 18 '11 at 18:52
using a WFS query would probably work better – iant Jul 18 '11 at 20:06
up vote 11 down vote accepted

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:

enter image description here

share|improve this answer
it worked with: var in_options = { 'internalProjection': new OpenLayers.Projection("EPSG:4326"), 'externalProjection': new OpenLayers.Projection("EPSG:4326") }; var fea= new OpenLayers.Format.WKT(in_options).read(data); – Uriel Jul 18 '11 at 21:06
Yeah, i had that originally in the code but decided to take it out and you'd figure out the projection part. Excellent. – CaptDragon Jul 18 '11 at 21:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.