Saturday, 25 February 2017

openlayers 2 - Converting GeoJSON to WKT with OpenLayers2


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]]]]}}
]

}

Answer



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/


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...