[GIS] Converting wkt to geojson with OpenLayers

geojsonopenlayers-2well-known-text

I am trying to convert wkt to geojson wiht OpenLayers. I wrote my code based on this question (geojson to wkt).

That's what I have so far, but it is not working. Any ideas?

var = 'GEOMETRYCOLLECTION(MULTIPOLYGON(((-0.12072212703174 51.51899157882951,-0.128597092699465 51.51191439062526,-0.129004788469729 51.51260880491084,-0.129584145616946 51.51374388239237,-0.130120587419924 51.51494569831,-0.130614113878664 51.51653471734371,-0.125507187914309 51.51718900318654,-0.121001076769289 51.519178508517115,-0.12072212703174 51.51899157882951))))';
var geojson_options = {};
var wkt_format = new OpenLayers.Format.WKT();
var testFeature = wkt_format.read(val);
var geojson_format = new OpenLayers.Format.GeoJSON(wkt_options);
var out = geojson.write(testFeature);

alert(out);

Try it in action: http://jsfiddle.net/Pp5FF/

Best Answer

The OpenLayers part itself it correct, but your snippet is not valid JavaScript. The problems are:

  • the fiddle doesn't import OpenLayers (use "External Resources" for that)
  • the WKT string variable is missing name
  • wkt_options not defined
  • geojson_format vs geojson

Hint: open your browser's Developer Console to see the errors reported one by one. And here's your fixed fiddle