0

I am using leaflet and turf.js to determine if a line passes through a polygon using turf.booleanIntersects(). I have the code working but with one slight inconsistency. The line in the attached image according to turf.booleanIntersects() passes through both of the polygons in the image, but the line drawn on map doesn't actually pass through the both polygons according to the image. Is the image incorrect (if so how can I fix it?) or does the line not actually go through the top polygon. Is it the projection inconsistency of a Geodesic line and geoJson turf polygon onto leaflet map?

Thank you

A snippet of important bit of my code

LayerGroup = L.layerGroup().addTo(mymap);
var line = new L.Geodesic([[point1, point2]], geodesicOptions).addTo(LayerGroup);
LinesList.push(line);


var line = turf.lineString([
        [LinesList[i].points[0][0].lng, LinesList[i].points[0][0].lat],
        [LinesList[i].points[0][1].lng, LinesList[i].points[0][1].lat]
        ]);

//multiple points make up polygonlines array
var point= [end.lng, end.lat];
polygonlines.push(point);

var geojsonPolygon =
    {
    "type": "Feature",
    "properties": {},
    "geometry": {
    "type": "Polygon",
    "coordinates": [polygonlines]
    }
}

var turfpolygon = turf.polygon(geojsonPolygon.geometry.coordinates);
if (turf.booleanIntersects(line, turfpolygon) == true) {
   const p2 = L.geoJSON(turfpolygon).addTo(mymap);
}                               

enter image description here

tj26
  • 197
  • 1
  • 10
  • 1
    Read https://github.com/henrythasler/Leaflet.Geodesic#line-options , and try displaying that geodesic line with more steps. – IvanSanchez Dec 05 '22 at 12:05
  • @IvanSanchez thank you, unfortunately i have the steps already set to 8. Maybe as it is a long line that is just the way it will be.... – tj26 Dec 05 '22 at 21:05
  • 1
    At least you've seen it looks different for different values of `steps`, right? You might want to manually cut it (with `libgeographic` or the like) around the area of interest. – IvanSanchez Dec 05 '22 at 21:29
  • thanks @IvanSanchez I couldn't see much difference with different values of steps. So I have cut the long line up into 2 parts and can now see the line going through the top polygon. Thanks for your help! – tj26 Dec 06 '22 at 00:04

0 Answers0