1

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

2
  • Please edit your question ans explain a bit more about "I can't pass a single of these objects to a turf function". What exactly "can't pass" mean? Nothing happens? Wrong things happen? Errors in the browser debugger console?
    – TomazicM
    Commented Jun 12, 2023 at 20:05
  • @TomazicM Hello; I've edited my question
    – Vincent Dc
    Commented Jun 12, 2023 at 20:35

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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