1

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

4

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.

  • thanks for the help, but in his answer me now skip the following error: Uncaught TypeError: Can not read property 'type' of undefined This error has also left me in many of my tests. Anyway mirare solving the "JSTS" – Sergio_Az Jan 12 '16 at 23:10
  • 2
    Can you provide a minimum working sample somewhere? I tried the code before answering so you made another error somewhere IMO. – ThomasG77 Jan 12 '16 at 23:31
0

This is the solution :

select2 = new ol.interaction.Select({
        condition: ol.events.condition.click
    });

map.addInteraction(select2);
//var feature_buff = select2.getFeatures();
select2.getFeatures().on('change:length', function(e){   

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)));

    prueba.getSource().addFeature(geojson.readFeature(json[json.length-1]) );
    polys_fc=JSON.parse(json[json.length-1]);
    console.log(polys_fc);
    prueba.getSource().addFeatures(geojsonToFeatures(polys_fc, {
      featureProjection: 'EPSG:3857'
    }));

    var buffer = turf.buffer(polys_fc, 50000, 'kilometers');
    buffer.properties = {
    "fill": "#6BC65F",
    "stroke": "#25561F",
    "stroke-width": 2
    };
    console.log(buffer);

    prueba.getSource().addFeatures(geojsonToFeatures(buffer));





}  });

But now i have other question, the distance buffer don't work well but i don't know where is the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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