1

I have a Leaflet Polyline:

var polyline = L.Polyline(polylineData).addTo(map);

and a Leaflet GeoJSON of Polygons:

var countries = L.geoJson(countries, {style:{color:'darkgrey', opacity:1,fillColor:'lightgrey', fillOpacity:0.7, weight:2}}).addTo(map);

They look like this on the map:

enter image description here

Imagine that the Polyline represents a driving route that goes across Europe. The GeoJSON is a collection of National Boundaries with associated attribute information. I need to know what countries the driving route goes through. For example, In the case of this line I need to know that it is passing through the United Kingdom, France and Switzerland. The GeoJSON contains the associated country names as properties in each feature.

I am hoping that a function within the Turf.js library is suitable though am not sure which one?

1
  • Have added code excerpts and image to illustrate.
    – Jamie
    Jan 13, 2022 at 10:35

1 Answer 1

2

You can use countries.eachLayer group layer method to iterate through layer features and then use turf.booleanIntersects method to check which country feature intersects polyline.

To log intersected countries on the console, code could then look something like this (assuming feature property name contains country name):

var polyline = L.Polyline(polylineData).addTo(map);
var polylineFeature = polyLine.toGeoJSON();

countries.eachLayer(function(layer) {
  if (turf.booleanIntersects(layer.feature, polylineFeature)) {
    console.log(layer.feature.properties.name);
  }
});
1
  • Amazing - worked perfect. Thank you so much!
    – Jamie
    Jan 13, 2022 at 15:32

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.