turf intersect point with leaflet geojson error

by ziggy   Last Updated March 14, 2019 20:22 PM

leaflet and turf. I am trying find all the layers on a leaflet map that intersect a point when the user clicks on the map. I am using the turf library to test for this. in the console when I click on the map it returns this error

turf.min.js:1 Uncaught Error: geojson must be a valid Feature or Geometry Object
    at J (turf.min.js:1)
    at Object.Lo [as intersect] (turf.min.js:1)
    at js2.js:22
    at eachLayer (leaflet.js:5)
    at e.<anonymous> (js2.js:20)
    at e.fire (leaflet.js:5)
    at e._fireDOMEvent (leaflet.js:5)
    at e._handleDOMEvent (leaflet.js:5)
    at HTMLDivElement.r (leaflet.js:5)

here is the code

map.on('click', function(e) {
        var str = "Latitude: " + e.latlng.lat.toFixed(5) + " Longitude: " + e.latlng.lng.toFixed(5) + " Zoom Level: " + map.getZoom();
        var lat = e.latlng.lat;
        var lng = e.latlng.lng;
        var pt = turf.point([lng, lat]);

        map.eachLayer(function(layer) {
            if (layer instanceof L.GeoJSON) {
                var click_intersection = turf.intersect(pt, layer.toGeoJSON());
                if (click_intersection) {
                    console.log(layer.feature.properties)
                }
            }
        });

reference questions turf.js intersect problem?

Get popup info in multiple layers from one click



Related Questions


Create buffer around point with Turf

Updated February 23, 2017 08:22 AM



Custom ordering/z-index of icons in Leaflet layer

Updated November 28, 2017 04:22 AM