[GIS] How to obtain geojson geometry ? OL3

geojsonopenlayersturf

I need to get a GeoJSON geometry of this, then use a buffer for the TURF library.

Get get data from GeoJSON turf but would not admit it in any way.
any ideas ?

This is my code:

var feature_buff = select2.getFeatures();
var geojson  = new ol.format.GeoJSON();

for (var i=0;i<feature_buff.getLength();i++){
json.push(geojson.writeFeature(feature_buff.item(i)));
var obj =geojson.writeFeatures(feature_buff.item(i))

var geom = geojson.readGeometry(obj);


var buffer = turf.buffer(geom, 2000, 'kilometers')

this code fail. and this is the error:

Uncaught TypeError: (0 , ls[b.type]) is not a function

Best Answer

You don't provide the "right" content to Turf (your geom is not the expected content for Turf)

feats = select2.getFeatures();

var geojson  = new ol.format.GeoJSON();

as_geojson = geojson.writeFeatures(feats, {
  featureProjection: 'EPSG:3857',
  dataProjection: 'EPSG:4326'
});

turf.buffer(as_geojson, 20, 'kilometers');

You can also see this demo I've made months ago and this other official one.

If you don't use extensively Turf, it's also possible to use JSTS (JavaScript Topology Suite), a library to manipulate geometry in JavaScript. A demo with buffer is available at the official website.