Leaflet – Extract Feature from GeoJSON Layer and Submit to Turf.js Function

featuresgeojsonleafletturf

A Leaflet layer holds some objects and each one has a bindpopup enabled,
now I can't pass a single of these objects to a turf function (in that case, buffer). I know I have to translate this single object into a GeoJSON layer, to make it readable, but I'm not successful.

"feature" with console.log, included in the code, reacts well in the following screenshot: it holds the single object that I need

enter image description here

but I'm unsuccessful with the rest :

Uncaught TypeError: datatoconv.addTo is not a function

This is Leaflet.js, with Turf.js
and maybe this code: https://mappingforyou.eu/javascript/toGeoJSON.js

   var polygonObjects;
polygonObjects = L.geoJson(null, {
    onEachFeature: function(feature, layer) {
        let btnBuffermodal = document.createElement('button');
        btnBuffermodal.className = 'ti-pencil';
        btnBuffermodal.innerHTML = '<b>Create perimeter...</b>';
        btnBuffermodal.onclick = function(targetmodal, context, geometry) {
            console.log(feature);
            var a = prompt("How many kilometers?", "10");
            if (a != null) {
                var bufferedLayer = L.geoJson(null, {
                    style: {
                        color: "black",
                        weight: 4,
                        fillColor: "white",
                        fillOpacity: 0
                    }
                }).addTo(map);
                var xdiv = a / 1;
                var datatoconv = feature.geometry;

                var templayer = new L.GeoJSON();
                templayer.addLayer(layer);
                //      console.log(templayer);
                templayer.addTo(map);
                var data = datatoconv.toGeoJSON();
                console.log(data);
                var buffered = turf.buffer(templayer, xdiv, {
                    units: 'kilometers'
                });
                console.log(buffered);

                _buildDigitiseModalBox('modalform', 'POLYGON', objectGeometry);
            }
        };

        let btnDiv = document.createElement('div')
        btnDiv.append(btnBuffermodal);

        layer.bindPopup(btnDiv);
    }
});

It's an analog code to this one:
https://github.com/andyprasetya/leaflet-CRUD/blob/master/app/create.js

Best Answer

feature parameter of onEachFeature option processing function is already pure plain GeoJSON object that is needed for turf.buffer method. As such it has no .toGeoJSON() method. You could apply this method to layer parameter which is actually Leaflet object, but you would get back exactly the same GeoJSON as in feature parameter.

So relevant part of your code is simply:

var buffered = turf.buffer(feature, xdiv, {
  units: 'kilometers'
});