I was just wondering if there was a good way to convert GeoJSONdata to WKT data using OpenLayers.

The method I've tried is to create a Feature.Vector with the GeoJson and then tried using WKT.write(feature) to get the WKT but it's not working. I'm obviously missing something.

var geojson_format = new OpenLayers.Format.GeoJSON();
var testFeature = geojson_format.read(val);
var wkt = new OpenLayers.Format.WKT(wkt_options);
wkt.write(testFeature);

Could anybody point me in the right direction?

My GeoJSON is

{ 
"type": "FeatureCollection",
    "features": 
    [
        {"type":"Feature", "geometry": {"type":"MultiPolygon","coordinates":[[[[-0.12072212703174,51.51899157882951],[-0.119842362474856,51.51708218165009],[-0.117203068804201,51.51314296254295],[-0.117803883623537,51.51300942372214],[-0.118125748705324,51.51283582266979],[-0.118533444475588,51.51254203476644],[-0.118855309557375,51.51192774484595],[-0.118898224901614,51.51126002901546],[-0.117846798967776,51.50983108425668],[-0.119220089983401,51.50931024277883],[-0.120335888933596,51.508549002215815],[-0.121151280474123,51.5077610380613],[-0.122095418047365,51.50653232100095],[-0.126429867815432,51.50729359525858],[-0.127266717028078,51.507440506370465],[-0.127266717028078,51.507801104364084],[-0.127266717028078,51.508094922841394],[-0.127331090044436,51.50848222612105],[-0.127567124437746,51.50939037262451],[-0.128146481584963,51.509764310040985],[-0.128511262010989,51.51021837277772],[-0.128597092699465,51.51120661132621],[-0.128597092699465,51.51191439062526],[-0.129004788469729,51.51260880491084],[-0.129584145616946,51.51374388239237],[-0.130120587419924,51.51494569831],[-0.130614113878664,51.51653471734371],[-0.126794648241457,51.51701541806259],[-0.125507187914309,51.51718900318654],[-0.121001076769289,51.519178508517115],[-0.12072212703174,51.51899157882951]]]]}}
    ]
}
  • 1
    Question:Why don't you just use the GeoJSON in Openlayers? – Mapperz Sep 24 '13 at 15:40
  • We use WKT to save all of our multipolygon data in MySQL spatial type. And now have the need to import some GeoJSON from another source and I was looking for a way to do this conversion on the front end – JasonM Sep 24 '13 at 15:52
up vote 2 down vote accepted

Not sure what you mean by "it's not working", I'm getting output that looks correct:

var val = ''; // your GeoJSON goes here
var wkt_options = {};
var geojson_format = new OpenLayers.Format.GeoJSON();
var testFeature = geojson_format.read(val);
var wkt = new OpenLayers.Format.WKT(wkt_options);
var out = wkt.write(testFeature);

alert(out);

See it in action: http://jsfiddle.net/FUeJj/

  • Thank you for a clear pair of eyes! What I had done does work (as you showed) but I was using a previously created WKT variable that uses a couple of options for internal and external projection and this seems to be the reason for unexpected results. Thanks again for your help/time – JasonM Sep 24 '13 at 16:43

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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