0

I'm trying to set a buffer with turfjs in a polygon. The polygon is loaded by L.geoJSON(); . When I execute bufferConcesion, this line var turfBuffer = turf.buffer(feature,value,{units:"kilometers"}); returns this error: Uncaught _u {} What might bethe problem.

This is my code:

    var featLayer = new L.GeoJSON(data, {
        style:{
            weight: 1,
            fillOpacity: 0.7,
            color: 'red'
        },
        onEachFeature: function(feature, layer) {
            layer.on('contextmenu',popUpBuffer);
        }
    });

    function popUpBuffer(e){
        var id = e.target.feature.properties.id;
        var popup = L.popup()
            .setLatLng(e.latlng)
            .setContent("<table><tr><td>Buffer (KM): </td><td><input type='text' id='buffer_value'><input type='hidden' id='ca_id' value='"+id+"'/></td><td><button onclick='bufferConcesion()'>Go</button></td></tr></table>")
            .openOn(map); 
    }
    function bufferConcesion(){
        var value = document.getElementById("buffer_value").value;
        if(value!==""){
            var layer = L.geoJSON(data,{
                filter:isID,
                onEachFeature: function(feature, layer) {
                    var turfBuffer = turf.buffer(feature,value,{units:"kilometers"});
                    var turfBufferLyr = L.geoJSON(turfBuffer, {style:{color:'yellow',dashArray:'5.5',fillOpacity:0}}).addTo(featLayer);
                }
            }).addTo(featLayer);
        }

    }
    function isID(feature){
        var id = document.getElementById("ca_id").value;
        if (feature.properties.id === id) return true
    }
2
  • What does a feature look like if you log one, like console.log(feature) in your onEachFeature function?
    – Alex Leith
    Jan 12 '18 at 2:24
  • like this: prntscr.com/hzaffe
    – tdaniel555
    Jan 12 '18 at 3:42
1

Turf doesn't see Leaflet layers as GeoJSON. When I had roads as a leaflet layer this worked.

var buffered = turf.buffer(road.toGeoJSON(), 2, {units: 'miles'});

In your code try:

var turfBuffer = turf.buffer(feature.toGeoJSON(),value,{units:"kilometers"});

and see if it works.

2
  • Thank you Bill. I tried before with that code, but showed me another error: Uncaught TypeError: feature.toGeoJSON is not a function.
    – tdaniel555
    Jan 15 '18 at 20:06
  • apparently it has to do with polygons. I copied a piece of code from turf (buffer a point) and pasted in my project and works fine. But then a changed to 'type':'Polygon' and added the coordinates and then returns Uncaught _u {}
    – tdaniel555
    Jan 15 '18 at 20:13

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.